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