]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Fix selection rect after porting from QFontMetrics::width()
[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 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 #include "kitemlistcontainer.h"
23
24 #include "kitemlistcontroller.h"
25 #include "kitemlistview.h"
26 #include "private/kitemlistsmoothscroller.h"
27
28 #include <QApplication>
29 #include <QFontMetrics>
30 #include <QGraphicsScene>
31 #include <QGraphicsView>
32 #include <QScrollBar>
33 #include <QStyleOption>
34
35 /**
36 * Replaces the default viewport of KItemListContainer by a
37 * non-scrollable viewport. The scrolling is done in an optimized
38 * way by KItemListView internally.
39 */
40 class KItemListContainerViewport : public QGraphicsView
41 {
42 Q_OBJECT
43
44 public:
45 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent);
46 protected:
47 void wheelEvent(QWheelEvent* event) override;
48 };
49
50 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent) :
51 QGraphicsView(scene, parent)
52 {
53 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
54 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
55 setViewportMargins(0, 0, 0, 0);
56 setFrameShape(QFrame::NoFrame);
57 }
58
59 void KItemListContainerViewport::wheelEvent(QWheelEvent* event)
60 {
61 // Assure that the wheel-event gets forwarded to the parent
62 // and not handled at all by QGraphicsView.
63 event->ignore();
64 }
65
66 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
67 QAbstractScrollArea(parent),
68 m_controller(controller),
69 m_horizontalSmoothScroller(nullptr),
70 m_verticalSmoothScroller(nullptr)
71 {
72 Q_ASSERT(controller);
73 controller->setParent(this);
74
75 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
76 setViewport(graphicsView);
77
78 m_horizontalSmoothScroller = new KItemListSmoothScroller(horizontalScrollBar(), this);
79 m_verticalSmoothScroller = new KItemListSmoothScroller(verticalScrollBar(), this);
80
81 if (controller->model()) {
82 slotModelChanged(controller->model(), nullptr);
83 }
84 if (controller->view()) {
85 slotViewChanged(controller->view(), nullptr);
86 }
87
88 connect(controller, &KItemListController::modelChanged,
89 this, &KItemListContainer::slotModelChanged);
90 connect(controller, &KItemListController::viewChanged,
91 this, &KItemListContainer::slotViewChanged);
92 }
93
94 KItemListContainer::~KItemListContainer()
95 {
96 // Don't rely on the QObject-order to delete the controller, otherwise
97 // the QGraphicsScene might get deleted before the view.
98 delete m_controller;
99 m_controller = nullptr;
100 }
101
102 KItemListController* KItemListContainer::controller() const
103 {
104 return m_controller;
105 }
106
107 void KItemListContainer::setEnabledFrame(bool enable)
108 {
109 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(viewport());
110 if (enable) {
111 setFrameShape(QFrame::StyledPanel);
112 graphicsView->setPalette(palette());
113 graphicsView->viewport()->setAutoFillBackground(true);
114 } else {
115 setFrameShape(QFrame::NoFrame);
116 // Make the background of the container transparent and apply the window-text color
117 // to the text color, so that enough contrast is given for all color
118 // schemes
119 QPalette p = graphicsView->palette();
120 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
121 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
122 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
123 graphicsView->setPalette(p);
124 graphicsView->viewport()->setAutoFillBackground(false);
125 }
126 }
127
128 bool KItemListContainer::enabledFrame() const
129 {
130 const QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(viewport());
131 return graphicsView->autoFillBackground();
132 }
133
134 void KItemListContainer::keyPressEvent(QKeyEvent* event)
135 {
136 // TODO: We should find a better way to handle the key press events in the view.
137 // The reasons why we need this hack are:
138 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
139 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
140 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
141 // does not work.
142 KItemListView* view = m_controller->view();
143 if (view) {
144 QApplication::sendEvent(view, event);
145 }
146 }
147
148 void KItemListContainer::showEvent(QShowEvent* event)
149 {
150 QAbstractScrollArea::showEvent(event);
151 updateGeometries();
152 }
153
154 void KItemListContainer::resizeEvent(QResizeEvent* event)
155 {
156 QAbstractScrollArea::resizeEvent(event);
157 updateGeometries();
158 }
159
160 void KItemListContainer::scrollContentsBy(int dx, int dy)
161 {
162 m_horizontalSmoothScroller->scrollContentsBy(dx);
163 m_verticalSmoothScroller->scrollContentsBy(dy);
164 }
165
166 void KItemListContainer::wheelEvent(QWheelEvent* event)
167 {
168 if (event->modifiers().testFlag(Qt::ControlModifier)) {
169 event->ignore();
170 return;
171 }
172
173 KItemListView* view = m_controller->view();
174 if (!view) {
175 event->ignore();
176 return;
177 }
178
179 const bool scrollHorizontally = (event->orientation() == Qt::Horizontal) ||
180 (event->orientation() == Qt::Vertical && !verticalScrollBar()->isVisible());
181 KItemListSmoothScroller* smoothScroller = scrollHorizontally ?
182 m_horizontalSmoothScroller : m_verticalSmoothScroller;
183
184 smoothScroller->handleWheelEvent(event);
185 }
186
187 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
188 {
189 Q_UNUSED(previous)
190 updateSmoothScrollers(current);
191 }
192
193 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
194 {
195 Q_UNUSED(current)
196 Q_UNUSED(previous)
197 }
198
199 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
200 {
201 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
202 if (previous) {
203 scene->removeItem(previous);
204 disconnect(previous, &KItemListView::scrollOrientationChanged,
205 this, &KItemListContainer::slotScrollOrientationChanged);
206 disconnect(previous, &KItemListView::scrollOffsetChanged,
207 this, &KItemListContainer::updateScrollOffsetScrollBar);
208 disconnect(previous, &KItemListView::maximumScrollOffsetChanged,
209 this, &KItemListContainer::updateScrollOffsetScrollBar);
210 disconnect(previous, &KItemListView::itemOffsetChanged,
211 this, &KItemListContainer::updateItemOffsetScrollBar);
212 disconnect(previous, &KItemListView::maximumItemOffsetChanged,
213 this, &KItemListContainer::updateItemOffsetScrollBar);
214 disconnect(previous, &KItemListView::scrollTo, this, &KItemListContainer::scrollTo);
215 m_horizontalSmoothScroller->setTargetObject(nullptr);
216 m_verticalSmoothScroller->setTargetObject(nullptr);
217 }
218 if (current) {
219 scene->addItem(current);
220 connect(current, &KItemListView::scrollOrientationChanged,
221 this, &KItemListContainer::slotScrollOrientationChanged);
222 connect(current, &KItemListView::scrollOffsetChanged,
223 this, &KItemListContainer::updateScrollOffsetScrollBar);
224 connect(current, &KItemListView::maximumScrollOffsetChanged,
225 this, &KItemListContainer::updateScrollOffsetScrollBar);
226 connect(current, &KItemListView::itemOffsetChanged,
227 this, &KItemListContainer::updateItemOffsetScrollBar);
228 connect(current, &KItemListView::maximumItemOffsetChanged,
229 this, &KItemListContainer::updateItemOffsetScrollBar);
230 connect(current, &KItemListView::scrollTo, this, &KItemListContainer::scrollTo);
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 = nullptr;
257 QScrollBar* scrollOffsetScrollBar = nullptr;
258 int singleStep = 0;
259 int pageStep = 0;
260 int maximum = 0;
261 if (view->scrollOrientation() == Qt::Vertical) {
262 smoothScroller = m_verticalSmoothScroller;
263 scrollOffsetScrollBar = verticalScrollBar();
264
265 // Don't scroll super fast when using a wheel mouse:
266 // We want to consider one "line" to be the text label which has a
267 // roughly fixed height rather than using the height of the icon which
268 // may be very tall
269 const QFontMetrics metrics(font());
270 singleStep = metrics.height() * QApplication::wheelScrollLines();
271
272 // We cannot use view->size().height() because this height might
273 // include the header widget, which is not part of the scrolled area.
274 pageStep = view->verticalPageStep();
275
276 // However, the total height of the view must be considered for the
277 // maximum value of the scroll bar. Note that the view's scrollOffset()
278 // refers to the offset of the top part of the view, which might be
279 // hidden behind the header.
280 maximum = qMax(0, int(view->maximumScrollOffset() - view->size().height()));
281 } else {
282 smoothScroller = m_horizontalSmoothScroller;
283 scrollOffsetScrollBar = horizontalScrollBar();
284 singleStep = view->itemSize().width();
285 pageStep = view->size().width();
286 maximum = qMax(0, int(view->maximumScrollOffset() - view->size().width()));
287 }
288
289 const int value = view->scrollOffset();
290 if (smoothScroller->requestScrollBarUpdate(maximum)) {
291 const bool updatePolicy = (scrollOffsetScrollBar->maximum() > 0 && maximum == 0)
292 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn;
293
294 scrollOffsetScrollBar->setSingleStep(singleStep);
295 scrollOffsetScrollBar->setPageStep(pageStep);
296 scrollOffsetScrollBar->setMinimum(0);
297 scrollOffsetScrollBar->setMaximum(maximum);
298 scrollOffsetScrollBar->setValue(value);
299
300 if (updatePolicy) {
301 // Prevent a potential endless layout loop (see bug #293318).
302 updateScrollOffsetScrollBarPolicy();
303 }
304 }
305 }
306
307 void KItemListContainer::updateItemOffsetScrollBar()
308 {
309 const KItemListView* view = m_controller->view();
310 if (!view) {
311 return;
312 }
313
314 KItemListSmoothScroller* smoothScroller = nullptr;
315 QScrollBar* itemOffsetScrollBar = nullptr;
316 int singleStep = 0;
317 int pageStep = 0;
318 if (view->scrollOrientation() == Qt::Vertical) {
319 smoothScroller = m_horizontalSmoothScroller;
320 itemOffsetScrollBar = horizontalScrollBar();
321 singleStep = view->size().width() / 10;
322 pageStep = view->size().width();
323 } else {
324 smoothScroller = m_verticalSmoothScroller;
325 itemOffsetScrollBar = verticalScrollBar();
326 singleStep = view->size().height() / 10;
327 pageStep = view->size().height();
328 }
329
330 const int value = view->itemOffset();
331 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
332 if (smoothScroller->requestScrollBarUpdate(maximum)) {
333 itemOffsetScrollBar->setSingleStep(singleStep);
334 itemOffsetScrollBar->setPageStep(pageStep);
335 itemOffsetScrollBar->setMinimum(0);
336 itemOffsetScrollBar->setMaximum(maximum);
337 itemOffsetScrollBar->setValue(value);
338 }
339 }
340
341 void KItemListContainer::updateGeometries()
342 {
343 QRect rect = geometry();
344
345 int extra = frameWidth() * 2;
346 QStyleOption option;
347 option.initFrom(this);
348 int scrollbarSpacing = 0;
349 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &option, this)) {
350 scrollbarSpacing = style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &option, this);
351 }
352
353 const int widthDec = verticalScrollBar()->isVisible()
354 ? extra + scrollbarSpacing + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
355 : extra;
356
357 const int heightDec = horizontalScrollBar()->isVisible()
358 ? extra + scrollbarSpacing + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
359 : extra;
360
361 const QRectF newGeometry(0, 0, rect.width() - widthDec,
362 rect.height() - heightDec);
363 if (m_controller->view()->geometry() != newGeometry) {
364 m_controller->view()->setGeometry(newGeometry);
365
366 // Get the real geometry of the view again since the scrollbars
367 // visibilities and the view geometry may have changed in re-layout.
368 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(m_controller->view()->geometry());
369 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(m_controller->view()->geometry().toRect());
370
371 updateScrollOffsetScrollBar();
372 updateItemOffsetScrollBar();
373 }
374 }
375
376 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
377 {
378 if (orientation == Qt::Vertical) {
379 m_verticalSmoothScroller->setPropertyName("scrollOffset");
380 m_horizontalSmoothScroller->setPropertyName("itemOffset");
381 } else {
382 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
383 m_verticalSmoothScroller->setPropertyName("itemOffset");
384 }
385 }
386
387 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
388 {
389 const KItemListView* view = m_controller->view();
390 Q_ASSERT(view);
391 const bool vertical = (view->scrollOrientation() == Qt::Vertical);
392
393 QStyleOption option;
394 option.initFrom(this);
395 const int scrollBarInc = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this);
396
397 QSizeF newViewSize = m_controller->view()->size();
398 if (vertical) {
399 newViewSize.rwidth() += scrollBarInc;
400 } else {
401 newViewSize.rheight() += scrollBarInc;
402 }
403
404 const Qt::ScrollBarPolicy policy = view->scrollBarRequired(newViewSize)
405 ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAsNeeded;
406 if (vertical) {
407 setVerticalScrollBarPolicy(policy);
408 } else {
409 setHorizontalScrollBarPolicy(policy);
410 }
411 }
412
413 #include "kitemlistcontainer.moc"