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
);
169 void KItemListContainer::showEvent(QShowEvent
* event
)
171 QAbstractScrollArea::showEvent(event
);
175 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
177 QAbstractScrollArea::resizeEvent(event
);
181 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
183 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
184 m_verticalSmoothScroller
->scrollContentsBy(dy
);
185 //QAccessible::updateAccessibility(view(), , );
188 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
190 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
195 KItemListView
* view
= m_controller
->view();
201 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
202 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
203 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
204 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
206 const int numDegrees
= event
->delta() / 8;
207 const int numSteps
= numDegrees
/ 15;
209 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
210 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
215 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
218 updateSmoothScrollers(current
);
221 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
227 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
229 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
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);
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());
255 void KItemListContainer::scrollTo(qreal offset
)
257 const KItemListView
* view
= m_controller
->view();
259 if (view
->scrollOrientation() == Qt::Vertical
) {
260 m_verticalSmoothScroller
->scrollTo(offset
);
262 m_horizontalSmoothScroller
->scrollTo(offset
);
267 void KItemListContainer::updateScrollOffsetScrollBar()
269 const KItemListView
* view
= m_controller
->view();
274 KItemListSmoothScroller
* smoothScroller
= 0;
275 QScrollBar
* scrollOffsetScrollBar
= 0;
278 if (view
->scrollOrientation() == Qt::Vertical
) {
279 smoothScroller
= m_verticalSmoothScroller
;
280 scrollOffsetScrollBar
= verticalScrollBar();
281 singleStep
= view
->itemSize().height();
282 pageStep
= view
->size().height();
284 smoothScroller
= m_horizontalSmoothScroller
;
285 scrollOffsetScrollBar
= horizontalScrollBar();
286 singleStep
= view
->itemSize().width();
287 pageStep
= view
->size().width();
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
;
296 scrollOffsetScrollBar
->setSingleStep(singleStep
);
297 scrollOffsetScrollBar
->setPageStep(pageStep
);
298 scrollOffsetScrollBar
->setMinimum(0);
299 scrollOffsetScrollBar
->setMaximum(maximum
);
300 scrollOffsetScrollBar
->setValue(value
);
303 // Prevent a potential endless layout loop (see bug #293318).
304 updateScrollOffsetScrollBarPolicy();
309 void KItemListContainer::updateItemOffsetScrollBar()
311 const KItemListView
* view
= m_controller
->view();
316 KItemListSmoothScroller
* smoothScroller
= 0;
317 QScrollBar
* itemOffsetScrollBar
= 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();
326 smoothScroller
= m_verticalSmoothScroller
;
327 itemOffsetScrollBar
= verticalScrollBar();
328 singleStep
= view
->size().height() / 10;
329 pageStep
= view
->size().height();
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
);
343 void KItemListContainer::updateGeometries()
345 QRect rect
= geometry();
347 int extra
= frameWidth() * 2;
349 option
.initFrom(this);
350 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
351 extra
+= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
354 const int widthDec
= verticalScrollBar()->isVisible()
355 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
358 const int heightDec
= horizontalScrollBar()->isVisible()
359 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
362 rect
.adjust(0, 0, -widthDec
, -heightDec
);
364 const QRectF
newGeometry(0, 0, rect
.width(), rect
.height());
365 if (m_controller
->view()->geometry() != newGeometry
) {
366 m_controller
->view()->setGeometry(newGeometry
);
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()));
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
);
378 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
380 if (orientation
== Qt::Vertical
) {
381 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
382 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
384 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
385 m_verticalSmoothScroller
->setPropertyName("itemOffset");
389 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
391 const KItemListView
* view
= m_controller
->view();
393 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
396 option
.initFrom(this);
397 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
399 QSizeF newViewSize
= m_controller
->view()->size();
401 newViewSize
.rwidth() += scrollBarInc
;
403 newViewSize
.rheight() += scrollBarInc
;
406 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
407 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
409 setVerticalScrollBarPolicy(policy
);
411 setHorizontalScrollBarPolicy(policy
);
415 #include "kitemlistcontainer.moc"