]>
cloud.milkyroute.net Git - dolphin.git/blob - src/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 <QCoreApplication>
26 #include <QMouseEvent>
31 DolphinViewAutoScroller::DolphinViewAutoScroller(QAbstractItemView
* parent
) :
33 m_rubberBandSelection(false),
34 m_horizontalScrollInc(0),
35 m_verticalScrollInc(0),
39 m_itemView
->setAutoScroll(false);
40 m_itemView
->viewport()->installEventFilter(this);
42 m_timer
= new QTimer(this);
43 m_timer
->setSingleShot(false);
44 m_timer
->setInterval(1000 / 25); // 25 frames per second
45 connect(m_timer
, SIGNAL(timeout()), this, SLOT(scrollViewport()));
48 DolphinViewAutoScroller::~DolphinViewAutoScroller()
52 bool DolphinViewAutoScroller::isActive() const
54 return m_timer
->isActive();
57 void DolphinViewAutoScroller::handleCurrentIndexChange(const QModelIndex
& current
,
58 const QModelIndex
& previous
)
60 // When the autoscroller is inactive and a key has been pressed, it must be
61 // assured that the current item stays visible. The check whether the previous
62 // item is valid is important because of #197951.
63 if (current
.isValid() && previous
.isValid() && !isActive()) {
64 m_itemView
->scrollTo(current
);
68 bool DolphinViewAutoScroller::eventFilter(QObject
* watched
, QEvent
* event
)
70 if (watched
== m_itemView
->viewport()) {
71 switch (event
->type()) {
72 case QEvent::MouseButtonPress
:
73 if (static_cast<QMouseEvent
*>(event
)->button() == Qt::LeftButton
) {
74 m_rubberBandSelection
= true;
78 case QEvent::MouseMove
:
79 if (m_rubberBandSelection
) {
84 case QEvent::MouseButtonRelease
:
85 m_rubberBandSelection
= false;
89 case QEvent::DragEnter
:
90 case QEvent::DragMove
:
91 m_rubberBandSelection
= false;
96 case QEvent::DragLeave
:
97 m_rubberBandSelection
= false;
106 return QObject::eventFilter(watched
, event
);
109 void DolphinViewAutoScroller::scrollViewport()
111 QScrollBar
* verticalScrollBar
= m_itemView
->verticalScrollBar();
112 if (verticalScrollBar
!= 0) {
113 const int value
= verticalScrollBar
->value();
114 verticalScrollBar
->setValue(value
+ m_verticalScrollInc
);
117 QScrollBar
* horizontalScrollBar
= m_itemView
->horizontalScrollBar();
118 if (horizontalScrollBar
!= 0) {
119 const int value
= horizontalScrollBar
->value();
120 horizontalScrollBar
->setValue(value
+ m_horizontalScrollInc
);
124 if (m_rubberBandSelection
) {
125 // The scrolling does not lead to an update of the rubberband
126 // selection. Fake a mouse move event to let the QAbstractItemView
127 // update the rubberband.
128 QWidget
* viewport
= m_itemView
->viewport();
129 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
130 QMouseEvent
event(QEvent::MouseMove
, pos
, Qt::LeftButton
, Qt::LeftButton
, Qt::NoModifier
);
131 QCoreApplication::sendEvent(viewport
, &event
);
135 void DolphinViewAutoScroller::triggerAutoScroll()
137 const bool verticalScrolling
= (m_itemView
->verticalScrollBar() != 0) &&
138 m_itemView
->verticalScrollBar()->isVisible();
139 const bool horizontalScrolling
= (m_itemView
->horizontalScrollBar() != 0) &&
140 m_itemView
->horizontalScrollBar()->isVisible();
141 if (!verticalScrolling
&& !horizontalScrolling
) {
142 // no scrollbars are shown at all, so no autoscrolling is necessary
146 QWidget
* viewport
= m_itemView
->viewport();
147 const QPoint pos
= viewport
->mapFromGlobal(QCursor::pos());
148 if (verticalScrolling
) {
149 m_verticalScrollInc
= calculateScrollIncrement(pos
.y(), viewport
->height());
151 if (horizontalScrolling
) {
152 m_horizontalScrollInc
= calculateScrollIncrement(pos
.x(), viewport
->width());
155 if (m_timer
->isActive()) {
156 if ((m_horizontalScrollInc
== 0) && (m_verticalScrollInc
== 0)) {
159 } else if ((m_horizontalScrollInc
!= 0) || (m_verticalScrollInc
!= 0)) {
164 void DolphinViewAutoScroller::stopAutoScroll()
167 m_horizontalScrollInc
= 0;
168 m_verticalScrollInc
= 0;
171 int DolphinViewAutoScroller::calculateScrollIncrement(int cursorPos
, int rangeSize
) const
175 const int minSpeed
= 4;
176 const int maxSpeed
= 768;
177 const int speedLimiter
= 48;
178 const int autoScrollBorder
= 64;
180 if (cursorPos
< autoScrollBorder
) {
181 inc
= -minSpeed
+ qAbs(cursorPos
- autoScrollBorder
) * (cursorPos
- autoScrollBorder
) / speedLimiter
;
182 if (inc
< -maxSpeed
) {
185 } else if (cursorPos
> rangeSize
- autoScrollBorder
) {
186 inc
= minSpeed
+ qAbs(cursorPos
- rangeSize
+ autoScrollBorder
) * (cursorPos
- rangeSize
+ autoScrollBorder
) / speedLimiter
;
187 if (inc
> maxSpeed
) {
195 #include "dolphinviewautoscroller.moc"