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