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 "kitemmodelbase.h"
29 #include "private/kitemlistsmoothscroller.h"
31 #include <QApplication>
32 #include <QGraphicsScene>
33 #include <QGraphicsView>
34 #include <QPropertyAnimation>
37 #include <QStyleOption>
42 * Replaces the default viewport of KItemListContainer by a
43 * non-scrollable viewport. The scrolling is done in an optimized
44 * way by KItemListView internally.
46 class KItemListContainerViewport
: public QGraphicsView
49 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
51 virtual void wheelEvent(QWheelEvent
* event
);
54 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
55 QGraphicsView(scene
, parent
)
57 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
58 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
59 setViewportMargins(0, 0, 0, 0);
60 setFrameShape(QFrame::NoFrame
);
63 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
65 // Assure that the wheel-event gets forwarded to the parent
66 // and not handled at all by QGraphicsView.
70 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
71 QAbstractScrollArea(parent
),
72 m_controller(controller
),
73 m_horizontalSmoothScroller(0),
74 m_verticalSmoothScroller(0)
77 controller
->setParent(this);
79 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
80 setViewport(graphicsView
);
82 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
83 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
85 if (controller
->model()) {
86 slotModelChanged(controller
->model(), 0);
88 if (controller
->view()) {
89 slotViewChanged(controller
->view(), 0);
92 connect(controller
, &KItemListController::modelChanged
,
93 this, &KItemListContainer::slotModelChanged
);
94 connect(controller
, &KItemListController::viewChanged
,
95 this, &KItemListContainer::slotViewChanged
);
98 KItemListContainer::~KItemListContainer()
100 // Don't rely on the QObject-order to delete the controller, otherwise
101 // the QGraphicsScene might get deleted before the view.
106 KItemListController
* KItemListContainer::controller() const
111 void KItemListContainer::setEnabledFrame(bool enable
)
113 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
115 setFrameShape(QFrame::StyledPanel
);
116 graphicsView
->setPalette(palette());
117 graphicsView
->viewport()->setAutoFillBackground(true);
119 setFrameShape(QFrame::NoFrame
);
120 // Make the background of the container transparent and apply the window-text color
121 // to the text color, so that enough contrast is given for all color
123 QPalette p
= graphicsView
->palette();
124 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
125 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
126 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
127 graphicsView
->setPalette(p
);
128 graphicsView
->viewport()->setAutoFillBackground(false);
132 bool KItemListContainer::enabledFrame() const
134 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
135 return graphicsView
->autoFillBackground();
138 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
140 // TODO: We should find a better way to handle the key press events in the view.
141 // The reasons why we need this hack are:
142 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
143 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
144 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
146 KItemListView
* view
= m_controller
->view();
148 QApplication::sendEvent(view
, event
);
152 void KItemListContainer::showEvent(QShowEvent
* event
)
154 QAbstractScrollArea::showEvent(event
);
158 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
160 QAbstractScrollArea::resizeEvent(event
);
164 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
166 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
167 m_verticalSmoothScroller
->scrollContentsBy(dy
);
170 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
172 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
177 KItemListView
* view
= m_controller
->view();
183 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
184 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
185 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
186 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
188 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
189 if (!event
->pixelDelta().isNull()) {
190 const int numPixels
= event
->pixelDelta().y();
191 smoothScroller
->scrollTo(scrollBar
->value() - numPixels
);
193 const int numDegrees
= event
->angleDelta().y() / 8;
194 const int numSteps
= numDegrees
/ 15;
195 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
201 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
204 updateSmoothScrollers(current
);
207 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
213 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
215 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
217 scene
->removeItem(previous
);
218 disconnect(previous
, &KItemListView::scrollOrientationChanged
,
219 this, &KItemListContainer::slotScrollOrientationChanged
);
220 disconnect(previous
, &KItemListView::scrollOffsetChanged
,
221 this, &KItemListContainer::updateScrollOffsetScrollBar
);
222 disconnect(previous
, &KItemListView::maximumScrollOffsetChanged
,
223 this, &KItemListContainer::updateScrollOffsetScrollBar
);
224 disconnect(previous
, &KItemListView::itemOffsetChanged
,
225 this, &KItemListContainer::updateItemOffsetScrollBar
);
226 disconnect(previous
, &KItemListView::maximumItemOffsetChanged
,
227 this, &KItemListContainer::updateItemOffsetScrollBar
);
228 disconnect(previous
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
229 m_horizontalSmoothScroller
->setTargetObject(0);
230 m_verticalSmoothScroller
->setTargetObject(0);
233 scene
->addItem(current
);
234 connect(current
, &KItemListView::scrollOrientationChanged
,
235 this, &KItemListContainer::slotScrollOrientationChanged
);
236 connect(current
, &KItemListView::scrollOffsetChanged
,
237 this, &KItemListContainer::updateScrollOffsetScrollBar
);
238 connect(current
, &KItemListView::maximumScrollOffsetChanged
,
239 this, &KItemListContainer::updateScrollOffsetScrollBar
);
240 connect(current
, &KItemListView::itemOffsetChanged
,
241 this, &KItemListContainer::updateItemOffsetScrollBar
);
242 connect(current
, &KItemListView::maximumItemOffsetChanged
,
243 this, &KItemListContainer::updateItemOffsetScrollBar
);
244 connect(current
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
245 m_horizontalSmoothScroller
->setTargetObject(current
);
246 m_verticalSmoothScroller
->setTargetObject(current
);
247 updateSmoothScrollers(current
->scrollOrientation());
251 void KItemListContainer::scrollTo(qreal offset
)
253 const KItemListView
* view
= m_controller
->view();
255 if (view
->scrollOrientation() == Qt::Vertical
) {
256 m_verticalSmoothScroller
->scrollTo(offset
);
258 m_horizontalSmoothScroller
->scrollTo(offset
);
263 void KItemListContainer::updateScrollOffsetScrollBar()
265 const KItemListView
* view
= m_controller
->view();
270 KItemListSmoothScroller
* smoothScroller
= 0;
271 QScrollBar
* scrollOffsetScrollBar
= 0;
275 if (view
->scrollOrientation() == Qt::Vertical
) {
276 smoothScroller
= m_verticalSmoothScroller
;
277 scrollOffsetScrollBar
= verticalScrollBar();
278 singleStep
= view
->itemSize().height();
279 // We cannot use view->size().height() because this height might
280 // include the header widget, which is not part of the scrolled area.
281 pageStep
= view
->verticalPageStep();
283 // However, the total height of the view must be considered for the
284 // maximum value of the scroll bar. Note that the view's scrollOffset()
285 // refers to the offset of the top part of the view, which might be
286 // hidden behind the header.
287 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().height()));
289 smoothScroller
= m_horizontalSmoothScroller
;
290 scrollOffsetScrollBar
= horizontalScrollBar();
291 singleStep
= view
->itemSize().width();
292 pageStep
= view
->size().width();
293 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().width()));
296 const int value
= view
->scrollOffset();
297 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
298 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
299 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
301 scrollOffsetScrollBar
->setSingleStep(singleStep
);
302 scrollOffsetScrollBar
->setPageStep(pageStep
);
303 scrollOffsetScrollBar
->setMinimum(0);
304 scrollOffsetScrollBar
->setMaximum(maximum
);
305 scrollOffsetScrollBar
->setValue(value
);
308 // Prevent a potential endless layout loop (see bug #293318).
309 updateScrollOffsetScrollBarPolicy();
314 void KItemListContainer::updateItemOffsetScrollBar()
316 const KItemListView
* view
= m_controller
->view();
321 KItemListSmoothScroller
* smoothScroller
= 0;
322 QScrollBar
* itemOffsetScrollBar
= 0;
325 if (view
->scrollOrientation() == Qt::Vertical
) {
326 smoothScroller
= m_horizontalSmoothScroller
;
327 itemOffsetScrollBar
= horizontalScrollBar();
328 singleStep
= view
->size().width() / 10;
329 pageStep
= view
->size().width();
331 smoothScroller
= m_verticalSmoothScroller
;
332 itemOffsetScrollBar
= verticalScrollBar();
333 singleStep
= view
->size().height() / 10;
334 pageStep
= view
->size().height();
337 const int value
= view
->itemOffset();
338 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
339 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
340 itemOffsetScrollBar
->setSingleStep(singleStep
);
341 itemOffsetScrollBar
->setPageStep(pageStep
);
342 itemOffsetScrollBar
->setMinimum(0);
343 itemOffsetScrollBar
->setMaximum(maximum
);
344 itemOffsetScrollBar
->setValue(value
);
348 void KItemListContainer::updateGeometries()
350 QRect rect
= geometry();
352 int extra
= frameWidth() * 2;
354 option
.initFrom(this);
355 int scrollbarSpacing
= 0;
356 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
357 scrollbarSpacing
= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
360 const int widthDec
= verticalScrollBar()->isVisible()
361 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
364 const int heightDec
= horizontalScrollBar()->isVisible()
365 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
368 const QRectF
newGeometry(0, 0, rect
.width() - widthDec
,
369 rect
.height() - heightDec
);
370 if (m_controller
->view()->geometry() != newGeometry
) {
371 m_controller
->view()->setGeometry(newGeometry
);
373 // Get the real geometry of the view again since the scrollbars
374 // visibilities and the view geometry may have changed in re-layout.
375 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(m_controller
->view()->geometry());
376 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(m_controller
->view()->geometry().toRect());
378 updateScrollOffsetScrollBar();
379 updateItemOffsetScrollBar();
383 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
385 if (orientation
== Qt::Vertical
) {
386 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
387 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
389 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
390 m_verticalSmoothScroller
->setPropertyName("itemOffset");
394 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
396 const KItemListView
* view
= m_controller
->view();
398 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
401 option
.initFrom(this);
402 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
404 QSizeF newViewSize
= m_controller
->view()->size();
406 newViewSize
.rwidth() += scrollBarInc
;
408 newViewSize
.rheight() += scrollBarInc
;
411 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
412 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
414 setVerticalScrollBarPolicy(policy
);
416 setHorizontalScrollBarPolicy(policy
);