From b3e29ad4a6a21db54c3fb7bb0806b29f1b59b845 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 29 Sep 2019 21:22:20 +0800 Subject: [PATCH] feat: resize window policy --- graphicsview.cpp | 53 +++++++++++++++++++++++++++++++++++++++++------- graphicsview.h | 6 ++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/graphicsview.cpp b/graphicsview.cpp index 44e5058..d028303 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -14,6 +14,7 @@ GraphicsView::GraphicsView(QWidget *parent) setDragMode(QGraphicsView::ScrollHandDrag); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setResizeAnchor(QGraphicsView::AnchorUnderMouse); setStyleSheet("background-color: rgba(0, 0, 0, 180);" "border-radius: 3px;"); setAcceptDrops(true); @@ -21,7 +22,12 @@ GraphicsView::GraphicsView(QWidget *parent) void GraphicsView::showImage(const QPixmap &pixmap) { + resetTransform(); scene()->showImage(pixmap); + if (!isThingSmallerThanWindowWith(transform())) { + m_enableFitInView = true; + fitInView(sceneRect(), Qt::KeepAspectRatio); + } } void GraphicsView::showText(const QString &text) @@ -41,23 +47,19 @@ void GraphicsView::setScene(GraphicsScene *scene) void GraphicsView::mousePressEvent(QMouseEvent *event) { - QGraphicsItem *item = itemAt(event->pos()); - if (!item) { + if (shouldIgnoreMousePressMoveEvent(event)) { event->ignore(); // blumia: return here, or the QMouseEvent event transparency won't // work if we set a QGraphicsView::ScrollHandDrag drag mode. return; } - qDebug() << item; - return QGraphicsView::mousePressEvent(event); } void GraphicsView::mouseMoveEvent(QMouseEvent *event) { - QGraphicsItem *item = itemAt(event->pos()); - if (!item) { + if (shouldIgnoreMousePressMoveEvent(event)) { event->ignore(); } @@ -76,13 +78,28 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event) void GraphicsView::wheelEvent(QWheelEvent *event) { - if(event->delta() > 0) { + m_enableFitInView = false; + if (event->delta() > 0) { scale(1.25, 1.25); } else { scale(0.8, 0.8); } } +void GraphicsView::resizeEvent(QResizeEvent *event) +{ + if (m_enableFitInView) { + if (isThingSmallerThanWindowWith(QTransform()) && transform().m11() >= 1) { + // no longer need to do fitInView() + // but we leave the m_enableFitInView value unchanged in case + // user resize down the window again. + } else { + fitInView(sceneRect(), Qt::KeepAspectRatio); + } + } + return QGraphicsView::resizeEvent(event); +} + void GraphicsView::dragEnterEvent(QDragEnterEvent *event) { if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) { @@ -129,5 +146,27 @@ void GraphicsView::dropEvent(QDropEvent *event) } } else if (mimeData->hasText()) { showText(mimeData->text()); + } else { + showText("Not supported mimedata: " + mimeData->formats().first()); } } + +bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const +{ + return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize()) + == rect().size(); +} + +bool GraphicsView::shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const +{ + if (isThingSmallerThanWindowWith(transform())) { + return true; + } + + QGraphicsItem *item = itemAt(event->pos()); + if (!item) { + return true; + } + + return false; +} diff --git a/graphicsview.h b/graphicsview.h index 956f8ac..9da4260 100644 --- a/graphicsview.h +++ b/graphicsview.h @@ -20,10 +20,16 @@ private: void mouseMoveEvent(QMouseEvent * event) override; void mouseReleaseEvent(QMouseEvent * event) override; void wheelEvent(QWheelEvent *event) override; + void resizeEvent(QResizeEvent *event) override; void dragEnterEvent(QDragEnterEvent *event) override; void dragMoveEvent(QDragMoveEvent *event) override; void dropEvent(QDropEvent *event) override; + + bool isThingSmallerThanWindowWith(const QTransform &transform) const; + bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const; + + bool m_enableFitInView = false; }; #endif // GRAPHICSVIEW_H