]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinviewautoscroller.cpp
Fix the nasty layout reparenting by adding the InformationPanelContent into a layout...
[dolphin.git] / src / dolphinviewautoscroller.cpp
index 767a5b45814fce33c1c1f55e8f073246a62826ab..45896a5eb5cb353087bab4083b0aafaca714b894 100644 (file)
 #include "dolphinviewautoscroller.h"
 
 #include <QAbstractItemView>
-#include <QCoreApplication>
+#include <QApplication>
 #include <QCursor>
 #include <QEvent>
 #include <QMouseEvent>
 #include <QScrollBar>
 #include <QTimer>
+#include <math.h>
 
 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView* parent) :
     QObject(parent),
     m_rubberBandSelection(false),
+    m_keyPressed(false),
+    m_initializedTimestamp(false),
     m_horizontalScrollInc(0),
     m_verticalScrollInc(0),
     m_itemView(parent),
-    m_timer()
+    m_timer(0),
+    m_timestamp()
 {
     m_itemView->setAutoScroll(false);
     m_itemView->viewport()->installEventFilter(this);
+    m_itemView->installEventFilter(this);
 
     m_timer = new QTimer(this);
     m_timer->setSingleShot(false);
@@ -48,6 +53,23 @@ DolphinViewAutoScroller::~DolphinViewAutoScroller()
 {
 }
 
+bool DolphinViewAutoScroller::isActive() const
+{
+    return m_timer->isActive();
+}
+
+void DolphinViewAutoScroller::handleCurrentIndexChange(const QModelIndex& current,
+                                                       const QModelIndex& previous)
+{
+    // When the autoscroller is inactive and a key has been pressed, it must be
+    // assured that the current item stays visible. The check whether the previous
+    // item is valid is important because of #197951. The keypress check is done
+    // because of #199833.
+    if (current.isValid() && (previous.isValid() || m_keyPressed) && !isActive()) {
+        m_itemView->scrollTo(current);
+    }
+}
+
 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
 {
     if (watched == m_itemView->viewport()) {
@@ -81,6 +103,19 @@ bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
             stopAutoScroll();
             break;
 
+        default:
+            break;
+        }
+    } else if (watched == m_itemView) {
+        switch (event->type()) {
+        case QEvent::KeyPress:
+            m_keyPressed = true;
+            break;
+
+        case QEvent::KeyRelease:
+            m_keyPressed = false;
+            break;
+
         default:
             break;
         }
@@ -91,6 +126,10 @@ bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
 
 void DolphinViewAutoScroller::scrollViewport()
 {
+    if (m_timestamp.elapsed() < QApplication::startDragTime()) {
+        return;
+    }
+
     QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
     if (verticalScrollBar != 0) {
         const int value = verticalScrollBar->value();
@@ -110,7 +149,7 @@ void DolphinViewAutoScroller::scrollViewport()
         // update the rubberband.
         QWidget* viewport = m_itemView->viewport();
         const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
-        QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
+        QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, QApplication::keyboardModifiers());
         QCoreApplication::sendEvent(viewport, &event);
     }
 }
@@ -123,6 +162,7 @@ void DolphinViewAutoScroller::triggerAutoScroll()
                                      m_itemView->horizontalScrollBar()->isVisible();
     if (!verticalScrolling && !horizontalScrolling) {
         // no scrollbars are shown at all, so no autoscrolling is necessary
+        stopAutoScroll();
         return;
     }
 
@@ -137,9 +177,13 @@ void DolphinViewAutoScroller::triggerAutoScroll()
 
     if (m_timer->isActive()) {
         if ((m_horizontalScrollInc == 0) && (m_verticalScrollInc == 0)) {
-            m_timer->stop();
+            stopAutoScroll();
         }
     } else if ((m_horizontalScrollInc != 0) || (m_verticalScrollInc != 0)) {
+        if (!m_initializedTimestamp) {
+            m_initializedTimestamp = true;
+            m_timestamp.start();
+        }
         m_timer->start();
     }
 }
@@ -149,24 +193,25 @@ void DolphinViewAutoScroller::stopAutoScroll()
     m_timer->stop();
     m_horizontalScrollInc = 0;
     m_verticalScrollInc = 0;
+    m_initializedTimestamp = false;
 }
 
 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos, int rangeSize) const
 {
     int inc = 0;
 
-    const int minSpeed = 2;
-    const int maxSpeed = 32;
-    const int speedLimiter = 8;
-    const int autoScrollBorder = 32;
+    const int minSpeed = 4;
+    const int maxSpeed = 768;
+    const int speedLimiter = 48;
+    const int autoScrollBorder = 64;
 
     if (cursorPos < autoScrollBorder) {
-        inc = -minSpeed + (cursorPos - autoScrollBorder) / speedLimiter;
+        inc = -minSpeed + qAbs(cursorPos - autoScrollBorder) * (cursorPos - autoScrollBorder) / speedLimiter;
         if (inc < -maxSpeed) {
             inc = -maxSpeed;
         }
     } else if (cursorPos > rangeSize - autoScrollBorder) {
-        inc = minSpeed + (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
+        inc = minSpeed + qAbs(cursorPos - rangeSize + autoScrollBorder) * (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
         if (inc > maxSpeed) {
             inc = maxSpeed;
         }