]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
KItemViews: Internal directory restructuration
[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
71
72 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
73 QAbstractScrollArea(parent),
74 m_controller(controller),
75 m_horizontalSmoothScroller(0),
76 m_verticalSmoothScroller(0)
77 {
78 Q_ASSERT(controller);
79 controller->setParent(this);
80 initialize();
81 }
82
83 KItemListContainer::KItemListContainer(QWidget* parent) :
84 QAbstractScrollArea(parent),
85 m_controller(0),
86 m_horizontalSmoothScroller(0),
87 m_verticalSmoothScroller(0)
88 {
89 initialize();
90 }
91
92 KItemListContainer::~KItemListContainer()
93 {
94 }
95
96 KItemListController* KItemListContainer::controller() const
97 {
98 return m_controller;
99 }
100
101 void KItemListContainer::keyPressEvent(QKeyEvent* event)
102 {
103 // TODO: We should find a better way to handle the key press events in the view.
104 // The reasons why we need this hack are:
105 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
106 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
107 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
108 // does not work.
109 KItemListView* view = m_controller->view();
110 if (view) {
111 QApplication::sendEvent(view, event);
112 }
113 }
114
115 void KItemListContainer::showEvent(QShowEvent* event)
116 {
117 QAbstractScrollArea::showEvent(event);
118 updateGeometries();
119 }
120
121 void KItemListContainer::resizeEvent(QResizeEvent* event)
122 {
123 QAbstractScrollArea::resizeEvent(event);
124 updateGeometries();
125 }
126
127 void KItemListContainer::scrollContentsBy(int dx, int dy)
128 {
129 m_horizontalSmoothScroller->scrollContentsBy(dx);
130 m_verticalSmoothScroller->scrollContentsBy(dy);
131 }
132
133 void KItemListContainer::wheelEvent(QWheelEvent* event)
134 {
135 if (event->modifiers().testFlag(Qt::ControlModifier)) {
136 event->ignore();
137 return;
138 }
139
140 KItemListView* view = m_controller->view();
141 if (!view) {
142 event->ignore();
143 return;
144 }
145
146 const bool scrollHorizontally = (event->orientation() == Qt::Horizontal) ||
147 (event->orientation() == Qt::Vertical && !verticalScrollBar()->isVisible());
148 KItemListSmoothScroller* smoothScroller = scrollHorizontally ?
149 m_horizontalSmoothScroller : m_verticalSmoothScroller;
150
151 const int numDegrees = event->delta() / 8;
152 const int numSteps = numDegrees / 15;
153
154 const QScrollBar* scrollBar = smoothScroller->scrollBar();
155 smoothScroller->scrollTo(scrollBar->value() - numSteps * scrollBar->pageStep() / 4);
156
157 event->accept();
158 }
159
160 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
161 {
162 Q_UNUSED(previous);
163 updateSmoothScrollers(current);
164 }
165
166 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
167 {
168 Q_UNUSED(current);
169 Q_UNUSED(previous);
170 }
171
172 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
173 {
174 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
175 if (previous) {
176 scene->removeItem(previous);
177 disconnect(previous, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
178 disconnect(previous, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
179 disconnect(previous, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
180 disconnect(previous, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
181 disconnect(previous, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
182 disconnect(previous, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
183 m_horizontalSmoothScroller->setTargetObject(0);
184 m_verticalSmoothScroller->setTargetObject(0);
185 }
186 if (current) {
187 scene->addItem(current);
188 connect(current, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
189 connect(current, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
190 connect(current, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
191 connect(current, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
192 connect(current, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
193 connect(current, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
194 m_horizontalSmoothScroller->setTargetObject(current);
195 m_verticalSmoothScroller->setTargetObject(current);
196 updateSmoothScrollers(current->scrollOrientation());
197 }
198 }
199
200 void KItemListContainer::scrollTo(qreal offset)
201 {
202 const KItemListView* view = m_controller->view();
203 if (view) {
204 if (view->scrollOrientation() == Qt::Vertical) {
205 m_verticalSmoothScroller->scrollTo(offset);
206 } else {
207 m_horizontalSmoothScroller->scrollTo(offset);
208 }
209 }
210 }
211
212 void KItemListContainer::updateScrollOffsetScrollBar()
213 {
214 const KItemListView* view = m_controller->view();
215 if (!view) {
216 return;
217 }
218
219 KItemListSmoothScroller* smoothScroller = 0;
220 QScrollBar* scrollOffsetScrollBar = 0;
221 int singleStep = 0;
222 int pageStep = 0;
223 if (view->scrollOrientation() == Qt::Vertical) {
224 smoothScroller = m_verticalSmoothScroller;
225 scrollOffsetScrollBar = verticalScrollBar();
226 singleStep = view->itemSize().height();
227 pageStep = view->size().height();
228 } else {
229 smoothScroller = m_horizontalSmoothScroller;
230 scrollOffsetScrollBar = horizontalScrollBar();
231 singleStep = view->itemSize().width();
232 pageStep = view->size().width();
233 }
234
235 const int value = view->scrollOffset();
236 const int maximum = qMax(0, int(view->maximumScrollOffset() - pageStep));
237 if (smoothScroller->requestScrollBarUpdate(maximum)) {
238 const bool updatePolicy = (scrollOffsetScrollBar->maximum() > 0 && maximum == 0)
239 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn;
240
241 scrollOffsetScrollBar->setSingleStep(singleStep);
242 scrollOffsetScrollBar->setPageStep(pageStep);
243 scrollOffsetScrollBar->setMinimum(0);
244 scrollOffsetScrollBar->setMaximum(maximum);
245 scrollOffsetScrollBar->setValue(value);
246
247 if (updatePolicy) {
248 // Prevent a potential endless layout loop (see bug #293318).
249 updateScrollOffsetScrollBarPolicy();
250 }
251 }
252 }
253
254 void KItemListContainer::updateItemOffsetScrollBar()
255 {
256 const KItemListView* view = m_controller->view();
257 if (!view) {
258 return;
259 }
260
261 KItemListSmoothScroller* smoothScroller = 0;
262 QScrollBar* itemOffsetScrollBar = 0;
263 int singleStep = 0;
264 int pageStep = 0;
265 if (view->scrollOrientation() == Qt::Vertical) {
266 smoothScroller = m_horizontalSmoothScroller;
267 itemOffsetScrollBar = horizontalScrollBar();
268 singleStep = view->size().width() / 10;
269 pageStep = view->size().width();
270 } else {
271 smoothScroller = m_verticalSmoothScroller;
272 itemOffsetScrollBar = verticalScrollBar();
273 singleStep = view->size().height() / 10;
274 pageStep = view->size().height();
275 }
276
277 const int value = view->itemOffset();
278 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
279 if (smoothScroller->requestScrollBarUpdate(maximum)) {
280 itemOffsetScrollBar->setSingleStep(singleStep);
281 itemOffsetScrollBar->setPageStep(pageStep);
282 itemOffsetScrollBar->setMinimum(0);
283 itemOffsetScrollBar->setMaximum(maximum);
284 itemOffsetScrollBar->setValue(value);
285 }
286 }
287
288 void KItemListContainer::updateGeometries()
289 {
290 QRect rect = geometry();
291
292 int extra = frameWidth() * 2;
293 QStyleOption option;
294 option.initFrom(this);
295 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &option, this)) {
296 extra += style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &option, this);
297 }
298
299 const int widthDec = verticalScrollBar()->isVisible()
300 ? extra + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
301 : extra;
302
303 const int heightDec = horizontalScrollBar()->isVisible()
304 ? extra + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
305 : extra;
306
307 rect.adjust(0, 0, -widthDec, -heightDec);
308
309 const QRectF newGeometry(0, 0, rect.width(), rect.height());
310 if (m_controller->view()->geometry() != newGeometry) {
311 m_controller->view()->setGeometry(newGeometry);
312
313 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
314 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
315
316 updateScrollOffsetScrollBar();
317 updateItemOffsetScrollBar();
318 }
319 }
320
321 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
322 {
323 if (orientation == Qt::Vertical) {
324 m_verticalSmoothScroller->setPropertyName("scrollOffset");
325 m_horizontalSmoothScroller->setPropertyName("itemOffset");
326 } else {
327 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
328 m_verticalSmoothScroller->setPropertyName("itemOffset");
329 }
330 }
331
332 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
333 {
334 const KItemListView* view = m_controller->view();
335 Q_ASSERT(view);
336 const bool vertical = (view->scrollOrientation() == Qt::Vertical);
337
338 QStyleOption option;
339 option.initFrom(this);
340 const int scrollBarInc = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this);
341
342 QSizeF newViewSize = m_controller->view()->size();
343 if (vertical) {
344 newViewSize.rwidth() += scrollBarInc;
345 } else {
346 newViewSize.rheight() += scrollBarInc;
347 }
348
349 const Qt::ScrollBarPolicy policy = view->scrollBarRequired(newViewSize)
350 ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAsNeeded;
351 if (vertical) {
352 setVerticalScrollBarPolicy(policy);
353 } else {
354 setHorizontalScrollBarPolicy(policy);
355 }
356 }
357
358 void KItemListContainer::initialize()
359 {
360 if (m_controller) {
361 if (m_controller->model()) {
362 slotModelChanged(m_controller->model(), 0);
363 }
364 if (m_controller->view()) {
365 slotViewChanged(m_controller->view(), 0);
366 }
367 } else {
368 m_controller = new KItemListController(this);
369 }
370
371 connect(m_controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
372 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
373 connect(m_controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
374 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
375
376 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
377 setViewport(graphicsView);
378
379 m_horizontalSmoothScroller = new KItemListSmoothScroller(horizontalScrollBar(), this);
380 m_verticalSmoothScroller = new KItemListSmoothScroller(verticalScrollBar(), this);
381 }
382
383 #include "kitemlistcontainer.moc"