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_scrollBarPressed(false),
63 m_smoothScrolling(false),
64 m_smoothScrollingAnimation(0)
67 controller
->setParent(this);
71 KItemListContainer::KItemListContainer(QWidget
* parent
) :
72 QAbstractScrollArea(parent
),
74 m_scrollBarPressed(false),
75 m_smoothScrolling(false),
76 m_smoothScrollingAnimation(0)
81 KItemListContainer::~KItemListContainer()
85 KItemListController
* KItemListContainer::controller() const
90 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
92 // TODO: We should find a better way to handle the key press events in the view.
93 // The reasons why we need this hack are:
94 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
95 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
96 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
98 KItemListView
* view
= m_controller
->view();
100 QApplication::sendEvent(view
, event
);
104 void KItemListContainer::showEvent(QShowEvent
* event
)
106 QAbstractScrollArea::showEvent(event
);
110 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
112 QAbstractScrollArea::resizeEvent(event
);
116 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
118 KItemListView
* view
= m_controller
->view();
123 const QScrollBar
* scrollBar
= (view
->scrollOrientation() == Qt::Vertical
)
124 ? verticalScrollBar() : horizontalScrollBar();
125 const qreal currentOffset
= view
->scrollOffset();
126 if (static_cast<int>(currentOffset
) == scrollBar
->value()) {
127 // The current offset is already synchronous to the scrollbar
131 qreal offsetDiff
= (view
->scrollOrientation() == Qt::Vertical
) ? dy
: dx
;
133 const bool animRunning
= (m_smoothScrollingAnimation
->state() == QAbstractAnimation::Running
);
135 // Stopping a running animation means skipping the range from the current offset
136 // until the target offset. To prevent skipping of the range the difference
137 // is added to the new target offset.
138 const qreal oldEndOffset
= m_smoothScrollingAnimation
->endValue().toReal();
139 offsetDiff
+= (currentOffset
- oldEndOffset
);
142 const qreal endOffset
= currentOffset
- offsetDiff
;
144 if (m_smoothScrolling
|| animRunning
) {
145 qreal startOffset
= currentOffset
;
147 // If the animation was running and has been interrupted by assigning a new end-offset
148 // one frame must be added to the start-offset to keep the animation smooth. This also
149 // assures that animation proceeds even in cases where new end-offset are triggered
150 // within a very short timeslots.
151 startOffset
+= (endOffset
- currentOffset
) * 1000 / (m_smoothScrollingAnimation
->duration() * 60);
154 m_smoothScrollingAnimation
->stop();
155 m_smoothScrollingAnimation
->setStartValue(startOffset
);
156 m_smoothScrollingAnimation
->setEndValue(endOffset
);
157 m_smoothScrollingAnimation
->setEasingCurve(animRunning
? QEasingCurve::OutQuad
: QEasingCurve::InOutQuad
);
158 m_smoothScrollingAnimation
->start();
159 view
->setScrollOffset(startOffset
);
161 view
->setScrollOffset(endOffset
);
165 bool KItemListContainer::eventFilter(QObject
* obj
, QEvent
* event
)
167 Q_ASSERT(obj
== horizontalScrollBar() || obj
== verticalScrollBar());
169 // Check whether the scrollbar has been adjusted by a mouse-event
170 // triggered by the user and remember this in m_scrollBarPressed.
171 // The smooth scrolling will only get active if m_scrollBarPressed
172 // is true (see scrollContentsBy()).
173 const bool scrollVertical
= (m_controller
->view()->scrollOrientation() == Qt::Vertical
);
174 const bool checkEvent
= ( scrollVertical
&& obj
== verticalScrollBar()) ||
175 (!scrollVertical
&& obj
== horizontalScrollBar());
177 switch (event
->type()) {
178 case QEvent::MouseButtonPress
:
179 m_scrollBarPressed
= true;
180 m_smoothScrolling
= true;
183 case QEvent::MouseButtonRelease
:
184 m_scrollBarPressed
= false;
185 m_smoothScrolling
= false;
189 wheelEvent(static_cast<QWheelEvent
*>(event
));
197 return QAbstractScrollArea::eventFilter(obj
, event
);
200 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
202 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
207 KItemListView
* view
= m_controller
->view();
209 if (!view
|| event
->orientation() != view
->scrollOrientation()) {
213 const int numDegrees
= event
->delta() / 8;
214 const int numSteps
= numDegrees
/ 15;
216 const bool previous
= m_smoothScrolling
;
217 m_smoothScrolling
= true;
218 if (view
->scrollOrientation() == Qt::Vertical
) {
219 const int value
= verticalScrollBar()->value();
220 verticalScrollBar()->setValue(value
- numSteps
* view
->size().height());
222 const int value
= horizontalScrollBar()->value();
223 horizontalScrollBar()->setValue(value
- numSteps
* view
->size().width());
225 m_smoothScrolling
= previous
;
230 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
236 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
238 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
240 scene
->removeItem(previous
);
241 disconnect(previous
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
242 disconnect(previous
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
243 disconnect(previous
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
244 disconnect(previous
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
245 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
246 m_smoothScrollingAnimation
->setTargetObject(0);
249 scene
->addItem(current
);
250 connect(current
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
251 connect(current
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
252 connect(current
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
253 connect(current
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
254 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
255 m_smoothScrollingAnimation
->setTargetObject(current
);
259 void KItemListContainer::slotAnimationStateChanged(QAbstractAnimation::State newState
,
260 QAbstractAnimation::State oldState
)
263 if (newState
== QAbstractAnimation::Stopped
&& m_smoothScrolling
&& !m_scrollBarPressed
) {
264 m_smoothScrolling
= false;
269 void KItemListContainer::scrollTo(qreal offset
)
271 const KItemListView
* view
= m_controller
->view();
276 m_smoothScrolling
= true;
277 QScrollBar
* scrollBar
= (view
->scrollOrientation() == Qt::Vertical
)
278 ? verticalScrollBar() : horizontalScrollBar();
279 scrollBar
->setValue(offset
);
282 void KItemListContainer::updateScrollOffsetScrollBar()
284 const KItemListView
* view
= m_controller
->view();
289 QScrollBar
* scrollOffsetScrollBar
= 0;
292 if (view
->scrollOrientation() == Qt::Vertical
) {
293 scrollOffsetScrollBar
= verticalScrollBar();
294 singleStep
= view
->itemSize().height();
295 pageStep
= view
->size().height();
297 scrollOffsetScrollBar
= horizontalScrollBar();
298 singleStep
= view
->itemSize().width();
299 pageStep
= view
->size().width();
302 const int value
= view
->scrollOffset();
303 const int maximum
= qMax(0, int(view
->maximumScrollOffset() - pageStep
));
304 if (m_smoothScrollingAnimation
->state() == QAbstractAnimation::Running
) {
305 if (maximum
== scrollOffsetScrollBar
->maximum()) {
306 // The value has been changed by the animation, no update
307 // of the scrollbars is required as their target state will be
308 // reached with the end of the animation.
312 // The maximum has been changed which indicates that the content
313 // of the view has been changed. Stop the animation in any case and
314 // update the scrollbars immediately.
315 m_smoothScrollingAnimation
->stop();
318 scrollOffsetScrollBar
->setSingleStep(singleStep
);
319 scrollOffsetScrollBar
->setPageStep(pageStep
);
320 scrollOffsetScrollBar
->setMinimum(0);
321 scrollOffsetScrollBar
->setMaximum(maximum
);
322 scrollOffsetScrollBar
->setValue(value
);
325 void KItemListContainer::updateItemOffsetScrollBar()
327 const KItemListView
* view
= m_controller
->view();
332 QScrollBar
* itemOffsetScrollBar
= 0;
335 if (view
->scrollOrientation() == Qt::Vertical
) {
336 itemOffsetScrollBar
= horizontalScrollBar();
337 singleStep
= view
->itemSize().width() / 10;
338 pageStep
= view
->size().width();
340 itemOffsetScrollBar
= verticalScrollBar();
341 singleStep
= view
->itemSize().height() / 10;
342 pageStep
= view
->size().height();
345 const int value
= view
->itemOffset();
346 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
348 itemOffsetScrollBar
->setSingleStep(singleStep
);
349 itemOffsetScrollBar
->setPageStep(pageStep
);
350 itemOffsetScrollBar
->setMinimum(0);
351 itemOffsetScrollBar
->setMaximum(maximum
);
352 itemOffsetScrollBar
->setValue(value
);
355 void KItemListContainer::updateGeometries()
357 QRect rect
= geometry();
359 const int widthDec
= verticalScrollBar()->isVisible()
360 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent
)
363 const int heightDec
= horizontalScrollBar()->isVisible()
364 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent
)
367 rect
.adjust(0, 0, -widthDec
, -heightDec
);
369 const QRectF
newGeometry(0, 0, rect
.width(), rect
.height());
370 if (m_controller
->view()->geometry() != newGeometry
) {
371 m_controller
->view()->setGeometry(newGeometry
);
373 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
374 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
376 updateScrollOffsetScrollBar();
377 updateItemOffsetScrollBar();
381 void KItemListContainer::initialize()
384 m_controller
= new KItemListController(this);
387 connect(m_controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)),
388 this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
389 connect(m_controller
, SIGNAL(viewChanged(KItemListView
*,KItemListView
*)),
390 this, SLOT(slotViewChanged(KItemListView
*,KItemListView
*)));
392 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
393 setViewport(graphicsView
);
395 m_smoothScrollingAnimation
= new QPropertyAnimation(this, "scrollOffset");
396 m_smoothScrollingAnimation
->setDuration(300);
397 connect(m_smoothScrollingAnimation
, SIGNAL(stateChanged(QAbstractAnimation::State
,QAbstractAnimation::State
)),
398 this, SLOT(slotAnimationStateChanged(QAbstractAnimation::State
,QAbstractAnimation::State
)));
400 horizontalScrollBar()->installEventFilter(this);
401 verticalScrollBar()->installEventFilter(this);
404 #include "kitemlistcontainer.moc"