source: trunk/doc/src/snippets/qabstractsliderisnippet.cpp@ 568

Last change on this file since 568 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 13.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42QAbstractSliderPrivate::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
50QAbstractSliderPrivate::~QAbstractSliderPrivate()
51{
52}
53
54oid 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
69void 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
77AbstractSlider::QAbstractSlider(QWidget *parent)
78 :QWidget(*new QAbstractSliderPrivate, parent, 0)
79{
80}
81
82QAbstractSlider::QAbstractSlider(QAbstractSliderPrivate &dd, QWidget *parent)
83 :QWidget(dd, parent, 0)
84{
85}
86
87QAbstractSlider::~QAbstractSlider()
88{
89}
90
91void 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
108Qt::Orientation QAbstractSlider::orientation() const
109{
110 Q_D(const QAbstractSlider);
111 return d->orientation;
112}
113
114
115void QAbstractSlider::setMinimum(int min)
116{
117 Q_D(QAbstractSlider);
118 setRange(min, qMax(d->maximum, min));
119}
120
121int QAbstractSlider::minimum() const
122{
123 Q_D(const QAbstractSlider);
124 return d->minimum;
125}
126
127
128void QAbstractSlider::setMaximum(int max)
129{
130 Q_D(QAbstractSlider);
131 setRange(qMin(d->minimum, max), max);
132}
133
134int QAbstractSlider::maximum() const
135{
136 Q_D(const QAbstractSlider);
137 return d->maximum;
138}
139
140
141
142void QAbstractSlider::setSingleStep(int step)
143{
144 Q_D(QAbstractSlider);
145 d->setSteps(step, d->pageStep);
146}
147
148int QAbstractSlider::singleStep() const
149{
150 Q_D(const QAbstractSlider);
151 return d->singleStep;
152}
153
154
155void QAbstractSlider::setPageStep(int step)
156{
157 Q_D(QAbstractSlider);
158 d->setSteps(d->singleStep, step);
159}
160
161int QAbstractSlider::pageStep() const
162{
163 Q_D(const QAbstractSlider);
164 return d->pageStep;
165}
166
167oid QAbstractSlider::setTracking(bool enable)
168{
169 Q_D(QAbstractSlider);
170 d->tracking = enable;
171}
172
173bool QAbstractSlider::hasTracking() const
174{
175 Q_D(const QAbstractSlider);
176 return d->tracking;
177}
178
179
180
181void 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
199bool QAbstractSlider::isSliderDown() const
200{
201 Q_D(const QAbstractSlider);
202 return d->pressed;
203}
204
205
206void 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
221int QAbstractSlider::sliderPosition() const
222{
223 Q_D(const QAbstractSlider);
224 return d->position;
225}
226
227
228
229int QAbstractSlider::value() const
230{
231 Q_D(const QAbstractSlider);
232 return d->value;
233}
234
235//! [0]
236void 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
260bool QAbstractSlider::invertedAppearance() const
261{
262 Q_D(const QAbstractSlider);
263 return d->invertedAppearance;
264}
265
266void QAbstractSlider::setInvertedAppearance(bool invert)
267{
268 Q_D(QAbstractSlider);
269 d->invertedAppearance = invert;
270 update();
271}
272
273
274
275bool QAbstractSlider::invertedControls() const
276{
277 Q_D(const QAbstractSlider);
278 return d->invertedControls;
279}
280
281void QAbstractSlider::setInvertedControls(bool invert)
282{
283 Q_D(QAbstractSlider);
284 d->invertedControls = invert;
285}
286
287void 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
319void 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
330QAbstractSlider::SliderAction QAbstractSlider::repeatAction() const
331{
332 Q_D(const QAbstractSlider);
333 return d->repeatAction;
334}
335
336void 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
353oid QAbstractSlider::sliderChange(SliderChange)
354{
355 update();
356}
357
358
359#ifndef QT_NO_WHEELEVENT
360void 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
395void 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
479void 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
494bool 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
Note: See TracBrowser for help on using the repository browser.