]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Draw styled header for the details-view
[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 <QApplication>
30 #include <QGraphicsScene>
31 #include <QGraphicsView>
32 #include <QPropertyAnimation>
33 #include <QScrollBar>
34 #include <QStyle>
35
36 #include <KDebug>
37
38 class KItemListContainerViewport : public QGraphicsView
39 {
40 public:
41 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent)
42 : QGraphicsView(scene, parent)
43 {
44 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 setViewportMargins(0, 0, 0, 0);
47 setFrameShape(QFrame::NoFrame);
48 }
49
50 void scrollContentsBy(int dx, int dy)
51 {
52 Q_UNUSED(dx);
53 Q_UNUSED(dy);
54 // Do nothing. This prevents that e.g. the wheel-event
55 // results in a moving of the scene items.
56 }
57 };
58
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)
65 {
66 Q_ASSERT(controller);
67 controller->setParent(this);
68 initialize();
69 }
70
71 KItemListContainer::KItemListContainer(QWidget* parent) :
72 QAbstractScrollArea(parent),
73 m_controller(0),
74 m_scrollBarPressed(false),
75 m_smoothScrolling(false),
76 m_smoothScrollingAnimation(0)
77 {
78 initialize();
79 }
80
81 KItemListContainer::~KItemListContainer()
82 {
83 }
84
85 KItemListController* KItemListContainer::controller() const
86 {
87 return m_controller;
88 }
89
90 void KItemListContainer::keyPressEvent(QKeyEvent* event)
91 {
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
97 // does not work.
98 KItemListView* view = m_controller->view();
99 if (view) {
100 QApplication::sendEvent(view, event);
101 }
102 }
103
104 void KItemListContainer::showEvent(QShowEvent* event)
105 {
106 QAbstractScrollArea::showEvent(event);
107 updateGeometries();
108 }
109
110 void KItemListContainer::resizeEvent(QResizeEvent* event)
111 {
112 QAbstractScrollArea::resizeEvent(event);
113 updateGeometries();
114 }
115
116 void KItemListContainer::scrollContentsBy(int dx, int dy)
117 {
118 KItemListView* view = m_controller->view();
119 if (!view) {
120 return;
121 }
122
123 const QScrollBar* scrollBar = (view->scrollOrientation() == Qt::Vertical)
124 ? verticalScrollBar() : horizontalScrollBar();
125 const qreal currentOffset = view->offset();
126 if (static_cast<int>(currentOffset) == scrollBar->value()) {
127 // The current offset is already synchronous to the scrollbar
128 return;
129 }
130
131 qreal offsetDiff = (view->scrollOrientation() == Qt::Vertical) ? dy : dx;
132
133 const bool animRunning = (m_smoothScrollingAnimation->state() == QAbstractAnimation::Running);
134 if (animRunning) {
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);
140 }
141
142 const qreal endOffset = currentOffset - offsetDiff;
143
144 if (m_smoothScrolling || animRunning) {
145 qreal startOffset = currentOffset;
146 if (animRunning) {
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);
152 }
153
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->setOffset(startOffset);
160 } else {
161 view->setOffset(endOffset);
162 }
163 }
164
165 bool KItemListContainer::eventFilter(QObject* obj, QEvent* event)
166 {
167 Q_ASSERT(obj == horizontalScrollBar() || obj == verticalScrollBar());
168
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());
176 if (checkEvent) {
177 switch (event->type()) {
178 case QEvent::MouseButtonPress:
179 m_scrollBarPressed = true;
180 m_smoothScrolling = true;
181 break;
182
183 case QEvent::MouseButtonRelease:
184 m_scrollBarPressed = false;
185 m_smoothScrolling = false;
186 break;
187
188 case QEvent::Wheel:
189 wheelEvent(static_cast<QWheelEvent*>(event));
190 break;
191
192 default:
193 break;
194 }
195 }
196
197 return QAbstractScrollArea::eventFilter(obj, event);
198 }
199
200 void KItemListContainer::wheelEvent(QWheelEvent* event)
201 {
202 if (event->modifiers().testFlag(Qt::ControlModifier)) {
203 event->ignore();
204 return;
205 }
206
207 KItemListView* view = m_controller->view();
208
209 if (!view || event->orientation() != view->scrollOrientation()) {
210 return;
211 }
212
213 const int numDegrees = event->delta() / 8;
214 const int numSteps = numDegrees / 15;
215
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());
221 } else {
222 const int value = horizontalScrollBar()->value();
223 horizontalScrollBar()->setValue(value - numSteps * view->size().width());
224 }
225 m_smoothScrolling = previous;
226
227 event->accept();
228 }
229
230 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
231 {
232 Q_UNUSED(current);
233 Q_UNUSED(previous);
234 }
235
236 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
237 {
238 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
239 if (previous) {
240 scene->removeItem(previous);
241 disconnect(previous, SIGNAL(offsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
242 disconnect(previous, SIGNAL(maximumOffsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
243 disconnect(previous, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
244 m_smoothScrollingAnimation->setTargetObject(0);
245 }
246 if (current) {
247 scene->addItem(current);
248 connect(current, SIGNAL(offsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
249 connect(current, SIGNAL(maximumOffsetChanged(qreal,qreal)), this, SLOT(updateScrollBars()));
250 connect(current, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
251 m_smoothScrollingAnimation->setTargetObject(current);
252 }
253 }
254
255 void KItemListContainer::slotAnimationStateChanged(QAbstractAnimation::State newState,
256 QAbstractAnimation::State oldState)
257 {
258 Q_UNUSED(oldState);
259 if (newState == QAbstractAnimation::Stopped && m_smoothScrolling && !m_scrollBarPressed) {
260 m_smoothScrolling = false;
261 }
262 }
263
264
265 void KItemListContainer::scrollTo(qreal offset)
266 {
267 const KItemListView* view = m_controller->view();
268 if (!view) {
269 return;
270 }
271
272 m_smoothScrolling = true;
273 QScrollBar* scrollBar = (view->scrollOrientation() == Qt::Vertical)
274 ? verticalScrollBar() : horizontalScrollBar();
275 scrollBar->setValue(offset);
276 }
277
278 void KItemListContainer::updateScrollBars()
279 {
280 const KItemListView* view = m_controller->view();
281 if (!view) {
282 return;
283 }
284
285 QScrollBar* scrollBar = 0;
286 int singleStep = 0;
287 int pageStep = 0;
288 QScrollBar* otherScrollBar = 0;
289 if (view->scrollOrientation() == Qt::Vertical) {
290 scrollBar = verticalScrollBar();
291 singleStep = view->itemSize().height();
292 pageStep = view->size().height();
293 otherScrollBar = horizontalScrollBar();
294 } else {
295 scrollBar = horizontalScrollBar();
296 singleStep = view->itemSize().width();
297 pageStep = view->size().width();
298 otherScrollBar = verticalScrollBar();
299 }
300
301 const int value = view->offset();
302 const int maximum = qMax(0, int(view->maximumOffset() - pageStep));
303 if (m_smoothScrollingAnimation->state() == QAbstractAnimation::Running) {
304 if (maximum == scrollBar->maximum()) {
305 // The value has been changed by the animation, no update
306 // of the scrollbars is required as their target state will be
307 // reached with the end of the animation.
308 return;
309 }
310
311 // The maximum has been changed which indicates that the content
312 // of the view has been changed. Stop the animation in any case and
313 // update the scrollbars immediately.
314 m_smoothScrollingAnimation->stop();
315 }
316
317 scrollBar->setSingleStep(singleStep);
318 scrollBar->setPageStep(pageStep);
319 scrollBar->setMinimum(0);
320 scrollBar->setMaximum(maximum);
321 scrollBar->setValue(value);
322
323 // Make sure that the other scroll bar is hidden
324 otherScrollBar->setMaximum(0);
325 otherScrollBar->setValue(0);
326 }
327
328 void KItemListContainer::updateGeometries()
329 {
330 QRect rect = geometry();
331
332 const int widthDec = verticalScrollBar()->isVisible()
333 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent)
334 : frameWidth() * 2;
335
336 const int heightDec = horizontalScrollBar()->isVisible()
337 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent)
338 : frameWidth() * 2;
339
340 rect.adjust(0, 0, -widthDec, -heightDec);
341
342 m_controller->view()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
343
344 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
345 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
346
347 updateScrollBars();
348 }
349
350 void KItemListContainer::initialize()
351 {
352 if (!m_controller) {
353 m_controller = new KItemListController(this);
354 }
355
356 connect(m_controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
357 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
358 connect(m_controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
359 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
360
361 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
362 setViewport(graphicsView);
363
364 m_smoothScrollingAnimation = new QPropertyAnimation(this, "offset");
365 m_smoothScrollingAnimation->setDuration(300);
366 connect(m_smoothScrollingAnimation, SIGNAL(stateChanged(QAbstractAnimation::State,QAbstractAnimation::State)),
367 this, SLOT(slotAnimationStateChanged(QAbstractAnimation::State,QAbstractAnimation::State)));
368
369 horizontalScrollBar()->installEventFilter(this);
370 verticalScrollBar()->installEventFilter(this);
371 }
372
373 #include "kitemlistcontainer.moc"