From 717a0a9d043bb5052930fa990865850e96a99eb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lund=20Martsum?= Date: Mon, 10 Dec 2012 08:16:31 +0100 Subject: QGraphicsView - emit signal when rubber band changes. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rubberBandRect function is nice to have, but this patch makes it easier to track the rubber band by emiting a signal on change. That makes it easier (and less clumsy/hacky) to show information related to the rubber band. Change-Id: If65eb85d743a1804be3fdb823a821423411e9745 Reviewed-by: Andreas Aardal Hanssen Reviewed-by: Samuel Rødal --- dist/changes-5.1.0 | 2 +- src/widgets/graphicsview/qgraphicsview.cpp | 34 ++++++++++++++++++++-- src/widgets/graphicsview/qgraphicsview.h | 5 ++++ src/widgets/graphicsview/qgraphicsview_p.h | 1 + .../qgraphicsview/rubberband/rubberbandtest.cpp | 27 ++++++----------- 5 files changed, 47 insertions(+), 22 deletions(-) diff --git a/dist/changes-5.1.0 b/dist/changes-5.1.0 index d8ac4efe2f..c9fdba2685 100644 --- a/dist/changes-5.1.0 +++ b/dist/changes-5.1.0 @@ -86,7 +86,7 @@ QtWidgets --------- - QGraphicsView: - * Added function rubberBandRect() + * Added function rubberBandRect() and signal rubberBandChanged. **************************************************************************** * Database Drivers * diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp index 741d6658cc..dd10c95e2a 100644 --- a/src/widgets/graphicsview/qgraphicsview.cpp +++ b/src/widgets/graphicsview/qgraphicsview.cpp @@ -256,6 +256,20 @@ static const int QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS = 503; // largest prime < \sa dragMode, QGraphicsScene::setSelectionArea() */ +/*! + \since 5.1 + + \fn void QGraphicsView::rubberBandChanged(QRect rubberBandRect, QPointF fromScenePoint, QPointF toScenePoint) + + This signal is emitted when the rubber band rect is changed. The viewport Rect is specified by \a rubberBandRect. + The drag start position and drag end position are provided in scene points with \a fromScenePoint and \a toScenePoint. + + When rubberband selection ends this signal will be emitted with null vales. + + \sa rubberBandRect() +*/ + + #include "qgraphicsview.h" #include "qgraphicsview_p.h" @@ -727,16 +741,27 @@ void QGraphicsViewPrivate::updateRubberBand(const QMouseEvent *event) // if we didn't get the release events). if (!event->buttons()) { rubberBanding = false; - rubberBandRect = QRect(); + if (!rubberBandRect.isNull()) { + rubberBandRect = QRect(); + emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF()); + } return; } + QRect oldRubberband = rubberBandRect; + // Update rubberband position const QPoint mp = q->mapFromScene(mousePressScenePoint); const QPoint ep = event->pos(); rubberBandRect = QRect(qMin(mp.x(), ep.x()), qMin(mp.y(), ep.y()), qAbs(mp.x() - ep.x()) + 1, qAbs(mp.y() - ep.y()) + 1); + if (rubberBandRect != oldRubberband || lastRubberbandScenePoint != lastMouseMoveScenePoint) { + lastRubberbandScenePoint = lastMouseMoveScenePoint; + oldRubberband = rubberBandRect; + emit q->rubberBandChanged(rubberBandRect, mousePressScenePoint, lastRubberbandScenePoint); + } + // Update new rubberband if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) { if (viewportUpdateMode != QGraphicsView::FullViewportUpdate) @@ -1518,7 +1543,7 @@ void QGraphicsView::setRubberBandSelectionMode(Qt::ItemSelectionMode mode) Notice that part of this QRect can be outise the visual viewport. It can e.g contain negative values. - \sa rubberBandSelectionMode + \sa rubberBandSelectionMode, rubberBandChanged() */ QRect QGraphicsView::rubberBandRect() const @@ -3316,7 +3341,10 @@ void QGraphicsView::mouseReleaseEvent(QMouseEvent *event) d->updateAll(); } d->rubberBanding = false; - d->rubberBandRect = QRect(); + if (!d->rubberBandRect.isNull()) { + d->rubberBandRect = QRect(); + emit rubberBandChanged(d->rubberBandRect, QPointF(), QPointF()); + } } } else #endif diff --git a/src/widgets/graphicsview/qgraphicsview.h b/src/widgets/graphicsview/qgraphicsview.h index a2981aec27..d812728237 100644 --- a/src/widgets/graphicsview/qgraphicsview.h +++ b/src/widgets/graphicsview/qgraphicsview.h @@ -227,6 +227,11 @@ public Q_SLOTS: void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers); void updateSceneRect(const QRectF &rect); +#ifndef QT_NO_RUBBERBAND +Q_SIGNALS: + void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint); +#endif + protected Q_SLOTS: void setupViewport(QWidget *widget); diff --git a/src/widgets/graphicsview/qgraphicsview_p.h b/src/widgets/graphicsview/qgraphicsview_p.h index d8654ff10f..44d37ef992 100644 --- a/src/widgets/graphicsview/qgraphicsview_p.h +++ b/src/widgets/graphicsview/qgraphicsview_p.h @@ -110,6 +110,7 @@ public: QPoint mousePressViewPoint; QPoint mousePressScreenPoint; QPointF lastMouseMoveScenePoint; + QPointF lastRubberbandScenePoint; QPoint lastMouseMoveScreenPoint; QPoint dirtyScrollOffset; Qt::MouseButton mousePressButton; diff --git a/tests/manual/widgets/qgraphicsview/rubberband/rubberbandtest.cpp b/tests/manual/widgets/qgraphicsview/rubberband/rubberbandtest.cpp index 85881ca67a..f6077e3044 100644 --- a/tests/manual/widgets/qgraphicsview/rubberband/rubberbandtest.cpp +++ b/tests/manual/widgets/qgraphicsview/rubberband/rubberbandtest.cpp @@ -60,11 +60,12 @@ public: class MyGraphicsView : public QGraphicsView { - + Q_OBJECT public: MyGraphicsView(QWidget *w, QLabel *l) : QGraphicsView(w), rubberbandLabel(l) { setDragMode(QGraphicsView::RubberBandDrag); + connect(this, SIGNAL(rubberBandChanged(QRect, QPointF, QPointF)), this, SLOT(updateRubberbandInfo(QRect, QPointF, QPointF))); } protected: void mouseMoveEvent(QMouseEvent *event) @@ -80,29 +81,17 @@ protected: int yglobal = event->globalY(); if (yglobal > bottomPos) verticalScrollBar()->setValue(verticalScrollBar()->value() + 10); - updateRubberbandInfo(); } - void mouseReleaseEvent(QMouseEvent *event) - { - QGraphicsView::mouseReleaseEvent(event); - updateRubberbandInfo(); - } - - void wheelEvent (QWheelEvent *event) - { - QGraphicsView::wheelEvent(event); - updateRubberbandInfo(); - } - - void updateRubberbandInfo() +protected slots: + void updateRubberbandInfo(QRect r, QPointF from, QPointF to) { QString textToShow; QDebug s(&textToShow); - s << rubberBandRect(); - if (rubberbandLabel->text() != textToShow) - rubberbandLabel->setText(textToShow); + s << r << from << to; + rubberbandLabel->setText(textToShow); } +protected: QLabel *rubberbandLabel; }; @@ -135,3 +124,5 @@ int main(int argc, char *argv[]) app.exec(); return 0; } + +#include "rubberbandtest.moc" -- cgit v1.2.3