From d106d9beee30c710c342e958c38f1df4366672dc Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 29 Sep 2019 23:53:29 +0800 Subject: [PATCH] svg? --- PineapplePictures.pro | 93 ++++++++++++++++++----------------- bottombuttongroup.cpp | 13 ++--- graphicsscene.cpp | 10 ++++ graphicsscene.h | 1 + graphicsview.cpp | 21 ++++++-- graphicsview.h | 1 + icons/object-rorate-right.svg | 70 ++++++++++++++++++++++++++ icons/view-fullscreen.svg | 86 ++++++++++++++++++++++++++++++++ icons/zoom-in.svg | 81 ++++++++++++++++++++++++++++++ icons/zoom-original.svg | 71 ++++++++++++++++++++++++++ icons/zoom-out.svg | 76 ++++++++++++++++++++++++++++ main.cpp | 1 + resources.qrc | 9 ++++ 13 files changed, 477 insertions(+), 56 deletions(-) create mode 100644 icons/object-rorate-right.svg create mode 100644 icons/view-fullscreen.svg create mode 100644 icons/zoom-in.svg create mode 100644 icons/zoom-original.svg create mode 100644 icons/zoom-out.svg create mode 100644 resources.qrc diff --git a/PineapplePictures.pro b/PineapplePictures.pro index 40bb3a5..ac419d1 100644 --- a/PineapplePictures.pro +++ b/PineapplePictures.pro @@ -1,45 +1,48 @@ -#------------------------------------------------- -# -# Project created by QtCreator 2019-09-26T23:36:07 -# -#------------------------------------------------- - -QT += core gui - -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets - -TARGET = PineapplePictures -TEMPLATE = app - -# The following define makes your compiler emit warnings if you use -# any feature of Qt which has been marked as deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - -CONFIG += c++11 - -SOURCES += \ - main.cpp \ - mainwindow.cpp \ - graphicsview.cpp \ - bottombuttongroup.cpp \ - graphicsscene.cpp - -HEADERS += \ - mainwindow.h \ - graphicsview.h \ - bottombuttongroup.h \ - graphicsscene.h - -FORMS += - -# Default rules for deployment. -qnx: target.path = /tmp/$${TARGET}/bin -else: unix:!android: target.path = /opt/$${TARGET}/bin -!isEmpty(target.path): INSTALLS += target +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-26T23:36:07 +# +#------------------------------------------------- + +QT += core gui svg + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = PineapplePictures +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + graphicsview.cpp \ + bottombuttongroup.cpp \ + graphicsscene.cpp + +HEADERS += \ + mainwindow.h \ + graphicsview.h \ + bottombuttongroup.h \ + graphicsscene.h + +FORMS += + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +RESOURCES += \ + resources.qrc diff --git a/bottombuttongroup.cpp b/bottombuttongroup.cpp index bab9677..370eff8 100644 --- a/bottombuttongroup.cpp +++ b/bottombuttongroup.cpp @@ -24,15 +24,16 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent) "}"); auto newBtn = [](QString text) -> QPushButton * { - QPushButton * btn = new QPushButton(text); + QPushButton * btn = new QPushButton(QIcon(QStringLiteral(":/icons/") + text), ""); + btn->setIconSize(QSize(40, 40)); btn->setFixedSize(40, 40); return btn; }; - addButton(newBtn("1:1")); - addButton(newBtn("Full")); - addButton(newBtn("Zoom+")); - addButton(newBtn("Zoom-")); - addButton(newBtn("Rorate")); + addButton(newBtn("zoom-original")); + addButton(newBtn("view-fullscreen")); + addButton(newBtn("zoom-in")); + addButton(newBtn("zoom-out")); + addButton(newBtn("object-rorate-right")); } void BottomButtonGroup::addButton(QAbstractButton *button) diff --git a/graphicsscene.cpp b/graphicsscene.cpp index 1fbd350..8f619f3 100644 --- a/graphicsscene.cpp +++ b/graphicsscene.cpp @@ -5,6 +5,7 @@ #include #include #include +#include GraphicsScene::GraphicsScene(QObject *parent) : QGraphicsScene(parent) @@ -32,3 +33,12 @@ void GraphicsScene::showText(const QString &text) m_theThing = textItem; this->setSceneRect(m_theThing->boundingRect()); } + +void GraphicsScene::showSvg(const QString &filepath) +{ + this->clear(); + QGraphicsSvgItem * svgItem = new QGraphicsSvgItem(filepath); + this->addItem(svgItem); + m_theThing = svgItem; + this->setSceneRect(m_theThing->boundingRect()); +} diff --git a/graphicsscene.h b/graphicsscene.h index e778a12..1ffc632 100644 --- a/graphicsscene.h +++ b/graphicsscene.h @@ -12,6 +12,7 @@ public: void showImage(const QPixmap &pixmap); void showText(const QString &text); + void showSvg(const QString &filepath); private: QGraphicsItem * m_theThing; diff --git a/graphicsview.cpp b/graphicsview.cpp index 6885b98..3810739 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -35,6 +35,11 @@ void GraphicsView::showText(const QString &text) scene()->showText(text); } +void GraphicsView::showSvg(const QString &filepath) +{ + scene()->showSvg(filepath); +} + GraphicsScene *GraphicsView::scene() const { return qobject_cast(QGraphicsView::scene()); @@ -129,12 +134,18 @@ void GraphicsView::dropEvent(QDropEvent *event) if (mimeData->hasUrls()) { QUrl url(mimeData->urls().first()); - QImageReader imageReader(url.toLocalFile()); - QImage::Format imageFormat = imageReader.imageFormat(); - if (imageFormat == QImage::Format_Invalid) { - showText("File is not a valid image"); + QString filePath(url.toLocalFile()); + + if (filePath.endsWith(".svg")) { + showSvg(filePath); } else { - showImage(QPixmap::fromImageReader(&imageReader)); + QImageReader imageReader(filePath); + QImage::Format imageFormat = imageReader.imageFormat(); + if (imageFormat == QImage::Format_Invalid) { + showText("File is not a valid image"); + } else { + showImage(QPixmap::fromImageReader(&imageReader)); + } } } else if (mimeData->hasImage()) { QImage img = qvariant_cast(mimeData->imageData()); diff --git a/graphicsview.h b/graphicsview.h index 9da4260..4939b1b 100644 --- a/graphicsview.h +++ b/graphicsview.h @@ -11,6 +11,7 @@ public: void showImage(const QPixmap &pixmap); void showText(const QString &text); + void showSvg(const QString &filepath); GraphicsScene * scene() const; void setScene(GraphicsScene *scene); diff --git a/icons/object-rorate-right.svg b/icons/object-rorate-right.svg new file mode 100644 index 0000000..e39a63f --- /dev/null +++ b/icons/object-rorate-right.svg @@ -0,0 +1,70 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + diff --git a/icons/view-fullscreen.svg b/icons/view-fullscreen.svg new file mode 100644 index 0000000..d5c938f --- /dev/null +++ b/icons/view-fullscreen.svg @@ -0,0 +1,86 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/icons/zoom-in.svg b/icons/zoom-in.svg new file mode 100644 index 0000000..2d0beb5 --- /dev/null +++ b/icons/zoom-in.svg @@ -0,0 +1,81 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + + diff --git a/icons/zoom-original.svg b/icons/zoom-original.svg new file mode 100644 index 0000000..88ccc88 --- /dev/null +++ b/icons/zoom-original.svg @@ -0,0 +1,71 @@ + + + + + + + + + + image/svg+xml + + + + + + + 1:1 + + diff --git a/icons/zoom-out.svg b/icons/zoom-out.svg new file mode 100644 index 0000000..db820cc --- /dev/null +++ b/icons/zoom-out.svg @@ -0,0 +1,76 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/main.cpp b/main.cpp index aab39bb..76bc6d9 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); + MainWindow w; w.show(); diff --git a/resources.qrc b/resources.qrc new file mode 100644 index 0000000..fb9d616 --- /dev/null +++ b/resources.qrc @@ -0,0 +1,9 @@ + + + icons/zoom-in.svg + icons/zoom-out.svg + icons/view-fullscreen.svg + icons/zoom-original.svg + icons/object-rorate-right.svg + +