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// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qmath.h>
8#include <QtGui/qpainter.h>
9#include <QtGui/qpainterpath.h>
10#include <QtQuick/private/qquickitem_p.h>
11
13
14QQuickBasicDial::QQuickBasicDial(QQuickItem *parent) :
16{
17}
18
19qreal QQuickBasicDial::progress() const
20{
21 return m_progress;
22}
23
24void QQuickBasicDial::setProgress(qreal progress)
25{
26 if (progress == m_progress)
27 return;
28
29 m_progress = progress;
30 update();
31}
32
33qreal QQuickBasicDial::startAngle() const
34{
35 return m_startAngle;
36}
37
38void QQuickBasicDial::setStartAngle(qreal startAngle)
39{
40 if (startAngle == m_startAngle)
41 return;
42
43 m_startAngle = startAngle;
44 update();
45}
46
47qreal QQuickBasicDial::endAngle() const
48{
49 return m_endAngle;
50}
51
52void QQuickBasicDial::setEndAngle(qreal endAngle)
53{
54 if (endAngle == m_endAngle)
55 return;
56
57 m_endAngle = endAngle;
58 update();
59}
60
61QColor QQuickBasicDial::color() const
62{
63 return m_color;
64}
65
66void QQuickBasicDial::setColor(const QColor &color)
67{
68 if (color == m_color)
69 return;
70
71 m_color = color;
72 update();
73}
74
75void QQuickBasicDial::paint(QPainter *painter)
76{
77 if (width() <= 0 || height() <= 0)
78 return;
79
80 QPen pen(m_color);
81 pen.setWidth(8);
82 pen.setCapStyle(Qt::FlatCap);
83 painter->setPen(pen);
84
85 const QRectF bounds = boundingRect();
86 const qreal smallest = qMin(bounds.width(), bounds.height());
87 QRectF rect = QRectF(pen.widthF() / 2.0 + 1, pen.widthF() / 2.0 + 1, smallest - pen.widthF() - 2, smallest - pen.widthF() - 2);
88 rect.moveCenter(bounds.center());
89
90 // Make sure the arc is aligned to whole pixels.
91 if (rect.x() - int(rect.x()) > 0)
92 rect.setX(qCeil(rect.x()));
93 if (rect.y() - int(rect.y()) > 0)
94 rect.setY(qCeil(rect.y()));
95 if (rect.width() - int(rect.width()) > 0)
96 rect.setWidth(qFloor(rect.width()));
97 if (rect.height() - int(rect.height()) > 0)
98 rect.setHeight(qFloor(rect.height()));
99
100 painter->setRenderHint(QPainter::Antialiasing);
101
102 const qreal startAngle = 90. - m_startAngle;
103 const qreal spanAngle = m_progress * (m_startAngle - m_endAngle);
104 QPainterPath path;
105 path.arcMoveTo(rect, startAngle);
106 path.arcTo(rect, startAngle, spanAngle);
107 painter->drawPath(path);
108
109 rect.adjust(-pen.widthF() / 2.0, -pen.widthF() / 2.0, pen.widthF() / 2.0, pen.widthF() / 2.0);
110 pen.setWidth(1);
111 painter->setPen(pen);
112
113 path = QPainterPath();
114 path.arcMoveTo(rect, 0);
115 path.arcTo(rect, 0, 360);
116 painter->drawPath(path);
117}
118
119QT_END_NAMESPACE
120
121#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:64