1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #include "kitemlistcontainer.h"
25 #include "kitemlistcontroller.h"
26 #include "kitemlistview.h"
27 #include "private/kitemlistsmoothscroller.h"
29 #include <QApplication>
30 #include <QFontMetrics>
31 #include <QGraphicsScene>
32 #include <QGraphicsView>
34 #include <QStyleOption>
37 * Replaces the default viewport of KItemListContainer by a
38 * non-scrollable viewport. The scrolling is done in an optimized
39 * way by KItemListView internally.
41 class KItemListContainerViewport
: public QGraphicsView
46 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
48 void wheelEvent(QWheelEvent
* event
) override
;
51 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
52 QGraphicsView(scene
, parent
)
54 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
55 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
56 setViewportMargins(0, 0, 0, 0);
57 setFrameShape(QFrame::NoFrame
);
60 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
62 // Assure that the wheel-event gets forwarded to the parent
63 // and not handled at all by QGraphicsView.
67 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
68 QAbstractScrollArea(parent
),
69 m_controller(controller
),
70 m_horizontalSmoothScroller(nullptr),
71 m_verticalSmoothScroller(nullptr)
74 controller
->setParent(this);
76 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
77 setViewport(graphicsView
);
79 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
80 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
82 if (controller
->model()) {
83 slotModelChanged(controller
->model(), nullptr);
85 if (controller
->view()) {
86 slotViewChanged(controller
->view(), nullptr);
89 connect(controller
, &KItemListController::modelChanged
,
90 this, &KItemListContainer::slotModelChanged
);
91 connect(controller
, &KItemListController::viewChanged
,
92 this, &KItemListContainer::slotViewChanged
);
95 KItemListContainer::~KItemListContainer()
97 // Don't rely on the QObject-order to delete the controller, otherwise
98 // the QGraphicsScene might get deleted before the view.
100 m_controller
= nullptr;
103 KItemListController
* KItemListContainer::controller() const
108 void KItemListContainer::setEnabledFrame(bool enable
)
110 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
112 setFrameShape(QFrame::StyledPanel
);
113 graphicsView
->setPalette(palette());
114 graphicsView
->viewport()->setAutoFillBackground(true);
116 setFrameShape(QFrame::NoFrame
);
117 // Make the background of the container transparent and apply the window-text color
118 // to the text color, so that enough contrast is given for all color
120 QPalette p
= graphicsView
->palette();
121 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
122 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
123 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
124 graphicsView
->setPalette(p
);
125 graphicsView
->viewport()->setAutoFillBackground(false);
129 bool KItemListContainer::enabledFrame() const
131 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
132 return graphicsView
->autoFillBackground();
135 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
137 // TODO: We should find a better way to handle the key press events in the view.
138 // The reasons why we need this hack are:
139 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
140 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
141 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
143 KItemListView
* view
= m_controller
->view();
145 QApplication::sendEvent(view
, event
);
149 void KItemListContainer::showEvent(QShowEvent
* event
)
151 QAbstractScrollArea::showEvent(event
);
155 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
157 QAbstractScrollArea::resizeEvent(event
);
161 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
163 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
164 m_verticalSmoothScroller
->scrollContentsBy(dy
);
167 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
169 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
174 KItemListView
* view
= m_controller
->view();
180 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
181 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
182 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
183 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
185 smoothScroller
->handleWheelEvent(event
);
188 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
191 updateSmoothScrollers(current
);
194 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
200 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
202 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
204 scene
->removeItem(previous
);
205 disconnect(previous
, &KItemListView::scrollOrientationChanged
,
206 this, &KItemListContainer::slotScrollOrientationChanged
);
207 disconnect(previous
, &KItemListView::scrollOffsetChanged
,
208 this, &KItemListContainer::updateScrollOffsetScrollBar
);
209 disconnect(previous
, &KItemListView::maximumScrollOffsetChanged
,
210 this, &KItemListContainer::updateScrollOffsetScrollBar
);
211 disconnect(previous
, &KItemListView::itemOffsetChanged
,
212 this, &KItemListContainer::updateItemOffsetScrollBar
);
213 disconnect(previous
, &KItemListView::maximumItemOffsetChanged
,
214 this, &KItemListContainer::updateItemOffsetScrollBar
);
215 disconnect(previous
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
216 m_horizontalSmoothScroller
->setTargetObject(nullptr);
217 m_verticalSmoothScroller
->setTargetObject(nullptr);
220 scene
->addItem(current
);
221 connect(current
, &KItemListView::scrollOrientationChanged
,
222 this, &KItemListContainer::slotScrollOrientationChanged
);
223 connect(current
, &KItemListView::scrollOffsetChanged
,
224 this, &KItemListContainer::updateScrollOffsetScrollBar
);
225 connect(current
, &KItemListView::maximumScrollOffsetChanged
,
226 this, &KItemListContainer::updateScrollOffsetScrollBar
);
227 connect(current
, &KItemListView::itemOffsetChanged
,
228 this, &KItemListContainer::updateItemOffsetScrollBar
);
229 connect(current
, &KItemListView::maximumItemOffsetChanged
,
230 this, &KItemListContainer::updateItemOffsetScrollBar
);
231 connect(current
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
232 m_horizontalSmoothScroller
->setTargetObject(current
);
233 m_verticalSmoothScroller
->setTargetObject(current
);
234 updateSmoothScrollers(current
->scrollOrientation());
238 void KItemListContainer::scrollTo(qreal offset
)
240 const KItemListView
* view
= m_controller
->view();
242 if (view
->scrollOrientation() == Qt::Vertical
) {
243 m_verticalSmoothScroller
->scrollTo(offset
);
245 m_horizontalSmoothScroller
->scrollTo(offset
);
250 void KItemListContainer::updateScrollOffsetScrollBar()
252 const KItemListView
* view
= m_controller
->view();
257 KItemListSmoothScroller
* smoothScroller
= nullptr;
258 QScrollBar
* scrollOffsetScrollBar
= nullptr;
262 if (view
->scrollOrientation() == Qt::Vertical
) {
263 smoothScroller
= m_verticalSmoothScroller
;
264 scrollOffsetScrollBar
= verticalScrollBar();
266 // Don't scroll super fast when using a wheel mouse:
267 // We want to consider one "line" to be the text label which has a
268 // roughly fixed height rather than using the height of the icon which
270 const QFontMetrics
metrics(font());
271 singleStep
= metrics
.height() * QApplication::wheelScrollLines();
273 // We cannot use view->size().height() because this height might
274 // include the header widget, which is not part of the scrolled area.
275 pageStep
= view
->verticalPageStep();
277 // However, the total height of the view must be considered for the
278 // maximum value of the scroll bar. Note that the view's scrollOffset()
279 // refers to the offset of the top part of the view, which might be
280 // hidden behind the header.
281 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().height()));
283 smoothScroller
= m_horizontalSmoothScroller
;
284 scrollOffsetScrollBar
= horizontalScrollBar();
285 singleStep
= view
->itemSize().width();
286 pageStep
= view
->size().width();
287 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().width()));
290 const int value
= view
->scrollOffset();
291 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
292 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
293 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
295 scrollOffsetScrollBar
->setSingleStep(singleStep
);
296 scrollOffsetScrollBar
->setPageStep(pageStep
);
297 scrollOffsetScrollBar
->setMinimum(0);
298 scrollOffsetScrollBar
->setMaximum(maximum
);
299 scrollOffsetScrollBar
->setValue(value
);
302 // Prevent a potential endless layout loop (see bug #293318).
303 updateScrollOffsetScrollBarPolicy();
308 void KItemListContainer::updateItemOffsetScrollBar()
310 const KItemListView
* view
= m_controller
->view();
315 KItemListSmoothScroller
* smoothScroller
= nullptr;
316 QScrollBar
* itemOffsetScrollBar
= nullptr;
319 if (view
->scrollOrientation() == Qt::Vertical
) {
320 smoothScroller
= m_horizontalSmoothScroller
;
321 itemOffsetScrollBar
= horizontalScrollBar();
322 singleStep
= view
->size().width() / 10;
323 pageStep
= view
->size().width();
325 smoothScroller
= m_verticalSmoothScroller
;
326 itemOffsetScrollBar
= verticalScrollBar();
327 singleStep
= view
->size().height() / 10;
328 pageStep
= view
->size().height();
331 const int value
= view
->itemOffset();
332 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
333 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
334 itemOffsetScrollBar
->setSingleStep(singleStep
);
335 itemOffsetScrollBar
->setPageStep(pageStep
);
336 itemOffsetScrollBar
->setMinimum(0);
337 itemOffsetScrollBar
->setMaximum(maximum
);
338 itemOffsetScrollBar
->setValue(value
);
342 void KItemListContainer::updateGeometries()
344 QRect rect
= geometry();
346 int extra
= frameWidth() * 2;
348 option
.initFrom(this);
349 int scrollbarSpacing
= 0;
350 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
351 scrollbarSpacing
= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
354 const int widthDec
= verticalScrollBar()->isVisible()
355 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
358 const int heightDec
= horizontalScrollBar()->isVisible()
359 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
362 const QRectF
newGeometry(0, 0, rect
.width() - widthDec
,
363 rect
.height() - heightDec
);
364 if (m_controller
->view()->geometry() != newGeometry
) {
365 m_controller
->view()->setGeometry(newGeometry
);
367 // Get the real geometry of the view again since the scrollbars
368 // visibilities and the view geometry may have changed in re-layout.
369 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(m_controller
->view()->geometry());
370 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(m_controller
->view()->geometry().toRect());
372 updateScrollOffsetScrollBar();
373 updateItemOffsetScrollBar();
377 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
379 if (orientation
== Qt::Vertical
) {
380 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
381 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
383 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
384 m_verticalSmoothScroller
->setPropertyName("itemOffset");
388 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
390 const KItemListView
* view
= m_controller
->view();
392 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
395 option
.initFrom(this);
396 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
398 QSizeF newViewSize
= m_controller
->view()->size();
400 newViewSize
.rwidth() += scrollBarInc
;
402 newViewSize
.rheight() += scrollBarInc
;
405 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
406 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
408 setVerticalScrollBarPolicy(policy
);
410 setHorizontalScrollBarPolicy(policy
);
414 #include "kitemlistcontainer.moc"