X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/35f762cf28dc7c270626ba3b95f3123e99a37616..b1c9b5126d:/src/dolphinviewautoscroller.cpp diff --git a/src/dolphinviewautoscroller.cpp b/src/dolphinviewautoscroller.cpp index c905bb784..2a57cc157 100644 --- a/src/dolphinviewautoscroller.cpp +++ b/src/dolphinviewautoscroller.cpp @@ -30,13 +30,14 @@ DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView* parent) : QObject(parent), m_rubberBandSelection(false), - m_scrollInc(0), + m_horizontalScrollInc(0), + m_verticalScrollInc(0), m_itemView(parent), m_timer() { m_itemView->setAutoScroll(false); m_itemView->viewport()->installEventFilter(this); - + m_timer = new QTimer(this); m_timer->setSingleShot(false); m_timer->setInterval(1000 / 25); // 25 frames per second @@ -47,37 +48,44 @@ DolphinViewAutoScroller::~DolphinViewAutoScroller() { } -#include +bool DolphinViewAutoScroller::isActive() const +{ + return m_timer->isActive(); +} + bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event) { if (watched == m_itemView->viewport()) { switch (event->type()) { case QEvent::MouseButtonPress: - m_rubberBandSelection = true; + if (static_cast(event)->button() == Qt::LeftButton) { + m_rubberBandSelection = true; + } break; - + case QEvent::MouseMove: if (m_rubberBandSelection) { triggerAutoScroll(); } break; - + case QEvent::MouseButtonRelease: m_rubberBandSelection = false; stopAutoScroll(); break; - + case QEvent::DragEnter: case QEvent::DragMove: m_rubberBandSelection = false; triggerAutoScroll(); break; - + + case QEvent::Drop: case QEvent::DragLeave: m_rubberBandSelection = false; stopAutoScroll(); break; - + default: break; } @@ -88,58 +96,55 @@ bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event) void DolphinViewAutoScroller::scrollViewport() { - // TODO: implement horizontal scrolling QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar(); if (verticalScrollBar != 0) { const int value = verticalScrollBar->value(); - verticalScrollBar->setValue(value + m_scrollInc); - - if (m_rubberBandSelection) { - // The scrolling does not lead to an update of the rubberband - // selection. Fake a mouse move event to let the QAbstractItemView - // 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); - QCoreApplication::sendEvent(viewport, &event); - } + verticalScrollBar->setValue(value + m_verticalScrollInc); + + } + QScrollBar* horizontalScrollBar = m_itemView->horizontalScrollBar(); + if (horizontalScrollBar != 0) { + const int value = horizontalScrollBar->value(); + horizontalScrollBar->setValue(value + m_horizontalScrollInc); + + } + + if (m_rubberBandSelection) { + // The scrolling does not lead to an update of the rubberband + // selection. Fake a mouse move event to let the QAbstractItemView + // 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); + QCoreApplication::sendEvent(viewport, &event); } } void DolphinViewAutoScroller::triggerAutoScroll() { - // TODO: implement horizontal scrolling - - const int startSpeed = 2; - const int speedLimiter = 8; - const int scrollIncMax = 32; - - const int autoScrollBorder = 32; - + const bool verticalScrolling = (m_itemView->verticalScrollBar() != 0) && + m_itemView->verticalScrollBar()->isVisible(); + const bool horizontalScrolling = (m_itemView->horizontalScrollBar() != 0) && + m_itemView->horizontalScrollBar()->isVisible(); + if (!verticalScrolling && !horizontalScrolling) { + // no scrollbars are shown at all, so no autoscrolling is necessary + return; + } + QWidget* viewport = m_itemView->viewport(); const QPoint pos = viewport->mapFromGlobal(QCursor::pos()); - if (pos.y() < autoScrollBorder) { - // scroll up - m_scrollInc = -startSpeed + (pos.y() - autoScrollBorder) / speedLimiter; - if (m_scrollInc < -scrollIncMax) { - m_scrollInc = -scrollIncMax; - } - } else if (pos.y() > viewport->height() - autoScrollBorder) { - // scroll down - m_scrollInc = startSpeed + (pos.y() - viewport->height() + autoScrollBorder) / speedLimiter; - if (m_scrollInc > scrollIncMax) { - m_scrollInc = scrollIncMax; - } - } else { - // no scrolling - m_scrollInc = 0; + if (verticalScrolling) { + m_verticalScrollInc = calculateScrollIncrement(pos.y(), viewport->height()); + } + if (horizontalScrolling) { + m_horizontalScrollInc = calculateScrollIncrement(pos.x(), viewport->width()); } - + if (m_timer->isActive()) { - if (m_scrollInc == 0) { + if ((m_horizontalScrollInc == 0) && (m_verticalScrollInc == 0)) { m_timer->stop(); } - } else if (m_scrollInc != 0) { + } else if ((m_horizontalScrollInc != 0) || (m_verticalScrollInc != 0)) { m_timer->start(); } } @@ -147,7 +152,32 @@ void DolphinViewAutoScroller::triggerAutoScroll() void DolphinViewAutoScroller::stopAutoScroll() { m_timer->stop(); - m_scrollInc = 0; + m_horizontalScrollInc = 0; + m_verticalScrollInc = 0; +} + +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; + + if (cursorPos < autoScrollBorder) { + inc = -minSpeed + (cursorPos - autoScrollBorder) / speedLimiter; + if (inc < -maxSpeed) { + inc = -maxSpeed; + } + } else if (cursorPos > rangeSize - autoScrollBorder) { + inc = minSpeed + (cursorPos - rangeSize + autoScrollBorder) / speedLimiter; + if (inc > maxSpeed) { + inc = maxSpeed; + } + } + + return inc; } #include "dolphinviewautoscroller.moc"