| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: http://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
| 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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPLv3 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.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 later as published by the Free |
| 28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
| 29 | ** the packaging of this file. Please review the following information to |
| 30 | ** ensure the GNU General Public License version 2.0 requirements will be |
| 31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
| 32 | ** |
| 33 | ** $QT_END_LICENSE$ |
| 34 | ** |
| 35 | ****************************************************************************/ |
| 36 | |
| 37 | #include "qquickdefaultdial_p.h" |
| 38 | |
| 39 | #include <QtCore/qmath.h> |
| 40 | #include <QtGui/qpainter.h> |
| 41 | #include <QtQuick/private/qquickitem_p.h> |
| 42 | |
| 43 | QT_BEGIN_NAMESPACE |
| 44 | |
| 45 | QQuickDefaultDial::QQuickDefaultDial(QQuickItem *parent) : |
| 46 | QQuickPaintedItem(parent) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | qreal QQuickDefaultDial::progress() const |
| 51 | { |
| 52 | return m_progress; |
| 53 | } |
| 54 | |
| 55 | void QQuickDefaultDial::setProgress(qreal progress) |
| 56 | { |
| 57 | if (progress == m_progress) |
| 58 | return; |
| 59 | |
| 60 | m_progress = progress; |
| 61 | update(); |
| 62 | } |
| 63 | |
| 64 | QColor QQuickDefaultDial::color() const |
| 65 | { |
| 66 | return m_color; |
| 67 | } |
| 68 | |
| 69 | void QQuickDefaultDial::setColor(const QColor &color) |
| 70 | { |
| 71 | if (color == m_color) |
| 72 | return; |
| 73 | |
| 74 | m_color = color; |
| 75 | update(); |
| 76 | } |
| 77 | |
| 78 | void QQuickDefaultDial::paint(QPainter *painter) |
| 79 | { |
| 80 | if (width() <= 0 || height() <= 0) |
| 81 | return; |
| 82 | |
| 83 | QPen pen(m_color); |
| 84 | pen.setWidth(8); |
| 85 | pen.setCapStyle(Qt::FlatCap); |
| 86 | painter->setPen(pen); |
| 87 | |
| 88 | const QRectF bounds = boundingRect(); |
| 89 | const qreal smallest = qMin(a: bounds.width(), b: bounds.height()); |
| 90 | QRectF rect = QRectF(pen.widthF() / 2.0 + 1, pen.widthF() / 2.0 + 1, smallest - pen.widthF() - 2, smallest - pen.widthF() - 2); |
| 91 | rect.moveCenter(p: bounds.center()); |
| 92 | |
| 93 | // Make sure the arc is aligned to whole pixels. |
| 94 | if (rect.x() - int(rect.x()) > 0) |
| 95 | rect.setX(qCeil(v: rect.x())); |
| 96 | if (rect.y() - int(rect.y()) > 0) |
| 97 | rect.setY(qCeil(v: rect.y())); |
| 98 | if (rect.width() - int(rect.width()) > 0) |
| 99 | rect.setWidth(qFloor(v: rect.width())); |
| 100 | if (rect.height() - int(rect.height()) > 0) |
| 101 | rect.setHeight(qFloor(v: rect.height())); |
| 102 | |
| 103 | painter->setRenderHint(hint: QPainter::Antialiasing); |
| 104 | |
| 105 | const qreal startAngle = (140 + 90); |
| 106 | const qreal spanAngle = (m_progress * 280) * -1; |
| 107 | QPainterPath path; |
| 108 | path.arcMoveTo(rect, angle: startAngle); |
| 109 | path.arcTo(rect, startAngle, arcLength: spanAngle); |
| 110 | painter->drawPath(path); |
| 111 | |
| 112 | rect.adjust(xp1: -pen.widthF() / 2.0, yp1: -pen.widthF() / 2.0, xp2: pen.widthF() / 2.0, yp2: pen.widthF() / 2.0); |
| 113 | pen.setWidth(1); |
| 114 | painter->setPen(pen); |
| 115 | |
| 116 | path = QPainterPath(); |
| 117 | path.arcMoveTo(rect, angle: 0); |
| 118 | path.arcTo(rect, startAngle: 0, arcLength: 360); |
| 119 | painter->drawPath(path); |
| 120 | } |
| 121 | |
| 122 | QT_END_NAMESPACE |
| 123 | |