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