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>
36 #include <QStyleOption>
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.
44 class KItemListContainerViewport
: public QGraphicsView
47 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
49 virtual void wheelEvent(QWheelEvent
* event
);
52 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
53 QGraphicsView(scene
, parent
)
55 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
56 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
57 setViewportMargins(0, 0, 0, 0);
58 setFrameShape(QFrame::NoFrame
);
61 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
63 // Assure that the wheel-event gets forwarded to the parent
64 // and not handled at all by QGraphicsView.
68 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
69 QAbstractScrollArea(parent
),
70 m_controller(controller
),
71 m_horizontalSmoothScroller(0),
72 m_verticalSmoothScroller(0)
75 controller
->setParent(this);
77 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
78 setViewport(graphicsView
);
80 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
81 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
83 if (controller
->model()) {
84 slotModelChanged(controller
->model(), 0);
86 if (controller
->view()) {
87 slotViewChanged(controller
->view(), 0);
90 connect(controller
, &KItemListController::modelChanged
,
91 this, &KItemListContainer::slotModelChanged
);
92 connect(controller
, &KItemListController::viewChanged
,
93 this, &KItemListContainer::slotViewChanged
);
96 KItemListContainer::~KItemListContainer()
98 // Don't rely on the QObject-order to delete the controller, otherwise
99 // the QGraphicsScene might get deleted before the view.
104 KItemListController
* KItemListContainer::controller() const
109 void KItemListContainer::setEnabledFrame(bool enable
)
111 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
113 setFrameShape(QFrame::StyledPanel
);
114 graphicsView
->setPalette(palette());
115 graphicsView
->viewport()->setAutoFillBackground(true);
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
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);
130 bool KItemListContainer::enabledFrame() const
132 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
133 return graphicsView
->autoFillBackground();
136 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
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
144 KItemListView
* view
= m_controller
->view();
146 QApplication::sendEvent(view
, event
);
150 void KItemListContainer::showEvent(QShowEvent
* event
)
152 QAbstractScrollArea::showEvent(event
);
156 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
158 QAbstractScrollArea::resizeEvent(event
);
162 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
164 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
165 m_verticalSmoothScroller
->scrollContentsBy(dy
);
168 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
170 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
175 KItemListView
* view
= m_controller
->view();
181 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
182 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
183 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
184 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
186 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
187 if (!event
->pixelDelta().isNull()) {
188 const int numPixels
= event
->pixelDelta().y();
189 smoothScroller
->scrollTo(scrollBar
->value() - numPixels
);
191 const int numDegrees
= event
->angleDelta().y() / 8;
192 const int numSteps
= numDegrees
/ 15;
193 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
199 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
202 updateSmoothScrollers(current
);
205 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
211 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
213 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
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);
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());
249 void KItemListContainer::scrollTo(qreal offset
)
251 const KItemListView
* view
= m_controller
->view();
253 if (view
->scrollOrientation() == Qt::Vertical
) {
254 m_verticalSmoothScroller
->scrollTo(offset
);
256 m_horizontalSmoothScroller
->scrollTo(offset
);
261 void KItemListContainer::updateScrollOffsetScrollBar()
263 const KItemListView
* view
= m_controller
->view();
268 KItemListSmoothScroller
* smoothScroller
= 0;
269 QScrollBar
* scrollOffsetScrollBar
= 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();
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()));
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()));
294 const int value
= view
->scrollOffset();
295 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
296 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
297 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
299 scrollOffsetScrollBar
->setSingleStep(singleStep
);
300 scrollOffsetScrollBar
->setPageStep(pageStep
);
301 scrollOffsetScrollBar
->setMinimum(0);
302 scrollOffsetScrollBar
->setMaximum(maximum
);
303 scrollOffsetScrollBar
->setValue(value
);
306 // Prevent a potential endless layout loop (see bug #293318).
307 updateScrollOffsetScrollBarPolicy();
312 void KItemListContainer::updateItemOffsetScrollBar()
314 const KItemListView
* view
= m_controller
->view();
319 KItemListSmoothScroller
* smoothScroller
= 0;
320 QScrollBar
* itemOffsetScrollBar
= 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();
329 smoothScroller
= m_verticalSmoothScroller
;
330 itemOffsetScrollBar
= verticalScrollBar();
331 singleStep
= view
->size().height() / 10;
332 pageStep
= view
->size().height();
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
);
346 void KItemListContainer::updateGeometries()
348 QRect rect
= geometry();
350 int extra
= frameWidth() * 2;
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);
358 const int widthDec
= verticalScrollBar()->isVisible()
359 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
362 const int heightDec
= horizontalScrollBar()->isVisible()
363 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
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
);
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());
376 updateScrollOffsetScrollBar();
377 updateItemOffsetScrollBar();
381 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
383 if (orientation
== Qt::Vertical
) {
384 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
385 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
387 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
388 m_verticalSmoothScroller
->setPropertyName("itemOffset");
392 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
394 const KItemListView
* view
= m_controller
->view();
396 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
399 option
.initFrom(this);
400 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
402 QSizeF newViewSize
= m_controller
->view()->size();
404 newViewSize
.rwidth() += scrollBarInc
;
406 newViewSize
.rheight() += scrollBarInc
;
409 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
410 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
412 setVerticalScrollBarPolicy(policy
);
414 setHorizontalScrollBarPolicy(policy
);