]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Improvements for selections, smooth scrolling, tooltips and info-panel
[dolphin.git] / src / kitemviews / kitemlistcontainer.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
22
23 #include "kitemlistcontainer.h"
24
25 #include "kitemlistcontroller.h"
26 #include "kitemlistview.h"
27 #include "kitemmodelbase.h"
28
29 #include <QGraphicsScene>
30 #include <QGraphicsView>
31 #include <QPropertyAnimation>
32 #include <QScrollBar>
33 #include <QStyle>
34
35 #include <KDebug>
36
37 class KItemListContainerViewport : public QGraphicsView
38 {
39 public:
40 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent)
41 : QGraphicsView(scene, parent)
42 {
43 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 setViewportMargins(0, 0, 0, 0);
46 setFrameShape(QFrame::NoFrame);
47 }
48
49 void scrollContentsBy(int dx, int dy)
50 {
51 Q_UNUSED(dx);
52 Q_UNUSED(dy);
53 // Do nothing. This prevents that e.g. the wheel-event
54 // results in a moving of the scene items.
55 }
56 };
57
58 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
59 QAbstractScrollArea(parent),
60 m_controller(controller),
61 m_sliderMovedByUser(false),
62 m_viewOffsetAnimation(0)
63 {
64 Q_ASSERT(controller);
65 controller->setParent(this);
66 initialize();
67 }
68
69 KItemListContainer::KItemListContainer(QWidget* parent) :
70 QAbstractScrollArea(parent),
71 m_controller(0)
72 {
73 initialize();
74 }
75
76 KItemListContainer::~KItemListContainer()
77 {
78 }
79
80 KItemListController* KItemListContainer::controller() const
81 {
82 return m_controller;
83 }
84
85 void KItemListContainer::showEvent(QShowEvent* event)
86 {
87 QAbstractScrollArea::showEvent(event);
88 updateGeometries();
89 }
90
91 void KItemListContainer::resizeEvent(QResizeEvent* event)
92 {
93 QAbstractScrollArea::resizeEvent(event);
94 updateGeometries();
95 }
96
97 void KItemListContainer::scrollContentsBy(int dx, int dy)
98 {
99 KItemListView* view = m_controller->view();
100 if (!view) {
101 return;
102 }
103
104 const qreal currentOffset = view->offset();
105 qreal offsetDiff = (view->scrollOrientation() == Qt::Vertical) ? dy : dx;
106
107 const bool animRunning = (m_viewOffsetAnimation->state() == QAbstractAnimation::Running);
108 if (animRunning) {
109 // Stopping a running animation means skipping the range from the current offset
110 // until the target offset. To prevent skipping of the range the difference
111 // is added to the new target offset.
112 m_viewOffsetAnimation->stop();
113 const qreal targetOffset = m_viewOffsetAnimation->endValue().toReal();
114 offsetDiff += (currentOffset - targetOffset);
115 }
116
117 const qreal newOffset = currentOffset - offsetDiff;
118
119 if (m_sliderMovedByUser || animRunning) {
120 m_viewOffsetAnimation->stop();
121 m_viewOffsetAnimation->setStartValue(currentOffset);
122 m_viewOffsetAnimation->setEndValue(newOffset);
123 m_viewOffsetAnimation->setEasingCurve(animRunning ? QEasingCurve::OutQuad : QEasingCurve::InOutQuad);
124 m_viewOffsetAnimation->start();
125 } else {
126 view->setOffset(newOffset);
127 }
128 }
129
130 bool KItemListContainer::eventFilter(QObject* obj, QEvent* event)
131 {
132 Q_ASSERT(obj == horizontalScrollBar() || obj == verticalScrollBar());
133
134 // Check whether the scrollbar has been adjusted by a mouse-event
135 // triggered by the user and remember this in m_sliderMovedByUser.
136 // The smooth scrolling will only get active if m_sliderMovedByUser
137 // is true (see scrollContentsBy()).
138 const bool scrollVertical = (m_controller->view()->scrollOrientation() == Qt::Vertical);
139 const bool checkEvent = ( scrollVertical && obj == verticalScrollBar()) ||
140 (!scrollVertical && obj == horizontalScrollBar());
141 if (checkEvent) {
142 switch (event->type()) {
143 case QEvent::MouseButtonPress:
144 m_sliderMovedByUser = true;
145 break;
146
147 case QEvent::MouseButtonRelease:
148 m_sliderMovedByUser = false;
149 break;
150
151 case QEvent::Wheel:
152 wheelEvent(static_cast<QWheelEvent*>(event));
153 break;
154
155 default:
156 break;
157 }
158 }
159
160 return QAbstractScrollArea::eventFilter(obj, event);
161 }
162
163 void KItemListContainer::wheelEvent(QWheelEvent* event)
164 {
165 KItemListView* view = m_controller->view();
166 if (!view || event->orientation() != view->scrollOrientation()) {
167 return;
168 }
169
170 const int numDegrees = event->delta() / 8;
171 const int numSteps = numDegrees / 15;
172
173 const bool previous = m_sliderMovedByUser;
174 m_sliderMovedByUser = true;
175 if (view->scrollOrientation() == Qt::Vertical) {
176 const int value = verticalScrollBar()->value();
177 verticalScrollBar()->setValue(value - numSteps * view->size().height());
178 } else {
179 const int value = horizontalScrollBar()->value();
180 horizontalScrollBar()->setValue(value - numSteps * view->size().width());
181 }
182 m_sliderMovedByUser = previous;
183
184 event->accept();
185 }
186
187 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
188 {
189 Q_UNUSED(current);
190 Q_UNUSED(previous);
191 }
192
193 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
194 {
195 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
196 if (previous) {
197 scene->removeItem(previous);
198 disconnect(previous, SIGNAL(offsetChanged(int,int)), this, SLOT(updateScrollBars()));
199 disconnect(previous, SIGNAL(maximumOffsetChanged(int,int)), this, SLOT(updateScrollBars()));
200 m_viewOffsetAnimation->setTargetObject(0);
201 }
202 if (current) {
203 scene->addItem(current);
204 connect(current, SIGNAL(offsetChanged(int,int)), this, SLOT(updateScrollBars()));
205 connect(current, SIGNAL(maximumOffsetChanged(int,int)), this, SLOT(updateScrollBars()));
206 m_viewOffsetAnimation->setTargetObject(current);
207 }
208 }
209
210 void KItemListContainer::updateScrollBars()
211 {
212 const KItemListView* view = m_controller->view();
213 if (!view) {
214 return;
215 }
216
217 QScrollBar* scrollBar = 0;
218 int singleStep = 0;
219 int pageStep = 0;
220 if (view->scrollOrientation() == Qt::Vertical) {
221 scrollBar = verticalScrollBar();
222 singleStep = view->itemSize().height();
223 pageStep = view->size().height();
224 } else {
225 scrollBar = horizontalScrollBar();
226 singleStep = view->itemSize().width();
227 pageStep = view->size().width();
228 }
229
230 const int value = view->offset();
231 const int maximum = qMax(0, int(view->maximumOffset() - pageStep));
232 if (m_viewOffsetAnimation->state() == QAbstractAnimation::Running) {
233 if (maximum == scrollBar->maximum()) {
234 // The value has been changed by the animation, no update
235 // of the scrollbars is required as their target state will be
236 // reached with the end of the animation.
237 return;
238 }
239
240 // The maximum has been changed which indicates that the content
241 // of the view has been changed. Stop the animation in any case and
242 // update the scrollbars immediately.
243 m_viewOffsetAnimation->stop();
244 }
245
246 scrollBar->setSingleStep(singleStep);
247 scrollBar->setPageStep(pageStep);
248 scrollBar->setMinimum(0);
249 scrollBar->setMaximum(maximum);
250 scrollBar->setValue(value);
251 }
252
253 void KItemListContainer::updateGeometries()
254 {
255 QRect rect = geometry();
256
257 int widthDec = frameWidth() * 2;
258 if (verticalScrollBar()->isVisible()) {
259 widthDec += style()->pixelMetric(QStyle::PM_ScrollBarExtent);
260 }
261
262 int heightDec = frameWidth() * 2;
263 if (horizontalScrollBar()->isVisible()) {
264 heightDec += style()->pixelMetric(QStyle::PM_ScrollBarExtent);
265 }
266
267 rect.adjust(0, 0, -widthDec, -heightDec);
268
269 m_controller->view()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
270
271 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
272 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
273
274 updateScrollBars();
275 }
276
277 void KItemListContainer::initialize()
278 {
279 if (!m_controller) {
280 m_controller = new KItemListController(this);
281 }
282
283 connect(m_controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
284 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
285 connect(m_controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
286 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
287
288 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
289 setViewport(graphicsView);
290
291 m_viewOffsetAnimation = new QPropertyAnimation(this, "offset");
292 m_viewOffsetAnimation->setDuration(500);
293
294 horizontalScrollBar()->installEventFilter(this);
295 verticalScrollBar()->installEventFilter(this);
296 }
297
298 #include "kitemlistcontainer.moc"