Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickbasicdial.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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
5
6#include <QtCore/qmath.h>
7#include <QtGui/qpainter.h>
8#include <QtGui/qpainterpath.h>
9#include <QtQuick/private/qquickitem_p.h>
10
12
13QQuickBasicDial::QQuickBasicDial(QQuickItem *parent) :
15{
16}
17
18qreal QQuickBasicDial::progress() const
19{
20 return m_progress;
21}
22
23void QQuickBasicDial::setProgress(qreal progress)
24{
25 if (progress == m_progress)
26 return;
27
28 m_progress = progress;
29 update();
30}
31
32qreal QQuickBasicDial::startAngle() const
33{
34 return m_startAngle;
35}
36
37void QQuickBasicDial::setStartAngle(qreal startAngle)
38{
39 if (startAngle == m_startAngle)
40 return;
41
42 m_startAngle = startAngle;
43 update();
44}
45
46qreal QQuickBasicDial::endAngle() const
47{
48 return m_endAngle;
49}
50
51void QQuickBasicDial::setEndAngle(qreal endAngle)
52{
53 if (endAngle == m_endAngle)
54 return;
55
56 m_endAngle = endAngle;
57 update();
58}
59
60QColor QQuickBasicDial::color() const
61{
62 return m_color;
63}
64
65void QQuickBasicDial::setColor(const QColor &color)
66{
67 if (color == m_color)
68 return;
69
70 m_color = color;
71 update();
72}
73
74void QQuickBasicDial::paint(QPainter *painter)
75{
76 if (width() <= 0 || height() <= 0)
77 return;
78
79 QPen pen(m_color);
80 pen.setWidth(8);
81 pen.setCapStyle(Qt::FlatCap);
82 painter->setPen(pen);
83
84 const QRectF bounds = boundingRect();
85 const qreal smallest = qMin(bounds.width(), bounds.height());
86 QRectF rect = QRectF(pen.widthF() / 2.0 + 1, pen.widthF() / 2.0 + 1, smallest - pen.widthF() - 2, smallest - pen.widthF() - 2);
87 rect.moveCenter(bounds.center());
88
89 // Make sure the arc is aligned to whole pixels.
90 if (rect.x() - int(rect.x()) > 0)
91 rect.setX(qCeil(rect.x()));
92 if (rect.y() - int(rect.y()) > 0)
93 rect.setY(qCeil(rect.y()));
94 if (rect.width() - int(rect.width()) > 0)
95 rect.setWidth(qFloor(rect.width()));
96 if (rect.height() - int(rect.height()) > 0)
97 rect.setHeight(qFloor(rect.height()));
98
99 painter->setRenderHint(QPainter::Antialiasing);
100
101 const qreal startAngle = 90. - m_startAngle;
102 const qreal spanAngle = m_progress * (m_startAngle - m_endAngle);
103 QPainterPath path;
104 path.arcMoveTo(rect, startAngle);
105 path.arcTo(rect, startAngle, spanAngle);
106 painter->drawPath(path);
107
108 rect.adjust(-pen.widthF() / 2.0, -pen.widthF() / 2.0, pen.widthF() / 2.0, pen.widthF() / 2.0);
109 pen.setWidth(1);
110 painter->setPen(pen);
111
112 path = QPainterPath();
113 path.arcMoveTo(rect, 0);
114 path.arcTo(rect, 0, 360);
115 painter->drawPath(path);
116}
117
118QT_END_NAMESPACE
119
120#include "moc_qquickbasicdial_p.cpp"
QObject * parent
Definition qobject.h:73
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
Combined button and popup list for selecting options.