qdial.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/widgets/widgets/qdial.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
1/****************************************************************************-
2**-
3** Copyright (C) 2015 The Qt Company Ltd.-
4** Contact: http://www.qt.io/licensing/-
5**-
6** This file is part of the QtWidgets module of the Qt Toolkit.-
7**-
8** $QT_BEGIN_LICENSE:LGPL21$-
9** Commercial License Usage-
10** Licensees holding valid commercial Qt licenses may use this file in-
11** accordance with the commercial license agreement provided with the-
12** Software or, alternatively, in accordance with the terms contained in-
13** a written agreement between you and The Qt Company. For licensing terms-
14** and conditions see http://www.qt.io/terms-conditions. For further-
15** information use the contact form at http://www.qt.io/contact-us.-
16**-
17** GNU Lesser General Public License Usage-
18** Alternatively, this file may be used under the terms of the GNU Lesser-
19** General Public License version 2.1 or version 3 as published by the Free-
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and-
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the-
22** following information to ensure the GNU Lesser General Public License-
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and-
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.-
25**-
26** As a special exception, The Qt Company gives you certain additional-
27** rights. These rights are described in The Qt Company LGPL Exception-
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.-
29**-
30** $QT_END_LICENSE$-
31**-
32****************************************************************************/-
33-
34#include "qdial.h"-
35-
36#ifndef QT_NO_DIAL-
37-
38#include <qapplication.h>-
39#include <qbitmap.h>-
40#include <qcolor.h>-
41#include <qevent.h>-
42#include <qpainter.h>-
43#include <qpolygon.h>-
44#include <qregion.h>-
45#include <qstyle.h>-
46#include <qstylepainter.h>-
47#include <qstyleoption.h>-
48#include <qslider.h>-
49#include <private/qabstractslider_p.h>-
50#include <private/qmath_p.h>-
51#ifndef QT_NO_ACCESSIBILITY-
52#include "qaccessible.h"-
53#endif-
54#include <qmath.h>-
55-
56QT_BEGIN_NAMESPACE-
57-
58class QDialPrivate : public QAbstractSliderPrivate-
59{-
60 Q_DECLARE_PUBLIC(QDial)-
61public:-
62 QDialPrivate()-
63 {-
64 wrapping = false;-
65 tracking = true;-
66 doNotEmit = false;-
67 target = qreal(3.7);-
68 }
never executed: end of block
0
69-
70 qreal target;-
71 uint showNotches : 1;-
72 uint wrapping : 1;-
73 uint doNotEmit : 1;-
74-
75 int valueFromPoint(const QPoint &) const;-
76 double angle(const QPoint &, const QPoint &) const;-
77 void init();-
78 virtual int bound(int val) const Q_DECL_OVERRIDE;-
79};-
80-
81void QDialPrivate::init()-
82{-
83 Q_Q(QDial);-
84 showNotches = false;-
85 q->setFocusPolicy(Qt::WheelFocus);-
86}
never executed: end of block
0
87-
88int QDialPrivate::bound(int val) const-
89{-
90 if (wrapping) {
wrappingDescription
TRUEnever evaluated
FALSEnever evaluated
0
91 if ((val >= minimum) && (val <= maximum))
(val >= minimum)Description
TRUEnever evaluated
FALSEnever evaluated
(val <= maximum)Description
TRUEnever evaluated
FALSEnever evaluated
0
92 return val;
never executed: return val;
0
93 val = minimum + ((val - minimum) % (maximum - minimum));-
94 if (val < minimum)
val < minimumDescription
TRUEnever evaluated
FALSEnever evaluated
0
95 val += maximum - minimum;
never executed: val += maximum - minimum;
0
96 return val;
never executed: return val;
0
97 } else {-
98 return QAbstractSliderPrivate::bound(val);
never executed: return QAbstractSliderPrivate::bound(val);
0
99 }-
100}-
101-
102/*!-
103 Initialize \a option with the values from this QDial. This method-
104 is useful for subclasses when they need a QStyleOptionSlider, but don't want-
105 to fill in all the information themselves.-
106-
107 \sa QStyleOption::initFrom()-
108*/-
109void QDial::initStyleOption(QStyleOptionSlider *option) const-
110{-
111 if (!option)
!optionDescription
TRUEnever evaluated
FALSEnever evaluated
0
112 return;
never executed: return;
0
113-
114 Q_D(const QDial);-
115 option->initFrom(this);-
116 option->minimum = d->minimum;-
117 option->maximum = d->maximum;-
118 option->sliderPosition = d->position;-
119 option->sliderValue = d->value;-
120 option->singleStep = d->singleStep;-
121 option->pageStep = d->pageStep;-
122 option->upsideDown = !d->invertedAppearance;-
123 option->notchTarget = d->target;-
124 option->dialWrapping = d->wrapping;-
125 option->subControls = QStyle::SC_All;-
126 option->activeSubControls = QStyle::SC_None;-
127 if (!d->showNotches) {
!d->showNotchesDescription
TRUEnever evaluated
FALSEnever evaluated
0
128 option->subControls &= ~QStyle::SC_DialTickmarks;-
129 option->tickPosition = QSlider::TicksAbove;-
130 } else {
never executed: end of block
0
131 option->tickPosition = QSlider::NoTicks;-
132 }
never executed: end of block
0
133 option->tickInterval = notchSize();-
134}
never executed: end of block
0
135-
136int QDialPrivate::valueFromPoint(const QPoint &p) const-
137{-
138 Q_Q(const QDial);-
139 double yy = q->height()/2.0 - p.y();-
140 double xx = p.x() - q->width()/2.0;-
141 double a = (xx || yy) ? std::atan2(yy, xx) : 0;
xxDescription
TRUEnever evaluated
FALSEnever evaluated
yyDescription
TRUEnever evaluated
FALSEnever evaluated
0
142-
143 if (a < Q_PI / -2)
a < Q_PI / -2Description
TRUEnever evaluated
FALSEnever evaluated
0
144 a = a + Q_PI * 2;
never executed: a = a + Q_PI * 2;
0
145-
146 int dist = 0;-
147 int minv = minimum, maxv = maximum;-
148-
149 if (minimum < 0) {
minimum < 0Description
TRUEnever evaluated
FALSEnever evaluated
0
150 dist = -minimum;-
151 minv = 0;-
152 maxv = maximum + dist;-
153 }
never executed: end of block
0
154-
155 int r = maxv - minv;-
156 int v;-
157 if (wrapping)
wrappingDescription
TRUEnever evaluated
FALSEnever evaluated
0
158 v = (int)(0.5 + minv + r * (Q_PI * 3 / 2 - a) / (2 * Q_PI));
never executed: v = (int)(0.5 + minv + r * (Q_PI * 3 / 2 - a) / (2 * Q_PI));
0
159 else-
160 v = (int)(0.5 + minv + r* (Q_PI * 4 / 3 - a) / (Q_PI * 10 / 6));
never executed: v = (int)(0.5 + minv + r* (Q_PI * 4 / 3 - a) / (Q_PI * 10 / 6));
0
161-
162 if (dist > 0)
dist > 0Description
TRUEnever evaluated
FALSEnever evaluated
0
163 v -= dist;
never executed: v -= dist;
0
164-
165 return !invertedAppearance ? bound(v) : maximum - bound(v);
never executed: return !invertedAppearance ? bound(v) : maximum - bound(v);
!invertedAppearanceDescription
TRUEnever evaluated
FALSEnever evaluated
0
166}-
167-
168/*!-
169 \class QDial-
170-
171 \brief The QDial class provides a rounded range control (like a speedometer or potentiometer).-
172-
173 \ingroup basicwidgets-
174 \inmodule QtWidgets-
175-
176 QDial is used when the user needs to control a value within a-
177 program-definable range, and the range either wraps around-
178 (for example, with angles measured from 0 to 359 degrees) or the-
179 dialog layout needs a square widget.-
180-
181 Since QDial inherits from QAbstractSlider, the dial behaves in-
182 a similar way to a \l{QSlider}{slider}. When wrapping() is false-
183 (the default setting) there is no real difference between a slider-
184 and a dial. They both share the same signals, slots and member-
185 functions. Which one you use depends on the expectations of-
186 your users and on the type of application.-
187-
188 The dial initially emits valueChanged() signals continuously while-
189 the slider is being moved; you can make it emit the signal less-
190 often by disabling the \l{QAbstractSlider::tracking} {tracking}-
191 property. The sliderMoved() signal is emitted continuously even-
192 when tracking is disabled.-
193-
194 The dial also emits sliderPressed() and sliderReleased() signals-
195 when the mouse button is pressed and released. Note that the-
196 dial's value can change without these signals being emitted since-
197 the keyboard and wheel can also be used to change the value.-
198-
199 Unlike the slider, QDial attempts to draw a "nice" number of-
200 notches rather than one per line step. If possible, the number of-
201 notches drawn is one per line step, but if there aren't enough pixels-
202 to draw every one, QDial will skip notches to try and draw a uniform-
203 set (e.g. by drawing every second or third notch).-
204-
205 Like the slider, the dial makes the QAbstractSlider function setValue()-
206 available as a slot.-
207-
208 The dial's keyboard interface is fairly simple: The-
209 \uicontrol{left}/\uicontrol{up} and \uicontrol{right}/\uicontrol{down} arrow keys adjust-
210 the dial's \l {QAbstractSlider::value} {value} by the defined-
211 \l {QAbstractSlider::singleStep} {singleStep}, \uicontrol{Page Up} and-
212 \uicontrol{Page Down} by the defined \l {QAbstractSlider::pageStep}-
213 {pageStep}, and the \uicontrol Home and \uicontrol End keys set the value to-
214 the defined \l {QAbstractSlider::minimum} {minimum} and-
215 \l {QAbstractSlider::maximum} {maximum} values.-
216-
217 If you are using the mouse wheel to adjust the dial, the increment-
218 value is determined by the lesser value of-
219 \l{QApplication::wheelScrollLines()} {wheelScrollLines} multipled-
220 by \l {QAbstractSlider::singleStep} {singleStep}, and-
221 \l {QAbstractSlider::pageStep} {pageStep}.-
222-
223 \table-
224 \row \li \inlineimage fusion-dial.png Screenshot of a dial in the Fusion widget style-
225 \li \inlineimage windowsvista-dial.png Screenshot of a dial in the Windows Vista widget style-
226 \li \inlineimage macintosh-dial.png Screenshot of a dial in the Macintosh widget style-
227 \row \li {3,1} Dials shown in various widget styles (from left to right):-
228 \l{Fusion Style Widget Gallery}{Fusion},-
229 \l{Windows Vista Style Widget Gallery}{Windows Vista},-
230 \l{Macintosh Style Widget Gallery}{Macintosh}.-
231 \endtable-
232-
233 \sa QScrollBar, QSpinBox, QSlider, {fowler}{GUI Design Handbook: Slider}, {Sliders Example}-
234*/-
235-
236/*!-
237 Constructs a dial.-
238-
239 The \a parent argument is sent to the QAbstractSlider constructor.-
240*/-
241QDial::QDial(QWidget *parent)-
242 : QAbstractSlider(*new QDialPrivate, parent)-
243{-
244 Q_D(QDial);-
245 d->init();-
246}
never executed: end of block
0
247-
248/*!-
249 Destroys the dial.-
250*/-
251QDial::~QDial()-
252{-
253}-
254-
255/*! \reimp */-
256void QDial::resizeEvent(QResizeEvent *e)-
257{-
258 QWidget::resizeEvent(e);-
259}
never executed: end of block
0
260-
261/*!-
262 \reimp-
263*/-
264-
265void QDial::paintEvent(QPaintEvent *)-
266{-
267 QStylePainter p(this);-
268 QStyleOptionSlider option;-
269 initStyleOption(&option);-
270 p.drawComplexControl(QStyle::CC_Dial, option);-
271}
never executed: end of block
0
272-
273/*!-
274 \reimp-
275*/-
276-
277void QDial::mousePressEvent(QMouseEvent *e)-
278{-
279 Q_D(QDial);-
280 if (d->maximum == d->minimum ||
d->maximum == d->minimumDescription
TRUEnever evaluated
FALSEnever evaluated
0
281 (e->button() != Qt::LeftButton) ||
(e->button() !...t::LeftButton)Description
TRUEnever evaluated
FALSEnever evaluated
0
282 (e->buttons() ^ e->button())) {
(e->buttons() ^ e->button())Description
TRUEnever evaluated
FALSEnever evaluated
0
283 e->ignore();-
284 return;
never executed: return;
0
285 }-
286 e->accept();-
287 setSliderPosition(d->valueFromPoint(e->pos()));-
288 // ### This isn't quite right,-
289 // we should be doing a hit test and only setting this if it's-
290 // the actual dial thingie (similar to what QSlider does), but we have no-
291 // subControls for QDial.-
292 setSliderDown(true);-
293}
never executed: end of block
0
294-
295-
296/*!-
297 \reimp-
298*/-
299-
300void QDial::mouseReleaseEvent(QMouseEvent * e)-
301{-
302 Q_D(QDial);-
303 if (e->buttons() & (~e->button()) ||
e->buttons() & (~e->button())Description
TRUEnever evaluated
FALSEnever evaluated
0
304 (e->button() != Qt::LeftButton)) {
(e->button() !...t::LeftButton)Description
TRUEnever evaluated
FALSEnever evaluated
0
305 e->ignore();-
306 return;
never executed: return;
0
307 }-
308 e->accept();-
309 setValue(d->valueFromPoint(e->pos()));-
310 setSliderDown(false);-
311}
never executed: end of block
0
312-
313-
314/*!-
315 \reimp-
316*/-
317-
318void QDial::mouseMoveEvent(QMouseEvent * e)-
319{-
320 Q_D(QDial);-
321 if (!(e->buttons() & Qt::LeftButton)) {
!(e->buttons()...t::LeftButton)Description
TRUEnever evaluated
FALSEnever evaluated
0
322 e->ignore();-
323 return;
never executed: return;
0
324 }-
325 e->accept();-
326 d->doNotEmit = true;-
327 setSliderPosition(d->valueFromPoint(e->pos()));-
328 d->doNotEmit = false;-
329}
never executed: end of block
0
330-
331-
332/*!-
333 \reimp-
334*/-
335-
336void QDial::sliderChange(SliderChange change)-
337{-
338 QAbstractSlider::sliderChange(change);-
339}
never executed: end of block
0
340-
341void QDial::setWrapping(bool enable)-
342{-
343 Q_D(QDial);-
344 if (d->wrapping == enable)
d->wrapping == enableDescription
TRUEnever evaluated
FALSEnever evaluated
0
345 return;
never executed: return;
0
346 d->wrapping = enable;-
347 update();-
348}
never executed: end of block
0
349-
350-
351/*!-
352 \property QDial::wrapping-
353 \brief whether wrapping is enabled-
354-
355 If true, wrapping is enabled; otherwise some space is inserted at the bottom-
356 of the dial to separate the ends of the range of valid values.-
357-
358 If enabled, the arrow can be oriented at any angle on the dial. If disabled,-
359 the arrow will be restricted to the upper part of the dial; if it is rotated-
360 into the space at the bottom of the dial, it will be clamped to the closest-
361 end of the valid range of values.-
362-
363 By default this property is \c false.-
364*/-
365-
366bool QDial::wrapping() const-
367{-
368 Q_D(const QDial);-
369 return d->wrapping;
never executed: return d->wrapping;
0
370}-
371-
372-
373/*!-
374 \property QDial::notchSize-
375 \brief the current notch size-
376-
377 The notch size is in range control units, not pixels, and if-
378 possible it is a multiple of singleStep() that results in an-
379 on-screen notch size near notchTarget().-
380-
381 By default, this property has a value of 1.-
382-
383 \sa notchTarget(), singleStep()-
384*/-
385-
386int QDial::notchSize() const-
387{-
388 Q_D(const QDial);-
389 // radius of the arc-
390 int r = qMin(width(), height())/2;-
391 // length of the whole arc-
392 int l = (int)(r * (d->wrapping ? 6 : 5) * Q_PI / 6);-
393 // length of the arc from minValue() to minValue()+pageStep()-
394 if (d->maximum > d->minimum + d->pageStep)
d->maximum > d... + d->pageStepDescription
TRUEnever evaluated
FALSEnever evaluated
0
395 l = (int)(0.5 + l * d->pageStep / (d->maximum - d->minimum));
never executed: l = (int)(0.5 + l * d->pageStep / (d->maximum - d->minimum));
0
396 // length of a singleStep arc-
397 l = l * d->singleStep / (d->pageStep ? d->pageStep : 1);
d->pageStepDescription
TRUEnever evaluated
FALSEnever evaluated
0
398 if (l < 1)
l < 1Description
TRUEnever evaluated
FALSEnever evaluated
0
399 l = 1;
never executed: l = 1;
0
400 // how many times singleStep can be draw in d->target pixels-
401 l = (int)(0.5 + d->target / l);-
402 // we want notchSize() to be a non-zero multiple of lineStep()-
403 if (!l)
!lDescription
TRUEnever evaluated
FALSEnever evaluated
0
404 l = 1;
never executed: l = 1;
0
405 return d->singleStep * l;
never executed: return d->singleStep * l;
0
406}-
407-
408void QDial::setNotchTarget(double target)-
409{-
410 Q_D(QDial);-
411 d->target = target;-
412 update();-
413}
never executed: end of block
0
414-
415/*!-
416 \property QDial::notchTarget-
417 \brief the target number of pixels between notches-
418-
419 The notch target is the number of pixels QDial attempts to put-
420 between each notch.-
421-
422 The actual size may differ from the target size.-
423-
424 The default notch target is 3.7 pixels.-
425*/-
426qreal QDial::notchTarget() const-
427{-
428 Q_D(const QDial);-
429 return d->target;
never executed: return d->target;
0
430}-
431-
432-
433void QDial::setNotchesVisible(bool visible)-
434{-
435 Q_D(QDial);-
436 d->showNotches = visible;-
437 update();-
438}
never executed: end of block
0
439-
440/*!-
441 \property QDial::notchesVisible-
442 \brief whether the notches are shown-
443-
444 If the property is \c true, a series of notches are drawn around the dial-
445 to indicate the range of values available; otherwise no notches are-
446 shown.-
447-
448 By default, this property is disabled.-
449*/-
450bool QDial::notchesVisible() const-
451{-
452 Q_D(const QDial);-
453 return d->showNotches;
never executed: return d->showNotches;
0
454}-
455-
456/*!-
457 \reimp-
458*/-
459-
460QSize QDial::minimumSizeHint() const-
461{-
462 return QSize(50, 50);
never executed: return QSize(50, 50);
0
463}-
464-
465/*!-
466 \reimp-
467*/-
468-
469QSize QDial::sizeHint() const-
470{-
471 return QSize(100, 100).expandedTo(QApplication::globalStrut());
never executed: return QSize(100, 100).expandedTo(QApplication::globalStrut());
0
472}-
473-
474/*!-
475 \reimp-
476*/-
477bool QDial::event(QEvent *e)-
478{-
479 return QAbstractSlider::event(e);
never executed: return QAbstractSlider::event(e);
0
480}-
481-
482QT_END_NAMESPACE-
483-
484#include "moc_qdial.cpp"-
485-
486#endif // QT_NO_DIAL-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9