]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistcontainer.cpp
Merged very early alpha-version of Dolphin 2.0
[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 "kitemlistview.h"
27 #include "kitemmodelbase.h"
28
29 #include <QGraphicsScene>
30 #include <QGraphicsView>
31 #include <QScrollBar>
32 #include <QStyle>
33
34 #include <KDebug>
35
36 class KItemListContainerViewport : public QGraphicsView
37 {
38 public:
39 KItemListContainerViewport(QGraphicsScene* scene, QWidget* parent)
40 : QGraphicsView(scene, parent)
41 {
42 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
43 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 setViewportMargins(0, 0, 0, 0);
45 setFrameShape(QFrame::NoFrame);
46 }
47
48 void scrollContentsBy(int dx, int dy)
49 {
50 Q_UNUSED(dx);
51 Q_UNUSED(dy);
52 // Do nothing. This prevents that e.g. the wheel-event
53 // results in a moving of the scene items.
54 }
55 };
56
57 KItemListContainer::KItemListContainer(KItemListController* controller, QWidget* parent) :
58 QAbstractScrollArea(parent),
59 m_controller(controller)
60 {
61 Q_ASSERT(controller);
62 controller->setParent(this);
63 initialize();
64 }
65
66 KItemListContainer::KItemListContainer(QWidget* parent) :
67 QAbstractScrollArea(parent),
68 m_controller(0)
69 {
70 initialize();
71 }
72
73 KItemListContainer::~KItemListContainer()
74 {
75 }
76
77 KItemListController* KItemListContainer::controller() const
78 {
79 return m_controller;
80 }
81
82 void KItemListContainer::showEvent(QShowEvent* event)
83 {
84 QAbstractScrollArea::showEvent(event);
85 updateGeometries();
86 }
87
88 void KItemListContainer::resizeEvent(QResizeEvent* event)
89 {
90 QAbstractScrollArea::resizeEvent(event);
91 updateGeometries();
92 }
93
94 void KItemListContainer::scrollContentsBy(int dx, int dy)
95 {
96 KItemListView* view = m_controller->view();
97 if (!view) {
98 return;
99 }
100
101 const qreal currentOffset = view->offset();
102 const qreal offsetDiff = (view->scrollOrientation() == Qt::Vertical) ? dy : dx;
103 view->setOffset(currentOffset - offsetDiff);
104 }
105
106 void KItemListContainer::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
107 {
108 Q_UNUSED(current);
109 Q_UNUSED(previous);
110 }
111
112 void KItemListContainer::slotViewChanged(KItemListView* current, KItemListView* previous)
113 {
114 QGraphicsScene* scene = static_cast<QGraphicsView*>(viewport())->scene();
115 if (previous) {
116 scene->removeItem(previous);
117 disconnect(previous, SIGNAL(offsetChanged(int,int)), this, SLOT(updateScrollBars()));
118 disconnect(previous, SIGNAL(maximumOffsetChanged(int,int)), this, SLOT(updateScrollBars()));
119 }
120 if (current) {
121 scene->addItem(current);
122 connect(previous, SIGNAL(offsetChanged(int,int)), this, SLOT(updateScrollBars()));
123 connect(current, SIGNAL(maximumOffsetChanged(int,int)), this, SLOT(updateScrollBars()));
124 }
125 }
126
127 void KItemListContainer::updateScrollBars()
128 {
129 const QSizeF size = m_controller->view()->size();
130
131 if (m_controller->view()->scrollOrientation() == Qt::Vertical) {
132 QScrollBar* scrollBar = verticalScrollBar();
133 const int value = m_controller->view()->offset();
134 const int maximum = qMax(0, int(m_controller->view()->maximumOffset() - size.height()));
135 scrollBar->setPageStep(size.height());
136 scrollBar->setMinimum(0);
137 scrollBar->setMaximum(maximum);
138 scrollBar->setValue(value);
139 horizontalScrollBar()->setMaximum(0);
140 } else {
141 QScrollBar* scrollBar = horizontalScrollBar();
142 const int value = m_controller->view()->offset();
143 const int maximum = qMax(0, int(m_controller->view()->maximumOffset() - size.width()));
144 scrollBar->setPageStep(size.width());
145 scrollBar->setMinimum(0);
146 scrollBar->setMaximum(maximum);
147 scrollBar->setValue(value);
148 verticalScrollBar()->setMaximum(0);
149 }
150 }
151
152 void KItemListContainer::updateGeometries()
153 {
154 QRect rect = geometry();
155
156 int widthDec = frameWidth() * 2;
157 if (verticalScrollBar()->isVisible()) {
158 widthDec += style()->pixelMetric(QStyle::PM_ScrollBarExtent);
159 }
160
161 int heightDec = frameWidth() * 2;
162 if (horizontalScrollBar()->isVisible()) {
163 heightDec += style()->pixelMetric(QStyle::PM_ScrollBarExtent);
164 }
165
166 rect.adjust(0, 0, -widthDec, -heightDec);
167
168 m_controller->view()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
169
170 static_cast<KItemListContainerViewport*>(viewport())->scene()->setSceneRect(0, 0, rect.width(), rect.height());
171 static_cast<KItemListContainerViewport*>(viewport())->viewport()->setGeometry(QRect(0, 0, rect.width(), rect.height()));
172
173 updateScrollBars();
174 }
175
176 void KItemListContainer::initialize()
177 {
178 if (!m_controller) {
179 m_controller = new KItemListController(this);
180 }
181
182 connect(m_controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)),
183 this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
184 connect(m_controller, SIGNAL(viewChanged(KItemListView*,KItemListView*)),
185 this, SLOT(slotViewChanged(KItemListView*,KItemListView*)));
186
187 QGraphicsView* graphicsView = new KItemListContainerViewport(new QGraphicsScene(this), this);
188 setViewport(graphicsView);
189 }
190
191 #include "kitemlistcontainer.moc"