diff options
author | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2018-03-06 03:01:02 +0100 |
---|---|---|
committer | Qt Forward Merge Bot <qt_forward_merge_bot@qt-project.org> | 2018-03-06 03:01:02 +0100 |
commit | 7d49d4e9841a61997384dd149a72d749ff0729e3 (patch) | |
tree | 8900bb15463dbfcc134dc0111e533734f0effa01 | |
parent | 848f0de244cff8565f323dfc3ffdf479ad2b0f2e (diff) | |
parent | 1787f95f5727a20a4f0871736ad987fcfdf0f3c6 (diff) | |
download | qtsvg-7d49d4e9841a61997384dd149a72d749ff0729e3.tar.gz |
Merge remote-tracking branch 'origin/5.11' into dev
Change-Id: I22feba1b24694d117e56999b6c940663d0957759
-rw-r--r-- | examples/svg/svgviewer/mainwindow.cpp | 20 | ||||
-rw-r--r-- | examples/svg/svgviewer/mainwindow.h | 5 | ||||
-rw-r--r-- | examples/svg/svgviewer/svgview.cpp | 34 | ||||
-rw-r--r-- | examples/svg/svgviewer/svgview.h | 10 |
4 files changed, 67 insertions, 2 deletions
diff --git a/examples/svg/svgviewer/mainwindow.cpp b/examples/svg/svgviewer/mainwindow.cpp index 8cb94d2..7dd240a 100644 --- a/examples/svg/svgviewer/mainwindow.cpp +++ b/examples/svg/svgviewer/mainwindow.cpp @@ -64,6 +64,7 @@ static inline QString picturesLocation() MainWindow::MainWindow() : QMainWindow() , m_view(new SvgView) + , m_zoomLabel(new QLabel) { QToolBar *toolBar = new QToolBar(this); addToolBar(Qt::TopToolBarArea, toolBar); @@ -94,6 +95,14 @@ MainWindow::MainWindow() m_outlineAction->setChecked(true); connect(m_outlineAction, &QAction::toggled, m_view, &SvgView::setViewOutline); + viewMenu->addSeparator(); + QAction *zoomAction = viewMenu->addAction(tr("Zoom &In"), m_view, &SvgView::zoomIn); + zoomAction->setShortcut(QKeySequence::ZoomIn); + zoomAction = viewMenu->addAction(tr("Zoom &Out"), m_view, &SvgView::zoomOut); + zoomAction->setShortcut(QKeySequence::ZoomOut); + zoomAction = viewMenu->addAction(tr("Reset Zoom"), m_view, &SvgView::resetZoom); + zoomAction->setShortcut(Qt::CTRL + Qt::Key_0); + QMenu *rendererMenu = menuBar()->addMenu(tr("&Renderer")); m_nativeAction = rendererMenu->addAction(tr("&Native")); m_nativeAction->setCheckable(true); @@ -134,6 +143,11 @@ MainWindow::MainWindow() help->addAction(tr("About Qt"), qApp, &QApplication::aboutQt); setCentralWidget(m_view); + + m_zoomLabel->setToolTip(tr("Use the mouse wheel to zoom")); + statusBar()->addPermanentWidget(m_zoomLabel); + updateZoomLabel(); + connect(m_view, &SvgView::zoomChanged, this, &MainWindow::updateZoomLabel); } void MainWindow::openFile() @@ -221,3 +235,9 @@ void MainWindow::exportImage() } } } + +void MainWindow::updateZoomLabel() +{ + const int percent = qRound(m_view->zoomFactor() * qreal(100)); + m_zoomLabel->setText(QString::number(percent) + QLatin1Char('%')); +} diff --git a/examples/svg/svgviewer/mainwindow.h b/examples/svg/svgviewer/mainwindow.h index 3d18fd1..6c2af7e 100644 --- a/examples/svg/svgviewer/mainwindow.h +++ b/examples/svg/svgviewer/mainwindow.h @@ -61,6 +61,7 @@ class QAction; class QGraphicsView; class QGraphicsScene; class QGraphicsRectItem; +class QLabel; QT_END_NAMESPACE class MainWindow : public QMainWindow @@ -77,6 +78,9 @@ public slots: void exportImage(); void setRenderer(int renderMode); +private slots: + void updateZoomLabel(); + private: QAction *m_nativeAction; QAction *m_glAction; @@ -86,6 +90,7 @@ private: QAction *m_outlineAction; SvgView *m_view; + QLabel *m_zoomLabel; QString m_currentPath; }; diff --git a/examples/svg/svgviewer/svgview.cpp b/examples/svg/svgviewer/svgview.cpp index bbc853c..ecc8409 100644 --- a/examples/svg/svgviewer/svgview.cpp +++ b/examples/svg/svgviewer/svgview.cpp @@ -178,6 +178,29 @@ void SvgView::setViewOutline(bool enable) m_outlineItem->setVisible(enable); } +qreal SvgView::zoomFactor() const +{ + return transform().m11(); +} + +void SvgView::zoomIn() +{ + zoomBy(2); +} + +void SvgView::zoomOut() +{ + zoomBy(0.5); +} + +void SvgView::resetZoom() +{ + if (!qFuzzyCompare(zoomFactor(), qreal(1))) { + resetTransform(); + emit zoomChanged(); + } +} + void SvgView::paintEvent(QPaintEvent *event) { if (m_renderer == Image) { @@ -199,9 +222,16 @@ void SvgView::paintEvent(QPaintEvent *event) void SvgView::wheelEvent(QWheelEvent *event) { - qreal factor = qPow(1.2, event->delta() / 240.0); + zoomBy(qPow(1.2, event->delta() / 240.0)); +} + +void SvgView::zoomBy(qreal factor) +{ + const qreal currentZoom = zoomFactor(); + if ((factor < 1 && currentZoom < 0.1) || (factor > 1 && currentZoom > 10)) + return; scale(factor, factor); - event->accept(); + emit zoomChanged(); } QSvgRenderer *SvgView::renderer() const diff --git a/examples/svg/svgviewer/svgview.h b/examples/svg/svgviewer/svgview.h index 2a1a5ed..1b6b33e 100644 --- a/examples/svg/svgviewer/svgview.h +++ b/examples/svg/svgviewer/svgview.h @@ -76,16 +76,26 @@ public: QSize svgSize() const; QSvgRenderer *renderer() const; + qreal zoomFactor() const; + public slots: void setHighQualityAntialiasing(bool highQualityAntialiasing); void setViewBackground(bool enable); void setViewOutline(bool enable); + void zoomIn(); + void zoomOut(); + void resetZoom(); + +signals: + void zoomChanged(); protected: void wheelEvent(QWheelEvent *event) override; void paintEvent(QPaintEvent *event) override; private: + void zoomBy(qreal factor); + RendererType m_renderer; QGraphicsSvgItem *m_svgItem; |