1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #include "kitemlistcontainer.h"
25 #include "kitemlistcontroller.h"
26 #include "kitemlistselectionmanager.h"
27 #include "kitemlistview.h"
28 #include "kitemmodelbase.h"
30 #include "private/kitemlistsmoothscroller.h"
32 #include <QApplication>
33 #include <QGraphicsScene>
34 #include <QGraphicsView>
35 #include <QPropertyAnimation>
38 #include <QStyleOption>
42 #include "kitemlistviewaccessible.h"
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.
49 class KItemListContainerViewport
: public QGraphicsView
52 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
54 virtual void wheelEvent(QWheelEvent
* event
);
57 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
58 QGraphicsView(scene
, parent
)
60 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
61 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
62 setViewportMargins(0, 0, 0, 0);
63 setFrameShape(QFrame::NoFrame
);
66 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
68 // Assure that the wheel-event gets forwarded to the parent
69 // and not handled at all by QGraphicsView.
73 QAccessibleInterface
* accessibleContainerFactory(const QString
&key
, QObject
*object
)
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
);
83 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
84 QAbstractScrollArea(parent
),
85 m_controller(controller
),
86 m_horizontalSmoothScroller(0),
87 m_verticalSmoothScroller(0)
90 controller
->setParent(this);
92 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
93 setViewport(graphicsView
);
95 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
96 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
98 if (controller
->model()) {
99 slotModelChanged(controller
->model(), 0);
101 if (controller
->view()) {
102 slotViewChanged(controller
->view(), 0);
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
*)));
110 QAccessible::installFactory(accessibleContainerFactory
);
113 KItemListContainer::~KItemListContainer()
115 // Don't rely on the QObject-order to delete the controller, otherwise
116 // the QGraphicsScene might get deleted before the view.
120 QAccessible::removeFactory(accessibleContainerFactory
);
123 KItemListController
* KItemListContainer::controller() const
128 void KItemListContainer::setEnabledFrame(bool enable
)
130 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
132 setFrameShape(QFrame::StyledPanel
);
133 graphicsView
->setPalette(palette());
134 graphicsView
->viewport()->setAutoFillBackground(true);
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
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);
149 bool KItemListContainer::enabledFrame() const
151 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
152 return graphicsView
->autoFillBackground();
155 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
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
163 KItemListView
* view
= m_controller
->view();
165 QApplication::sendEvent(view
, event
);
167 QAccessible::updateAccessibility(view
, m_controller
->selectionManager()->currentItem()+1, QAccessible::Focus
);
168 QAccessible::updateAccessibility(view
, m_controller
->selectionManager()->currentItem()+1, QAccessible::LocationChanged
);
171 void KItemListContainer::showEvent(QShowEvent
* event
)
173 QAbstractScrollArea::showEvent(event
);
177 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
179 QAbstractScrollArea::resizeEvent(event
);
183 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
185 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
186 m_verticalSmoothScroller
->scrollContentsBy(dy
);
187 QAccessible::updateAccessibility(m_controller
->view(), m_controller
->selectionManager()->currentItem()+1, QAccessible::Focus
);
188 QAccessible::updateAccessibility(m_controller
->view(), m_controller
->selectionManager()->currentItem()+1, QAccessible::LocationChanged
);
191 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
193 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
198 KItemListView
* view
= m_controller
->view();
204 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
205 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
206 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
207 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
209 const int numDegrees
= event
->delta() / 8;
210 const int numSteps
= numDegrees
/ 15;
212 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
213 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
218 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
221 updateSmoothScrollers(current
);
224 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
230 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
232 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
234 scene
->removeItem(previous
);
235 disconnect(previous
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
236 disconnect(previous
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
237 disconnect(previous
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
238 disconnect(previous
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
239 disconnect(previous
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
240 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
241 m_horizontalSmoothScroller
->setTargetObject(0);
242 m_verticalSmoothScroller
->setTargetObject(0);
245 scene
->addItem(current
);
246 connect(current
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
247 connect(current
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
248 connect(current
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
249 connect(current
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
250 connect(current
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
251 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
252 m_horizontalSmoothScroller
->setTargetObject(current
);
253 m_verticalSmoothScroller
->setTargetObject(current
);
254 updateSmoothScrollers(current
->scrollOrientation());
258 void KItemListContainer::scrollTo(qreal offset
)
260 const KItemListView
* view
= m_controller
->view();
262 if (view
->scrollOrientation() == Qt::Vertical
) {
263 m_verticalSmoothScroller
->scrollTo(offset
);
265 m_horizontalSmoothScroller
->scrollTo(offset
);
270 void KItemListContainer::updateScrollOffsetScrollBar()
272 const KItemListView
* view
= m_controller
->view();
277 KItemListSmoothScroller
* smoothScroller
= 0;
278 QScrollBar
* scrollOffsetScrollBar
= 0;
281 if (view
->scrollOrientation() == Qt::Vertical
) {
282 smoothScroller
= m_verticalSmoothScroller
;
283 scrollOffsetScrollBar
= verticalScrollBar();
284 singleStep
= view
->itemSize().height();
285 pageStep
= view
->size().height();
287 smoothScroller
= m_horizontalSmoothScroller
;
288 scrollOffsetScrollBar
= horizontalScrollBar();
289 singleStep
= view
->itemSize().width();
290 pageStep
= view
->size().width();
293 const int value
= view
->scrollOffset();
294 const int maximum
= qMax(0, int(view
->maximumScrollOffset() - pageStep
));
295 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
296 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
297 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
299 scrollOffsetScrollBar
->setSingleStep(singleStep
);
300 scrollOffsetScrollBar
->setPageStep(pageStep
);
301 scrollOffsetScrollBar
->setMinimum(0);
302 scrollOffsetScrollBar
->setMaximum(maximum
);
303 scrollOffsetScrollBar
->setValue(value
);
306 // Prevent a potential endless layout loop (see bug #293318).
307 updateScrollOffsetScrollBarPolicy();
312 void KItemListContainer::updateItemOffsetScrollBar()
314 const KItemListView
* view
= m_controller
->view();
319 KItemListSmoothScroller
* smoothScroller
= 0;
320 QScrollBar
* itemOffsetScrollBar
= 0;
323 if (view
->scrollOrientation() == Qt::Vertical
) {
324 smoothScroller
= m_horizontalSmoothScroller
;
325 itemOffsetScrollBar
= horizontalScrollBar();
326 singleStep
= view
->size().width() / 10;
327 pageStep
= view
->size().width();
329 smoothScroller
= m_verticalSmoothScroller
;
330 itemOffsetScrollBar
= verticalScrollBar();
331 singleStep
= view
->size().height() / 10;
332 pageStep
= view
->size().height();
335 const int value
= view
->itemOffset();
336 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
337 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
338 itemOffsetScrollBar
->setSingleStep(singleStep
);
339 itemOffsetScrollBar
->setPageStep(pageStep
);
340 itemOffsetScrollBar
->setMinimum(0);
341 itemOffsetScrollBar
->setMaximum(maximum
);
342 itemOffsetScrollBar
->setValue(value
);
346 void KItemListContainer::updateGeometries()
348 QRect rect
= geometry();
350 int extra
= frameWidth() * 2;
352 option
.initFrom(this);
353 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
354 extra
+= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
357 const int widthDec
= verticalScrollBar()->isVisible()
358 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
361 const int heightDec
= horizontalScrollBar()->isVisible()
362 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
365 rect
.adjust(0, 0, -widthDec
, -heightDec
);
367 const QRectF
newGeometry(0, 0, rect
.width(), rect
.height());
368 if (m_controller
->view()->geometry() != newGeometry
) {
369 m_controller
->view()->setGeometry(newGeometry
);
371 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
372 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
374 updateScrollOffsetScrollBar();
375 updateItemOffsetScrollBar();
376 QAccessible::updateAccessibility(m_controller
->view(), 0, QAccessible::LocationChanged
);
377 QAccessible::updateAccessibility(m_controller
->view(), m_controller
->selectionManager()->currentItem()+1, QAccessible::LocationChanged
);
378 QAccessible::updateAccessibility(m_controller
->view(), m_controller
->selectionManager()->currentItem()+1, QAccessible::Focus
);
382 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
384 if (orientation
== Qt::Vertical
) {
385 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
386 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
388 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
389 m_verticalSmoothScroller
->setPropertyName("itemOffset");
393 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
395 const KItemListView
* view
= m_controller
->view();
397 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
400 option
.initFrom(this);
401 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
403 QSizeF newViewSize
= m_controller
->view()->size();
405 newViewSize
.rwidth() += scrollBarInc
;
407 newViewSize
.rheight() += scrollBarInc
;
410 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
411 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
413 setVerticalScrollBarPolicy(policy
);
415 setHorizontalScrollBarPolicy(policy
);
419 #include "kitemlistcontainer.moc"