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_sliderMovedByUser(false),
63 m_viewOffsetAnimation(0)
66 controller
->setParent(this);
70 KItemListContainer::KItemListContainer(QWidget
* parent
) :
71 QAbstractScrollArea(parent
),
77 KItemListContainer::~KItemListContainer()
81 KItemListController
* KItemListContainer::controller() const
86 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
88 // TODO: We should find a better way to handle the key press events in the view.
89 // The reasons why we need this hack are:
90 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
91 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
92 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
94 KItemListView
* view
= m_controller
->view();
96 QApplication::sendEvent(view
, event
);
100 void KItemListContainer::showEvent(QShowEvent
* event
)
102 QAbstractScrollArea::showEvent(event
);
106 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
108 QAbstractScrollArea::resizeEvent(event
);
112 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
114 KItemListView
* view
= m_controller
->view();
119 const qreal currentOffset
= view
->offset();
120 qreal offsetDiff
= (view
->scrollOrientation() == Qt::Vertical
) ? dy
: dx
;
122 const bool animRunning
= (m_viewOffsetAnimation
->state() == QAbstractAnimation::Running
);
124 // Stopping a running animation means skipping the range from the current offset
125 // until the target offset. To prevent skipping of the range the difference
126 // is added to the new target offset.
127 m_viewOffsetAnimation
->stop();
128 const qreal targetOffset
= m_viewOffsetAnimation
->endValue().toReal();
129 offsetDiff
+= (currentOffset
- targetOffset
);
132 const qreal newOffset
= currentOffset
- offsetDiff
;
134 if (m_sliderMovedByUser
|| animRunning
) {
135 m_viewOffsetAnimation
->stop();
136 m_viewOffsetAnimation
->setStartValue(currentOffset
);
137 m_viewOffsetAnimation
->setEndValue(newOffset
);
138 m_viewOffsetAnimation
->setEasingCurve(animRunning
? QEasingCurve::OutQuad
: QEasingCurve::InOutQuad
);
139 m_viewOffsetAnimation
->start();
141 view
->setOffset(newOffset
);
145 bool KItemListContainer::eventFilter(QObject
* obj
, QEvent
* event
)
147 Q_ASSERT(obj
== horizontalScrollBar() || obj
== verticalScrollBar());
149 // Check whether the scrollbar has been adjusted by a mouse-event
150 // triggered by the user and remember this in m_sliderMovedByUser.
151 // The smooth scrolling will only get active if m_sliderMovedByUser
152 // is true (see scrollContentsBy()).
153 const bool scrollVertical
= (m_controller
->view()->scrollOrientation() == Qt::Vertical
);
154 const bool checkEvent
= ( scrollVertical
&& obj
== verticalScrollBar()) ||
155 (!scrollVertical
&& obj
== horizontalScrollBar());
157 switch (event
->type()) {
158 case QEvent::MouseButtonPress
:
159 m_sliderMovedByUser
= true;
162 case QEvent::MouseButtonRelease
:
163 m_sliderMovedByUser
= false;
167 wheelEvent(static_cast<QWheelEvent
*>(event
));
175 return QAbstractScrollArea::eventFilter(obj
, event
);
178 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
180 KItemListView
* view
= m_controller
->view();
181 if (!view
|| event
->orientation() != view
->scrollOrientation()) {
185 const int numDegrees
= event
->delta() / 8;
186 const int numSteps
= numDegrees
/ 15;
188 const bool previous
= m_sliderMovedByUser
;
189 m_sliderMovedByUser
= true;
190 if (view
->scrollOrientation() == Qt::Vertical
) {
191 const int value
= verticalScrollBar()->value();
192 verticalScrollBar()->setValue(value
- numSteps
* view
->size().height());
194 const int value
= horizontalScrollBar()->value();
195 horizontalScrollBar()->setValue(value
- numSteps
* view
->size().width());
197 m_sliderMovedByUser
= previous
;
202 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
208 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
210 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
212 scene
->removeItem(previous
);
213 disconnect(previous
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
214 disconnect(previous
, SIGNAL(maximumOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
215 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
216 m_viewOffsetAnimation
->setTargetObject(0);
219 scene
->addItem(current
);
220 connect(current
, SIGNAL(offsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
221 connect(current
, SIGNAL(maximumOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollBars()));
222 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
223 m_viewOffsetAnimation
->setTargetObject(current
);
227 void KItemListContainer::scrollTo(qreal offset
)
229 const KItemListView
* view
= m_controller
->view();
234 QScrollBar
* scrollBar
= (view
->scrollOrientation() == Qt::Vertical
)
235 ? verticalScrollBar() : horizontalScrollBar();
236 scrollBar
->setValue(offset
);
239 void KItemListContainer::updateScrollBars()
241 const KItemListView
* view
= m_controller
->view();
246 QScrollBar
* scrollBar
= 0;
249 QScrollBar
* otherScrollBar
= 0;
250 if (view
->scrollOrientation() == Qt::Vertical
) {
251 scrollBar
= verticalScrollBar();
252 singleStep
= view
->itemSize().height();
253 pageStep
= view
->size().height();
254 otherScrollBar
= horizontalScrollBar();
256 scrollBar
= horizontalScrollBar();
257 singleStep
= view
->itemSize().width();
258 pageStep
= view
->size().width();
259 otherScrollBar
= verticalScrollBar();
262 const int value
= view
->offset();
263 const int maximum
= qMax(0, int(view
->maximumOffset() - pageStep
));
264 if (m_viewOffsetAnimation
->state() == QAbstractAnimation::Running
) {
265 if (maximum
== scrollBar
->maximum()) {
266 // The value has been changed by the animation, no update
267 // of the scrollbars is required as their target state will be
268 // reached with the end of the animation.
272 // The maximum has been changed which indicates that the content
273 // of the view has been changed. Stop the animation in any case and
274 // update the scrollbars immediately.
275 m_viewOffsetAnimation
->stop();
278 scrollBar
->setSingleStep(singleStep
);
279 scrollBar
->setPageStep(pageStep
);
280 scrollBar
->setMinimum(0);
281 scrollBar
->setMaximum(maximum
);
282 scrollBar
->setValue(value
);
284 // Make sure that the other scroll bar is hidden
285 otherScrollBar
->setMaximum(0);
286 otherScrollBar
->setValue(0);
289 void KItemListContainer::updateGeometries()
291 QRect rect
= geometry();
293 int widthDec
= frameWidth() * 2;
294 if (verticalScrollBar()->isVisible()) {
295 widthDec
+= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
298 int heightDec
= frameWidth() * 2;
299 if (horizontalScrollBar()->isVisible()) {
300 heightDec
+= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
303 rect
.adjust(0, 0, -widthDec
, -heightDec
);
305 m_controller
->view()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
307 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
308 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
313 void KItemListContainer::initialize()
316 m_controller
= new KItemListController(this);
319 connect(m_controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)),
320 this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
321 connect(m_controller
, SIGNAL(viewChanged(KItemListView
*,KItemListView
*)),
322 this, SLOT(slotViewChanged(KItemListView
*,KItemListView
*)));
324 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
325 setViewport(graphicsView
);
327 m_viewOffsetAnimation
= new QPropertyAnimation(this, "offset");
328 m_viewOffsetAnimation
->setDuration(300);
330 horizontalScrollBar()->installEventFilter(this);
331 verticalScrollBar()->installEventFilter(this);
334 #include "kitemlistcontainer.moc"