Line data Source code
1 : #include "../include/autonomy.h"
2 :
3 : //constructor
4 7 : Autonomy::Autonomy(QWidget *parent)
5 7 : : QWidget{parent}
6 : {
7 7 : if (parent)
8 : {
9 7 : setMinimumSize(parent->width() * 0.2, parent->height() * 0.16);
10 7 : setMaximumSize(parent->width() * 0.2, parent->height() * 0.16);
11 : }
12 7 : main_layout = new QVBoxLayout(this);
13 7 : main_layout->setSpacing(height() * 0.05);
14 7 : layout = new QHBoxLayout();
15 7 : layout->setSpacing(width() * 0.0155);
16 7 : nb_sections = 6;
17 49 : for (int i = 0; i < nb_sections; ++i)
18 : {
19 42 : QWidget *section = new QWidget(this);
20 42 : section->setFixedHeight(height() * 0.3);
21 42 : section->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
22 42 : layout->addWidget(section);
23 42 : sections.append(section);
24 : }
25 7 : main_layout->addLayout(layout);
26 7 : label = new QLabel(this);
27 7 : set_autonomy(8);
28 7 : }
29 :
30 : //destructor
31 14 : Autonomy::~Autonomy()
32 : {
33 7 : std::cout << "Remove Autonomy" << std::endl;
34 7 : delete layout;
35 14 : }
36 :
37 : //setter
38 7 : void Autonomy::set_autonomy(int aut)
39 : {
40 7 : int sections_color = static_cast<int>((aut / 10.0) * nb_sections);
41 49 : for (int i = nb_sections -1; i >= 0; i--)
42 : {
43 42 : if (i >= nb_sections - sections_color)
44 : {
45 28 : QColor section_color;
46 28 : if (aut > 6)
47 : {
48 28 : int red = std::max(0, 120 - ((nb_sections - 1 - i) * (255 / nb_sections)));
49 28 : int green = std::min(255, 80 + ((nb_sections - 1 - i) * (20 / nb_sections)));
50 28 : int blue = std::min(255, 50 + ((nb_sections - 1 - i) * (50 / nb_sections)));
51 28 : section_color.setRgb(red, green, blue);
52 : } else
53 : {
54 0 : int red = std::max(0, 120 - ((nb_sections - 1 - i) * (255 / nb_sections)));
55 0 : int green = std::min(255, 80 + ((nb_sections - 1 - i) * (20 / nb_sections)));
56 0 : int blue = std::min(255, 50 + ((nb_sections - 1 - i) * (50 / nb_sections)));
57 0 : section_color.setRgb(red, green, blue);
58 : }
59 28 : sections[i]->setStyleSheet(QString("background-color: %1").arg(section_color.name()));
60 : } else
61 : {
62 14 : QColor inactive_color(22, 32, 60);
63 14 : sections[i]->setStyleSheet(QString("background-color: %1").arg(inactive_color.name()));
64 : }
65 : }
66 7 : label->setTextFormat(Qt::RichText); // Enable rich text
67 7 : label->setText("<span style='font-family: Digital-7; font-size: 27px;'>" + QString::number(aut) +
68 : "</span><span style='font-family: Calculator; font-size: 27px;'> km</span>");
69 7 : label->setStyleSheet("color: rgb(0, 120, 140);");
70 7 : label->setAlignment(Qt::AlignTop | Qt::AlignRight);
71 7 : label->setContentsMargins(0, 0, 5, 0);
72 7 : main_layout->addWidget(label);
73 7 : }
74 :
75 : // getters
76 1 : int Autonomy::get_nbsections()
77 : {
78 1 : return nb_sections;
79 : }
80 :
81 1 : QVector<QWidget*> Autonomy::get_sections()
82 : {
83 1 : return sections;
84 : }
85 :
86 1 : QHBoxLayout* Autonomy::get_layout()
87 : {
88 1 : return layout;
89 : }
90 :
91 1 : QVBoxLayout* Autonomy::get_mainlayout()
92 : {
93 1 : return main_layout;
94 : }
95 :
96 1 : QLabel* Autonomy::get_label()
97 : {
98 1 : return label;
99 : }
|