]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Take into account QApplication::wheelScrollLines() in wheel events
[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 "kitemlistview.h"
27 #include "kitemmodelbase.h"
28
29 #include "private/kitemlistsmoothscroller.h"
30
31 #include <QApplication>
32 #include <QGraphicsScene>
33 #include <QGraphicsView>
34 #include <QScrollBar>
35 #include <QStyle>
36 #include <QStyleOption>
37
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 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
69 QAbstractScrollArea(parent),
70 m_controller(controller),
71 m_horizontalSmoothScroller(0),
72 m_verticalSmoothScroller(0)
73 {
74 Q_ASSERT(controller);
75 controller->setParent(this);
76
77 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
78 setViewport(graphicsView);
79
80 m_horizontalSmoothScroller = new KItemListSmoothScroller(horizontalScrollBar(), this);
81 m_verticalSmoothScroller = new KItemListSmoothScroller(verticalScrollBar(), this);
82
83 if (controller->model()) {
84 slotModelChanged(controller->model(), 0);
85 }
86 if (controller->view()) {
87 slotViewChanged(controller->view(), 0);
88 }
89
90 connect(controller, &KItemListController::modelChanged,
91 this, &KItemListContainer::slotModelChanged);
92 connect(controller, &KItemListController::viewChanged,
93 this, &KItemListContainer::slotViewChanged);
94 }
95
96 KItemListContainer::~KItemListContainer()
97 {
98 // Don't rely on the QObject-order to delete the controller, otherwise
99 // the QGraphicsScene might get deleted before the view.
100 delete m_controller;
101 m_controller = 0;
102 }
103
104 KItemListController* KItemListContainer::controller() const
105 {
106 return m_controller;
107 }
108
109 void KItemListContainer::setEnabledFrame(bool enable)
110 {
111 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(viewport());
112 if (enable) {
113 setFrameShape(QFrame::StyledPanel);
114 graphicsView->setPalette(palette());
115 graphicsView->viewport()->setAutoFillBackground(true);
116 } else {
117 setFrameShape(QFrame::NoFrame);
118 // Make the background of the container transparent and apply the window-text color
119 // to the text color, so that enough contrast is given for all color
120 // schemes
121 QPalette p = graphicsView->palette();
122 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
123 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
124 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
125 graphicsView->setPalette(p);
126 graphicsView->viewport()->setAutoFillBackground(false);
127 }
128 }
129
130 bool KItemListContainer::enabledFrame() const
131 {
132 const QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(viewport());
133 return graphicsView->autoFillBackground();
134 }
135
136 void KItemListContainer::keyPressEvent(QKeyEvent* event)
137 {
138 // TODO: We should find a better way to handle the key press events in the view.
139 // The reasons why we need this hack are:
140 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
141 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
142 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
143 // does not work.
144 KItemListView* view = m_controller->view();
145 if (view) {
146 QApplication::sendEvent(view, event);
147 }
148 }
149
150 void KItemListContainer::showEvent(QShowEvent* event)
151 {
152 QAbstractScrollArea::showEvent(event);
153 updateGeometries();
154 }
155
156 void KItemListContainer::resizeEvent(QResizeEvent* event)
157 {
158 QAbstractScrollArea::resizeEvent(event);
159 updateGeometries();
160 }
161
162 void KItemListContainer::scrollContentsBy(int dx, int dy)
163 {
164 m_horizontalSmoothScroller->scrollContentsBy(dx);
165 m_verticalSmoothScroller->scrollContentsBy(dy);
166 }
167
168 void KItemListContainer::wheelEvent(QWheelEvent* event)
169 {
170 if (event->modifiers().testFlag(Qt::ControlModifier)) {
171 event->ignore();
172 return;
173 }
174
175 KItemListView* view = m_controller->view();
176 if (!view) {
177 event->ignore();
178 return;
179 }
180
181 const bool scrollHorizontally = (event->orientation() == Qt::Horizontal) ||
182 (event->orientation() == Qt::Vertical && !verticalScrollBar()->isVisible());
183 KItemListSmoothScroller* smoothScroller = scrollHorizontally ?
184 m_horizontalSmoothScroller : m_verticalSmoothScroller;
185
186 const QScrollBar* scrollBar = smoothScroller->scrollBar();
187 if (!event->pixelDelta().isNull()) {
188 const int numPixels = event->pixelDelta().y();
189 if (event->modifiers().testFlag(Qt::ShiftModifier)) {
190 const int scrollingDirection = numPixels > 0 ? 1 : -1;
191 smoothScroller->scrollTo(scrollBar->value() - scrollBar->pageStep() * scrollingDirection);
192 } else {
193 smoothScroller->scrollTo(scrollBar->value() - numPixels);
194 }
195 } else {
196 const int numDegrees = event->angleDelta().y() / 8;
197 const int numSteps = qApp->wheelScrollLines() * numDegrees / 15;
198 if (event->modifiers().testFlag(Qt::ShiftModifier)) {
199 const int scrollingDirection = numSteps > 0 ? 1 : -1;
200 smoothScroller->scrollTo(scrollBar->value() - scrollBar->pageStep() * scrollingDirection);
201 } else {
202 smoothScroller->scrollTo(scrollBar->value() - numSteps * scrollBar->pageStep() / 12);
203 }
204 }
205
206 event->accept();
207 }
208
209 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
210 {
211 Q_UNUSED(previous);
212 updateSmoothScrollers(current);
213 }
214
215 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
216 {
217 Q_UNUSED(current);
218 Q_UNUSED(previous);
219 }
220
221 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
222 {
223 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
224 if (previous) {
225 scene->removeItem(previous);
226 disconnect(previous, &KItemListView::scrollOrientationChanged,
227 this, &KItemListContainer::slotScrollOrientationChanged);
228 disconnect(previous, &KItemListView::scrollOffsetChanged,
229 this, &KItemListContainer::updateScrollOffsetScrollBar);
230 disconnect(previous, &KItemListView::maximumScrollOffsetChanged,
231 this, &KItemListContainer::updateScrollOffsetScrollBar);
232 disconnect(previous, &KItemListView::itemOffsetChanged,
233 this, &KItemListContainer::updateItemOffsetScrollBar);
234 disconnect(previous, &KItemListView::maximumItemOffsetChanged,
235 this, &KItemListContainer::updateItemOffsetScrollBar);
236 disconnect(previous, &KItemListView::scrollTo, this, &KItemListContainer::scrollTo);
237 m_horizontalSmoothScroller->setTargetObject(0);
238 m_verticalSmoothScroller->setTargetObject(0);
239 }
240 if (current) {
241 scene->addItem(current);
242 connect(current, &KItemListView::scrollOrientationChanged,
243 this, &KItemListContainer::slotScrollOrientationChanged);
244 connect(current, &KItemListView::scrollOffsetChanged,
245 this, &KItemListContainer::updateScrollOffsetScrollBar);
246 connect(current, &KItemListView::maximumScrollOffsetChanged,
247 this, &KItemListContainer::updateScrollOffsetScrollBar);
248 connect(current, &KItemListView::itemOffsetChanged,
249 this, &KItemListContainer::updateItemOffsetScrollBar);
250 connect(current, &KItemListView::maximumItemOffsetChanged,
251 this, &KItemListContainer::updateItemOffsetScrollBar);
252 connect(current, &KItemListView::scrollTo, this, &KItemListContainer::scrollTo);
253 m_horizontalSmoothScroller->setTargetObject(current);
254 m_verticalSmoothScroller->setTargetObject(current);
255 updateSmoothScrollers(current->scrollOrientation());
256 }
257 }
258
259 void KItemListContainer::scrollTo(qreal offset)
260 {
261 const KItemListView* view = m_controller->view();
262 if (view) {
263 if (view->scrollOrientation() == Qt::Vertical) {
264 m_verticalSmoothScroller->scrollTo(offset);
265 } else {
266 m_horizontalSmoothScroller->scrollTo(offset);
267 }
268 }
269 }
270
271 void KItemListContainer::updateScrollOffsetScrollBar()
272 {
273 const KItemListView* view = m_controller->view();
274 if (!view) {
275 return;
276 }
277
278 KItemListSmoothScroller* smoothScroller = 0;
279 QScrollBar* scrollOffsetScrollBar = 0;
280 int singleStep = 0;
281 int pageStep = 0;
282 int maximum = 0;
283 if (view->scrollOrientation() == Qt::Vertical) {
284 smoothScroller = m_verticalSmoothScroller;
285 scrollOffsetScrollBar = verticalScrollBar();
286 singleStep = view->itemSize().height();
287 // We cannot use view->size().height() because this height might
288 // include the header widget, which is not part of the scrolled area.
289 pageStep = view->verticalPageStep();
290
291 // However, the total height of the view must be considered for the
292 // maximum value of the scroll bar. Note that the view's scrollOffset()
293 // refers to the offset of the top part of the view, which might be
294 // hidden behind the header.
295 maximum = qMax(0, int(view->maximumScrollOffset() - view->size().height()));
296 } else {
297 smoothScroller = m_horizontalSmoothScroller;
298 scrollOffsetScrollBar = horizontalScrollBar();
299 singleStep = view->itemSize().width();
300 pageStep = view->size().width();
301 maximum = qMax(0, int(view->maximumScrollOffset() - view->size().width()));
302 }
303
304 const int value = view->scrollOffset();
305 if (smoothScroller->requestScrollBarUpdate(maximum)) {
306 const bool updatePolicy = (scrollOffsetScrollBar->maximum() > 0 && maximum == 0)
307 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn;
308
309 scrollOffsetScrollBar->setSingleStep(singleStep);
310 scrollOffsetScrollBar->setPageStep(pageStep);
311 scrollOffsetScrollBar->setMinimum(0);
312 scrollOffsetScrollBar->setMaximum(maximum);
313 scrollOffsetScrollBar->setValue(value);
314
315 if (updatePolicy) {
316 // Prevent a potential endless layout loop (see bug #293318).
317 updateScrollOffsetScrollBarPolicy();
318 }
319 }
320 }
321
322 void KItemListContainer::updateItemOffsetScrollBar()
323 {
324 const KItemListView* view = m_controller->view();
325 if (!view) {
326 return;
327 }
328
329 KItemListSmoothScroller* smoothScroller = 0;
330 QScrollBar* itemOffsetScrollBar = 0;
331 int singleStep = 0;
332 int pageStep = 0;
333 if (view->scrollOrientation() == Qt::Vertical) {
334 smoothScroller = m_horizontalSmoothScroller;
335 itemOffsetScrollBar = horizontalScrollBar();
336 singleStep = view->size().width() / 10;
337 pageStep = view->size().width();
338 } else {
339 smoothScroller = m_verticalSmoothScroller;
340 itemOffsetScrollBar = verticalScrollBar();
341 singleStep = view->size().height() / 10;
342 pageStep = view->size().height();
343 }
344
345 const int value = view->itemOffset();
346 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
347 if (smoothScroller->requestScrollBarUpdate(maximum)) {
348 itemOffsetScrollBar->setSingleStep(singleStep);
349 itemOffsetScrollBar->setPageStep(pageStep);
350 itemOffsetScrollBar->setMinimum(0);
351 itemOffsetScrollBar->setMaximum(maximum);
352 itemOffsetScrollBar->setValue(value);
353 }
354 }
355
356 void KItemListContainer::updateGeometries()
357 {
358 QRect rect = geometry();
359
360 int extra = frameWidth() * 2;
361 QStyleOption option;
362 option.initFrom(this);
363 int scrollbarSpacing = 0;
364 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &option, this)) {
365 scrollbarSpacing = style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &option, this);
366 }
367
368 const int widthDec = verticalScrollBar()->isVisible()
369 ? extra + scrollbarSpacing + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
370 : extra;
371
372 const int heightDec = horizontalScrollBar()->isVisible()
373 ? extra + scrollbarSpacing + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
374 : extra;
375
376 const QRectF newGeometry(0, 0, rect.width() - widthDec,
377 rect.height() - heightDec);
378 if (m_controller->view()->geometry() != newGeometry) {
379 m_controller->view()->setGeometry(newGeometry);
380
381 // Get the real geometry of the view again since the scrollbars
382 // visibilities and the view geometry may have changed in re-layout.
383 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(m_controller->view()->geometry());
384 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(m_controller->view()->geometry().toRect());
385
386 updateScrollOffsetScrollBar();
387 updateItemOffsetScrollBar();
388 }
389 }
390
391 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
392 {
393 if (orientation == Qt::Vertical) {
394 m_verticalSmoothScroller->setPropertyName("scrollOffset");
395 m_horizontalSmoothScroller->setPropertyName("itemOffset");
396 } else {
397 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
398 m_verticalSmoothScroller->setPropertyName("itemOffset");
399 }
400 }
401
402 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
403 {
404 const KItemListView* view = m_controller->view();
405 Q_ASSERT(view);
406 const bool vertical = (view->scrollOrientation() == Qt::Vertical);
407
408 QStyleOption option;
409 option.initFrom(this);
410 const int scrollBarInc = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this);
411
412 QSizeF newViewSize = m_controller->view()->size();
413 if (vertical) {
414 newViewSize.rwidth() += scrollBarInc;
415 } else {
416 newViewSize.rheight() += scrollBarInc;
417 }
418
419 const Qt::ScrollBarPolicy policy = view->scrollBarRequired(newViewSize)
420 ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAsNeeded;
421 if (vertical) {
422 setVerticalScrollBarPolicy(policy);
423 } else {
424 setHorizontalScrollBarPolicy(policy);
425 }
426 }
427