|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
5 ** |
|
6 ** This file is part of the examples of the Qt Toolkit. |
|
7 ** |
|
8 ** $QT_BEGIN_LICENSE:LGPL$ |
|
9 ** No Commercial Usage |
|
10 ** This file contains pre-release code and may not be distributed. |
|
11 ** You may use this file in accordance with the terms and conditions |
|
12 ** contained in the either Technology Preview License Agreement or the |
|
13 ** Beta Release License Agreement. |
|
14 ** |
|
15 ** GNU Lesser General Public License Usage |
|
16 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
17 ** General Public License version 2.1 as published by the Free Software |
|
18 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
19 ** packaging of this file. Please review the following information to |
|
20 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
22 ** |
|
23 ** In addition, as a special exception, Nokia gives you certain |
|
24 ** additional rights. These rights are described in the Nokia Qt LGPL |
|
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
|
26 ** package. |
|
27 ** |
|
28 ** GNU General Public License Usage |
|
29 ** Alternatively, this file may be used under the terms of the GNU |
|
30 ** General Public License version 3.0 as published by the Free Software |
|
31 ** Foundation and appearing in the file LICENSE.GPL included in the |
|
32 ** packaging of this file. Please review the following information to |
|
33 ** ensure the GNU General Public License version 3.0 requirements will be |
|
34 ** met: http://www.gnu.org/copyleft/gpl.html. |
|
35 ** |
|
36 ** If you are unsure which license is appropriate for your use, please |
|
37 ** contact the sales department at http://qt.nokia.com/contact. |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QGraphicsSceneResizeEvent> |
|
43 #include <QGraphicsWidget> |
|
44 #include <QDebug> |
|
45 #include "abstractscrollarea.h" |
|
46 #include "scrollbar.h" |
|
47 |
|
48 AbstractScrollArea::AbstractScrollArea(QGraphicsWidget *parent) |
|
49 : GvbWidget(parent) |
|
50 , m_viewport(0) |
|
51 , m_horizontalScrollBar(0) |
|
52 , m_verticalScrollBar(0) |
|
53 , m_prevHorizontalValue(0.0) |
|
54 , m_prevVerticalValue(0.0) |
|
55 { |
|
56 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
|
57 setContentsMargins(0, 0, 0, 0); |
|
58 |
|
59 m_horizontalScrollBar = new ScrollBar(Qt::Horizontal, this); |
|
60 m_horizontalScrollBar->hide(); |
|
61 m_horizontalScrollBar->setContentsMargins(0, 0, 0, 0); |
|
62 m_horizontalScrollBarPolicy = Qt::ScrollBarAsNeeded; |
|
63 m_horizontalScrollBar->setZValue(zValue()+1); // Raise scroll bar to top |
|
64 m_horizontalScrollBar->setVisible(false); |
|
65 |
|
66 connect(m_horizontalScrollBar, SIGNAL(sliderPositionChange(qreal)), |
|
67 this, SLOT(horizontalScroll(qreal))); |
|
68 connect(m_horizontalScrollBar, SIGNAL(sliderPressed()), |
|
69 this, SLOT(horizontalScrollStart())); |
|
70 |
|
71 m_verticalScrollBar = new ScrollBar(Qt::Vertical, this); |
|
72 m_verticalScrollBar->hide(); |
|
73 m_verticalScrollBar->setContentsMargins(0, 0, 0, 0); |
|
74 m_verticalScrollBarPolicy = Qt::ScrollBarAsNeeded; |
|
75 m_verticalScrollBar->setZValue(zValue()+1); // Raise scroll bar to top |
|
76 m_verticalScrollBar->setVisible(false); |
|
77 |
|
78 connect(m_verticalScrollBar, SIGNAL(sliderPositionChange(qreal)), |
|
79 this, SLOT(verticalScroll(qreal))); |
|
80 connect(m_verticalScrollBar, SIGNAL(sliderPressed()), |
|
81 this, SLOT(verticalScrollStart())); |
|
82 |
|
83 QGraphicsWidget *viewport = new QGraphicsWidget; |
|
84 setViewport(viewport); |
|
85 } |
|
86 |
|
87 AbstractScrollArea::~AbstractScrollArea() |
|
88 { |
|
89 } |
|
90 |
|
91 ScrollBar *AbstractScrollArea::verticalScrollBar() const |
|
92 { |
|
93 return m_verticalScrollBar; |
|
94 } |
|
95 |
|
96 ScrollBar *AbstractScrollArea::horizontalScrollBar() const |
|
97 { |
|
98 return m_horizontalScrollBar; |
|
99 } |
|
100 |
|
101 void AbstractScrollArea::setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy policy) |
|
102 { |
|
103 m_horizontalScrollBarPolicy = policy; |
|
104 } |
|
105 |
|
106 void AbstractScrollArea::setVerticalScrollBarPolicy(Qt::ScrollBarPolicy policy) |
|
107 { |
|
108 m_verticalScrollBarPolicy = policy; |
|
109 } |
|
110 |
|
111 Qt::ScrollBarPolicy AbstractScrollArea::verticalScrollBarPolicy() const |
|
112 { |
|
113 return m_verticalScrollBarPolicy; |
|
114 } |
|
115 |
|
116 Qt::ScrollBarPolicy AbstractScrollArea::horizontalScrollBarPolicy() const |
|
117 { |
|
118 return m_horizontalScrollBarPolicy; |
|
119 } |
|
120 |
|
121 QGraphicsWidget *AbstractScrollArea::viewport() const |
|
122 { |
|
123 return m_viewport; |
|
124 } |
|
125 |
|
126 void AbstractScrollArea::setViewport(QGraphicsWidget *viewport) |
|
127 { |
|
128 if (m_viewport) { |
|
129 m_viewport->setParentItem(0); |
|
130 |
|
131 QList<QGraphicsItem*> children = m_viewport->childItems(); |
|
132 |
|
133 foreach (QGraphicsItem *child, children) |
|
134 child->setParentItem(0); |
|
135 |
|
136 delete m_viewport; |
|
137 } |
|
138 |
|
139 m_viewport = viewport; |
|
140 |
|
141 if (viewport) { |
|
142 |
|
143 m_viewport->setParentItem(this); |
|
144 m_viewport->setContentsMargins(0, 0, 0, 0); |
|
145 |
|
146 adjustScrollBars(); |
|
147 } |
|
148 |
|
149 emit viewportChanged(viewport); |
|
150 } |
|
151 |
|
152 bool AbstractScrollArea::event(QEvent *e) |
|
153 { |
|
154 if (e->type() == QEvent::ApplicationLayoutDirectionChange |
|
155 || e->type() == QEvent::LayoutDirectionChange) { |
|
156 } else if (e->type() == QEvent::GraphicsSceneResize) { |
|
157 QGraphicsSceneResizeEvent *event = |
|
158 static_cast<QGraphicsSceneResizeEvent*>(e); |
|
159 |
|
160 QSizeF newSize = event->newSize(); |
|
161 QRectF hrect = m_horizontalScrollBar->boundingRect(); |
|
162 QRectF vrect = m_verticalScrollBar->boundingRect(); |
|
163 |
|
164 QSizeF vpSize = newSize; |
|
165 |
|
166 if (m_horizontalScrollBarPolicy != Qt::ScrollBarAlwaysOff) |
|
167 vpSize.setHeight(newSize.height() - hrect.height()); |
|
168 if (m_verticalScrollBarPolicy != Qt::ScrollBarAlwaysOff) |
|
169 vpSize.setWidth(newSize.width() - vrect.width()); |
|
170 |
|
171 m_viewport->resize(vpSize); |
|
172 |
|
173 adjustScrollBars(); |
|
174 } |
|
175 |
|
176 return QGraphicsWidget::event(e); |
|
177 } |
|
178 |
|
179 |
|
180 void AbstractScrollArea::scrollContentsBy(qreal dx, qreal dy) |
|
181 { |
|
182 Q_UNUSED(dx) |
|
183 Q_UNUSED(dy) |
|
184 prepareGeometryChange(); |
|
185 } |
|
186 |
|
187 void AbstractScrollArea::verticalScrollStart() |
|
188 { |
|
189 m_prevVerticalValue = m_verticalScrollBar->sliderPosition(); |
|
190 } |
|
191 |
|
192 void AbstractScrollArea::verticalScroll(qreal value) |
|
193 { |
|
194 qreal dy = value - m_prevVerticalValue; |
|
195 if (!qFuzzyCompare(dy,qreal(0.0))) { |
|
196 scrollContentsBy(0.0, dy); |
|
197 m_prevVerticalValue = value; |
|
198 } |
|
199 } |
|
200 |
|
201 void AbstractScrollArea::horizontalScrollStart() |
|
202 { |
|
203 m_prevHorizontalValue = m_horizontalScrollBar->sliderPosition(); |
|
204 } |
|
205 |
|
206 void AbstractScrollArea::horizontalScroll(qreal value) |
|
207 { |
|
208 qreal dx = value - m_prevHorizontalValue; |
|
209 if (!qFuzzyCompare(dx,qreal(0.0))) { |
|
210 scrollContentsBy(dx, 0.0); |
|
211 m_prevHorizontalValue = value; |
|
212 } |
|
213 } |
|
214 |
|
215 void AbstractScrollArea::adjustScrollBars() |
|
216 { |
|
217 if (m_horizontalScrollBarPolicy == Qt::ScrollBarAlwaysOff) { |
|
218 m_horizontalScrollBar->hide(); |
|
219 } else { |
|
220 m_horizontalScrollBar->show(); |
|
221 |
|
222 QRectF sbgeom = boundingRect(); |
|
223 |
|
224 sbgeom.setTop(sbgeom.bottom() - m_horizontalScrollBar->boundingRect().height()); |
|
225 sbgeom.setRight(sbgeom.right() - m_verticalScrollBar->boundingRect().width()); |
|
226 m_horizontalScrollBar->setGeometry(sbgeom); |
|
227 } |
|
228 |
|
229 if (m_verticalScrollBarPolicy == Qt::ScrollBarAlwaysOff) { |
|
230 m_verticalScrollBar->hide(); |
|
231 QRectF sbgeom = boundingRect(); |
|
232 sbgeom.setLeft(sbgeom.right()); |
|
233 sbgeom.setBottom(sbgeom.bottom()); |
|
234 m_verticalScrollBar->setGeometry(sbgeom); |
|
235 } else { |
|
236 m_verticalScrollBar->show(); |
|
237 |
|
238 QRectF sbgeom = boundingRect(); |
|
239 |
|
240 sbgeom.setLeft(sbgeom.right() - m_verticalScrollBar->boundingRect().width()); |
|
241 if (m_horizontalScrollBarPolicy != Qt::ScrollBarAlwaysOff) |
|
242 sbgeom.setBottom(sbgeom.bottom() - m_horizontalScrollBar->boundingRect().height()); |
|
243 m_verticalScrollBar->setGeometry(sbgeom); |
|
244 } |
|
245 } |
|
246 |
|
247 |
|
248 |
|
249 |