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