]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Prevent repeated re-layouting of all items while previews are generated
[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 <QPropertyAnimation>
35 #include <QScrollBar>
36 #include <QStyle>
37 #include <QStyleOption>
38
39 #include <KDebug>
40
41 /**
42 * Replaces the default viewport of KItemListContainer by a
43 * non-scrollable viewport. The scrolling is done in an optimized
44 * way by KItemListView internally.
45 */
46 class KItemListContainerViewport : public QGraphicsView
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, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
93 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
94 connect(controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
95 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
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 int numDegrees = event->delta() / 8;
189 const int numSteps = numDegrees / 15;
190
191 const QScrollBar* scrollBar = smoothScroller->scrollBar();
192 smoothScroller->scrollTo(scrollBar->value() - numSteps * scrollBar->pageStep() / 4);
193
194 event->accept();
195 }
196
197 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
198 {
199 Q_UNUSED(previous);
200 updateSmoothScrollers(current);
201 }
202
203 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
204 {
205 Q_UNUSED(current);
206 Q_UNUSED(previous);
207 }
208
209 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
210 {
211 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
212 if (previous) {
213 scene->removeItem(previous);
214 disconnect(previous, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
215 disconnect(previous, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
216 disconnect(previous, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
217 disconnect(previous, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
218 disconnect(previous, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
219 disconnect(previous, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
220 m_horizontalSmoothScroller->setTargetObject(0);
221 m_verticalSmoothScroller->setTargetObject(0);
222 }
223 if (current) {
224 scene->addItem(current);
225 connect(current, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
226 connect(current, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
227 connect(current, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
228 connect(current, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
229 connect(current, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
230 connect(current, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
231 m_horizontalSmoothScroller->setTargetObject(current);
232 m_verticalSmoothScroller->setTargetObject(current);
233 updateSmoothScrollers(current->scrollOrientation());
234 }
235 }
236
237 void KItemListContainer::scrollTo(qreal offset)
238 {
239 const KItemListView* view = m_controller->view();
240 if (view) {
241 if (view->scrollOrientation() == Qt::Vertical) {
242 m_verticalSmoothScroller->scrollTo(offset);
243 } else {
244 m_horizontalSmoothScroller->scrollTo(offset);
245 }
246 }
247 }
248
249 void KItemListContainer::updateScrollOffsetScrollBar()
250 {
251 const KItemListView* view = m_controller->view();
252 if (!view) {
253 return;
254 }
255
256 KItemListSmoothScroller* smoothScroller = 0;
257 QScrollBar* scrollOffsetScrollBar = 0;
258 int singleStep = 0;
259 int pageStep = 0;
260 if (view->scrollOrientation() == Qt::Vertical) {
261 smoothScroller = m_verticalSmoothScroller;
262 scrollOffsetScrollBar = verticalScrollBar();
263 singleStep = view->itemSize().height();
264 pageStep = view->size().height();
265 } else {
266 smoothScroller = m_horizontalSmoothScroller;
267 scrollOffsetScrollBar = horizontalScrollBar();
268 singleStep = view->itemSize().width();
269 pageStep = view->size().width();
270 }
271
272 const int value = view->scrollOffset();
273 const int maximum = qMax(0, int(view->maximumScrollOffset() - pageStep));
274 if (smoothScroller->requestScrollBarUpdate(maximum)) {
275 const bool updatePolicy = (scrollOffsetScrollBar->maximum() > 0 && maximum == 0)
276 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn;
277
278 scrollOffsetScrollBar->setSingleStep(singleStep);
279 scrollOffsetScrollBar->setPageStep(pageStep);
280 scrollOffsetScrollBar->setMinimum(0);
281 scrollOffsetScrollBar->setMaximum(maximum);
282 scrollOffsetScrollBar->setValue(value);
283
284 if (updatePolicy) {
285 // Prevent a potential endless layout loop (see bug #293318).
286 updateScrollOffsetScrollBarPolicy();
287 }
288 }
289 }
290
291 void KItemListContainer::updateItemOffsetScrollBar()
292 {
293 const KItemListView* view = m_controller->view();
294 if (!view) {
295 return;
296 }
297
298 KItemListSmoothScroller* smoothScroller = 0;
299 QScrollBar* itemOffsetScrollBar = 0;
300 int singleStep = 0;
301 int pageStep = 0;
302 if (view->scrollOrientation() == Qt::Vertical) {
303 smoothScroller = m_horizontalSmoothScroller;
304 itemOffsetScrollBar = horizontalScrollBar();
305 singleStep = view->size().width() / 10;
306 pageStep = view->size().width();
307 } else {
308 smoothScroller = m_verticalSmoothScroller;
309 itemOffsetScrollBar = verticalScrollBar();
310 singleStep = view->size().height() / 10;
311 pageStep = view->size().height();
312 }
313
314 const int value = view->itemOffset();
315 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
316 if (smoothScroller->requestScrollBarUpdate(maximum)) {
317 itemOffsetScrollBar->setSingleStep(singleStep);
318 itemOffsetScrollBar->setPageStep(pageStep);
319 itemOffsetScrollBar->setMinimum(0);
320 itemOffsetScrollBar->setMaximum(maximum);
321 itemOffsetScrollBar->setValue(value);
322 }
323 }
324
325 void KItemListContainer::updateGeometries()
326 {
327 QRect rect = geometry();
328
329 int extra = frameWidth() * 2;
330 QStyleOption option;
331 option.initFrom(this);
332 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &option, this)) {
333 extra += style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &option, this);
334 }
335
336 const int widthDec = verticalScrollBar()->isVisible()
337 ? extra + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
338 : extra;
339
340 const int heightDec = horizontalScrollBar()->isVisible()
341 ? extra + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
342 : extra;
343
344 rect.adjust(0, 0, -widthDec, -heightDec);
345
346 const QRectF newGeometry(0, 0, rect.width(), rect.height());
347 if (m_controller->view()->geometry() != newGeometry) {
348 m_controller->view()->setGeometry(newGeometry);
349
350 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
351 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
352
353 updateScrollOffsetScrollBar();
354 updateItemOffsetScrollBar();
355 }
356 }
357
358 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
359 {
360 if (orientation == Qt::Vertical) {
361 m_verticalSmoothScroller->setPropertyName("scrollOffset");
362 m_horizontalSmoothScroller->setPropertyName("itemOffset");
363 } else {
364 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
365 m_verticalSmoothScroller->setPropertyName("itemOffset");
366 }
367 }
368
369 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
370 {
371 const KItemListView* view = m_controller->view();
372 Q_ASSERT(view);
373 const bool vertical = (view->scrollOrientation() == Qt::Vertical);
374
375 QStyleOption option;
376 option.initFrom(this);
377 const int scrollBarInc = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this);
378
379 QSizeF newViewSize = m_controller->view()->size();
380 if (vertical) {
381 newViewSize.rwidth() += scrollBarInc;
382 } else {
383 newViewSize.rheight() += scrollBarInc;
384 }
385
386 const Qt::ScrollBarPolicy policy = view->scrollBarRequired(newViewSize)
387 ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAsNeeded;
388 if (vertical) {
389 setVerticalScrollBarPolicy(policy);
390 } else {
391 setHorizontalScrollBarPolicy(policy);
392 }
393 }
394
395 #include "kitemlistcontainer.moc"