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