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 "kitemlistsmoothscroller_p.h"
27 #include "kitemlistview.h"
28 #include "kitemmodelbase.h"
30 #include <QApplication>
31 #include <QGraphicsScene>
32 #include <QGraphicsView>
33 #include <QPropertyAnimation>
40 * Replaces the default viewport of KItemListContainer by a
41 * non-scrollable viewport. The scrolling is done in an optimized
42 * way by KItemListView internally.
44 class KItemListContainerViewport
: public QGraphicsView
47 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
);
49 virtual void wheelEvent(QWheelEvent
* event
);
52 KItemListContainerViewport::KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
) :
53 QGraphicsView(scene
, parent
)
55 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
56 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
57 setViewportMargins(0, 0, 0, 0);
58 setFrameShape(QFrame::NoFrame
);
61 void KItemListContainerViewport::wheelEvent(QWheelEvent
* event
)
63 // Assure that the wheel-event gets forwarded to the parent
64 // and not handled at all by QGraphicsView.
70 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
71 QAbstractScrollArea(parent
),
72 m_controller(controller
),
73 m_horizontalSmoothScroller(0),
74 m_verticalSmoothScroller(0)
77 controller
->setParent(this);
81 KItemListContainer::KItemListContainer(QWidget
* parent
) :
82 QAbstractScrollArea(parent
),
84 m_horizontalSmoothScroller(0),
85 m_verticalSmoothScroller(0)
90 KItemListContainer::~KItemListContainer()
94 KItemListController
* KItemListContainer::controller() const
99 void KItemListContainer::keyPressEvent(QKeyEvent
* event
)
101 // TODO: We should find a better way to handle the key press events in the view.
102 // The reasons why we need this hack are:
103 // 1. Without reimplementing keyPressEvent() here, the event would not reach the QGraphicsView.
104 // 2. By default, the KItemListView does not have the keyboard focus in the QGraphicsScene, so
105 // simply sending the event to the QGraphicsView which is the KItemListContainer's viewport
107 KItemListView
* view
= m_controller
->view();
109 QApplication::sendEvent(view
, event
);
113 void KItemListContainer::showEvent(QShowEvent
* event
)
115 QAbstractScrollArea::showEvent(event
);
119 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
121 QAbstractScrollArea::resizeEvent(event
);
125 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
127 m_horizontalSmoothScroller
->scrollContentsBy(dx
);
128 m_verticalSmoothScroller
->scrollContentsBy(dy
);
131 void KItemListContainer::wheelEvent(QWheelEvent
* event
)
133 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
138 KItemListView
* view
= m_controller
->view();
144 const bool scrollHorizontally
= (event
->orientation() == Qt::Horizontal
) ||
145 (event
->orientation() == Qt::Vertical
&& !verticalScrollBar()->isVisible());
146 KItemListSmoothScroller
* smoothScroller
= scrollHorizontally
?
147 m_horizontalSmoothScroller
: m_verticalSmoothScroller
;
149 const int numDegrees
= event
->delta() / 8;
150 const int numSteps
= numDegrees
/ 15;
152 const QScrollBar
* scrollBar
= smoothScroller
->scrollBar();
153 smoothScroller
->scrollTo(scrollBar
->value() - numSteps
* scrollBar
->pageStep());
158 void KItemListContainer::slotScrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
161 updateSmoothScrollers(current
);
164 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
170 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
172 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
174 scene
->removeItem(previous
);
175 disconnect(current
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
176 disconnect(previous
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
177 disconnect(previous
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
178 disconnect(previous
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
179 disconnect(previous
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
180 disconnect(previous
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
181 m_horizontalSmoothScroller
->setTargetObject(0);
182 m_verticalSmoothScroller
->setTargetObject(0);
185 scene
->addItem(current
);
186 connect(current
, SIGNAL(scrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)), this, SLOT(slotScrollOrientationChanged(Qt::Orientation
,Qt::Orientation
)));
187 connect(current
, SIGNAL(scrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
188 connect(current
, SIGNAL(maximumScrollOffsetChanged(qreal
,qreal
)), this, SLOT(updateScrollOffsetScrollBar()));
189 connect(current
, SIGNAL(itemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
190 connect(current
, SIGNAL(maximumItemOffsetChanged(qreal
,qreal
)), this, SLOT(updateItemOffsetScrollBar()));
191 connect(current
, SIGNAL(scrollTo(qreal
)), this, SLOT(scrollTo(qreal
)));
192 m_horizontalSmoothScroller
->setTargetObject(current
);
193 m_verticalSmoothScroller
->setTargetObject(current
);
194 updateSmoothScrollers(current
->scrollOrientation());
198 void KItemListContainer::scrollTo(qreal offset
)
200 const KItemListView
* view
= m_controller
->view();
202 if (view
->scrollOrientation() == Qt::Vertical
) {
203 m_verticalSmoothScroller
->scrollTo(offset
);
205 m_horizontalSmoothScroller
->scrollTo(offset
);
210 void KItemListContainer::updateScrollOffsetScrollBar()
212 const KItemListView
* view
= m_controller
->view();
217 KItemListSmoothScroller
* smoothScroller
= 0;
218 QScrollBar
* scrollOffsetScrollBar
= 0;
221 if (view
->scrollOrientation() == Qt::Vertical
) {
222 smoothScroller
= m_verticalSmoothScroller
;
223 scrollOffsetScrollBar
= verticalScrollBar();
224 singleStep
= view
->itemSize().height();
225 pageStep
= view
->size().height();
227 smoothScroller
= m_horizontalSmoothScroller
;
228 scrollOffsetScrollBar
= horizontalScrollBar();
229 singleStep
= view
->itemSize().width();
230 pageStep
= view
->size().width();
233 const int value
= view
->scrollOffset();
234 const int maximum
= qMax(0, int(view
->maximumScrollOffset() - pageStep
));
235 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
236 scrollOffsetScrollBar
->setSingleStep(singleStep
);
237 scrollOffsetScrollBar
->setPageStep(pageStep
);
238 scrollOffsetScrollBar
->setMinimum(0);
239 scrollOffsetScrollBar
->setMaximum(maximum
);
240 scrollOffsetScrollBar
->setValue(value
);
244 void KItemListContainer::updateItemOffsetScrollBar()
246 const KItemListView
* view
= m_controller
->view();
251 KItemListSmoothScroller
* smoothScroller
= 0;
252 QScrollBar
* itemOffsetScrollBar
= 0;
255 if (view
->scrollOrientation() == Qt::Vertical
) {
256 smoothScroller
= m_horizontalSmoothScroller
;
257 itemOffsetScrollBar
= horizontalScrollBar();
258 singleStep
= view
->size().width() / 10;
259 pageStep
= view
->size().width();
261 smoothScroller
= m_verticalSmoothScroller
;
262 itemOffsetScrollBar
= verticalScrollBar();
263 singleStep
= view
->size().height() / 10;
264 pageStep
= view
->size().height();
267 const int value
= view
->itemOffset();
268 const int maximum
= qMax(0, int(view
->maximumItemOffset()) - pageStep
);
269 if (smoothScroller
->requestScrollBarUpdate(maximum
)) {
270 itemOffsetScrollBar
->setSingleStep(singleStep
);
271 itemOffsetScrollBar
->setPageStep(pageStep
);
272 itemOffsetScrollBar
->setMinimum(0);
273 itemOffsetScrollBar
->setMaximum(maximum
);
274 itemOffsetScrollBar
->setValue(value
);
278 void KItemListContainer::updateGeometries()
280 QRect rect
= geometry();
282 const int widthDec
= verticalScrollBar()->isVisible()
283 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent
)
286 const int heightDec
= horizontalScrollBar()->isVisible()
287 ? frameWidth() + style()->pixelMetric(QStyle::PM_ScrollBarExtent
)
290 rect
.adjust(0, 0, -widthDec
, -heightDec
);
292 const QRectF
newGeometry(0, 0, rect
.width(), rect
.height());
293 if (m_controller
->view()->geometry() != newGeometry
) {
294 m_controller
->view()->setGeometry(newGeometry
);
296 static_cast<KItemListContainerViewport
*>(viewport())->scene()->setSceneRect(0, 0, rect
.width(), rect
.height());
297 static_cast<KItemListContainerViewport
*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
299 updateScrollOffsetScrollBar();
300 updateItemOffsetScrollBar();
304 void KItemListContainer::updateSmoothScrollers(Qt::Orientation orientation
)
306 if (orientation
== Qt::Vertical
) {
307 m_verticalSmoothScroller
->setPropertyName("scrollOffset");
308 m_horizontalSmoothScroller
->setPropertyName("itemOffset");
310 m_horizontalSmoothScroller
->setPropertyName("scrollOffset");
311 m_verticalSmoothScroller
->setPropertyName("itemOffset");
315 void KItemListContainer::initialize()
318 m_controller
= new KItemListController(this);
321 connect(m_controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)),
322 this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
323 connect(m_controller
, SIGNAL(viewChanged(KItemListView
*,KItemListView
*)),
324 this, SLOT(slotViewChanged(KItemListView
*,KItemListView
*)));
326 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
327 setViewport(graphicsView
);
329 m_horizontalSmoothScroller
= new KItemListSmoothScroller(horizontalScrollBar(), this);
330 m_verticalSmoothScroller
= new KItemListSmoothScroller(verticalScrollBar(), this);
333 #include "kitemlistcontainer.moc"