OpenCoverage

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

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