diff --git a/graphicsview.cpp b/graphicsview.cpp index 8bc6594..e88871b 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -104,6 +104,7 @@ void GraphicsView::resetTransform() void GraphicsView::zoomView(qreal scaleFactor) { + m_enableFitInView = false; m_scaleFactor *= scaleFactor; reapplyViewTransform(); emit navigatorViewRequired(!isThingSmallerThanWindowWith(transform()), m_rotateAngle); @@ -175,12 +176,9 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event) void GraphicsView::wheelEvent(QWheelEvent *event) { - m_enableFitInView = false; - if (event->delta() > 0) { - zoomView(1.25); - } else { - zoomView(0.8); - } + event->ignore(); + + return QGraphicsView::wheelEvent(event); } void GraphicsView::resizeEvent(QResizeEvent *event) @@ -243,6 +241,17 @@ void GraphicsView::dropEvent(QDropEvent *event) } } +void GraphicsView::paintEvent(QPaintEvent *event) +{ + // A little dirty hack way to get the viewport "pan" event + // but still doesn't works well with gif which scaled up bigger than the window. + if (event->rect() == this->rect() && !isThingSmallerThanWindowWith(transform())) { + emit viewportRectChanged(); +// qDebug() << "paintEvent" << event << QObject::sender(); + } + return QGraphicsView::paintEvent(event); +} + bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const { return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize()) diff --git a/graphicsview.h b/graphicsview.h index 4fa73ef..168820d 100644 --- a/graphicsview.h +++ b/graphicsview.h @@ -33,6 +33,7 @@ public: signals: void navigatorViewRequired(bool required, qreal angle); + void viewportRectChanged(); public slots: void toggleCheckerboard(); @@ -48,6 +49,8 @@ private: void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; + void paintEvent(QPaintEvent *event) override; + bool isThingSmallerThanWindowWith(const QTransform &transform) const; bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const; void setCheckerboardEnabled(bool enabled); diff --git a/mainwindow.cpp b/mainwindow.cpp index 3cc8db3..4c1f9b4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -48,6 +48,7 @@ MainWindow::MainWindow(QWidget *parent) : m_gv = new NavigatorView(this); m_gv->setFixedSize(250, 160); m_gv->setScene(scene); + m_gv->setMainView(m_graphicsView); m_gv->fitInView(m_gv->sceneRect(), Qt::KeepAspectRatio); connect(m_graphicsView, &GraphicsView::navigatorViewRequired, @@ -58,6 +59,9 @@ MainWindow::MainWindow(QWidget *parent) : m_gv->setVisible(required); }); + connect(m_graphicsView, &GraphicsView::viewportRectChanged, + m_gv, &NavigatorView::updateMainViewportRegion); + m_closeButton = new QPushButton(m_graphicsView); m_closeButton->setFlat(true); m_closeButton->setFixedSize(50, 50); @@ -211,6 +215,15 @@ void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) return QMainWindow::mouseDoubleClickEvent(event); } +void MainWindow::wheelEvent(QWheelEvent *event) +{ + if (event->delta() > 0) { + m_graphicsView->zoomView(1.25); + } else { + m_graphicsView->zoomView(0.8); + } +} + void MainWindow::resizeEvent(QResizeEvent *event) { updateWidgetsPosition(); diff --git a/mainwindow.h b/mainwindow.h index 6e3b7bf..e2e7bff 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -33,6 +33,7 @@ protected slots: void mouseMoveEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; void mouseDoubleClickEvent(QMouseEvent *event) override; + void wheelEvent(QWheelEvent *event) override; void resizeEvent(QResizeEvent *event) override; bool nativeEvent(const QByteArray& eventType, void* message, long* result) override; diff --git a/navigatorview.cpp b/navigatorview.cpp index 546d664..9d01edf 100644 --- a/navigatorview.cpp +++ b/navigatorview.cpp @@ -1,9 +1,13 @@ #include "navigatorview.h" +#include "graphicsview.h" + #include +#include NavigatorView::NavigatorView(QWidget *parent) : QGraphicsView (parent) + , m_viewportRegion(this->rect()) { setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -11,8 +15,31 @@ NavigatorView::NavigatorView(QWidget *parent) "border-radius: 3px;"); } +// doesn't take or manage its ownership +void NavigatorView::setMainView(GraphicsView *mainView) +{ + m_mainView = mainView; +} + +void NavigatorView::updateMainViewportRegion() +{ + if (m_mainView != nullptr) { + m_viewportRegion = mapFromScene(m_mainView->mapToScene(m_mainView->rect())); + qDebug() << m_mainView->rect() << m_mainView->mapToScene(m_mainView->rect()) << m_viewportRegion; + } +} + void NavigatorView::wheelEvent(QWheelEvent *event) { event->ignore(); return QGraphicsView::wheelEvent(event); } + +void NavigatorView::paintEvent(QPaintEvent *event) +{ + QGraphicsView::paintEvent(event); + + QPainter painter(viewport()); + painter.setPen(QPen(Qt::gray, 2)); + painter.drawRect(m_viewportRegion.boundingRect()); +} diff --git a/navigatorview.h b/navigatorview.h index 49b73e2..e082a06 100644 --- a/navigatorview.h +++ b/navigatorview.h @@ -3,14 +3,24 @@ #include +class GraphicsView; class NavigatorView : public QGraphicsView { Q_OBJECT public: NavigatorView(QWidget *parent = nullptr); + void setMainView(GraphicsView *mainView); + +public slots: + void updateMainViewportRegion(); + private: void wheelEvent(QWheelEvent *event) override; + void paintEvent(QPaintEvent *event) override; + + QPolygon m_viewportRegion; + QGraphicsView *m_mainView = nullptr; }; #endif // NAVIGATORVIEW_H