|
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 <QGraphicsWidget> |
|
43 #include <QPainter> |
|
44 #include <QGraphicsSceneMouseEvent> |
|
45 #include <QDebug> |
|
46 |
|
47 #include "scrollbar.h" |
|
48 #include "theme.h" |
|
49 |
|
50 class ScrollBarPrivate { |
|
51 Q_DECLARE_PUBLIC(ScrollBar) |
|
52 |
|
53 public: |
|
54 |
|
55 ScrollBarPrivate(Qt::Orientation orientation, ScrollBar *scrollBar) |
|
56 : orientation(orientation) |
|
57 , sliderPosition(0.0) |
|
58 , sliderSize(0.0) |
|
59 , sliderDown(false) |
|
60 , q_ptr(scrollBar) |
|
61 { |
|
62 construct(); |
|
63 } |
|
64 |
|
65 void themeChange() |
|
66 { |
|
67 construct(); |
|
68 updateSlider(); |
|
69 } |
|
70 |
|
71 void construct() |
|
72 { |
|
73 scrollerPixmap = Theme::p()->pixmap("scroll.svg"); |
|
74 scrollBarPixmap = Theme::p()->pixmap("scrollbar.svg"); |
|
75 |
|
76 if (orientation == Qt::Horizontal) { |
|
77 scrollerPixmap = scrollerPixmap.transformed(QTransform().rotate(90)); |
|
78 scrollBarPixmap = scrollBarPixmap.transformed(QTransform().rotate(90)); |
|
79 } |
|
80 } |
|
81 |
|
82 void setSliderPosition(qreal pos) |
|
83 { |
|
84 if (pos < 0.0) |
|
85 pos = 0.0; |
|
86 |
|
87 if (pos > sliderSize) |
|
88 pos = sliderSize; |
|
89 |
|
90 sliderPosition = pos; |
|
91 |
|
92 if (!qFuzzyCompare(pos, sliderPosition)) |
|
93 updateSlider(); |
|
94 } |
|
95 |
|
96 void updateSlider() |
|
97 { |
|
98 QRectF oldSlider = slider; |
|
99 slider = q_func()->boundingRect(); |
|
100 |
|
101 qreal x = 0; |
|
102 qreal y = 0; |
|
103 qreal w = scrollerPixmap.width(); |
|
104 qreal h = scrollerPixmap.height(); |
|
105 |
|
106 //Adjust the scrollBar in relation to the scroller |
|
107 |
|
108 if (orientation == Qt::Horizontal) { |
|
109 qreal scrollBarHeight = scrollBarPixmap.height(); |
|
110 |
|
111 if (h > scrollBarHeight) { |
|
112 slider.setTop((h - scrollBarHeight)/2.0); |
|
113 slider.setHeight(scrollBarHeight); |
|
114 } |
|
115 } else { |
|
116 qreal scrollBarWidth = scrollBarPixmap.width(); |
|
117 |
|
118 if (w > scrollBarWidth) { |
|
119 slider.setLeft((w - scrollBarWidth)/2.0); |
|
120 } |
|
121 slider.setWidth(scrollBarWidth); |
|
122 } |
|
123 |
|
124 if(oldSlider != slider && (slider.size().width() > 0 &&slider.size().height() > 0 )) { |
|
125 scrollBarPixmap = Theme::p()->pixmap("scrollbar.svg", slider.size().toSize()); |
|
126 } |
|
127 cursor = QRectF(x, y, w, h); |
|
128 |
|
129 if (orientation == Qt::Horizontal) { |
|
130 qreal dx = qreal(int(sliderPosition)) * (slider.width() - cursor.width()) / sliderSize; |
|
131 cursor.translate(dx, 0.0); |
|
132 } else { |
|
133 qreal dy = qreal(int(sliderPosition)) * (slider.height() - cursor.height()) / sliderSize; |
|
134 cursor.translate(0.0, dy); |
|
135 } |
|
136 } |
|
137 |
|
138 Qt::Orientation orientation; |
|
139 qreal sliderPosition; |
|
140 qreal sliderSize; |
|
141 |
|
142 QPointF pressPos; |
|
143 bool sliderDown; |
|
144 |
|
145 QRectF slider; |
|
146 QRectF cursor; |
|
147 QPixmap scrollerPixmap; |
|
148 QPixmap scrollBarPixmap; |
|
149 |
|
150 ScrollBar *q_ptr; |
|
151 }; |
|
152 |
|
153 ScrollBar::ScrollBar(Qt::Orientation orientation, QGraphicsWidget *parent) |
|
154 : QGraphicsWidget(parent) |
|
155 , d_ptr(new ScrollBarPrivate(orientation, this)) |
|
156 { |
|
157 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding); |
|
158 setContentsMargins(0, 0, 0, 0); |
|
159 |
|
160 connect(Theme::p(), SIGNAL(themeChanged()), this, SLOT(themeChange())); |
|
161 } |
|
162 |
|
163 ScrollBar::~ScrollBar() |
|
164 { |
|
165 delete d_ptr; |
|
166 } |
|
167 |
|
168 qreal ScrollBar::sliderSize() const |
|
169 { |
|
170 Q_D(const ScrollBar); |
|
171 return d->sliderSize; |
|
172 } |
|
173 |
|
174 void ScrollBar::setSliderSize(const qreal s) |
|
175 { |
|
176 Q_D(ScrollBar); |
|
177 d->sliderSize = s; |
|
178 } |
|
179 |
|
180 void ScrollBar::setSliderPosition(qreal pos) |
|
181 { |
|
182 Q_D(ScrollBar); |
|
183 |
|
184 d->setSliderPosition(pos); |
|
185 prepareGeometryChange(); |
|
186 emit sliderPositionChange(d->sliderPosition); |
|
187 } |
|
188 |
|
189 qreal ScrollBar::sliderPosition() const |
|
190 { |
|
191 Q_D(const ScrollBar); |
|
192 return d->sliderPosition; |
|
193 } |
|
194 |
|
195 bool ScrollBar::sliderDown() const |
|
196 { |
|
197 Q_D(const ScrollBar); |
|
198 return d->sliderDown; |
|
199 } |
|
200 |
|
201 void ScrollBar::paint(QPainter *painter, |
|
202 const QStyleOptionGraphicsItem *option, |
|
203 QWidget *widget) |
|
204 { |
|
205 Q_D(ScrollBar); |
|
206 Q_UNUSED(widget); |
|
207 Q_UNUSED(option); |
|
208 |
|
209 d->updateSlider(); |
|
210 |
|
211 QRect sliderRect = d->slider.toRect(); |
|
212 painter->drawPixmap(sliderRect.topLeft(), d->scrollBarPixmap); |
|
213 |
|
214 QRect cursorRect = d->cursor.toRect(); |
|
215 painter->drawPixmap(cursorRect.topLeft(), d->scrollerPixmap); |
|
216 } |
|
217 |
|
218 QSizeF ScrollBar::sizeHint(Qt::SizeHint which, |
|
219 const QSizeF &constraint) const |
|
220 { |
|
221 Q_D(const ScrollBar); |
|
222 |
|
223 QSizeF s; |
|
224 |
|
225 if (d->orientation == Qt::Horizontal) |
|
226 s = QSizeF(-1, qMax(d->scrollBarPixmap.height(), d->scrollerPixmap.height())); |
|
227 else |
|
228 s = QSizeF(qMax(d->scrollBarPixmap.width(), d->scrollerPixmap.width()), -1); |
|
229 |
|
230 switch (which) |
|
231 { |
|
232 case Qt::MinimumSize: |
|
233 return s; |
|
234 |
|
235 case Qt::MaximumSize: |
|
236 return s; |
|
237 |
|
238 default: |
|
239 return QGraphicsWidget::sizeHint(which, constraint); |
|
240 } |
|
241 } |
|
242 |
|
243 void ScrollBar::mousePressEvent(QGraphicsSceneMouseEvent *event) |
|
244 { |
|
245 Q_D(ScrollBar); |
|
246 |
|
247 d->updateSlider(); |
|
248 |
|
249 if (d->cursor.contains(event->pos())) { |
|
250 d->sliderDown = true; |
|
251 d->pressPos = event->pos(); |
|
252 emit sliderPressed(); |
|
253 } |
|
254 } |
|
255 |
|
256 void ScrollBar::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) |
|
257 { |
|
258 Q_D(ScrollBar); |
|
259 Q_UNUSED(event); |
|
260 |
|
261 d->sliderDown = false; |
|
262 } |
|
263 |
|
264 void ScrollBar::mouseMoveEvent(QGraphicsSceneMouseEvent *event) |
|
265 { |
|
266 Q_D(ScrollBar); |
|
267 |
|
268 if (!d->sliderDown) |
|
269 return; |
|
270 |
|
271 if (d->orientation == Qt::Horizontal) { |
|
272 qreal f = (event->pos().x() - d->pressPos.x())/(d->slider.width() - d->cursor.width()); |
|
273 qreal dx = f * d->sliderSize; |
|
274 |
|
275 d->setSliderPosition(d->sliderPosition + dx); |
|
276 } else { |
|
277 qreal f = (event->pos().y() - d->pressPos.y())/(d->slider.height() - d->cursor.height()); |
|
278 qreal dy = f * d->sliderSize; |
|
279 |
|
280 d->setSliderPosition(d->sliderPosition + dy); |
|
281 } |
|
282 |
|
283 d->pressPos = event->pos(); |
|
284 |
|
285 prepareGeometryChange(); |
|
286 emit sliderPositionChange(d->sliderPosition); |
|
287 } |
|
288 |
|
289 void ScrollBar::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
290 { |
|
291 QGraphicsWidget::resizeEvent(event); |
|
292 } |
|
293 |
|
294 void ScrollBar::themeChange() |
|
295 { |
|
296 Q_D(ScrollBar); |
|
297 d->themeChange(); |
|
298 } |
|
299 |