Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdial.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qdial.h"
5
6#include <qapplication.h>
7#include <qbitmap.h>
8#include <qcolor.h>
9#include <qevent.h>
10#include <qpainter.h>
11#include <qpolygon.h>
12#include <qregion.h>
13#include <qstyle.h>
14#include <qstylepainter.h>
15#include <qstyleoption.h>
16#include <qslider.h>
17#include <private/qabstractslider_p.h>
18#include <private/qmath_p.h>
19#if QT_CONFIG(accessibility)
20#include "qaccessible.h"
21#endif
22#include <qmath.h>
23
25
27{
28 Q_DECLARE_PUBLIC(QDial)
29public:
31 {
32 wrapping = false;
33 tracking = true;
34 doNotEmit = false;
35 target = qreal(3.7);
36 }
37
42
43 int valueFromPoint(const QPoint &) const;
44 double angle(const QPoint &, const QPoint &) const;
45 void init();
46 virtual int bound(int val) const override;
47};
48
50{
51 Q_Q(QDial);
52 showNotches = false;
53 q->setFocusPolicy(Qt::WheelFocus);
54}
55
57{
58 if (wrapping) {
59 if ((val >= minimum) && (val <= maximum))
60 return val;
61 if (minimum == maximum)
62 return minimum;
63 val = minimum + ((val - minimum) % (maximum - minimum));
64 if (val < minimum)
65 val += maximum - minimum;
66 return val;
67 } else {
69 }
70}
71
79void QDial::initStyleOption(QStyleOptionSlider *option) const
80{
81 if (!option)
82 return;
83
84 Q_D(const QDial);
85 option->initFrom(this);
86 option->minimum = d->minimum;
87 option->maximum = d->maximum;
88 option->sliderPosition = d->position;
89 option->sliderValue = d->value;
90 option->singleStep = d->singleStep;
91 option->pageStep = d->pageStep;
92 option->upsideDown = !d->invertedAppearance;
93 option->notchTarget = d->target;
94 option->dialWrapping = d->wrapping;
95 option->subControls = QStyle::SC_All;
96 option->activeSubControls = QStyle::SC_None;
97 if (!d->showNotches) {
98 option->subControls &= ~QStyle::SC_DialTickmarks;
99 option->tickPosition = QSlider::TicksAbove;
100 } else {
101 option->tickPosition = QSlider::NoTicks;
102 }
103 option->tickInterval = notchSize();
104}
105
107{
108 Q_Q(const QDial);
109 double yy = q->height()/2.0 - p.y();
110 double xx = p.x() - q->width()/2.0;
111 double a = (xx || yy) ? std::atan2(yy, xx) : 0;
112
113 if (a < Q_PI / -2)
114 a = a + Q_PI * 2;
115
116 int dist = 0;
117 int minv = minimum, maxv = maximum;
118
119 if (minimum < 0) {
120 dist = -minimum;
121 minv = 0;
122 maxv = maximum + dist;
123 }
124
125 int r = maxv - minv;
126 int v;
127 if (wrapping)
128 v = (int)(0.5 + minv + r * (Q_PI * 3 / 2 - a) / (2 * Q_PI));
129 else
130 v = (int)(0.5 + minv + r* (Q_PI * 4 / 3 - a) / (Q_PI * 10 / 6));
131
132 if (dist > 0)
133 v -= dist;
134
135 return !invertedAppearance ? bound(v) : maximum - bound(v);
136}
137
204 : QAbstractSlider(*new QDialPrivate, parent)
205{
206 Q_D(QDial);
207 d->init();
208}
209
214{
215}
216
222
228{
229 QStylePainter p(this);
230 QStyleOptionSlider option;
232 p.drawComplexControl(QStyle::CC_Dial, option);
233}
234
240{
241 Q_D(QDial);
242 if (d->maximum == d->minimum ||
243 (e->button() != Qt::LeftButton) ||
244 (e->buttons() ^ e->button())) {
245 e->ignore();
246 return;
247 }
248 e->accept();
249 setSliderPosition(d->valueFromPoint(e->position().toPoint()));
250 // ### This isn't quite right,
251 // we should be doing a hit test and only setting this if it's
252 // the actual dial thingie (similar to what QSlider does), but we have no
253 // subControls for QDial.
254 setSliderDown(true);
255}
256
257
263{
264 Q_D(QDial);
265 if (e->buttons() & (~e->button()) ||
266 (e->button() != Qt::LeftButton)) {
267 e->ignore();
268 return;
269 }
270 e->accept();
271 setValue(d->valueFromPoint(e->position().toPoint()));
272 setSliderDown(false);
273}
274
275
281{
282 Q_D(QDial);
283 if (!(e->buttons() & Qt::LeftButton)) {
284 e->ignore();
285 return;
286 }
287 e->accept();
288 d->doNotEmit = true;
289 setSliderPosition(d->valueFromPoint(e->position().toPoint()));
290 d->doNotEmit = false;
291}
292
293
302
304{
305 Q_D(QDial);
306 if (d->wrapping == enable)
307 return;
308 d->wrapping = enable;
309 update();
310}
311
312
328bool QDial::wrapping() const
329{
330 Q_D(const QDial);
331 return d->wrapping;
332}
333
334
347{
348 Q_D(const QDial);
349 // radius of the arc
350 qreal r = qMin(width(), height())/2.0;
351 // length of the whole arc
352 int l = qRound(r * (d->wrapping ? 6.0 : 5.0) * Q_PI / 6.0);
353 // length of the arc from minValue() to minValue()+pageStep()
354 if (d->maximum > d->minimum + d->pageStep)
355 l = qRound(l * d->pageStep / double(d->maximum - d->minimum));
356 // length of a singleStep arc
357 l = qMax(l * d->singleStep / (d->pageStep ? d->pageStep : 1), 1);
358 // how many times singleStep can be draw in d->target pixels
359 l = qMax(qRound(d->target / l), 1);
360 // we want notchSize() to be a non-zero multiple of singleStep()
361 return d->singleStep * l;
362}
363
365{
366 Q_D(QDial);
367 d->target = target;
368 update();
369}
370
383{
384 Q_D(const QDial);
385 return d->target;
386}
387
388
389void QDial::setNotchesVisible(bool visible)
390{
391 Q_D(QDial);
392 d->showNotches = visible;
393 update();
394}
395
407{
408 Q_D(const QDial);
409 return d->showNotches;
410}
411
417{
418 return QSize(50, 50);
419}
420
426{
427 return QSize(100, 100);
428}
429
434{
435 return QAbstractSlider::event(e);
436}
437
439
440#include "moc_qdial.cpp"
virtual int bound(int val) const
The QAbstractSlider class provides an integer value within a range.
virtual void sliderChange(SliderChange change)
Reimplement this virtual function to track slider changes such as \l SliderRangeChange,...
bool event(QEvent *e) override
\reimp
void setSliderPosition(int)
SliderChange
\value SliderRangeChange \value SliderOrientationChange \value SliderStepsChange \value SliderValueCh...
uint wrapping
Definition qdial.cpp:40
uint doNotEmit
Definition qdial.cpp:41
virtual int bound(int val) const override
Definition qdial.cpp:56
void init()
Definition qdial.cpp:49
qreal target
Definition qdial.cpp:38
uint showNotches
Definition qdial.cpp:39
double angle(const QPoint &, const QPoint &) const
int valueFromPoint(const QPoint &) const
Definition qdial.cpp:106
The QDial class provides a rounded range control (like a speedometer or potentiometer).
Definition qdial.h:20
void mousePressEvent(QMouseEvent *me) override
\reimp
Definition qdial.cpp:239
void setNotchesVisible(bool visible)
Definition qdial.cpp:389
bool notchesVisible
whether the notches are shown
Definition qdial.h:26
bool wrapping
whether wrapping is enabled
Definition qdial.h:23
QDial(QWidget *parent=nullptr)
Constructs a dial.
Definition qdial.cpp:203
bool event(QEvent *e) override
\reimp
Definition qdial.cpp:433
void mouseMoveEvent(QMouseEvent *me) override
\reimp
Definition qdial.cpp:280
void sliderChange(SliderChange change) override
\reimp
Definition qdial.cpp:298
void setNotchTarget(double target)
Definition qdial.cpp:364
void setWrapping(bool on)
Definition qdial.cpp:303
void resizeEvent(QResizeEvent *re) override
\reimp
Definition qdial.cpp:218
void paintEvent(QPaintEvent *pe) override
\reimp
Definition qdial.cpp:227
int notchSize
the current notch size
Definition qdial.h:24
~QDial()
Destroys the dial.
Definition qdial.cpp:213
void mouseReleaseEvent(QMouseEvent *me) override
\reimp
Definition qdial.cpp:262
QSize minimumSizeHint() const override
\reimp
Definition qdial.cpp:416
virtual void initStyleOption(QStyleOptionSlider *option) const
Initialize option with the values from this QDial.
Definition qdial.cpp:79
QSize sizeHint() const override
\reimp
Definition qdial.cpp:425
qreal notchTarget
the target number of pixels between notches
Definition qdial.h:25
\inmodule QtCore
Definition qcoreevent.h:45
void ignore()
Clears the accept flag parameter of the event object, the equivalent of calling setAccepted(false).
Definition qcoreevent.h:311
void accept()
Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
Definition qcoreevent.h:310
\inmodule QtGui
Definition qevent.h:196
The QPaintEvent class contains event parameters for paint events.
Definition qevent.h:486
constexpr QPoint toPoint() const
Rounds the coordinates of this point to the nearest integer, and returns a QPoint object with the rou...
Definition qpoint.h:404
\inmodule QtCore\reentrant
Definition qpoint.h:25
The QResizeEvent class contains event parameters for resize events.
Definition qevent.h:548
QPointF position() const
Returns the position of the point in this event, relative to the widget or item that received the eve...
Definition qevent.h:119
Qt::MouseButton button() const
Returns the button that caused the event.
Definition qevent.h:116
Qt::MouseButtons buttons() const
Returns the button state when the event was generated.
Definition qevent.h:117
\inmodule QtCore
Definition qsize.h:25
@ TicksAbove
Definition qslider.h:27
@ NoTicks
Definition qslider.h:26
The QStylePainter class is a convenience class for drawing QStyle elements inside a widget.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI.
Definition qstyle.h:29
@ CC_Dial
Definition qstyle.h:338
@ SC_All
Definition qstyle.h:400
@ SC_None
Definition qstyle.h:348
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
int width
the width of the widget excluding any window frame
Definition qwidget.h:114
int height
the height of the widget excluding any window frame
Definition qwidget.h:115
void update()
Updates the widget unless updates are disabled or the widget is hidden.
virtual void resizeEvent(QResizeEvent *event)
This event handler can be reimplemented in a subclass to receive widget resize events which are passe...
Definition qwidget.cpp:9822
bool visible
whether the widget is visible
Definition qwidget.h:144
Combined button and popup list for selecting options.
@ LeftButton
Definition qnamespace.h:58
@ WheelFocus
Definition qnamespace.h:111
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
static QT_BEGIN_NAMESPACE const qreal Q_PI
Definition qmath_p.h:24
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLsizei const GLfloat * v
[13]
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLenum target
GLboolean enable
GLuint GLfloat * val
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLfloat GLfloat p
[1]
GLuint GLenum option
unsigned int uint
Definition qtypes.h:34
double qreal
Definition qtypes.h:187
std::uniform_real_distribution dist(1, 2.5)
[2]