Line data Source code
1 : #include "../include/speed.h"
2 :
3 9 : Speed::Speed(QWidget *parent)
4 9 : : QWidget(parent), max(10), current_angle(70), target_angle(75), current(6)
5 : {
6 9 : if (parent) {
7 7 : setMinimumSize(parent->width() * 0.5, parent->height() * 0.7);
8 7 : setMaximumSize(parent->width() * 0.5, parent->height() * 0.7);
9 : }
10 9 : QString path = QCoreApplication::applicationDirPath();
11 9 : QString digital_path = QDir(path).filePath("../fonts_icon/digital-7.ttf");
12 9 : digital_path = QDir::cleanPath(digital_path);
13 9 : QString calculator_path = QDir(path).filePath("../fonts_icon/Calculator.ttf");
14 9 : calculator_path = QDir::cleanPath(calculator_path);
15 9 : font_id = font.addApplicationFont(digital_path);
16 9 : font_id2 = font2.addApplicationFont(calculator_path);
17 9 : is_animating = false;
18 18 : }
19 :
20 : //fading
21 58 : void Speed::paintEvent(QPaintEvent *event) {
22 58 : QPainter painter(this);
23 58 : painter.setRenderHint(QPainter::Antialiasing);
24 58 : int radius = qMin(width(), height()) / 2.5;
25 58 : int centerX = width() / 2;
26 58 : int centerY = height() / 2;
27 58 : int segments = 400;
28 58 : float segment_angle = 270.0f / segments;
29 23258 : for (int i = 0; i < segments; ++i) {
30 23200 : float t = static_cast<float>(i) / (segments);
31 23200 : int alpha = static_cast<int>(120 * (1 - std::abs(2 * t - 1)));
32 23200 : QColor color(0, 40, 60, alpha);
33 23200 : QPen pen(color, std::max(1, width() / 50));
34 23200 : painter.setPen(pen);
35 23200 : float overlap = 1.1f;
36 23200 : painter.drawArc(centerX - radius, centerY - radius, radius * 2, radius * 2,
37 23200 : (270 - i * segment_angle) * 16, -segment_angle * 16 * overlap);
38 23200 : }
39 :
40 58 : QColor start_color(0, 70, 90);
41 58 : QColor end_color(0, 255, 255);
42 58 : int active_segments = static_cast<int>((current_angle / 270.0f) * segments);
43 2797 : for (int i = 0; i < active_segments; ++i) {
44 2739 : float t = static_cast<float>(i) / segments;
45 2739 : QColor color = QColor::fromRgbF(
46 2739 : (1 - t) * start_color.redF() + t * end_color.redF(),
47 2739 : (1 - t) * start_color.greenF() + t * end_color.greenF(),
48 2739 : (1 - t) * start_color.blueF() + t * end_color.blueF()
49 8217 : );
50 2739 : int alpha = static_cast<int>(255 * (1 - std::abs(2 * t - 1)));
51 2739 : color.setAlpha(alpha);
52 2739 : QPen pen(color, std::max(1, width() / 50));
53 2739 : painter.setPen(pen);
54 2739 : painter.drawArc(centerX - radius, centerY - radius, radius * 2, radius * 2, (270 - i * segment_angle) * 16, -segment_angle * 16);
55 2739 : }
56 58 : paint_text(painter);
57 116 : }
58 :
59 58 : void Speed::paint_text(QPainter &painter) {
60 58 : painter.setPen(QPen(Qt::cyan));
61 58 : painter.setFont(QFont("Digital-7", width() / 5, QFont::Bold));
62 58 : QRect currentTextRect = painter.boundingRect(rect(), Qt::AlignCenter, QString::number(current));
63 58 : painter.drawText(currentTextRect, Qt::AlignCenter, QString::number(current));
64 58 : painter.setPen(QPen(Qt::darkCyan));
65 58 : painter.setFont(QFont("Calculator", width() / 14));
66 58 : QRect kmhRect = painter.boundingRect(rect(), Qt::AlignCenter, "km/h");
67 58 : int yPosition = currentTextRect.bottom() + kmhRect.height();
68 58 : int xPosition = (width() - kmhRect.width()) / 2;
69 58 : painter.drawText(QPoint(xPosition, yPosition ), "km/h");
70 58 : }
71 :
72 10 : void Speed::set_current(float n) {
73 10 : current = n * 3.6;
74 10 : float new_target = (current * 270.0f) / max;
75 10 : new_target = std::min(new_target, 270.0f);
76 10 : if (std::abs(new_target - target_angle) > 1.0f) {
77 5 : target_angle = new_target;
78 5 : if (is_animating == false)
79 5 : animation();
80 : }
81 10 : }
82 :
83 5 : void Speed::animation() {
84 5 : is_animating = true;
85 5 : QTimer *timer = new QTimer(this);
86 5 : connect(timer, &QTimer::timeout, this, [this, timer]() {
87 82 : float step = std::max(1.0f, std::abs(target_angle - current_angle) / 20.0f); // Adjust step dynamically
88 82 : if (current_angle < target_angle) {
89 13 : current_angle = std::min(current_angle + step, target_angle);
90 69 : } else if (current_angle > target_angle) {
91 68 : current_angle = std::max(current_angle - step, target_angle);
92 : } else {
93 1 : timer->stop();
94 1 : timer->deleteLater();
95 1 : is_animating = false;
96 : }
97 82 : update();
98 82 : });
99 5 : timer->start(16);
100 5 : }
101 :
102 1 : bool Speed::get_is_animating() {
103 1 : return is_animating;
104 : }
105 :
106 2 : float Speed::get_current() {
107 2 : return current;
108 : }
109 :
110 1 : float Speed::get_current_angle() {
111 1 : return current_angle;
112 : }
113 :
114 1 : float Speed::get_target_angle() {
115 1 : return target_angle;
116 : }
117 :
118 1 : int Speed::get_max() {
119 1 : return max;
120 : }
121 :
122 18 : Speed::~Speed() {
123 9 : QFontDatabase::removeApplicationFont(font_id);
124 9 : QFontDatabase::removeApplicationFont(font_id2);
125 9 : std::cout << "Remove Speed" << std::endl;
126 18 : }
|