navigator view draw viewport rect

This commit is contained in:
Gary Wang 2019-10-04 09:54:13 +08:00
parent ca1b10dc28
commit 820ad4e419
6 changed files with 69 additions and 6 deletions

View File

@ -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())

View File

@ -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);

View File

@ -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();

View File

@ -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;

View File

@ -1,9 +1,13 @@
#include "navigatorview.h"
#include "graphicsview.h"
#include <QMouseEvent>
#include <QDebug>
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());
}

View File

@ -3,14 +3,24 @@
#include <QGraphicsView>
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