Line data Source code
1 : #include "../include/autonomy.h"
2 :
3 : //constructor
4 8 : Autonomy::Autonomy(QWidget *parent)
5 8 : : QWidget{parent}
6 : {
7 8 : label = new QLabel(this);
8 8 : main_layout = new QVBoxLayout(this);
9 8 : layout = new QHBoxLayout();
10 :
11 8 : label->setAlignment(Qt::AlignCenter);
12 8 : label->setMinimumWidth(120);
13 8 : main_layout->setSpacing(6);
14 8 : main_layout->setContentsMargins(0, 0, 0, 0); // Remove margins
15 :
16 8 : layout->setSpacing(2);
17 8 : layout->setContentsMargins(0, 0, 0, 0);
18 :
19 8 : nb_sections = 6;
20 56 : for (int i = 0; i < nb_sections; ++i)
21 : {
22 48 : QWidget *section = new QWidget(this);
23 48 : section->setFixedSize(22, 32);
24 48 : layout->addWidget(section);
25 48 : sections.append(section);
26 : }
27 :
28 8 : main_layout->addLayout(layout);
29 8 : set_autonomy(7);
30 8 : main_layout->addWidget(label); // Add label to the layout
31 8 : }
32 :
33 : //destructor
34 16 : Autonomy::~Autonomy()
35 : {
36 8 : std::cout << "Remove Autonomy" << std::endl;
37 16 : }
38 :
39 : //setter
40 9 : void Autonomy::set_autonomy(int aut)
41 : {
42 9 : autonomy = aut;
43 9 : int sections_color = std::max(1, static_cast<int>(std::ceil((aut / 10.0) * nb_sections)));
44 63 : for (int i = nb_sections -1; i >= 0; i--)
45 : {
46 54 : if (i >= nb_sections - sections_color)
47 : {
48 45 : QColor endColor;
49 45 : QColor startColor(0, 80, 60); // dark cyan-green
50 :
51 45 : if (aut >= 5)
52 : {
53 45 : endColor = QColor(0, 60, 40); // slightly brighter cyan-green
54 : }
55 : else
56 : {
57 0 : startColor = QColor(80, 20, 0);
58 0 : endColor = QColor(50, 10, 0);
59 : }
60 : // Interpolate based on **active section index** (0 = leftmost active)
61 45 : int activeIndex = i - (nb_sections - sections_color); // 0..sections_active-1
62 45 : float t = (sections_color > 1) ? float(activeIndex) / (sections_color - 1) : 0.0f; // 0..1
63 :
64 45 : int red = startColor.red() + t * (endColor.red() - startColor.red());
65 45 : int green = startColor.green() + t * (endColor.green() - startColor.green());
66 45 : int blue = startColor.blue() + t * (endColor.blue() - startColor.blue());
67 :
68 45 : QColor section_color(red, green, blue); // correct: QColor object
69 45 : sections[i]->setStyleSheet(QString("background-color: %1").arg(section_color.name()));
70 :
71 :
72 : } else
73 : {
74 9 : QColor inactive_color(22, 32, 60);
75 9 : sections[i]->setStyleSheet(QString("background-color: %1").arg(inactive_color.name()));
76 : }
77 : }
78 9 : label->setTextFormat(Qt::RichText); // Enable rich text
79 9 : label->setText("<span style='font-family: Digital-7; font-size: 30px;'>" + QString::number(aut) +
80 : "</span><span style='font-family: Calculator; font-size: 30px;'> km</span>");
81 9 : label->setStyleSheet("color: rgb(0, 120, 140);");
82 9 : label->setAlignment(Qt::AlignRight);
83 9 : label->setContentsMargins(0, 0, 5, 0);
84 9 : }
85 :
86 : // getters
87 1 : int Autonomy::get_nbsections()
88 : {
89 1 : return nb_sections;
90 : }
91 :
92 1 : int Autonomy::get_autonomy()
93 : {
94 1 : return autonomy;
95 : }
96 :
97 1 : QVector<QWidget*> Autonomy::get_sections()
98 : {
99 1 : return sections;
100 : }
101 :
102 1 : QHBoxLayout* Autonomy::get_layout()
103 : {
104 1 : return layout;
105 : }
106 :
107 1 : QVBoxLayout* Autonomy::get_mainlayout()
108 : {
109 1 : return main_layout;
110 : }
111 :
112 1 : QLabel* Autonomy::get_label()
113 : {
114 1 : return label;
115 : }
|