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 <QApplication>
30 #include <QGraphicsScene>
31 #include <QGraphicsView>
32 #include <QPropertyAnimation>
38 class KItemListContainerViewport
: public QGraphicsView
41 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
)
42 : QGraphicsView(scene
, parent
)
44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
45 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
46 setViewportMargins(0, 0, 0, 0);
47 setFrameShape(QFrame::NoFrame
);
50 void scrollContentsBy(int dx
, int dy
)
54 // Do nothing. This prevents that e.g. the wheel-event
55 // results in a moving of the scene items.
59 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
60 QAbstractScrollArea(parent
),
61 m_controller(controller
),
62 m_smoothScrolling(false),
63 m_smoothScrollingAnimation(0)
66 controller
->setParent(this);
70 KItemListContainer::KItemListContainer(QWidget
* parent
) :
71 QAbstractScrollArea(parent
),
73 m_smoothScrolling(false),
74 m_smoothScrollingAnimation(0)
79 KItemListContainer::~KItemListContainer()
83 KItemListController
* KItemListContainer::controller() const
88 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
90 // TODO: We should find a better way to handle the key press events in the view.
91 // The reasons why we need this hack are:
92 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
93 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
94 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
96 KItemListView
* view
= m_controller
->view();
98 QApplication::sendEvent(view
, event
);
102 void KItemListContainer::showEvent(QShowEvent
* event
)
104 QAbstractScrollArea::showEvent(event
);
108 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
110 QAbstractScrollArea::resizeEvent(event
);
114 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
116 KItemListView
* view
= m_controller
->view();
121 const qreal currentOffset
= view
->offset();
122 qreal offsetDiff
= (view
->scrollOrientation() == Qt::Vertical
) ? dy
: dx
;
124 const bool animRunning
= (m_smoothScrollingAnimation
->state() == QAbstractAnimation::Running
);
126 // Stopping a running animation means skipping the range from the current offset
127 // until the target offset. To prevent skipping of the range the difference
128 // is added to the new target offset.
129 const qreal targetOffset
= m_smoothScrollingAnimation
->endValue().toReal();
130 offsetDiff
+= (currentOffset
- targetOffset
);
133 const qreal newOffset
= currentOffset
- offsetDiff
;
135 if (m_smoothScrolling
|| animRunning
) {
136 m_smoothScrollingAnimation
->stop();
137 m_smoothScrollingAnimation
->setStartValue(currentOffset
);
138 m_smoothScrollingAnimation
->setEndValue(newOffset
);
139 m_smoothScrollingAnimation
->setEasingCurve(animRunning
? QEasingCurve::OutQuad
: QEasingCurve::InOutQuad
);
140 m_smoothScrollingAnimation
->start();
141 view
->setOffset(currentOffset
);
143 view
->setOffset(newOffset
);
147 bool KItemListContainer::eventFilter(QObject
* obj
, QEvent
* event
)
149 Q_ASSERT(obj
== horizontalScrollBar() || obj
== verticalScrollBar());
151 // Check whether the scrollbar has been adjusted by a mouse-event
152 // triggered by the user and remember this in m_smoothScrolling.
153 // The smooth scrolling will only get active if m_smoothScrolling
154 // is true (see scrollContentsBy()).
155 const bool scrollVertical
= (m_controller
->view()->scrollOrientation() == Qt::Vertical
);
156 const bool checkEvent
= ( scrollVertical
&& obj
== verticalScrollBar()) ||
157 (!scrollVertical
&& obj
== horizontalScrollBar());
159 switch (event
->type()) {
160 case QEvent::MouseButtonPress
:
161 m_smoothScrolling
= true;
164 case QEvent::MouseButtonRelease
:
165 m_smoothScrolling
= false;
169 wheelEvent(static_cast<QWheelEvent
*>(event
));
177 return QAbstractScrollArea::eventFilter(obj
, event
);
180 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
182 KItemListView
* view
= m_controller
->view();
183 if (!view
|| event
->orientation() != view
->scrollOrientation()) {
187 const int numDegrees
= event
->delta() / 8;
188 const int numSteps
= numDegrees
/ 15;
190 const bool previous
= m_smoothScrolling
;
191 m_smoothScrolling
= true;
192 if (view
->scrollOrientation() == Qt::Vertical
) {
193 const int value
= verticalScrollBar()->value();
194 verticalScrollBar()->setValue(value
- numSteps
* view
->size().height());
196 const int value
= horizontalScrollBar()->value();
197 horizontalScrollBar()->setValue(value
- numSteps
* view
->size().width());
199 m_smoothScrolling
= previous
;
204 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
210 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
212 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
214 scene
->removeItem(previous
);
215 disconnect(previous
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
216 disconnect(previous
, SIGNAL(maximumOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
217 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
218 m_smoothScrollingAnimation
->setTargetObject(0);
221 scene
->addItem(current
);
222 connect(current
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
223 connect(current
, SIGNAL(maximumOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
224 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
225 m_smoothScrollingAnimation
->setTargetObject(current
);
229 void KItemListContainer::slotAnimationStateChanged(QAbstractAnimation::State newState
,
230 QAbstractAnimation::State oldState
)
233 if (newState
== QAbstractAnimation::Stopped
) {
234 m_smoothScrolling
= false;
239 void KItemListContainer::scrollTo(qreal offset
)
241 const KItemListView
* view
= m_controller
->view();
246 m_smoothScrolling
= true;
247 QScrollBar
* scrollBar
= (view
->scrollOrientation() == Qt::Vertical
)
248 ? verticalScrollBar() : horizontalScrollBar();
249 scrollBar
->setValue(offset
);
252 void KItemListContainer::updateScrollBars()
254 const KItemListView
* view
= m_controller
->view();
259 QScrollBar
* scrollBar
= 0;
262 QScrollBar
* otherScrollBar
= 0;
263 if (view
->scrollOrientation() == Qt::Vertical
) {
264 scrollBar
= verticalScrollBar();
265 singleStep
= view
->itemSize().height();
266 pageStep
= view
->size().height();
267 otherScrollBar
= horizontalScrollBar();
269 scrollBar
= horizontalScrollBar();
270 singleStep
= view
->itemSize().width();
271 pageStep
= view
->size().width();
272 otherScrollBar
= verticalScrollBar();
275 const int value
= view
->offset();
276 const int maximum
= qMax(0, int(view
->maximumOffset() - pageStep
));
277 if (m_smoothScrollingAnimation
->state() == QAbstractAnimation::Running
) {
278 if (maximum
== scrollBar
->maximum()) {
279 // The value has been changed by the animation, no update
280 // of the scrollbars is required as their target state will be
281 // reached with the end of the animation.
285 // The maximum has been changed which indicates that the content
286 // of the view has been changed. Stop the animation in any case and
287 // update the scrollbars immediately.
288 m_smoothScrollingAnimation
->stop();
291 scrollBar
->setSingleStep(singleStep
);
292 scrollBar
->setPageStep(pageStep
);
293 scrollBar
->setMinimum(0);
294 scrollBar
->setMaximum(maximum
);
295 scrollBar
->setValue(value
);
297 // Make sure that the other scroll bar is hidden
298 otherScrollBar
->setMaximum(0);
299 otherScrollBar
->setValue(0);
302 void KItemListContainer::updateGeometries()
304 QRect rect
= geometry();
306 int widthDec
= frameWidth() * 2;
307 if (verticalScrollBar()->isVisible()) {
308 widthDec
+= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
311 int heightDec
= frameWidth() * 2;
312 if (horizontalScrollBar()->isVisible()) {
313 heightDec
+= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
316 rect
.adjust(0, 0, -widthDec
, -heightDec
);
318 m_controller
->view()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
320 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
321 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
326 void KItemListContainer::initialize()
329 m_controller
= new KItemListController(this);
332 connect(m_controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)),
333 this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
334 connect(m_controller
, SIGNAL(viewChanged(KItemListView
*,KItemListView
*)),
335 this, SLOT(slotViewChanged(KItemListView
*,KItemListView
*)));
337 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
338 setViewport(graphicsView
);
340 m_smoothScrollingAnimation
= new QPropertyAnimation(this, "offset");
341 m_smoothScrollingAnimation
->setDuration(300);
342 connect(m_smoothScrollingAnimation
, SIGNAL(stateChanged(QAbstractAnimation::State
,QAbstractAnimation::State
)),
343 this, SLOT(slotAnimationStateChanged(QAbstractAnimation::State
,QAbstractAnimation::State
)));
345 horizontalScrollBar()->installEventFilter(this);
346 verticalScrollBar()->installEventFilter(this);
349 #include "kitemlistcontainer.moc"