QGraphicsView - fix rubberband to expand on wheel event
authorThorbjørn Lund Martsum <tmartsum@gmail.com>
Sun, 2 Dec 2012 16:04:47 +0000 (17:04 +0100)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 11 Dec 2012 12:37:57 +0000 (13:37 +0100)
In SHA 51914375b615ddcac711171ac31779fea01b4323 the rubberband
selection was fixed, so it followed the scene-point on mousemove.

However wheelEvent could move the view - but avoid update of the
rubberband (that would not be updated until next mouse-move).

This patch fixes that (and generally improves rubberband behavior)
since QGraphicsViewPrivate::mouseMoveEventHandler is called by
replayLastMouseEvent, which is called from various places,
where we need to update the rubberband (e.g scrollContentsBy).

Change-Id: I1b78c27edaaecea797a2319086d7a11d437d2bd3
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
src/widgets/graphicsview/qgraphicsview.cpp
tests/manual/widgets/qgraphicsview/rubberband/rubberbandtest.cpp

index 3da202c..a1ee562 100644 (file)
@@ -616,6 +616,10 @@ void QGraphicsViewPrivate::mouseMoveEventHandler(QMouseEvent *event)
 {
     Q_Q(QGraphicsView);
 
+#ifndef QT_NO_RUBBERBAND
+    updateRubberBand(event);
+#endif
+
     storeMouseEvent(event);
     lastMouseEvent.setAccepted(false);
 
@@ -3263,10 +3267,6 @@ void QGraphicsView::mouseMoveEvent(QMouseEvent *event)
 {
     Q_D(QGraphicsView);
 
-#ifndef QT_NO_RUBBERBAND
-    d->updateRubberBand(event);
-#endif
-
     if (d->dragMode == QGraphicsView::ScrollHandDrag) {
         if (d->handScrolling) {
             QScrollBar *hBar = horizontalScrollBar();
index d8c2de6..186203e 100644 (file)
@@ -69,11 +69,17 @@ public:
 protected:
     void mouseMoveEvent(QMouseEvent *event)
     {
+        QGraphicsView::mouseMoveEvent(event);
+
         int rightmostInView = viewport()->mapToGlobal(viewport()->geometry().topRight()).x();
         int xglobal = event->globalX();
         if (xglobal > rightmostInView)
             horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 10);
-        QGraphicsView::mouseMoveEvent(event);
+
+        int bottomPos = viewport()->mapToGlobal(viewport()->geometry().bottomRight()).y();
+        int yglobal = event->globalY();
+        if (yglobal > bottomPos)
+            verticalScrollBar()->setValue(verticalScrollBar()->value() + 10);
     }
 };
 
@@ -82,17 +88,18 @@ int main(int argc, char *argv[])
     QApplication app(argc, argv);
     MyGraphicsView v;
 
-    QGraphicsScene s(0.0, 0.0, 10000.0, 100.0);
+    QGraphicsScene s(0.0, 0.0, 5000.0, 5000.0);
     v.setScene(&s);
     v.setInteractive(true);
     v.setRubberBandSelectionMode(Qt::IntersectsItemBoundingRect);
     s.addRect( (qreal) 0.0, 0.0, 1000.0, 50.0, QPen(),QBrush(QColor(0,0,255)));
 
-    for (int u = 0; u < 100; ++u) {
-        MyGraphicsItem *item = new MyGraphicsItem();
-        item->setRect(QRectF(u * 100, 50.0, 50.0, 20.0));
-        s.addItem(item);
-    }
+    for (int u = 0; u < 100; ++u)
+        for (int v = 0; v < 100; ++v) {
+            MyGraphicsItem *item = new MyGraphicsItem();
+            item->setRect(QRectF(v * 80.0, u * 80.0, 50.0, 20.0));
+            s.addItem(item);
+        }
     v.show();
     app.exec();
     return 0;