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