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 <QGraphicsScene>
30 #include <QGraphicsView>
36 class KItemListContainerViewport
: public QGraphicsView
39 KItemListContainerViewport(QGraphicsScene
* scene
, QWidget
* parent
)
40 : QGraphicsView(scene
, parent
)
42 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
43 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff
);
44 setViewportMargins(0, 0, 0, 0);
45 setFrameShape(QFrame::NoFrame
);
48 void scrollContentsBy(int dx
, int dy
)
52 // Do nothing. This prevents that e.g. the wheel-event
53 // results in a moving of the scene items.
57 KItemListContainer::KItemListContainer(KItemListController
* controller
, QWidget
* parent
) :
58 QAbstractScrollArea(parent
),
59 m_controller(controller
)
62 controller
->setParent(this);
66 KItemListContainer::KItemListContainer(QWidget
* parent
) :
67 QAbstractScrollArea(parent
),
73 KItemListContainer::~KItemListContainer()
77 KItemListController
* KItemListContainer::controller() const
82 void KItemListContainer::showEvent(QShowEvent
* event
)
84 QAbstractScrollArea::showEvent(event
);
88 void KItemListContainer::resizeEvent(QResizeEvent
* event
)
90 QAbstractScrollArea::resizeEvent(event
);
94 void KItemListContainer::scrollContentsBy(int dx
, int dy
)
96 KItemListView
* view
= m_controller
->view();
101 const qreal currentOffset
= view
->offset();
102 const qreal offsetDiff
= (view
->scrollOrientation() == Qt::Vertical
) ? dy
: dx
;
103 view
->setOffset(currentOffset
- offsetDiff
);
106 void KItemListContainer::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
112 void KItemListContainer::slotViewChanged(KItemListView
* current
, KItemListView
* previous
)
114 QGraphicsScene
* scene
= static_cast<QGraphicsView
*>(viewport())->scene();
116 scene
->removeItem(previous
);
117 disconnect(previous
, SIGNAL(offsetChanged(int,int)), this, SLOT(updateScrollBars()));
118 disconnect(previous
, SIGNAL(maximumOffsetChanged(int,int)), this, SLOT(updateScrollBars()));
121 scene
->addItem(current
);
122 connect(previous
, SIGNAL(offsetChanged(int,int)), this, SLOT(updateScrollBars()));
123 connect(current
, SIGNAL(maximumOffsetChanged(int,int)), this, SLOT(updateScrollBars()));
127 void KItemListContainer::updateScrollBars()
129 const QSizeF size
= m_controller
->view()->size();
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);
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);
152 void KItemListContainer::updateGeometries()
154 QRect rect
= geometry();
156 int widthDec
= frameWidth() * 2;
157 if (verticalScrollBar()->isVisible()) {
158 widthDec
+= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
161 int heightDec
= frameWidth() * 2;
162 if (horizontalScrollBar()->isVisible()) {
163 heightDec
+= style()->pixelMetric(QStyle::PM_ScrollBarExtent
);
166 rect
.adjust(0, 0, -widthDec
, -heightDec
);
168 m_controller
->view()->setGeometry(QRect(0, 0, rect
.width(), rect
.height()));
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()));
176 void KItemListContainer::initialize()
179 m_controller
= new KItemListController(this);
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
*)));
187 QGraphicsView
* graphicsView
= new KItemListContainerViewport(new QGraphicsScene(this), this);
188 setViewport(graphicsView
);
191 #include "kitemlistcontainer.moc"