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>
41 * Replaces the default viewport of KItemListContainer by a
42 * non-scrollable viewport. The scrolling is done in an optimized
43 * way by KItemListView internally.
45 class KItemListContainerViewport
: public QGraphicsView
48 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
50 virtual void wheelEvent(QWheelEvent
* event
);
53 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
54 QGraphicsView(scene
, parent
)
56 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
57 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
58 setViewportMargins(0, 0, 0, 0);
59 setFrameShape(QFrame::NoFrame
);
62 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
64 // Assure that the wheel-event gets forwarded to the parent
65 // and not handled at all by QGraphicsView.
69 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
70 QAbstractScrollArea(parent
),
71 m_controller(controller
),
72 m_horizontalSmoothScroller(0),
73 m_verticalSmoothScroller(0)
76 controller
->setParent(this);
78 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
79 setViewport(graphicsView
);
81 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
82 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
84 if (controller
->model()) {
85 slotModelChanged(controller
->model(), 0);
87 if (controller
->view()) {
88 slotViewChanged(controller
->view(), 0);
91 connect(controller
, &KItemListController::modelChanged
,
92 this, &KItemListContainer::slotModelChanged
);
93 connect(controller
, &KItemListController::viewChanged
,
94 this, &KItemListContainer::slotViewChanged
);
97 KItemListContainer::~KItemListContainer()
99 // Don't rely on the QObject-order to delete the controller, otherwise
100 // the QGraphicsScene might get deleted before the view.
105 KItemListController
* KItemListContainer::controller() const
110 void KItemListContainer::setEnabledFrame(bool enable
)
112 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
114 setFrameShape(QFrame::StyledPanel
);
115 graphicsView
->setPalette(palette());
116 graphicsView
->viewport()->setAutoFillBackground(true);
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
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);
131 bool KItemListContainer::enabledFrame() const
133 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
134 return graphicsView
->autoFillBackground();
137 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
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
145 KItemListView
* view
= m_controller
->view();
147 QApplication::sendEvent(view
, event
);
151 void KItemListContainer::showEvent(QShowEvent
* event
)
153 QAbstractScrollArea::showEvent(event
);
157 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
159 QAbstractScrollArea::resizeEvent(event
);
163 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
165 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
166 m_verticalSmoothScroller
->scrollContentsBy(dy
);
169 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
171 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
176 KItemListView
* view
= m_controller
->view();
182 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
183 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
184 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
185 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
187 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
188 if (!event
->pixelDelta().isNull()) {
189 const int numPixels
= event
->pixelDelta().y();
190 smoothScroller
->scrollTo(scrollBar
->value() - numPixels
);
192 const int numDegrees
= event
->angleDelta().y() / 8;
193 const int numSteps
= numDegrees
/ 15;
194 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
200 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
203 updateSmoothScrollers(current
);
206 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
212 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
214 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
216 scene
->removeItem(previous
);
217 disconnect(previous
, &KItemListView::scrollOrientationChanged
,
218 this, &KItemListContainer::slotScrollOrientationChanged
);
219 disconnect(previous
, &KItemListView::scrollOffsetChanged
,
220 this, &KItemListContainer::updateScrollOffsetScrollBar
);
221 disconnect(previous
, &KItemListView::maximumScrollOffsetChanged
,
222 this, &KItemListContainer::updateScrollOffsetScrollBar
);
223 disconnect(previous
, &KItemListView::itemOffsetChanged
,
224 this, &KItemListContainer::updateItemOffsetScrollBar
);
225 disconnect(previous
, &KItemListView::maximumItemOffsetChanged
,
226 this, &KItemListContainer::updateItemOffsetScrollBar
);
227 disconnect(previous
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
228 m_horizontalSmoothScroller
->setTargetObject(0);
229 m_verticalSmoothScroller
->setTargetObject(0);
232 scene
->addItem(current
);
233 connect(current
, &KItemListView::scrollOrientationChanged
,
234 this, &KItemListContainer::slotScrollOrientationChanged
);
235 connect(current
, &KItemListView::scrollOffsetChanged
,
236 this, &KItemListContainer::updateScrollOffsetScrollBar
);
237 connect(current
, &KItemListView::maximumScrollOffsetChanged
,
238 this, &KItemListContainer::updateScrollOffsetScrollBar
);
239 connect(current
, &KItemListView::itemOffsetChanged
,
240 this, &KItemListContainer::updateItemOffsetScrollBar
);
241 connect(current
, &KItemListView::maximumItemOffsetChanged
,
242 this, &KItemListContainer::updateItemOffsetScrollBar
);
243 connect(current
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
244 m_horizontalSmoothScroller
->setTargetObject(current
);
245 m_verticalSmoothScroller
->setTargetObject(current
);
246 updateSmoothScrollers(current
->scrollOrientation());
250 void KItemListContainer::scrollTo(qreal offset
)
252 const KItemListView
* view
= m_controller
->view();
254 if (view
->scrollOrientation() == Qt::Vertical
) {
255 m_verticalSmoothScroller
->scrollTo(offset
);
257 m_horizontalSmoothScroller
->scrollTo(offset
);
262 void KItemListContainer::updateScrollOffsetScrollBar()
264 const KItemListView
* view
= m_controller
->view();
269 KItemListSmoothScroller
* smoothScroller
= 0;
270 QScrollBar
* scrollOffsetScrollBar
= 0;
274 if (view
->scrollOrientation() == Qt::Vertical
) {
275 smoothScroller
= m_verticalSmoothScroller
;
276 scrollOffsetScrollBar
= verticalScrollBar();
277 singleStep
= view
->itemSize().height();
278 // We cannot use view->size().height() because this height might
279 // include the header widget, which is not part of the scrolled area.
280 pageStep
= view
->verticalPageStep();
282 // However, the total height of the view must be considered for the
283 // maximum value of the scroll bar. Note that the view's scrollOffset()
284 // refers to the offset of the top part of the view, which might be
285 // hidden behind the header.
286 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().height()));
288 smoothScroller
= m_horizontalSmoothScroller
;
289 scrollOffsetScrollBar
= horizontalScrollBar();
290 singleStep
= view
->itemSize().width();
291 pageStep
= view
->size().width();
292 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().width()));
295 const int value
= view
->scrollOffset();
296 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
297 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
298 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
300 scrollOffsetScrollBar
->setSingleStep(singleStep
);
301 scrollOffsetScrollBar
->setPageStep(pageStep
);
302 scrollOffsetScrollBar
->setMinimum(0);
303 scrollOffsetScrollBar
->setMaximum(maximum
);
304 scrollOffsetScrollBar
->setValue(value
);
307 // Prevent a potential endless layout loop (see bug #293318).
308 updateScrollOffsetScrollBarPolicy();
313 void KItemListContainer::updateItemOffsetScrollBar()
315 const KItemListView
* view
= m_controller
->view();
320 KItemListSmoothScroller
* smoothScroller
= 0;
321 QScrollBar
* itemOffsetScrollBar
= 0;
324 if (view
->scrollOrientation() == Qt::Vertical
) {
325 smoothScroller
= m_horizontalSmoothScroller
;
326 itemOffsetScrollBar
= horizontalScrollBar();
327 singleStep
= view
->size().width() / 10;
328 pageStep
= view
->size().width();
330 smoothScroller
= m_verticalSmoothScroller
;
331 itemOffsetScrollBar
= verticalScrollBar();
332 singleStep
= view
->size().height() / 10;
333 pageStep
= view
->size().height();
336 const int value
= view
->itemOffset();
337 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
338 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
339 itemOffsetScrollBar
->setSingleStep(singleStep
);
340 itemOffsetScrollBar
->setPageStep(pageStep
);
341 itemOffsetScrollBar
->setMinimum(0);
342 itemOffsetScrollBar
->setMaximum(maximum
);
343 itemOffsetScrollBar
->setValue(value
);
347 void KItemListContainer::updateGeometries()
349 QRect rect
= geometry();
351 int extra
= frameWidth() * 2;
353 option
.initFrom(this);
354 int scrollbarSpacing
= 0;
355 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
356 scrollbarSpacing
= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
359 const int widthDec
= verticalScrollBar()->isVisible()
360 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
363 const int heightDec
= horizontalScrollBar()->isVisible()
364 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
367 const QRectF
newGeometry(0, 0, rect
.width() - widthDec
,
368 rect
.height() - heightDec
);
369 if (m_controller
->view()->geometry() != newGeometry
) {
370 m_controller
->view()->setGeometry(newGeometry
);
372 // Get the real geometry of the view again since the scrollbars
373 // visibilities and the view geometry may have changed in re-layout.
374 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(m_controller
->view()->geometry());
375 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(m_controller
->view()->geometry().toRect());
377 updateScrollOffsetScrollBar();
378 updateItemOffsetScrollBar();
382 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
384 if (orientation
== Qt::Vertical
) {
385 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
386 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
388 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
389 m_verticalSmoothScroller
->setPropertyName("itemOffset");
393 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
395 const KItemListView
* view
= m_controller
->view();
397 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
400 option
.initFrom(this);
401 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
403 QSizeF newViewSize
= m_controller
->view()->size();
405 newViewSize
.rwidth() += scrollBarInc
;
407 newViewSize
.rheight() += scrollBarInc
;
410 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
411 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
413 setVerticalScrollBarPolicy(policy
);
415 setHorizontalScrollBarPolicy(policy
);