delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / dolphinviewautoscroller.cpp
blob2a57cc157c060e1951b1146db5ace15048abf2a9
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "dolphinviewautoscroller.h"
22 #include <QAbstractItemView>
23 #include <QCoreApplication>
24 #include <QCursor>
25 #include <QEvent>
26 #include <QMouseEvent>
27 #include <QScrollBar>
28 #include <QTimer>
30 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView* parent) :
31 QObject(parent),
32 m_rubberBandSelection(false),
33 m_horizontalScrollInc(0),
34 m_verticalScrollInc(0),
35 m_itemView(parent),
36 m_timer()
38 m_itemView->setAutoScroll(false);
39 m_itemView->viewport()->installEventFilter(this);
41 m_timer = new QTimer(this);
42 m_timer->setSingleShot(false);
43 m_timer->setInterval(1000 / 25); // 25 frames per second
44 connect(m_timer, SIGNAL(timeout()), this, SLOT(scrollViewport()));
47 DolphinViewAutoScroller::~DolphinViewAutoScroller()
51 bool DolphinViewAutoScroller::isActive() const
53 return m_timer->isActive();
56 bool DolphinViewAutoScroller::eventFilter(QObject* watched, QEvent* event)
58 if (watched == m_itemView->viewport()) {
59 switch (event->type()) {
60 case QEvent::MouseButtonPress:
61 if (static_cast<QMouseEvent*>(event)->button() == Qt::LeftButton) {
62 m_rubberBandSelection = true;
64 break;
66 case QEvent::MouseMove:
67 if (m_rubberBandSelection) {
68 triggerAutoScroll();
70 break;
72 case QEvent::MouseButtonRelease:
73 m_rubberBandSelection = false;
74 stopAutoScroll();
75 break;
77 case QEvent::DragEnter:
78 case QEvent::DragMove:
79 m_rubberBandSelection = false;
80 triggerAutoScroll();
81 break;
83 case QEvent::Drop:
84 case QEvent::DragLeave:
85 m_rubberBandSelection = false;
86 stopAutoScroll();
87 break;
89 default:
90 break;
94 return QObject::eventFilter(watched, event);
97 void DolphinViewAutoScroller::scrollViewport()
99 QScrollBar* verticalScrollBar = m_itemView->verticalScrollBar();
100 if (verticalScrollBar != 0) {
101 const int value = verticalScrollBar->value();
102 verticalScrollBar->setValue(value + m_verticalScrollInc);
105 QScrollBar* horizontalScrollBar = m_itemView->horizontalScrollBar();
106 if (horizontalScrollBar != 0) {
107 const int value = horizontalScrollBar->value();
108 horizontalScrollBar->setValue(value + m_horizontalScrollInc);
112 if (m_rubberBandSelection) {
113 // The scrolling does not lead to an update of the rubberband
114 // selection. Fake a mouse move event to let the QAbstractItemView
115 // update the rubberband.
116 QWidget* viewport = m_itemView->viewport();
117 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
118 QMouseEvent event(QEvent::MouseMove, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
119 QCoreApplication::sendEvent(viewport, &event);
123 void DolphinViewAutoScroller::triggerAutoScroll()
125 const bool verticalScrolling = (m_itemView->verticalScrollBar() != 0) &&
126 m_itemView->verticalScrollBar()->isVisible();
127 const bool horizontalScrolling = (m_itemView->horizontalScrollBar() != 0) &&
128 m_itemView->horizontalScrollBar()->isVisible();
129 if (!verticalScrolling && !horizontalScrolling) {
130 // no scrollbars are shown at all, so no autoscrolling is necessary
131 return;
134 QWidget* viewport = m_itemView->viewport();
135 const QPoint pos = viewport->mapFromGlobal(QCursor::pos());
136 if (verticalScrolling) {
137 m_verticalScrollInc = calculateScrollIncrement(pos.y(), viewport->height());
139 if (horizontalScrolling) {
140 m_horizontalScrollInc = calculateScrollIncrement(pos.x(), viewport->width());
143 if (m_timer->isActive()) {
144 if ((m_horizontalScrollInc == 0) && (m_verticalScrollInc == 0)) {
145 m_timer->stop();
147 } else if ((m_horizontalScrollInc != 0) || (m_verticalScrollInc != 0)) {
148 m_timer->start();
152 void DolphinViewAutoScroller::stopAutoScroll()
154 m_timer->stop();
155 m_horizontalScrollInc = 0;
156 m_verticalScrollInc = 0;
159 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos, int rangeSize) const
161 int inc = 0;
163 const int minSpeed = 2;
164 const int maxSpeed = 32;
165 const int speedLimiter = 8;
166 const int autoScrollBorder = 32;
168 if (cursorPos < autoScrollBorder) {
169 inc = -minSpeed + (cursorPos - autoScrollBorder) / speedLimiter;
170 if (inc < -maxSpeed) {
171 inc = -maxSpeed;
173 } else if (cursorPos > rangeSize - autoScrollBorder) {
174 inc = minSpeed + (cursorPos - rangeSize + autoScrollBorder) / speedLimiter;
175 if (inc > maxSpeed) {
176 inc = maxSpeed;
180 return inc;
183 #include "dolphinviewautoscroller.moc"