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.
72 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
73 QAbstractScrollArea(parent
),
74 m_controller(controller
),
75 m_horizontalSmoothScroller(0),
76 m_verticalSmoothScroller(0)
79 controller
->setParent(this);
83 KItemListContainer::KItemListContainer(QWidget
* parent
) :
84 QAbstractScrollArea(parent
),
86 m_horizontalSmoothScroller(0),
87 m_verticalSmoothScroller(0)
92 KItemListContainer::~KItemListContainer()
96 KItemListController
* KItemListContainer::controller() const
101 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
103 // TODO: We should find a better way to handle the key press events in the view.
104 // The reasons why we need this hack are:
105 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
106 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
107 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
109 KItemListView
* view
= m_controller
->view();
111 QApplication::sendEvent(view
, event
);
115 void KItemListContainer::showEvent(QShowEvent
* event
)
117 QAbstractScrollArea::showEvent(event
);
121 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
123 QAbstractScrollArea::resizeEvent(event
);
127 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
129 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
130 m_verticalSmoothScroller
->scrollContentsBy(dy
);
133 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
135 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
140 KItemListView
* view
= m_controller
->view();
146 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
147 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
148 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
149 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
151 const int numDegrees
= event
->delta() / 8;
152 const int numSteps
= numDegrees
/ 15;
154 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
155 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
160 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
163 updateSmoothScrollers(current
);
166 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
172 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
174 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
176 scene
->removeItem(previous
);
177 disconnect(previous
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
178 disconnect(previous
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
179 disconnect(previous
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
180 disconnect(previous
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
181 disconnect(previous
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
182 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
183 m_horizontalSmoothScroller
->setTargetObject(0);
184 m_verticalSmoothScroller
->setTargetObject(0);
187 scene
->addItem(current
);
188 connect(current
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
189 connect(current
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
190 connect(current
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
191 connect(current
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
192 connect(current
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
193 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
194 m_horizontalSmoothScroller
->setTargetObject(current
);
195 m_verticalSmoothScroller
->setTargetObject(current
);
196 updateSmoothScrollers(current
->scrollOrientation());
200 void KItemListContainer::scrollTo(qreal offset
)
202 const KItemListView
* view
= m_controller
->view();
204 if (view
->scrollOrientation() == Qt::Vertical
) {
205 m_verticalSmoothScroller
->scrollTo(offset
);
207 m_horizontalSmoothScroller
->scrollTo(offset
);
212 void KItemListContainer::updateScrollOffsetScrollBar()
214 const KItemListView
* view
= m_controller
->view();
219 KItemListSmoothScroller
* smoothScroller
= 0;
220 QScrollBar
* scrollOffsetScrollBar
= 0;
223 if (view
->scrollOrientation() == Qt::Vertical
) {
224 smoothScroller
= m_verticalSmoothScroller
;
225 scrollOffsetScrollBar
= verticalScrollBar();
226 singleStep
= view
->itemSize().height();
227 pageStep
= view
->size().height();
229 smoothScroller
= m_horizontalSmoothScroller
;
230 scrollOffsetScrollBar
= horizontalScrollBar();
231 singleStep
= view
->itemSize().width();
232 pageStep
= view
->size().width();
235 const int value
= view
->scrollOffset();
236 const int maximum
= qMax(0, int(view
->maximumScrollOffset() - pageStep
));
237 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
238 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
239 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
241 scrollOffsetScrollBar
->setSingleStep(singleStep
);
242 scrollOffsetScrollBar
->setPageStep(pageStep
);
243 scrollOffsetScrollBar
->setMinimum(0);
244 scrollOffsetScrollBar
->setMaximum(maximum
);
245 scrollOffsetScrollBar
->setValue(value
);
248 // Prevent a potential endless layout loop (see bug #293318).
249 updateScrollOffsetScrollBarPolicy();
254 void KItemListContainer::updateItemOffsetScrollBar()
256 const KItemListView
* view
= m_controller
->view();
261 KItemListSmoothScroller
* smoothScroller
= 0;
262 QScrollBar
* itemOffsetScrollBar
= 0;
265 if (view
->scrollOrientation() == Qt::Vertical
) {
266 smoothScroller
= m_horizontalSmoothScroller
;
267 itemOffsetScrollBar
= horizontalScrollBar();
268 singleStep
= view
->size().width() / 10;
269 pageStep
= view
->size().width();
271 smoothScroller
= m_verticalSmoothScroller
;
272 itemOffsetScrollBar
= verticalScrollBar();
273 singleStep
= view
->size().height() / 10;
274 pageStep
= view
->size().height();
277 const int value
= view
->itemOffset();
278 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
279 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
280 itemOffsetScrollBar
->setSingleStep(singleStep
);
281 itemOffsetScrollBar
->setPageStep(pageStep
);
282 itemOffsetScrollBar
->setMinimum(0);
283 itemOffsetScrollBar
->setMaximum(maximum
);
284 itemOffsetScrollBar
->setValue(value
);
288 void KItemListContainer::updateGeometries()
290 QRect rect
= geometry();
292 int extra
= frameWidth() * 2;
294 option
.initFrom(this);
295 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
296 extra
+= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
299 const int widthDec
= verticalScrollBar()->isVisible()
300 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
303 const int heightDec
= horizontalScrollBar()->isVisible()
304 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
307 rect
.adjust(0, 0, -widthDec
, -heightDec
);
309 const QRectF
newGeometry(0, 0, rect
.width(), rect
.height());
310 if (m_controller
->view()->geometry() != newGeometry
) {
311 m_controller
->view()->setGeometry(newGeometry
);
313 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
314 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
316 updateScrollOffsetScrollBar();
317 updateItemOffsetScrollBar();
321 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
323 if (orientation
== Qt::Vertical
) {
324 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
325 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
327 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
328 m_verticalSmoothScroller
->setPropertyName("itemOffset");
332 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
334 const KItemListView
* view
= m_controller
->view();
336 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
339 option
.initFrom(this);
340 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
342 QSizeF newViewSize
= m_controller
->view()->size();
344 newViewSize
.rwidth() += scrollBarInc
;
346 newViewSize
.rheight() += scrollBarInc
;
349 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
350 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
352 setVerticalScrollBarPolicy(policy
);
354 setHorizontalScrollBarPolicy(policy
);
358 void KItemListContainer::initialize()
361 if (m_controller
->model()) {
362 slotModelChanged(m_controller
->model(), 0);
364 if (m_controller
->view()) {
365 slotViewChanged(m_controller
->view(), 0);
368 m_controller
= new KItemListController(this);
371 connect(m_controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)),
372 this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
373 connect(m_controller
, SIGNAL(viewChanged(KItemListView
*,KItemListView
*)),
374 this, SLOT(slotViewChanged(KItemListView
*,KItemListView
*)));
376 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
377 setViewport(graphicsView
);
379 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
380 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
383 #include "kitemlistcontainer.moc"