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"
28 #include "private/kitemlistsmoothscroller.h"
30 #include <QApplication>
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();
265 singleStep
= view
->itemSizeHint().height();
266 // We cannot use view->size().height() because this height might
267 // include the header widget, which is not part of the scrolled area.
268 pageStep
= view
->verticalPageStep();
270 // However, the total height of the view must be considered for the
271 // maximum value of the scroll bar. Note that the view's scrollOffset()
272 // refers to the offset of the top part of the view, which might be
273 // hidden behind the header.
274 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().height()));
276 smoothScroller
= m_horizontalSmoothScroller
;
277 scrollOffsetScrollBar
= horizontalScrollBar();
278 singleStep
= view
->itemSize().width();
279 pageStep
= view
->size().width();
280 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().width()));
283 const int value
= view
->scrollOffset();
284 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
285 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
286 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
288 scrollOffsetScrollBar
->setSingleStep(singleStep
);
289 scrollOffsetScrollBar
->setPageStep(pageStep
);
290 scrollOffsetScrollBar
->setMinimum(0);
291 scrollOffsetScrollBar
->setMaximum(maximum
);
292 scrollOffsetScrollBar
->setValue(value
);
295 // Prevent a potential endless layout loop (see bug #293318).
296 updateScrollOffsetScrollBarPolicy();
301 void KItemListContainer::updateItemOffsetScrollBar()
303 const KItemListView
* view
= m_controller
->view();
308 KItemListSmoothScroller
* smoothScroller
= nullptr;
309 QScrollBar
* itemOffsetScrollBar
= nullptr;
312 if (view
->scrollOrientation() == Qt::Vertical
) {
313 smoothScroller
= m_horizontalSmoothScroller
;
314 itemOffsetScrollBar
= horizontalScrollBar();
315 singleStep
= view
->size().width() / 10;
316 pageStep
= view
->size().width();
318 smoothScroller
= m_verticalSmoothScroller
;
319 itemOffsetScrollBar
= verticalScrollBar();
320 singleStep
= view
->size().height() / 10;
321 pageStep
= view
->size().height();
324 const int value
= view
->itemOffset();
325 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
326 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
327 itemOffsetScrollBar
->setSingleStep(singleStep
);
328 itemOffsetScrollBar
->setPageStep(pageStep
);
329 itemOffsetScrollBar
->setMinimum(0);
330 itemOffsetScrollBar
->setMaximum(maximum
);
331 itemOffsetScrollBar
->setValue(value
);
335 void KItemListContainer::updateGeometries()
337 QRect rect
= geometry();
339 int extra
= frameWidth() * 2;
341 option
.initFrom(this);
342 int scrollbarSpacing
= 0;
343 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
344 scrollbarSpacing
= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
347 const int widthDec
= verticalScrollBar()->isVisible()
348 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
351 const int heightDec
= horizontalScrollBar()->isVisible()
352 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
355 const QRectF
newGeometry(0, 0, rect
.width() - widthDec
,
356 rect
.height() - heightDec
);
357 if (m_controller
->view()->geometry() != newGeometry
) {
358 m_controller
->view()->setGeometry(newGeometry
);
360 // Get the real geometry of the view again since the scrollbars
361 // visibilities and the view geometry may have changed in re-layout.
362 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(m_controller
->view()->geometry());
363 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(m_controller
->view()->geometry().toRect());
365 updateScrollOffsetScrollBar();
366 updateItemOffsetScrollBar();
370 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
372 if (orientation
== Qt::Vertical
) {
373 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
374 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
376 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
377 m_verticalSmoothScroller
->setPropertyName("itemOffset");
381 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
383 const KItemListView
* view
= m_controller
->view();
385 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
388 option
.initFrom(this);
389 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
391 QSizeF newViewSize
= m_controller
->view()->size();
393 newViewSize
.rwidth() += scrollBarInc
;
395 newViewSize
.rheight() += scrollBarInc
;
398 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
399 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
401 setVerticalScrollBarPolicy(policy
);
403 setHorizontalScrollBarPolicy(policy
);
407 #include "kitemlistcontainer.moc"