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 <QGraphicsScene>
31 #include <QGraphicsView>
33 #include <QStyleOption>
36 * Replaces the default viewport of KItemListContainer by a
37 * non-scrollable viewport. The scrolling is done in an optimized
38 * way by KItemListView internally.
40 class KItemListContainerViewport
: public QGraphicsView
45 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
47 void wheelEvent(QWheelEvent
* event
) override
;
50 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
51 QGraphicsView(scene
, parent
)
53 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
54 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
55 setViewportMargins(0, 0, 0, 0);
56 setFrameShape(QFrame::NoFrame
);
59 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
61 // Assure that the wheel-event gets forwarded to the parent
62 // and not handled at all by QGraphicsView.
66 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
67 QAbstractScrollArea(parent
),
68 m_controller(controller
),
69 m_horizontalSmoothScroller(nullptr),
70 m_verticalSmoothScroller(nullptr)
73 controller
->setParent(this);
75 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
76 setViewport(graphicsView
);
78 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
79 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
81 if (controller
->model()) {
82 slotModelChanged(controller
->model(), nullptr);
84 if (controller
->view()) {
85 slotViewChanged(controller
->view(), nullptr);
88 connect(controller
, &KItemListController::modelChanged
,
89 this, &KItemListContainer::slotModelChanged
);
90 connect(controller
, &KItemListController::viewChanged
,
91 this, &KItemListContainer::slotViewChanged
);
94 KItemListContainer::~KItemListContainer()
96 // Don't rely on the QObject-order to delete the controller, otherwise
97 // the QGraphicsScene might get deleted before the view.
99 m_controller
= nullptr;
102 KItemListController
* KItemListContainer::controller() const
107 void KItemListContainer::setEnabledFrame(bool enable
)
109 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
111 setFrameShape(QFrame::StyledPanel
);
112 graphicsView
->setPalette(palette());
113 graphicsView
->viewport()->setAutoFillBackground(true);
115 setFrameShape(QFrame::NoFrame
);
116 // Make the background of the container transparent and apply the window-text color
117 // to the text color, so that enough contrast is given for all color
119 QPalette p
= graphicsView
->palette();
120 p
.setColor(QPalette::Active
, QPalette::Text
, p
.color(QPalette::Active
, QPalette::WindowText
));
121 p
.setColor(QPalette::Inactive
, QPalette::Text
, p
.color(QPalette::Inactive
, QPalette::WindowText
));
122 p
.setColor(QPalette::Disabled
, QPalette::Text
, p
.color(QPalette::Disabled
, QPalette::WindowText
));
123 graphicsView
->setPalette(p
);
124 graphicsView
->viewport()->setAutoFillBackground(false);
128 bool KItemListContainer::enabledFrame() const
130 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
131 return graphicsView
->autoFillBackground();
134 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
136 // TODO: We should find a better way to handle the key press events in the view.
137 // The reasons why we need this hack are:
138 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
139 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
140 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
142 KItemListView
* view
= m_controller
->view();
144 QApplication::sendEvent(view
, event
);
148 void KItemListContainer::showEvent(QShowEvent
* event
)
150 QAbstractScrollArea::showEvent(event
);
154 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
156 QAbstractScrollArea::resizeEvent(event
);
160 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
162 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
163 m_verticalSmoothScroller
->scrollContentsBy(dy
);
166 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
168 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
173 KItemListView
* view
= m_controller
->view();
179 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
180 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
181 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
182 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
184 smoothScroller
->handleWheelEvent(event
);
187 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
190 updateSmoothScrollers(current
);
193 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
199 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
201 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
203 scene
->removeItem(previous
);
204 disconnect(previous
, &KItemListView::scrollOrientationChanged
,
205 this, &KItemListContainer::slotScrollOrientationChanged
);
206 disconnect(previous
, &KItemListView::scrollOffsetChanged
,
207 this, &KItemListContainer::updateScrollOffsetScrollBar
);
208 disconnect(previous
, &KItemListView::maximumScrollOffsetChanged
,
209 this, &KItemListContainer::updateScrollOffsetScrollBar
);
210 disconnect(previous
, &KItemListView::itemOffsetChanged
,
211 this, &KItemListContainer::updateItemOffsetScrollBar
);
212 disconnect(previous
, &KItemListView::maximumItemOffsetChanged
,
213 this, &KItemListContainer::updateItemOffsetScrollBar
);
214 disconnect(previous
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
215 m_horizontalSmoothScroller
->setTargetObject(nullptr);
216 m_verticalSmoothScroller
->setTargetObject(nullptr);
219 scene
->addItem(current
);
220 connect(current
, &KItemListView::scrollOrientationChanged
,
221 this, &KItemListContainer::slotScrollOrientationChanged
);
222 connect(current
, &KItemListView::scrollOffsetChanged
,
223 this, &KItemListContainer::updateScrollOffsetScrollBar
);
224 connect(current
, &KItemListView::maximumScrollOffsetChanged
,
225 this, &KItemListContainer::updateScrollOffsetScrollBar
);
226 connect(current
, &KItemListView::itemOffsetChanged
,
227 this, &KItemListContainer::updateItemOffsetScrollBar
);
228 connect(current
, &KItemListView::maximumItemOffsetChanged
,
229 this, &KItemListContainer::updateItemOffsetScrollBar
);
230 connect(current
, &KItemListView::scrollTo
, this, &KItemListContainer::scrollTo
);
231 m_horizontalSmoothScroller
->setTargetObject(current
);
232 m_verticalSmoothScroller
->setTargetObject(current
);
233 updateSmoothScrollers(current
->scrollOrientation());
237 void KItemListContainer::scrollTo(qreal offset
)
239 const KItemListView
* view
= m_controller
->view();
241 if (view
->scrollOrientation() == Qt::Vertical
) {
242 m_verticalSmoothScroller
->scrollTo(offset
);
244 m_horizontalSmoothScroller
->scrollTo(offset
);
249 void KItemListContainer::updateScrollOffsetScrollBar()
251 const KItemListView
* view
= m_controller
->view();
256 KItemListSmoothScroller
* smoothScroller
= nullptr;
257 QScrollBar
* scrollOffsetScrollBar
= nullptr;
261 if (view
->scrollOrientation() == Qt::Vertical
) {
262 smoothScroller
= m_verticalSmoothScroller
;
263 scrollOffsetScrollBar
= verticalScrollBar();
264 singleStep
= view
->itemSizeHint().height();
265 // We cannot use view->size().height() because this height might
266 // include the header widget, which is not part of the scrolled area.
267 pageStep
= view
->verticalPageStep();
269 // However, the total height of the view must be considered for the
270 // maximum value of the scroll bar. Note that the view's scrollOffset()
271 // refers to the offset of the top part of the view, which might be
272 // hidden behind the header.
273 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().height()));
275 smoothScroller
= m_horizontalSmoothScroller
;
276 scrollOffsetScrollBar
= horizontalScrollBar();
277 singleStep
= view
->itemSize().width();
278 pageStep
= view
->size().width();
279 maximum
= qMax(0, int(view
->maximumScrollOffset() - view
->size().width()));
282 const int value
= view
->scrollOffset();
283 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
284 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
285 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
287 scrollOffsetScrollBar
->setSingleStep(singleStep
);
288 scrollOffsetScrollBar
->setPageStep(pageStep
);
289 scrollOffsetScrollBar
->setMinimum(0);
290 scrollOffsetScrollBar
->setMaximum(maximum
);
291 scrollOffsetScrollBar
->setValue(value
);
294 // Prevent a potential endless layout loop (see bug #293318).
295 updateScrollOffsetScrollBarPolicy();
300 void KItemListContainer::updateItemOffsetScrollBar()
302 const KItemListView
* view
= m_controller
->view();
307 KItemListSmoothScroller
* smoothScroller
= nullptr;
308 QScrollBar
* itemOffsetScrollBar
= nullptr;
311 if (view
->scrollOrientation() == Qt::Vertical
) {
312 smoothScroller
= m_horizontalSmoothScroller
;
313 itemOffsetScrollBar
= horizontalScrollBar();
314 singleStep
= view
->size().width() / 10;
315 pageStep
= view
->size().width();
317 smoothScroller
= m_verticalSmoothScroller
;
318 itemOffsetScrollBar
= verticalScrollBar();
319 singleStep
= view
->size().height() / 10;
320 pageStep
= view
->size().height();
323 const int value
= view
->itemOffset();
324 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
325 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
326 itemOffsetScrollBar
->setSingleStep(singleStep
);
327 itemOffsetScrollBar
->setPageStep(pageStep
);
328 itemOffsetScrollBar
->setMinimum(0);
329 itemOffsetScrollBar
->setMaximum(maximum
);
330 itemOffsetScrollBar
->setValue(value
);
334 void KItemListContainer::updateGeometries()
336 QRect rect
= geometry();
338 int extra
= frameWidth() * 2;
340 option
.initFrom(this);
341 int scrollbarSpacing
= 0;
342 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
343 scrollbarSpacing
= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
346 const int widthDec
= verticalScrollBar()->isVisible()
347 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
350 const int heightDec
= horizontalScrollBar()->isVisible()
351 ? extra
+ scrollbarSpacing
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
354 const QRectF
newGeometry(0, 0, rect
.width() - widthDec
,
355 rect
.height() - heightDec
);
356 if (m_controller
->view()->geometry() != newGeometry
) {
357 m_controller
->view()->setGeometry(newGeometry
);
359 // Get the real geometry of the view again since the scrollbars
360 // visibilities and the view geometry may have changed in re-layout.
361 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(m_controller
->view()->geometry());
362 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(m_controller
->view()->geometry().toRect());
364 updateScrollOffsetScrollBar();
365 updateItemOffsetScrollBar();
369 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
371 if (orientation
== Qt::Vertical
) {
372 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
373 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
375 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
376 m_verticalSmoothScroller
->setPropertyName("itemOffset");
380 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
382 const KItemListView
* view
= m_controller
->view();
384 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
387 option
.initFrom(this);
388 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
390 QSizeF newViewSize
= m_controller
->view()->size();
392 newViewSize
.rwidth() += scrollBarInc
;
394 newViewSize
.rheight() += scrollBarInc
;
397 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
398 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
400 setVerticalScrollBarPolicy(policy
);
402 setHorizontalScrollBarPolicy(policy
);
406 #include "kitemlistcontainer.moc"