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