summaryrefslogtreecommitdiffstats
path: root/src/widgets/graphicsview/qgraphicsview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/widgets/graphicsview/qgraphicsview.cpp')
-rw-r--r--src/widgets/graphicsview/qgraphicsview.cpp135
1 files changed, 89 insertions, 46 deletions
diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp
index 22ec829507..0424517346 100644
--- a/src/widgets/graphicsview/qgraphicsview.cpp
+++ b/src/widgets/graphicsview/qgraphicsview.cpp
@@ -3,7 +3,7 @@
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
-** This file is part of the QtGui module of the Qt Toolkit.
+** This file is part of the QtWidgets module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
@@ -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"
@@ -706,56 +720,61 @@ QRegion QGraphicsViewPrivate::rubberBandRegion(const QWidget *widget, const QRec
return tmp;
}
-void QGraphicsViewPrivate::updateRubberBand(QMouseEvent *event)
+void QGraphicsViewPrivate::updateRubberBand(const QMouseEvent *event)
{
Q_Q(QGraphicsView);
- if (dragMode == QGraphicsView::RubberBandDrag && sceneInteractionAllowed) {
- storeMouseEvent(event);
- if (rubberBanding) {
- // Check for enough drag distance
- if ((mousePressViewPoint - event->pos()).manhattanLength()
- < QApplication::startDragDistance()) {
- return;
- }
+ if (dragMode != QGraphicsView::RubberBandDrag || !sceneInteractionAllowed || !rubberBanding)
+ return;
+ // Check for enough drag distance
+ if ((mousePressViewPoint - event->pos()).manhattanLength() < QApplication::startDragDistance())
+ return;
- // Update old rubberband
- if (viewportUpdateMode != QGraphicsView::NoViewportUpdate && !rubberBandRect.isEmpty()) {
- if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
- q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
- else
- updateAll();
- }
+ // Update old rubberband
+ if (viewportUpdateMode != QGraphicsView::NoViewportUpdate && !rubberBandRect.isEmpty()) {
+ if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
+ q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
+ else
+ updateAll();
+ }
- // Stop rubber banding if the user has let go of all buttons (even
- // if we didn't get the release events).
- if (!event->buttons()) {
- rubberBanding = false;
- rubberBandRect = QRect();
- return;
- }
+ // Stop rubber banding if the user has let go of all buttons (even
+ // if we didn't get the release events).
+ if (!event->buttons()) {
+ rubberBanding = false;
+ if (!rubberBandRect.isNull()) {
+ rubberBandRect = QRect();
+ emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
+ }
+ return;
+ }
- // 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);
+ QRect oldRubberband = rubberBandRect;
- // Update new rubberband
- if (viewportUpdateMode != QGraphicsView::NoViewportUpdate){
- if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
- q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
- else
- updateAll();
- }
- // Set the new selection area
- QPainterPath selectionArea;
- selectionArea.addPolygon(mapToScene(rubberBandRect));
- selectionArea.closeSubpath();
- if (scene)
- scene->setSelectionArea(selectionArea, rubberBandSelectionMode,
- q->viewportTransform());
- }
+ // 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)
+ q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
+ else
+ updateAll();
+ }
+ // Set the new selection area
+ QPainterPath selectionArea;
+ selectionArea.addPolygon(mapToScene(rubberBandRect));
+ selectionArea.closeSubpath();
+ if (scene)
+ scene->setSelectionArea(selectionArea, rubberBandSelectionMode, q->viewportTransform());
}
#endif
@@ -1502,7 +1521,7 @@ void QGraphicsView::setDragMode(DragMode mode)
The default value is Qt::IntersectsItemShape; all items whose shape
intersects with or is contained by the rubber band are selected.
- \sa dragMode, items()
+ \sa dragMode, items(), rubberBandRect()
*/
Qt::ItemSelectionMode QGraphicsView::rubberBandSelectionMode() const
{
@@ -1514,6 +1533,27 @@ void QGraphicsView::setRubberBandSelectionMode(Qt::ItemSelectionMode mode)
Q_D(QGraphicsView);
d->rubberBandSelectionMode = mode;
}
+
+/*!
+ \since 5.1
+ This functions returns the current rubber band area (in viewport coordinates) if the user
+ is currently doing an itemselection with rubber band. When the user is not using the
+ rubber band this functions returns (a null) QRectF().
+
+ Notice that part of this QRect can be outise the visual viewport. It can e.g
+ contain negative values.
+
+ \sa rubberBandSelectionMode, rubberBandChanged()
+*/
+
+QRect QGraphicsView::rubberBandRect() const
+{
+ Q_D(const QGraphicsView);
+ if (d->dragMode != QGraphicsView::RubberBandDrag || !d->sceneInteractionAllowed || !d->rubberBanding)
+ return QRect();
+
+ return d->rubberBandRect;
+}
#endif
/*!
@@ -3301,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