]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Instantiate KItemListContainerAccessible when needed.
[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 "private/kitemlistsmoothscroller.h"
30
31 #include <QApplication>
32 #include <QGraphicsScene>
33 #include <QGraphicsView>
34 #include <QPropertyAnimation>
35 #include <QScrollBar>
36 #include <QStyle>
37 #include <QStyleOption>
38
39 #include <KDebug>
40
41 #include "kitemlistviewaccessible.h"
42
43 /**
44 * Replaces the default viewport of KItemListContainer by a
45 * non-scrollable viewport. The scrolling is done in an optimized
46 * way by KItemListView internally.
47 */
48 class KItemListContainerViewport : public QGraphicsView
49 {
50 public:
51 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent);
52 protected:
53 virtual void wheelEvent(QWheelEvent* event);
54 };
55
56 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent) :
57 QGraphicsView(scene, parent)
58 {
59 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
61 setViewportMargins(0, 0, 0, 0);
62 setFrameShape(QFrame::NoFrame);
63 }
64
65 void KItemListContainerViewport::wheelEvent(QWheelEvent* event)
66 {
67 // Assure that the wheel-event gets forwarded to the parent
68 // and not handled at all by QGraphicsView.
69 event->ignore();
70 }
71
72 QAccessibleInterface* accessibleContainerFactory(const QString &key, QObject *object)
73 {
74 Q_UNUSED(key)
75 if (KItemListContainer*view = qobject_cast<KItemListContainer*>(object))
76 return new KItemListContainerAccessible(view);
77 if (KItemListView *view = qobject_cast<KItemListView*>(object))
78 return new KItemListViewAccessible(view);
79 return 0;
80 }
81
82 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
83 QAbstractScrollArea(parent),
84 m_controller(controller),
85 m_horizontalSmoothScroller(0),
86 m_verticalSmoothScroller(0)
87 {
88 Q_ASSERT(controller);
89 controller->setParent(this);
90
91 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
92 setViewport(graphicsView);
93
94 m_horizontalSmoothScroller = new KItemListSmoothScroller(horizontalScrollBar(), this);
95 m_verticalSmoothScroller = new KItemListSmoothScroller(verticalScrollBar(), this);
96
97 if (controller->model()) {
98 slotModelChanged(controller->model(), 0);
99 }
100 if (controller->view()) {
101 slotViewChanged(controller->view(), 0);
102 }
103
104 connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
105 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
106 connect(controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
107 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
108
109 QAccessible::installFactory(accessibleContainerFactory);
110 }
111
112 KItemListContainer::~KItemListContainer()
113 {
114 // Don't rely on the QObject-order to delete the controller, otherwise
115 // the QGraphicsScene might get deleted before the view.
116 delete m_controller;
117 m_controller = 0;
118
119 QAccessible::removeFactory(accessibleContainerFactory);
120 }
121
122 KItemListController* KItemListContainer::controller() const
123 {
124 return m_controller;
125 }
126
127 void KItemListContainer::setEnabledFrame(bool enable)
128 {
129 QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(viewport());
130 if (enable) {
131 setFrameShape(QFrame::StyledPanel);
132 graphicsView->setPalette(palette());
133 graphicsView->viewport()->setAutoFillBackground(true);
134 } else {
135 setFrameShape(QFrame::NoFrame);
136 // Make the background of the container transparent and apply the window-text color
137 // to the text color, so that enough contrast is given for all color
138 // schemes
139 QPalette p = graphicsView->palette();
140 p.setColor(QPalette::Active, QPalette::Text, p.color(QPalette::Active, QPalette::WindowText));
141 p.setColor(QPalette::Inactive, QPalette::Text, p.color(QPalette::Inactive, QPalette::WindowText));
142 p.setColor(QPalette::Disabled, QPalette::Text, p.color(QPalette::Disabled, QPalette::WindowText));
143 graphicsView->setPalette(p);
144 graphicsView->viewport()->setAutoFillBackground(false);
145 }
146 }
147
148 bool KItemListContainer::enabledFrame() const
149 {
150 const QGraphicsView* graphicsView = qobject_cast<QGraphicsView*>(viewport());
151 return graphicsView->autoFillBackground();
152 }
153
154 void KItemListContainer::keyPressEvent(QKeyEvent* event)
155 {
156 // TODO: We should find a better way to handle the key press events in the view.
157 // The reasons why we need this hack are:
158 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
159 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
160 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
161 // does not work.
162 KItemListView* view = m_controller->view();
163 if (view) {
164 QApplication::sendEvent(view, event);
165 }
166 }
167
168 void KItemListContainer::showEvent(QShowEvent* event)
169 {
170 QAbstractScrollArea::showEvent(event);
171 updateGeometries();
172 }
173
174 void KItemListContainer::resizeEvent(QResizeEvent* event)
175 {
176 QAbstractScrollArea::resizeEvent(event);
177 updateGeometries();
178 }
179
180 void KItemListContainer::scrollContentsBy(int dx, int dy)
181 {
182 m_horizontalSmoothScroller->scrollContentsBy(dx);
183 m_verticalSmoothScroller->scrollContentsBy(dy);
184 }
185
186 void KItemListContainer::wheelEvent(QWheelEvent* event)
187 {
188 if (event->modifiers().testFlag(Qt::ControlModifier)) {
189 event->ignore();
190 return;
191 }
192
193 KItemListView* view = m_controller->view();
194 if (!view) {
195 event->ignore();
196 return;
197 }
198
199 const bool scrollHorizontally = (event->orientation() == Qt::Horizontal) ||
200 (event->orientation() == Qt::Vertical && !verticalScrollBar()->isVisible());
201 KItemListSmoothScroller* smoothScroller = scrollHorizontally ?
202 m_horizontalSmoothScroller : m_verticalSmoothScroller;
203
204 const int numDegrees = event->delta() / 8;
205 const int numSteps = numDegrees / 15;
206
207 const QScrollBar* scrollBar = smoothScroller->scrollBar();
208 smoothScroller->scrollTo(scrollBar->value() - numSteps * scrollBar->pageStep() / 4);
209
210 event->accept();
211 }
212
213 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
214 {
215 Q_UNUSED(previous);
216 updateSmoothScrollers(current);
217 }
218
219 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
220 {
221 Q_UNUSED(current);
222 Q_UNUSED(previous);
223 }
224
225 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
226 {
227 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
228 if (previous) {
229 scene->removeItem(previous);
230 disconnect(previous, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
231 disconnect(previous, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
232 disconnect(previous, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
233 disconnect(previous, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
234 disconnect(previous, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
235 disconnect(previous, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
236 m_horizontalSmoothScroller->setTargetObject(0);
237 m_verticalSmoothScroller->setTargetObject(0);
238 }
239 if (current) {
240 scene->addItem(current);
241 connect(current, SIGNAL(scrollOrientationChanged(Qt::Orientation,Qt::Orientation)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation,Qt::Orientation)));
242 connect(current, SIGNAL(scrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
243 connect(current, SIGNAL(maximumScrollOffsetChanged(qreal,qreal)), this, SLOT(updateScrollOffsetScrollBar()));
244 connect(current, SIGNAL(itemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
245 connect(current, SIGNAL(maximumItemOffsetChanged(qreal,qreal)), this, SLOT(updateItemOffsetScrollBar()));
246 connect(current, SIGNAL(scrollTo(qreal)), this, SLOT(scrollTo(qreal)));
247 m_horizontalSmoothScroller->setTargetObject(current);
248 m_verticalSmoothScroller->setTargetObject(current);
249 updateSmoothScrollers(current->scrollOrientation());
250 }
251 }
252
253 void KItemListContainer::scrollTo(qreal offset)
254 {
255 const KItemListView* view = m_controller->view();
256 if (view) {
257 if (view->scrollOrientation() == Qt::Vertical) {
258 m_verticalSmoothScroller->scrollTo(offset);
259 } else {
260 m_horizontalSmoothScroller->scrollTo(offset);
261 }
262 }
263 }
264
265 void KItemListContainer::updateScrollOffsetScrollBar()
266 {
267 const KItemListView* view = m_controller->view();
268 if (!view) {
269 return;
270 }
271
272 KItemListSmoothScroller* smoothScroller = 0;
273 QScrollBar* scrollOffsetScrollBar = 0;
274 int singleStep = 0;
275 int pageStep = 0;
276 if (view->scrollOrientation() == Qt::Vertical) {
277 smoothScroller = m_verticalSmoothScroller;
278 scrollOffsetScrollBar = verticalScrollBar();
279 singleStep = view->itemSize().height();
280 pageStep = view->size().height();
281 } else {
282 smoothScroller = m_horizontalSmoothScroller;
283 scrollOffsetScrollBar = horizontalScrollBar();
284 singleStep = view->itemSize().width();
285 pageStep = view->size().width();
286 }
287
288 const int value = view->scrollOffset();
289 const int maximum = qMax(0, int(view->maximumScrollOffset() - pageStep));
290 if (smoothScroller->requestScrollBarUpdate(maximum)) {
291 const bool updatePolicy = (scrollOffsetScrollBar->maximum() > 0 && maximum == 0)
292 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn;
293
294 scrollOffsetScrollBar->setSingleStep(singleStep);
295 scrollOffsetScrollBar->setPageStep(pageStep);
296 scrollOffsetScrollBar->setMinimum(0);
297 scrollOffsetScrollBar->setMaximum(maximum);
298 scrollOffsetScrollBar->setValue(value);
299
300 if (updatePolicy) {
301 // Prevent a potential endless layout loop (see bug #293318).
302 updateScrollOffsetScrollBarPolicy();
303 }
304 }
305 }
306
307 void KItemListContainer::updateItemOffsetScrollBar()
308 {
309 const KItemListView* view = m_controller->view();
310 if (!view) {
311 return;
312 }
313
314 KItemListSmoothScroller* smoothScroller = 0;
315 QScrollBar* itemOffsetScrollBar = 0;
316 int singleStep = 0;
317 int pageStep = 0;
318 if (view->scrollOrientation() == Qt::Vertical) {
319 smoothScroller = m_horizontalSmoothScroller;
320 itemOffsetScrollBar = horizontalScrollBar();
321 singleStep = view->size().width() / 10;
322 pageStep = view->size().width();
323 } else {
324 smoothScroller = m_verticalSmoothScroller;
325 itemOffsetScrollBar = verticalScrollBar();
326 singleStep = view->size().height() / 10;
327 pageStep = view->size().height();
328 }
329
330 const int value = view->itemOffset();
331 const int maximum = qMax(0, int(view->maximumItemOffset()) - pageStep);
332 if (smoothScroller->requestScrollBarUpdate(maximum)) {
333 itemOffsetScrollBar->setSingleStep(singleStep);
334 itemOffsetScrollBar->setPageStep(pageStep);
335 itemOffsetScrollBar->setMinimum(0);
336 itemOffsetScrollBar->setMaximum(maximum);
337 itemOffsetScrollBar->setValue(value);
338 }
339 }
340
341 void KItemListContainer::updateGeometries()
342 {
343 QRect rect = geometry();
344
345 int extra = frameWidth() * 2;
346 QStyleOption option;
347 option.initFrom(this);
348 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents, &option, this)) {
349 extra += style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing, &option, this);
350 }
351
352 const int widthDec = verticalScrollBar()->isVisible()
353 ? extra + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
354 : extra;
355
356 const int heightDec = horizontalScrollBar()->isVisible()
357 ? extra + style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this)
358 : extra;
359
360 rect.adjust(0, 0, -widthDec, -heightDec);
361
362 const QRectF newGeometry(0, 0, rect.width(), rect.height());
363 if (m_controller->view()->geometry() != newGeometry) {
364 m_controller->view()->setGeometry(newGeometry);
365
366 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
367 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
368
369 updateScrollOffsetScrollBar();
370 updateItemOffsetScrollBar();
371 }
372 }
373
374 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation)
375 {
376 if (orientation == Qt::Vertical) {
377 m_verticalSmoothScroller->setPropertyName("scrollOffset");
378 m_horizontalSmoothScroller->setPropertyName("itemOffset");
379 } else {
380 m_horizontalSmoothScroller->setPropertyName("scrollOffset");
381 m_verticalSmoothScroller->setPropertyName("itemOffset");
382 }
383 }
384
385 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
386 {
387 const KItemListView* view = m_controller->view();
388 Q_ASSERT(view);
389 const bool vertical = (view->scrollOrientation() == Qt::Vertical);
390
391 QStyleOption option;
392 option.initFrom(this);
393 const int scrollBarInc = style()->pixelMetric(QStyle::PM_ScrollBarExtent, &option, this);
394
395 QSizeF newViewSize = m_controller->view()->size();
396 if (vertical) {
397 newViewSize.rwidth() += scrollBarInc;
398 } else {
399 newViewSize.rheight() += scrollBarInc;
400 }
401
402 const Qt::ScrollBarPolicy policy = view->scrollBarRequired(newViewSize)
403 ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAsNeeded;
404 if (vertical) {
405 setVerticalScrollBarPolicy(policy);
406 } else {
407 setHorizontalScrollBarPolicy(policy);
408 }
409 }
410
411 #include "kitemlistcontainer.moc"