]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Mousewheel-support: Use smaller scroll-steps
[dolphin.git] / src / kitemviews / kitemlistcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kitemlistcontainer.h"
24
25 #include "kitemlistcontroller.h"
26 #include "kitemlistsmoothscroller_p.h"
27 #include "kitemlistview.h"
28 #include "kitemmodelbase.h"
29
30 #include <QApplication>
31 #include <QGraphicsScene>
32 #include <QGraphicsView>
33 #include <QPropertyAnimation>
34 #include <QScrollBar>
35 #include <QStyle>
36
37 #include <KDebug>
38
39 /**
40 * Replaces the default viewport of KItemListContainer by a
41 * non-scrollable viewport. The scrolling is done in an optimized
42 * way by KItemListView internally.
43 */
44 class KItemListContainerViewport : public QGraphicsView
45 {
46 public:
47 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent);
48 protected:
49 virtual void wheelEvent(QWheelEvent* event);
50 };
51
52 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent) :
53 QGraphicsView(scene, parent)
54 {
55 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
56 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57 setViewportMargins(0, 0, 0, 0);
58 setFrameShape(QFrame::NoFrame);
59 }
60
61 void KItemListContainerViewport::wheelEvent(QWheelEvent* event)
62 {
63 // Assure that the wheel-event gets forwarded to the parent
64 // and not handled at all by QGraphicsView.
65 event->ignore();
66 }
67
68
69
70 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
71 QAbstractScrollArea(parent),
72 m_controller(controller),
73 m_horizontalSmoothScroller(0),
74 m_verticalSmoothScroller(0)
75 {
76 Q_ASSERT(controller);
77 controller->setParent(this);
78 initialize();
79 }
80
81 KItemListContainer::KItemListContainer(QWidget* parent) :
82 QAbstractScrollArea(parent),
83 m_controller(0),
84 m_horizontalSmoothScroller(0),
85 m_verticalSmoothScroller(0)
86 {
87 initialize();
88 }
89
90 KItemListContainer::~KItemListContainer()
91 {
92 }
93
94 KItemListController* KItemListContainer::controller() const
95 {
96 return m_controller;
97 }
98
99 void KItemListContainer::keyPressEvent(QKeyEvent* event)
100 {
101 // TODO: We should find a better way to handle the key press events in the view.
102 // The reasons why we need this hack are:
103 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
104 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
105 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
106 // does not work.
107 KItemListView* view = m_controller->view();
108 if (view) {
109 QApplication::sendEvent(view, event);
110 }
111 }
112
113 void KItemListContainer::showEvent(QShowEvent* event)
114 {
115 QAbstractScrollArea::showEvent(event);
116 updateGeometries();
117 }
118
119 void KItemListContainer::resizeEvent(QResizeEvent* event)
120 {
121 QAbstractScrollArea::resizeEvent(event);
122 updateGeometries();
123 }
124
125 void KItemListContainer::scrollContentsBy(int dx, int dy)
126 {
127 m_horizontalSmoothScroller->scrollContentsBy(dx);
128 m_verticalSmoothScroller->scrollContentsBy(dy);
129 }
130
131 void KItemListContainer::wheelEvent(QWheelEvent* event)
132 {
133 if (event->modifiers().testFlag(Qt::ControlModifier)) {
134 event->ignore();
135 return;
136 }
137
138 KItemListView* view = m_controller->view();
139 if (!view) {
140 event->ignore();
141 return;
142 }
143
144 const bool scrollHorizontally = (event->orientation() == Qt::Horizontal) ||
145 (event->orientation() == Qt::Vertical && !verticalScrollBar()->isVisible());
146 KItemListSmoothScroller* smoothScroller = scrollHorizontally ?
147 m_horizontalSmoothScroller : m_verticalSmoothScroller;
148
149 const int numDegrees = event->delta() / 8;
150 const int numSteps = numDegrees / 15;
151
152 const QScrollBar* scrollBar = smoothScroller->scrollBar();
153 smoothScroller->scrollTo(scrollBar->value() - numSteps * scrollBar->pageStep() / 4);
154
155 event->accept();
156 }
157
158 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
159 {
160 Q_UNUSED(previous);
161 updateSmoothScrollers(current);
162 }
163
164 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
165 {
166 Q_UNUSED(current);
167 Q_UNUSED(previous);
168 }
169
170 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
171 {
172 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
173 if (previous) {
174 scene->removeItem(previous);
175 disconnect(current, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
176 disconnect(previous, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
177 disconnect(previous, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
178 disconnect(previous, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
179 disconnect(previous, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
180 disconnect(previous, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
181 m_horizontalSmoothScroller->setTargetObject(0);
182 m_verticalSmoothScroller->setTargetObject(0);
183 }
184 if (current) {
185 scene->addItem(current);
186 connect(current, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
187 connect(current, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
188 connect(current, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
189 connect(current, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
190 connect(current, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
191 connect(current, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
192 m_horizontalSmoothScroller->setTargetObject(current);
193 m_verticalSmoothScroller->setTargetObject(current);
194 updateSmoothScrollers(current->scrollOrientation());
195 }
196 }
197
198 void KItemListContainer::scrollTo(qreal offset)
199 {
200 const KItemListView* view = m_controller->view();
201 if (view) {
202 if (view->scrollOrientation() == Qt::Vertical) {
203 m_verticalSmoothScroller->scrollTo(offset);
204 } else {
205 m_horizontalSmoothScroller->scrollTo(offset);
206 }
207 }
208 }
209
210 void KItemListContainer::updateScrollOffsetScrollBar()
211 {
212 const KItemListView* view = m_controller->view();
213 if (!view) {
214 return;
215 }
216
217 KItemListSmoothScroller* smoothScroller = 0;
218 QScrollBar* scrollOffsetScrollBar = 0;
219 int singleStep = 0;
220 int pageStep = 0;
221 if (view->scrollOrientation() == Qt::Vertical) {
222 smoothScroller = m_verticalSmoothScroller;
223 scrollOffsetScrollBar = verticalScrollBar();
224 singleStep = view->itemSize().height();
225 pageStep = view->size().height();
226 } else {
227 smoothScroller = m_horizontalSmoothScroller;
228 scrollOffsetScrollBar = horizontalScrollBar();
229 singleStep = view->itemSize().width();
230 pageStep = view->size().width();
231 }
232
233 const int value = view->scrollOffset();
234 const int maximum = qMax(0, int(view->maximumScrollOffset() - pageStep));
235 if (smoothScroller->requestScrollBarUpdate(maximum)) {
236 scrollOffsetScrollBar->setSingleStep(singleStep);
237 scrollOffsetScrollBar->setPageStep(pageStep);
238 scrollOffsetScrollBar->setMinimum(0);
239 scrollOffsetScrollBar->setMaximum(maximum);
240 scrollOffsetScrollBar->setValue(value);
241 }
242 }
243
244 void KItemListContainer::updateItemOffsetScrollBar()
245 {
246 const KItemListView* view = m_controller->view();
247 if (!view) {
248 return;
249 }
250
251 KItemListSmoothScroller* smoothScroller = 0;
252 QScrollBar* itemOffsetScrollBar = 0;
253 int singleStep = 0;
254 int pageStep = 0;
255 if (view->scrollOrientation() == Qt::Vertical) {
256 smoothScroller = m_horizontalSmoothScroller;
257 itemOffsetScrollBar = horizontalScrollBar();
258 singleStep = view->size().width() / 10;
259 pageStep = view->size().width();
260 } else {
261 smoothScroller = m_verticalSmoothScroller;
262 itemOffsetScrollBar = verticalScrollBar();
263 singleStep = view->size().height() / 10;
264 pageStep = view->size().height();
265 }
266
267 const int value = view->itemOffset();
268 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
269 if (smoothScroller->requestScrollBarUpdate(maximum)) {
270 itemOffsetScrollBar->setSingleStep(singleStep);
271 itemOffsetScrollBar->setPageStep(pageStep);
272 itemOffsetScrollBar->setMinimum(0);
273 itemOffsetScrollBar->setMaximum(maximum);
274 itemOffsetScrollBar->setValue(value);
275 }
276 }
277
278 void KItemListContainer::updateGeometries()
279 {
280 QRect rect = geometry();
281
282 const int widthDec = verticalScrollBar()->isVisible()
283 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent)
284 : frameWidth() * 2;
285
286 const int heightDec = horizontalScrollBar()->isVisible()
287 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent)
288 : frameWidth() * 2;
289
290 rect.adjust(0, 0, -widthDec, -heightDec);
291
292 const QRectF newGeometry(0, 0, rect.width(), rect.height());
293 if (m_controller->view()->geometry() != newGeometry) {
294 m_controller->view()->setGeometry(newGeometry);
295
296 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
297 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
298
299 updateScrollOffsetScrollBar();
300 updateItemOffsetScrollBar();
301 }
302 }
303
304 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
305 {
306 if (orientation == Qt::Vertical) {
307 m_verticalSmoothScroller->setPropertyName("scrollOffset");
308 m_horizontalSmoothScroller->setPropertyName("itemOffset");
309 } else {
310 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
311 m_verticalSmoothScroller->setPropertyName("itemOffset");
312 }
313 }
314
315 void KItemListContainer::initialize()
316 {
317 if (!m_controller) {
318 m_controller = new KItemListController(this);
319 }
320
321 connect(m_controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
322 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
323 connect(m_controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
324 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
325
326 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
327 setViewport(graphicsView);
328
329 m_horizontalSmoothScroller = new KItemListSmoothScroller(horizontalScrollBar(), this);
330 m_verticalSmoothScroller = new KItemListSmoothScroller(verticalScrollBar(), this);
331 }
332
333 #include "kitemlistcontainer.moc"