]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinviewautoscroller.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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 <QApplication>
26 #include <QMouseEvent>
31 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView
* parent
) :
33 m_rubberBandSelection(false),
35 m_initializedTimestamp(false),
36 m_horizontalScrollInc(0),
37 m_verticalScrollInc(0),
42 m_itemView
->setAutoScroll(false);
43 m_itemView
->viewport()->installEventFilter(this);
44 m_itemView
->installEventFilter(this);
46 m_timer
= new QTimer(this);
47 m_timer
->setSingleShot(false);
48 m_timer
->setInterval(1000 / 25); // 25 frames per second
49 connect(m_timer
, SIGNAL(timeout()), this, SLOT(scrollViewport()));
52 DolphinViewAutoScroller::~DolphinViewAutoScroller()
56 bool DolphinViewAutoScroller::isActive() const
58 return m_timer
->isActive();
61 void DolphinViewAutoScroller::handleCurrentIndexChange(const QModelIndex
& current
,
62 const QModelIndex
& previous
)
64 // When the autoscroller is inactive and a key has been pressed, it must be
65 // assured that the current item stays visible. The check whether the previous
66 // item is valid is important because of #197951. The keypress check is done
67 // because of #199833.
68 if (current
.isValid() && (previous
.isValid() || m_keyPressed
) && !isActive()) {
69 m_itemView
->scrollTo(current
);
73 bool DolphinViewAutoScroller::eventFilter(QObject
* watched
, QEvent
* event
)
75 if (watched
== m_itemView
->viewport()) {
76 switch (event
->type()) {
77 case QEvent::MouseButtonPress
:
78 if (static_cast<QMouseEvent
*>(event
)->button() == Qt::LeftButton
) {
79 m_rubberBandSelection
= true;
83 case QEvent::MouseMove
:
84 if (m_rubberBandSelection
) {
89 case QEvent::MouseButtonRelease
:
90 m_rubberBandSelection
= false;
94 case QEvent::DragEnter
:
95 case QEvent::DragMove
:
96 m_rubberBandSelection
= false;
101 case QEvent::DragLeave
:
102 m_rubberBandSelection
= false;
109 } else if (watched
== m_itemView
) {
110 switch (event
->type()) {
111 case QEvent::KeyPress
:
115 case QEvent::KeyRelease
:
116 m_keyPressed
= false;
124 return QObject::eventFilter(watched
, event
);
127 void DolphinViewAutoScroller::scrollViewport()
129 if (m_timestamp
.elapsed() < QApplication::startDragTime()) {
133 QScrollBar
* verticalScrollBar
= m_itemView
->verticalScrollBar();
134 if (verticalScrollBar
!= 0) {
135 const int value
= verticalScrollBar
->value();
136 verticalScrollBar
->setValue(value
+ m_verticalScrollInc
);
139 QScrollBar
* horizontalScrollBar
= m_itemView
->horizontalScrollBar();
140 if (horizontalScrollBar
!= 0) {
141 const int value
= horizontalScrollBar
->value();
142 horizontalScrollBar
->setValue(value
+ m_horizontalScrollInc
);
146 if (m_rubberBandSelection
) {
147 // The scrolling does not lead to an update of the rubberband
148 // selection. Fake a mouse move event to let the QAbstractItemView
149 // update the rubberband.
150 QWidget
* viewport
= m_itemView
->viewport();
151 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
152 QMouseEvent
event(QEvent::MouseMove
, pos
, Qt::LeftButton
, Qt::LeftButton
, QApplication::keyboardModifiers());
153 QCoreApplication::sendEvent(viewport
, &event
);
157 void DolphinViewAutoScroller::triggerAutoScroll()
159 const bool verticalScrolling
= (m_itemView
->verticalScrollBar() != 0) &&
160 m_itemView
->verticalScrollBar()->isVisible();
161 const bool horizontalScrolling
= (m_itemView
->horizontalScrollBar() != 0) &&
162 m_itemView
->horizontalScrollBar()->isVisible();
163 if (!verticalScrolling
&& !horizontalScrolling
) {
164 // no scrollbars are shown at all, so no autoscrolling is necessary
169 QWidget
* viewport
= m_itemView
->viewport();
170 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
171 if (verticalScrolling
) {
172 m_verticalScrollInc
= calculateScrollIncrement(pos
.y(), viewport
->height());
174 if (horizontalScrolling
) {
175 m_horizontalScrollInc
= calculateScrollIncrement(pos
.x(), viewport
->width());
178 if (m_timer
->isActive()) {
179 if ((m_horizontalScrollInc
== 0) && (m_verticalScrollInc
== 0)) {
182 } else if ((m_horizontalScrollInc
!= 0) || (m_verticalScrollInc
!= 0)) {
183 if (!m_initializedTimestamp
) {
184 m_initializedTimestamp
= true;
191 void DolphinViewAutoScroller::stopAutoScroll()
194 m_horizontalScrollInc
= 0;
195 m_verticalScrollInc
= 0;
196 m_initializedTimestamp
= false;
199 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos
, int rangeSize
) const
203 const int minSpeed
= 4;
204 const int maxSpeed
= 768;
205 const int speedLimiter
= 48;
206 const int autoScrollBorder
= 64;
208 if (cursorPos
< autoScrollBorder
) {
209 inc
= -minSpeed
+ qAbs(cursorPos
- autoScrollBorder
) * (cursorPos
- autoScrollBorder
) / speedLimiter
;
210 if (inc
< -maxSpeed
) {
213 } else if (cursorPos
> rangeSize
- autoScrollBorder
) {
214 inc
= minSpeed
+ qAbs(cursorPos
- rangeSize
+ autoScrollBorder
) * (cursorPos
- rangeSize
+ autoScrollBorder
) / speedLimiter
;
215 if (inc
> maxSpeed
) {
223 #include "dolphinviewautoscroller.moc"