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