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 "kitemlistview.h"
27 #include "kitemmodelbase.h"
29 #include "private/kitemlistsmoothscroller.h"
31 #include <QApplication>
32 #include <QGraphicsScene>
33 #include <QGraphicsView>
34 #include <QPropertyAnimation>
37 #include <QStyleOption>
41 #include "kitemlistviewaccessible.h"
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.
48 class KItemListContainerViewport
: public QGraphicsView
51 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
53 virtual void wheelEvent(QWheelEvent
* event
);
56 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
57 QGraphicsView(scene
, parent
)
59 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
60 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
61 setViewportMargins(0, 0, 0, 0);
62 setFrameShape(QFrame::NoFrame
);
65 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
67 // Assure that the wheel-event gets forwarded to the parent
68 // and not handled at all by QGraphicsView.
72 QAccessibleInterface
* accessibleContainerFactory(const QString
&key
, QObject
*object
)
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
);
82 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
83 QAbstractScrollArea(parent
),
84 m_controller(controller
),
85 m_horizontalSmoothScroller(0),
86 m_verticalSmoothScroller(0)
89 controller
->setParent(this);
91 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
92 setViewport(graphicsView
);
94 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
95 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
97 if (controller
->model()) {
98 slotModelChanged(controller
->model(), 0);
100 if (controller
->view()) {
101 slotViewChanged(controller
->view(), 0);
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
*)));
109 QAccessible::installFactory(accessibleContainerFactory
);
112 KItemListContainer::~KItemListContainer()
114 // Don't rely on the QObject-order to delete the controller, otherwise
115 // the QGraphicsScene might get deleted before the view.
119 QAccessible::removeFactory(accessibleContainerFactory
);
122 KItemListController
* KItemListContainer::controller() const
127 void KItemListContainer::setEnabledFrame(bool enable
)
129 QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
131 setFrameShape(QFrame::StyledPanel
);
132 graphicsView
->setPalette(palette());
133 graphicsView
->viewport()->setAutoFillBackground(true);
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
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);
148 bool KItemListContainer::enabledFrame() const
150 const QGraphicsView
* graphicsView
= qobject_cast
<QGraphicsView
*>(viewport());
151 return graphicsView
->autoFillBackground();
154 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
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
162 KItemListView
* view
= m_controller
->view();
164 QApplication::sendEvent(view
, event
);
166 //QAccessible::updateAccessibility(view, 0, QAccessible::Focus);
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
);
187 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
189 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
194 KItemListView
* view
= m_controller
->view();
200 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
201 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
202 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
203 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
205 const int numDegrees
= event
->delta() / 8;
206 const int numSteps
= numDegrees
/ 15;
208 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
209 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep() / 4);
214 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
217 updateSmoothScrollers(current
);
220 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
226 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
228 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
230 scene
->removeItem(previous
);
231 disconnect(previous
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
232 disconnect(previous
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
233 disconnect(previous
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
234 disconnect(previous
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
235 disconnect(previous
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
236 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
237 m_horizontalSmoothScroller
->setTargetObject(0);
238 m_verticalSmoothScroller
->setTargetObject(0);
241 scene
->addItem(current
);
242 connect(current
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
243 connect(current
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
244 connect(current
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
245 connect(current
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
246 connect(current
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
247 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
248 m_horizontalSmoothScroller
->setTargetObject(current
);
249 m_verticalSmoothScroller
->setTargetObject(current
);
250 updateSmoothScrollers(current
->scrollOrientation());
254 void KItemListContainer::scrollTo(qreal offset
)
256 const KItemListView
* view
= m_controller
->view();
258 if (view
->scrollOrientation() == Qt::Vertical
) {
259 m_verticalSmoothScroller
->scrollTo(offset
);
261 m_horizontalSmoothScroller
->scrollTo(offset
);
266 void KItemListContainer::updateScrollOffsetScrollBar()
268 const KItemListView
* view
= m_controller
->view();
273 KItemListSmoothScroller
* smoothScroller
= 0;
274 QScrollBar
* scrollOffsetScrollBar
= 0;
277 if (view
->scrollOrientation() == Qt::Vertical
) {
278 smoothScroller
= m_verticalSmoothScroller
;
279 scrollOffsetScrollBar
= verticalScrollBar();
280 singleStep
= view
->itemSize().height();
281 pageStep
= view
->size().height();
283 smoothScroller
= m_horizontalSmoothScroller
;
284 scrollOffsetScrollBar
= horizontalScrollBar();
285 singleStep
= view
->itemSize().width();
286 pageStep
= view
->size().width();
289 const int value
= view
->scrollOffset();
290 const int maximum
= qMax(0, int(view
->maximumScrollOffset() - pageStep
));
291 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
292 const bool updatePolicy
= (scrollOffsetScrollBar
->maximum() > 0 && maximum
== 0)
293 || horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOn
;
295 scrollOffsetScrollBar
->setSingleStep(singleStep
);
296 scrollOffsetScrollBar
->setPageStep(pageStep
);
297 scrollOffsetScrollBar
->setMinimum(0);
298 scrollOffsetScrollBar
->setMaximum(maximum
);
299 scrollOffsetScrollBar
->setValue(value
);
302 // Prevent a potential endless layout loop (see bug #293318).
303 updateScrollOffsetScrollBarPolicy();
308 void KItemListContainer::updateItemOffsetScrollBar()
310 const KItemListView
* view
= m_controller
->view();
315 KItemListSmoothScroller
* smoothScroller
= 0;
316 QScrollBar
* itemOffsetScrollBar
= 0;
319 if (view
->scrollOrientation() == Qt::Vertical
) {
320 smoothScroller
= m_horizontalSmoothScroller
;
321 itemOffsetScrollBar
= horizontalScrollBar();
322 singleStep
= view
->size().width() / 10;
323 pageStep
= view
->size().width();
325 smoothScroller
= m_verticalSmoothScroller
;
326 itemOffsetScrollBar
= verticalScrollBar();
327 singleStep
= view
->size().height() / 10;
328 pageStep
= view
->size().height();
331 const int value
= view
->itemOffset();
332 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
333 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
334 itemOffsetScrollBar
->setSingleStep(singleStep
);
335 itemOffsetScrollBar
->setPageStep(pageStep
);
336 itemOffsetScrollBar
->setMinimum(0);
337 itemOffsetScrollBar
->setMaximum(maximum
);
338 itemOffsetScrollBar
->setValue(value
);
342 void KItemListContainer::updateGeometries()
344 QRect rect
= geometry();
346 int extra
= frameWidth() * 2;
348 option
.initFrom(this);
349 if (style()->styleHint(QStyle::SH_ScrollView_FrameOnlyAroundContents
, &option
, this)) {
350 extra
+= style()->pixelMetric(QStyle::PM_ScrollView_ScrollBarSpacing
, &option
, this);
353 const int widthDec
= verticalScrollBar()->isVisible()
354 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
357 const int heightDec
= horizontalScrollBar()->isVisible()
358 ? extra
+ style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this)
361 rect
.adjust(0, 0, -widthDec
, -heightDec
);
363 const QRectF
newGeometry(0, 0, rect
.width(), rect
.height());
364 if (m_controller
->view()->geometry() != newGeometry
) {
365 m_controller
->view()->setGeometry(newGeometry
);
367 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
368 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
370 updateScrollOffsetScrollBar();
371 updateItemOffsetScrollBar();
375 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
377 if (orientation
== Qt::Vertical
) {
378 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
379 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
381 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
382 m_verticalSmoothScroller
->setPropertyName("itemOffset");
386 void KItemListContainer::updateScrollOffsetScrollBarPolicy()
388 const KItemListView
* view
= m_controller
->view();
390 const bool vertical
= (view
->scrollOrientation() == Qt::Vertical
);
393 option
.initFrom(this);
394 const int scrollBarInc
= style()->pixelMetric(QStyle::PM_ScrollBarExtent
, &option
, this);
396 QSizeF newViewSize
= m_controller
->view()->size();
398 newViewSize
.rwidth() += scrollBarInc
;
400 newViewSize
.rheight() += scrollBarInc
;
403 const Qt::ScrollBarPolicy policy
= view
->scrollBarRequired(newViewSize
)
404 ? Qt::ScrollBarAlwaysOn
: Qt::ScrollBarAsNeeded
;
406 setVerticalScrollBarPolicy(policy
);
408 setHorizontalScrollBarPolicy(policy
);
412 #include "kitemlistcontainer.moc"