QGraphicsView: Clear the rubber band when dragMode changes

Without this change, and ongoing rubber-band selection will not be
cleared when the dragmode property changes (e.g. because of a key press)
which leaves a rubber-band displayed on the view.

Move the rubber-band-clearing code from mouseReleaseEvent into a private
helper that is then called from setDragMode.

Change-Id: I32c15f7fe4ef2b8002ef5dd58e22f4bb30dd2409
Fixes: QTBUG-65186
Reviewed-by: Jan Arve Sæther <jan-arve.saether@qt.io>
This commit is contained in:
Volker Hilsheimer 2020-02-17 16:01:20 +01:00
parent a5ea2f7e99
commit 02e0c30a8a
2 changed files with 27 additions and 14 deletions

View File

@ -786,6 +786,27 @@ void QGraphicsViewPrivate::updateRubberBand(const QMouseEvent *event)
if (scene)
scene->setSelectionArea(selectionArea, rubberBandSelectionOperation, rubberBandSelectionMode, q->viewportTransform());
}
void QGraphicsViewPrivate::clearRubberBand()
{
Q_Q(QGraphicsView);
if (dragMode != QGraphicsView::RubberBandDrag || !sceneInteractionAllowed || !rubberBanding)
return;
if (viewportUpdateMode != QGraphicsView::NoViewportUpdate) {
if (viewportUpdateMode != QGraphicsView::FullViewportUpdate)
q->viewport()->update(rubberBandRegion(q->viewport(), rubberBandRect));
else
updateAll();
}
rubberBanding = false;
rubberBandSelectionOperation = Qt::ReplaceSelection;
if (!rubberBandRect.isNull()) {
rubberBandRect = QRect();
emit q->rubberBandChanged(rubberBandRect, QPointF(), QPointF());
}
}
#endif
/*!
@ -1489,6 +1510,10 @@ void QGraphicsView::setDragMode(DragMode mode)
if (d->dragMode == mode)
return;
#if QT_CONFIG(rubberband)
d->clearRubberBand();
#endif
#ifndef QT_NO_CURSOR
if (d->dragMode == ScrollHandDrag)
viewport()->unsetCursor();
@ -3358,20 +3383,7 @@ void QGraphicsView::mouseReleaseEvent(QMouseEvent *event)
#if QT_CONFIG(rubberband)
if (d->dragMode == QGraphicsView::RubberBandDrag && d->sceneInteractionAllowed && !event->buttons()) {
if (d->rubberBanding) {
if (d->viewportUpdateMode != QGraphicsView::NoViewportUpdate){
if (d->viewportUpdateMode != FullViewportUpdate)
viewport()->update(d->rubberBandRegion(viewport(), d->rubberBandRect));
else
d->updateAll();
}
d->rubberBanding = false;
d->rubberBandSelectionOperation = Qt::ReplaceSelection;
if (!d->rubberBandRect.isNull()) {
d->rubberBandRect = QRect();
emit rubberBandChanged(d->rubberBandRect, QPointF(), QPointF());
}
}
d->clearRubberBand();
} else
#endif
if (d->dragMode == QGraphicsView::ScrollHandDrag && event->button() == Qt::LeftButton) {

View File

@ -140,6 +140,7 @@ public:
QRect rubberBandRect;
QRegion rubberBandRegion(const QWidget *widget, const QRect &rect) const;
void updateRubberBand(const QMouseEvent *event);
void clearRubberBand();
bool rubberBanding;
Qt::ItemSelectionMode rubberBandSelectionMode;
Qt::ItemSelectionOperation rubberBandSelectionOperation;