|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the documentation of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 QAbstractSliderPrivate::QAbstractSliderPrivate() |
|
43 : minimum(0), maximum(99), singleStep(1), pageStep(10), |
|
44 value(0), position(0), pressValue(-1), tracking(true), blocktracking(false), pressed(false), |
|
45 invertedAppearance(false), invertedControls(false), |
|
46 orientation(Qt::Horizontal), repeatAction(QAbstractSlider::SliderNoAction) |
|
47 { |
|
48 } |
|
49 |
|
50 QAbstractSliderPrivate::~QAbstractSliderPrivate() |
|
51 { |
|
52 } |
|
53 |
|
54 oid QAbstractSlider::setRange(int min, int max) |
|
55 { |
|
56 Q_D(QAbstractSlider); |
|
57 int oldMin = d->minimum; |
|
58 int oldMax = d->maximum; |
|
59 d->minimum = min; |
|
60 d->maximum = qMax(min, max); |
|
61 if (oldMin != d->minimum || oldMax != d->maximum) { |
|
62 sliderChange(SliderRangeChange); |
|
63 emit rangeChanged(d->minimum, d->maximum); |
|
64 setValue(d->value); // re-bound |
|
65 } |
|
66 } |
|
67 |
|
68 |
|
69 void QAbstractSliderPrivate::setSteps(int single, int page) |
|
70 { |
|
71 Q_Q(QAbstractSlider); |
|
72 singleStep = qAbs(single); |
|
73 pageStep = qAbs(page); |
|
74 q->sliderChange(QAbstractSlider::SliderStepsChange); |
|
75 } |
|
76 |
|
77 AbstractSlider::QAbstractSlider(QWidget *parent) |
|
78 :QWidget(*new QAbstractSliderPrivate, parent, 0) |
|
79 { |
|
80 } |
|
81 |
|
82 QAbstractSlider::QAbstractSlider(QAbstractSliderPrivate &dd, QWidget *parent) |
|
83 :QWidget(dd, parent, 0) |
|
84 { |
|
85 } |
|
86 |
|
87 QAbstractSlider::~QAbstractSlider() |
|
88 { |
|
89 } |
|
90 |
|
91 void QAbstractSlider::setOrientation(Qt::Orientation orientation) |
|
92 { |
|
93 Q_D(QAbstractSlider); |
|
94 if (d->orientation == orientation) |
|
95 return; |
|
96 |
|
97 d->orientation = orientation; |
|
98 if (!testAttribute(Qt::WA_WState_OwnSizePolicy)) { |
|
99 QSizePolicy sp = sizePolicy(); |
|
100 sp.transpose(); |
|
101 setSizePolicy(sp); |
|
102 setAttribute(Qt::WA_WState_OwnSizePolicy, false); |
|
103 } |
|
104 update(); |
|
105 updateGeometry(); |
|
106 } |
|
107 |
|
108 Qt::Orientation QAbstractSlider::orientation() const |
|
109 { |
|
110 Q_D(const QAbstractSlider); |
|
111 return d->orientation; |
|
112 } |
|
113 |
|
114 |
|
115 void QAbstractSlider::setMinimum(int min) |
|
116 { |
|
117 Q_D(QAbstractSlider); |
|
118 setRange(min, qMax(d->maximum, min)); |
|
119 } |
|
120 |
|
121 int QAbstractSlider::minimum() const |
|
122 { |
|
123 Q_D(const QAbstractSlider); |
|
124 return d->minimum; |
|
125 } |
|
126 |
|
127 |
|
128 void QAbstractSlider::setMaximum(int max) |
|
129 { |
|
130 Q_D(QAbstractSlider); |
|
131 setRange(qMin(d->minimum, max), max); |
|
132 } |
|
133 |
|
134 int QAbstractSlider::maximum() const |
|
135 { |
|
136 Q_D(const QAbstractSlider); |
|
137 return d->maximum; |
|
138 } |
|
139 |
|
140 |
|
141 |
|
142 void QAbstractSlider::setSingleStep(int step) |
|
143 { |
|
144 Q_D(QAbstractSlider); |
|
145 d->setSteps(step, d->pageStep); |
|
146 } |
|
147 |
|
148 int QAbstractSlider::singleStep() const |
|
149 { |
|
150 Q_D(const QAbstractSlider); |
|
151 return d->singleStep; |
|
152 } |
|
153 |
|
154 |
|
155 void QAbstractSlider::setPageStep(int step) |
|
156 { |
|
157 Q_D(QAbstractSlider); |
|
158 d->setSteps(d->singleStep, step); |
|
159 } |
|
160 |
|
161 int QAbstractSlider::pageStep() const |
|
162 { |
|
163 Q_D(const QAbstractSlider); |
|
164 return d->pageStep; |
|
165 } |
|
166 |
|
167 oid QAbstractSlider::setTracking(bool enable) |
|
168 { |
|
169 Q_D(QAbstractSlider); |
|
170 d->tracking = enable; |
|
171 } |
|
172 |
|
173 bool QAbstractSlider::hasTracking() const |
|
174 { |
|
175 Q_D(const QAbstractSlider); |
|
176 return d->tracking; |
|
177 } |
|
178 |
|
179 |
|
180 |
|
181 void QAbstractSlider::setSliderDown(bool down) |
|
182 { |
|
183 Q_D(QAbstractSlider); |
|
184 bool doEmit = d->pressed != down; |
|
185 |
|
186 d->pressed = down; |
|
187 |
|
188 if (doEmit) { |
|
189 if (down) |
|
190 emit sliderPressed(); |
|
191 else |
|
192 emit sliderReleased(); |
|
193 } |
|
194 |
|
195 if (!down && d->position != d->value) |
|
196 triggerAction(SliderMove); |
|
197 } |
|
198 |
|
199 bool QAbstractSlider::isSliderDown() const |
|
200 { |
|
201 Q_D(const QAbstractSlider); |
|
202 return d->pressed; |
|
203 } |
|
204 |
|
205 |
|
206 void QAbstractSlider::setSliderPosition(int position) |
|
207 { |
|
208 Q_D(QAbstractSlider); |
|
209 position = d->bound(position); |
|
210 if (position == d->position) |
|
211 return; |
|
212 d->position = position; |
|
213 if (!d->tracking) |
|
214 update(); |
|
215 if (d->pressed) |
|
216 emit sliderMoved(position); |
|
217 if (d->tracking && !d->blocktracking) |
|
218 triggerAction(SliderMove); |
|
219 } |
|
220 |
|
221 int QAbstractSlider::sliderPosition() const |
|
222 { |
|
223 Q_D(const QAbstractSlider); |
|
224 return d->position; |
|
225 } |
|
226 |
|
227 |
|
228 |
|
229 int QAbstractSlider::value() const |
|
230 { |
|
231 Q_D(const QAbstractSlider); |
|
232 return d->value; |
|
233 } |
|
234 |
|
235 //! [0] |
|
236 void QAbstractSlider::setValue(int value) |
|
237 //! [0] |
|
238 { |
|
239 Q_D(QAbstractSlider); |
|
240 value = d->bound(value); |
|
241 if (d->value == value && d->position == value) |
|
242 return; |
|
243 d->value = value; |
|
244 if (d->position != value) { |
|
245 d->position = value; |
|
246 if (d->pressed) |
|
247 emit sliderMoved((d->position = value)); |
|
248 } |
|
249 #ifndef QT_NO_ACCESSIBILITY |
|
250 //! [1] |
|
251 QAccessible::updateAccessibility(this, 0, QAccessible::ValueChanged); |
|
252 //! [1] |
|
253 #endif |
|
254 sliderChange(SliderValueChange); |
|
255 emit valueChanged(value); |
|
256 //! [2] |
|
257 } |
|
258 //! [2] |
|
259 |
|
260 bool QAbstractSlider::invertedAppearance() const |
|
261 { |
|
262 Q_D(const QAbstractSlider); |
|
263 return d->invertedAppearance; |
|
264 } |
|
265 |
|
266 void QAbstractSlider::setInvertedAppearance(bool invert) |
|
267 { |
|
268 Q_D(QAbstractSlider); |
|
269 d->invertedAppearance = invert; |
|
270 update(); |
|
271 } |
|
272 |
|
273 |
|
274 |
|
275 bool QAbstractSlider::invertedControls() const |
|
276 { |
|
277 Q_D(const QAbstractSlider); |
|
278 return d->invertedControls; |
|
279 } |
|
280 |
|
281 void QAbstractSlider::setInvertedControls(bool invert) |
|
282 { |
|
283 Q_D(QAbstractSlider); |
|
284 d->invertedControls = invert; |
|
285 } |
|
286 |
|
287 void QAbstractSlider::triggerAction(SliderAction action) |
|
288 { |
|
289 Q_D(QAbstractSlider); |
|
290 d->blocktracking = true; |
|
291 switch (action) { |
|
292 case SliderSingleStepAdd: |
|
293 setSliderPosition(d->value + d->singleStep); |
|
294 break; |
|
295 case SliderSingleStepSub: |
|
296 setSliderPosition(d->value - d->singleStep); |
|
297 break; |
|
298 case SliderPageStepAdd: |
|
299 setSliderPosition(d->value + d->pageStep); |
|
300 break; |
|
301 case SliderPageStepSub: |
|
302 setSliderPosition(d->value - d->pageStep); |
|
303 break; |
|
304 case SliderToMinimum: |
|
305 setSliderPosition(d->minimum); |
|
306 break; |
|
307 case SliderToMaximum: |
|
308 setSliderPosition(d->maximum); |
|
309 break; |
|
310 case SliderMove: |
|
311 case SliderNoAction: |
|
312 break; |
|
313 }; |
|
314 emit actionTriggered(action); |
|
315 d->blocktracking = false; |
|
316 setValue(d->position); |
|
317 } |
|
318 |
|
319 void QAbstractSlider::setRepeatAction(SliderAction action, int thresholdTime, int repeatTime) |
|
320 { |
|
321 Q_D(QAbstractSlider); |
|
322 if ((d->repeatAction = action) == SliderNoAction) { |
|
323 d->repeatActionTimer.stop(); |
|
324 } else { |
|
325 d->repeatActionTime = repeatTime; |
|
326 d->repeatActionTimer.start(thresholdTime, this); |
|
327 } |
|
328 } |
|
329 |
|
330 QAbstractSlider::SliderAction QAbstractSlider::repeatAction() const |
|
331 { |
|
332 Q_D(const QAbstractSlider); |
|
333 return d->repeatAction; |
|
334 } |
|
335 |
|
336 void QAbstractSlider::timerEvent(QTimerEvent *e) |
|
337 { |
|
338 Q_D(QAbstractSlider); |
|
339 if (e->timerId() == d->repeatActionTimer.timerId()) { |
|
340 if (d->repeatActionTime) { // was threshold time, use repeat time next time |
|
341 d->repeatActionTimer.start(d->repeatActionTime, this); |
|
342 d->repeatActionTime = 0; |
|
343 } |
|
344 if (d->repeatAction == SliderPageStepAdd) |
|
345 d->setAdjustedSliderPosition(d->value + d->pageStep); |
|
346 else if (d->repeatAction == SliderPageStepSub) |
|
347 d->setAdjustedSliderPosition(d->value - d->pageStep); |
|
348 else |
|
349 triggerAction(d->repeatAction); |
|
350 } |
|
351 } |
|
352 |
|
353 oid QAbstractSlider::sliderChange(SliderChange) |
|
354 { |
|
355 update(); |
|
356 } |
|
357 |
|
358 |
|
359 #ifndef QT_NO_WHEELEVENT |
|
360 void QAbstractSlider::wheelEvent(QWheelEvent * e) |
|
361 { |
|
362 Q_D(QAbstractSlider); |
|
363 e->ignore(); |
|
364 if (e->orientation() != d->orientation && !rect().contains(e->pos())) |
|
365 return; |
|
366 |
|
367 static qreal offset = 0; |
|
368 static QAbstractSlider *offset_owner = 0; |
|
369 if (offset_owner != this){ |
|
370 offset_owner = this; |
|
371 offset = 0; |
|
372 } |
|
373 |
|
374 int step = qMin(QApplication::wheelScrollLines() * d->singleStep, d->pageStep); |
|
375 if ((e->modifiers() & Qt::ControlModifier) || (e->modifiers() & Qt::ShiftModifier)) |
|
376 step = d->pageStep; |
|
377 offset += e->delta() * step / 120; |
|
378 if (d->invertedControls) |
|
379 offset = -offset; |
|
380 |
|
381 if (qAbs(offset) < 1) |
|
382 return; |
|
383 |
|
384 int prevValue = d->value; |
|
385 d->position = d->value + int(offset); // value will be updated by triggerAction() |
|
386 triggerAction(SliderMove); |
|
387 if (prevValue == d->value) { |
|
388 offset = 0; |
|
389 } else { |
|
390 offset -= int(offset); |
|
391 e->accept(); |
|
392 } |
|
393 } |
|
394 #endif |
|
395 void QAbstractSlider::keyPressEvent(QKeyEvent *ev) |
|
396 { |
|
397 Q_D(QAbstractSlider); |
|
398 SliderAction action = SliderNoAction; |
|
399 switch (ev->key()) { |
|
400 #ifdef QT_KEYPAD_NAVIGATION |
|
401 case Qt::Key_Select: |
|
402 if (QApplication::keypadNavigationEnabled()) |
|
403 setEditFocus(!hasEditFocus()); |
|
404 else |
|
405 ev->ignore(); |
|
406 break; |
|
407 case Qt::Key_Back: |
|
408 if (QApplication::keypadNavigationEnabled() && hasEditFocus()) { |
|
409 setValue(d->origValue); |
|
410 setEditFocus(false); |
|
411 } else |
|
412 ev->ignore(); |
|
413 break; |
|
414 #endif |
|
415 |
|
416 // It seems we need to use invertedAppearance for Left and right, otherwise, things look weird. |
|
417 case Qt::Key_Left: |
|
418 #ifdef QT_KEYPAD_NAVIGATION |
|
419 if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { |
|
420 ev->ignore(); |
|
421 return; |
|
422 } |
|
423 if (QApplication::keypadNavigationEnabled() && d->orientation == Qt::Vertical) |
|
424 action = d->invertedControls ? SliderSingleStepSub : SliderSingleStepAdd; |
|
425 else |
|
426 #endif |
|
427 action = !d->invertedAppearance ? SliderSingleStepSub : SliderSingleStepAdd; |
|
428 break; |
|
429 case Qt::Key_Right: |
|
430 #ifdef QT_KEYPAD_NAVIGATION |
|
431 if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { |
|
432 ev->ignore(); |
|
433 return; |
|
434 } |
|
435 if (QApplication::keypadNavigationEnabled() && d->orientation == Qt::Vertical) |
|
436 action = d->invertedControls ? SliderSingleStepAdd : SliderSingleStepSub; |
|
437 else |
|
438 #endif |
|
439 action = !d->invertedAppearance ? SliderSingleStepAdd : SliderSingleStepSub; |
|
440 break; |
|
441 case Qt::Key_Up: |
|
442 #ifdef QT_KEYPAD_NAVIGATION |
|
443 if (QApplication::keypadNavigationEnabled()) { |
|
444 ev->ignore(); |
|
445 break; |
|
446 } |
|
447 #endif |
|
448 action = d->invertedControls ? SliderSingleStepSub : SliderSingleStepAdd; |
|
449 break; |
|
450 case Qt::Key_Down: |
|
451 #ifdef QT_KEYPAD_NAVIGATION |
|
452 if (QApplication::keypadNavigationEnabled()) { |
|
453 ev->ignore(); |
|
454 break; |
|
455 } |
|
456 #endif |
|
457 action = d->invertedControls ? SliderSingleStepAdd : SliderSingleStepSub; |
|
458 break; |
|
459 case Qt::Key_PageUp: |
|
460 action = d->invertedControls ? SliderPageStepSub : SliderPageStepAdd; |
|
461 break; |
|
462 case Qt::Key_PageDown: |
|
463 action = d->invertedControls ? SliderPageStepAdd : SliderPageStepSub; |
|
464 break; |
|
465 case Qt::Key_Home: |
|
466 action = SliderToMinimum; |
|
467 break; |
|
468 case Qt::Key_End: |
|
469 action = SliderToMaximum; |
|
470 break; |
|
471 default: |
|
472 ev->ignore(); |
|
473 break; |
|
474 } |
|
475 if (action) |
|
476 triggerAction(action); |
|
477 } |
|
478 |
|
479 void QAbstractSlider::changeEvent(QEvent *ev) |
|
480 { |
|
481 Q_D(QAbstractSlider); |
|
482 switch (ev->type()) { |
|
483 case QEvent::EnabledChange: |
|
484 if (!isEnabled()) { |
|
485 d->repeatActionTimer.stop(); |
|
486 setSliderDown(false); |
|
487 } |
|
488 // fall through... |
|
489 default: |
|
490 QWidget::changeEvent(ev); |
|
491 } |
|
492 } |
|
493 |
|
494 bool QAbstractSlider::event(QEvent *e) |
|
495 { |
|
496 #ifdef QT_KEYPAD_NAVIGATION |
|
497 Q_D(QAbstractSlider); |
|
498 switch (e->type()) { |
|
499 case QEvent::FocusIn: |
|
500 d->origValue = d->value; |
|
501 break; |
|
502 default: |
|
503 break; |
|
504 } |
|
505 #endif |
|
506 |
|
507 return QWidget::event(e); |
|
508 } |
|
509 |
|
510 |