QGraphicsView - emit signal when rubber band changes.

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 <andreas@hanssen.name>
Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
This commit is contained in:
Thorbjørn Lund Martsum 2012-12-10 08:16:31 +01:00 committed by The Qt Project
parent c56f73cc1e
commit 717a0a9d04
5 changed files with 47 additions and 22 deletions

2
dist/changes-5.1.0 vendored
View File

@ -86,7 +86,7 @@ QtWidgets
--------- ---------
- QGraphicsView: - QGraphicsView:
* Added function rubberBandRect() * Added function rubberBandRect() and signal rubberBandChanged.
**************************************************************************** ****************************************************************************
* Database Drivers * * Database Drivers *

View File

@ -256,6 +256,20 @@ static const int QGRAPHICSVIEW_PREALLOC_STYLE_OPTIONS = 503; // largest prime <
\sa dragMode, QGraphicsScene::setSelectionArea() \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.h"
#include "qgraphicsview_p.h" #include "qgraphicsview_p.h"
@ -727,16 +741,27 @@ void QGraphicsViewPrivate::updateRubberBand(const QMouseEvent *event)
// if we didn't get the release events). // if we didn't get the release events).
if (!event->buttons()) { if (!event->buttons()) {
rubberBanding = false; rubberBanding = false;
if (!rubberBandRect.isNull()) {
rubberBandRect = QRect(); rubberBandRect = QRect();
emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
}
return; return;
} }
QRect oldRubberband = rubberBandRect;
// Update rubberband position // Update rubberband position
const QPoint mp = q->mapFromScene(mousePressScenePoint); const QPoint mp = q->mapFromScene(mousePressScenePoint);
const QPoint ep = event->pos(); const QPoint ep = event->pos();
rubberBandRect = QRect(qMin(mp.x(), ep.x()), qMin(mp.y(), ep.y()), rubberBandRect = QRect(qMin(mp.x(), ep.x()), qMin(mp.y(), ep.y()),
qAbs(mp.x() - ep.x()) + 1, qAbs(mp.y() - ep.y()) + 1); 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 // Update new rubberband
if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) { if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
if (viewportUpdateMode != QGraphicsView::FullViewportUpdate) 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 Notice that part of this QRect can be outise the visual viewport. It can e.g
contain negative values. contain negative values.
\sa rubberBandSelectionMode \sa rubberBandSelectionMode, rubberBandChanged()
*/ */
QRect QGraphicsView::rubberBandRect() const QRect QGraphicsView::rubberBandRect() const
@ -3316,7 +3341,10 @@ void QGraphicsView::mouseReleaseEvent(QMouseEvent *event)
d->updateAll(); d->updateAll();
} }
d->rubberBanding = false; d->rubberBanding = false;
if (!d->rubberBandRect.isNull()) {
d->rubberBandRect = QRect(); d->rubberBandRect = QRect();
emit rubberBandChanged(d->rubberBandRect, QPointF(), QPointF());
}
} }
} else } else
#endif #endif

View File

@ -227,6 +227,11 @@ public Q_SLOTS:
void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers); void invalidateScene(const QRectF &rect = QRectF(), QGraphicsScene::SceneLayers layers = QGraphicsScene::AllLayers);
void updateSceneRect(const QRectF &rect); void updateSceneRect(const QRectF &rect);
#ifndef QT_NO_RUBBERBAND
Q_SIGNALS:
void rubberBandChanged(QRect viewportRect, QPointF fromScenePoint, QPointF toScenePoint);
#endif
protected Q_SLOTS: protected Q_SLOTS:
void setupViewport(QWidget *widget); void setupViewport(QWidget *widget);

View File

@ -110,6 +110,7 @@ public:
QPoint mousePressViewPoint; QPoint mousePressViewPoint;
QPoint mousePressScreenPoint; QPoint mousePressScreenPoint;
QPointF lastMouseMoveScenePoint; QPointF lastMouseMoveScenePoint;
QPointF lastRubberbandScenePoint;
QPoint lastMouseMoveScreenPoint; QPoint lastMouseMoveScreenPoint;
QPoint dirtyScrollOffset; QPoint dirtyScrollOffset;
Qt::MouseButton mousePressButton; Qt::MouseButton mousePressButton;

View File

@ -60,11 +60,12 @@ public:
class MyGraphicsView : public QGraphicsView class MyGraphicsView : public QGraphicsView
{ {
Q_OBJECT
public: public:
MyGraphicsView(QWidget *w, QLabel *l) : QGraphicsView(w), rubberbandLabel(l) MyGraphicsView(QWidget *w, QLabel *l) : QGraphicsView(w), rubberbandLabel(l)
{ {
setDragMode(QGraphicsView::RubberBandDrag); setDragMode(QGraphicsView::RubberBandDrag);
connect(this, SIGNAL(rubberBandChanged(QRect, QPointF, QPointF)), this, SLOT(updateRubberbandInfo(QRect, QPointF, QPointF)));
} }
protected: protected:
void mouseMoveEvent(QMouseEvent *event) void mouseMoveEvent(QMouseEvent *event)
@ -80,29 +81,17 @@ protected:
int yglobal = event->globalY(); int yglobal = event->globalY();
if (yglobal > bottomPos) if (yglobal > bottomPos)
verticalScrollBar()->setValue(verticalScrollBar()->value() + 10); verticalScrollBar()->setValue(verticalScrollBar()->value() + 10);
updateRubberbandInfo();
} }
void mouseReleaseEvent(QMouseEvent *event) protected slots:
{ void updateRubberbandInfo(QRect r, QPointF from, QPointF to)
QGraphicsView::mouseReleaseEvent(event);
updateRubberbandInfo();
}
void wheelEvent (QWheelEvent *event)
{
QGraphicsView::wheelEvent(event);
updateRubberbandInfo();
}
void updateRubberbandInfo()
{ {
QString textToShow; QString textToShow;
QDebug s(&textToShow); QDebug s(&textToShow);
s << rubberBandRect(); s << r << from << to;
if (rubberbandLabel->text() != textToShow)
rubberbandLabel->setText(textToShow); rubberbandLabel->setText(textToShow);
} }
protected:
QLabel *rubberbandLabel; QLabel *rubberbandLabel;
}; };
@ -135,3 +124,5 @@ int main(int argc, char *argv[])
app.exec(); app.exec();
return 0; return 0;
} }
#include "rubberbandtest.moc"