]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Merge branch 'Applications/17.12'
[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 * Replaces the default viewport of KItemListContainer by a
40 * non-scrollable viewport. The scrolling is done in an optimized
41 * way by KItemListView internally.
42 */
43 class KItemListContainerViewport : public QGraphicsView
44 {
45 Q_OBJECT
46
47 public:
48 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent);
49 protected:
50 void wheelEvent(QWheelEvent* event) override;
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(nullptr),
73 m_verticalSmoothScroller(nullptr)
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(), nullptr);
86 }
87 if (controller->view()) {
88 slotViewChanged(controller->view(), nullptr);
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 = nullptr;
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 smoothScroller->handleWheelEvent(event);
188 }
189
190 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
191 {
192 Q_UNUSED(previous);
193 updateSmoothScrollers(current);
194 }
195
196 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
197 {
198 Q_UNUSED(current);
199 Q_UNUSED(previous);
200 }
201
202 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
203 {
204 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
205 if (previous) {
206 scene->removeItem(previous);
207 disconnect(previous, &KItemListView::scrollOrientationChanged,
208 this, &KItemListContainer::slotScrollOrientationChanged);
209 disconnect(previous, &KItemListView::scrollOffsetChanged,
210 this, &KItemListContainer::updateScrollOffsetScrollBar);
211 disconnect(previous, &KItemListView::maximumScrollOffsetChanged,
212 this, &KItemListContainer::updateScrollOffsetScrollBar);
213 disconnect(previous, &KItemListView::itemOffsetChanged,
214 this, &KItemListContainer::updateItemOffsetScrollBar);
215 disconnect(previous, &KItemListView::maximumItemOffsetChanged,
216 this, &KItemListContainer::updateItemOffsetScrollBar);
217 disconnect(previous, &KItemListView::scrollTo, this, &KItemListContainer::scrollTo);
218 m_horizontalSmoothScroller->setTargetObject(nullptr);
219 m_verticalSmoothScroller->setTargetObject(nullptr);
220 }
221 if (current) {
222 scene->addItem(current);
223 connect(current, &KItemListView::scrollOrientationChanged,
224 this, &KItemListContainer::slotScrollOrientationChanged);
225 connect(current, &KItemListView::scrollOffsetChanged,
226 this, &KItemListContainer::updateScrollOffsetScrollBar);
227 connect(current, &KItemListView::maximumScrollOffsetChanged,
228 this, &KItemListContainer::updateScrollOffsetScrollBar);
229 connect(current, &KItemListView::itemOffsetChanged,
230 this, &KItemListContainer::updateItemOffsetScrollBar);
231 connect(current, &KItemListView::maximumItemOffsetChanged,
232 this, &KItemListContainer::updateItemOffsetScrollBar);
233 connect(current, &KItemListView::scrollTo, this, &KItemListContainer::scrollTo);
234 m_horizontalSmoothScroller->setTargetObject(current);
235 m_verticalSmoothScroller->setTargetObject(current);
236 updateSmoothScrollers(current->scrollOrientation());
237 }
238 }
239
240 void KItemListContainer::scrollTo(qreal offset)
241 {
242 const KItemListView* view = m_controller->view();
243 if (view) {
244 if (view->scrollOrientation() == Qt::Vertical) {
245 m_verticalSmoothScroller->scrollTo(offset);
246 } else {
247 m_horizontalSmoothScroller->scrollTo(offset);
248 }
249 }
250 }
251
252 void KItemListContainer::updateScrollOffsetScrollBar()
253 {
254 const KItemListView* view = m_controller->view();
255 if (!view) {
256 return;
257 }
258
259 KItemListSmoothScroller* smoothScroller = nullptr;
260 QScrollBar* scrollOffsetScrollBar = nullptr;
261 int singleStep = 0;
262 int pageStep = 0;
263 int maximum = 0;
264 if (view->scrollOrientation() == Qt::Vertical) {
265 smoothScroller = m_verticalSmoothScroller;
266 scrollOffsetScrollBar = verticalScrollBar();
267 singleStep = view->itemSizeHint().height();
268 // We cannot use view->size().height() because this height might
269 // include the header widget, which is not part of the scrolled area.
270 pageStep = view->verticalPageStep();
271
272 // However, the total height of the view must be considered for the
273 // maximum value of the scroll bar. Note that the view's scrollOffset()
274 // refers to the offset of the top part of the view, which might be
275 // hidden behind the header.
276 maximum = qMax(0, int(view->maximumScrollOffset() - view->size().height()));
277 } else {
278 smoothScroller = m_horizontalSmoothScroller;
279 scrollOffsetScrollBar = horizontalScrollBar();
280 singleStep = view->itemSize().width();
281 pageStep = view->size().width();
282 maximum = qMax(0, int(view->maximumScrollOffset() - view->size().width()));
283 }
284
285 const int value = view->scrollOffset();
286 if (smoothScroller->requestScrollBarUpdate(maximum)) {
287 const bool updatePolicy = (scrollOffsetScrollBar->maximum() > 0 && maximum == 0)
288 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn;
289
290 scrollOffsetScrollBar->setSingleStep(singleStep);
291 scrollOffsetScrollBar->setPageStep(pageStep);
292 scrollOffsetScrollBar->setMinimum(0);
293 scrollOffsetScrollBar->setMaximum(maximum);
294 scrollOffsetScrollBar->setValue(value);
295
296 if (updatePolicy) {
297 // Prevent a potential endless layout loop (see bug #293318).
298 updateScrollOffsetScrollBarPolicy();
299 }
300 }
301 }
302
303 void KItemListContainer::updateItemOffsetScrollBar()
304 {
305 const KItemListView* view = m_controller->view();
306 if (!view) {
307 return;
308 }
309
310 KItemListSmoothScroller* smoothScroller = nullptr;
311 QScrollBar* itemOffsetScrollBar = nullptr;
312 int singleStep = 0;
313 int pageStep = 0;
314 if (view->scrollOrientation() == Qt::Vertical) {
315 smoothScroller = m_horizontalSmoothScroller;
316 itemOffsetScrollBar = horizontalScrollBar();
317 singleStep = view->size().width() / 10;
318 pageStep = view->size().width();
319 } else {
320 smoothScroller = m_verticalSmoothScroller;
321 itemOffsetScrollBar = verticalScrollBar();
322 singleStep = view->size().height() / 10;
323 pageStep = view->size().height();
324 }
325
326 const int value = view->itemOffset();
327 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
328 if (smoothScroller->requestScrollBarUpdate(maximum)) {
329 itemOffsetScrollBar->setSingleStep(singleStep);
330 itemOffsetScrollBar->setPageStep(pageStep);
331 itemOffsetScrollBar->setMinimum(0);
332 itemOffsetScrollBar->setMaximum(maximum);
333 itemOffsetScrollBar->setValue(value);
334 }
335 }
336
337 void KItemListContainer::updateGeometries()
338 {
339 QRect rect = geometry();
340
341 int extra = frameWidth() * 2;
342 QStyleOption option;
343 option.initFrom(this);
344 int scrollbarSpacing = 0;
345 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &option, this)) {
346 scrollbarSpacing = style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &option, this);
347 }
348
349 const int widthDec = verticalScrollBar()->isVisible()
350 ? extra + scrollbarSpacing + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
351 : extra;
352
353 const int heightDec = horizontalScrollBar()->isVisible()
354 ? extra + scrollbarSpacing + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
355 : extra;
356
357 const QRectF newGeometry(0, 0, rect.width() - widthDec,
358 rect.height() - heightDec);
359 if (m_controller->view()->geometry() != newGeometry) {
360 m_controller->view()->setGeometry(newGeometry);
361
362 // Get the real geometry of the view again since the scrollbars
363 // visibilities and the view geometry may have changed in re-layout.
364 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(m_controller->view()->geometry());
365 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(m_controller->view()->geometry().toRect());
366
367 updateScrollOffsetScrollBar();
368 updateItemOffsetScrollBar();
369 }
370 }
371
372 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
373 {
374 if (orientation == Qt::Vertical) {
375 m_verticalSmoothScroller->setPropertyName("scrollOffset");
376 m_horizontalSmoothScroller->setPropertyName("itemOffset");
377 } else {
378 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
379 m_verticalSmoothScroller->setPropertyName("itemOffset");
380 }
381 }
382
383 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
384 {
385 const KItemListView* view = m_controller->view();
386 Q_ASSERT(view);
387 const bool vertical = (view->scrollOrientation() == Qt::Vertical);
388
389 QStyleOption option;
390 option.initFrom(this);
391 const int scrollBarInc = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this);
392
393 QSizeF newViewSize = m_controller->view()->size();
394 if (vertical) {
395 newViewSize.rwidth() += scrollBarInc;
396 } else {
397 newViewSize.rheight() += scrollBarInc;
398 }
399
400 const Qt::ScrollBarPolicy policy = view->scrollBarRequired(newViewSize)
401 ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAsNeeded;
402 if (vertical) {
403 setVerticalScrollBarPolicy(policy);
404 } else {
405 setHorizontalScrollBarPolicy(policy);
406 }
407 }
408
409 #include "kitemlistcontainer.moc"