Debian Codegraph @

src/customdial.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2019 Mario Stephan <[email protected]>
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "customdial.h"
19
20 #include <QColor>
21 #include <QPainter>
22
23 #include <cmath>
24
25 CustomDial::CustomDial(QWidget* parent)
26 : QDial(parent)
27 {
28 qDebug() << Q_FUNC_INFO << "START";
29 // Defaults
30 QDial::setRange(0, 360);
31 }
32
33 void CustomDial::paintEvent(QPaintEvent*)
34 {
35 static const double degree270 = 1.5 * M_PI;
36 static const double degree225 = 1.25 * M_PI;
37 static const int scaleDiameter = static_cast<int>(round((this->height() * 0.8) / 2) * 2);
38 static const int scaleOffset = static_cast<int>((this->height() - scaleDiameter) / 2);
39 static const int dialDiameter = static_cast<int>(round((this->height() * 0.65) / 2) * 2);
40 static const int dialOffset = static_cast<int>((this->height() - dialDiameter) / 2);
41
42 QPainter painter(this);
43
44 // Smooth out the circle
45 painter.setRenderHint(QPainter::Antialiasing);
46
47 // Gradient for dial
48 QRadialGradient radialGrad(QPointF(dialOffset, dialOffset), dialDiameter);
49 radialGrad.setColorAt(0.1, "#999");
50 radialGrad.setColorAt(0.5, "#777");
51 radialGrad.setColorAt(1, "#222");
52
53 QBrush brush(radialGrad);
54 painter.setBrush(brush);
55 QPen pen;
56 pen.setStyle(Qt::NoPen);
57 painter.setPen(pen);
58
59 // Draw dial
60 painter.drawEllipse(dialOffset, dialOffset, dialDiameter, dialDiameter);
61
62 // Draw black border of dial
63 painter.setBrush(Qt::NoBrush);
64 painter.setPen(QPen(QColor("#333"), 3));
65 painter.drawEllipse(dialOffset, dialOffset, dialDiameter, dialDiameter);
66
67 // Draw scale arc
68 int startAngle = 225 * 16;
69 int spanAngle = -270 * 16;
70 QRectF rectScale(scaleOffset, scaleOffset, scaleDiameter, scaleDiameter);
71 painter.setPen(QPen(QColor("#444"), 6));
72 painter.drawArc(rectScale, startAngle, spanAngle);
73 painter.setPen(QPen(QColor("#777"), 2));
74 painter.drawArc(rectScale, startAngle, spanAngle);
75
76 // Calc notch position
77 double ratio = static_cast<double>(QDial::value()) / QDial::maximum();
78 double angle = ratio * degree270 - degree225;
79 double r = dialDiameter / 2.0;
80 double y = sin(angle) * (r - dialOffset / 2) + r + dialOffset;
81 double x = cos(angle) * (r - dialOffset / 2) + r + dialOffset;
82 double c = r + dialOffset;
83
84 // Draw the notch
85 painter.setPen(QPen(QColor("#ccc"), 2));
86 painter.drawLine(QPointF(x, y), QPointF(c, c));
87 }