Line data Source code
1 : #include "../include/temperature.h"
2 :
3 8 : Temperature::Temperature(QWidget *parent)
4 8 : : QWidget{parent}
5 : {
6 8 : layout = new QHBoxLayout();
7 8 : main_layout = new QVBoxLayout(this);
8 8 : label = new QLabel(this);
9 :
10 : // setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
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 for precise control
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 48 : QWidget *section = new QWidget(this);
22 48 : section->setFixedSize(22, 32);
23 : // section->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
24 48 : layout->addWidget(section);
25 48 : sections.append(section);
26 : }
27 :
28 8 : main_layout->addLayout(layout);
29 8 : set_temperature(40);
30 8 : main_layout->addWidget(label); // Ensure label is added to the layout
31 8 : }
32 :
33 16 : Temperature::~Temperature()
34 : {
35 8 : std::cout << "Remove Temperature" << std::endl;
36 16 : }
37 :
38 9 : void Temperature::set_text(int temp)
39 : {
40 9 : label->setTextFormat(Qt::RichText);
41 9 : label->setText("<span style='font-family: Noto Sans; font-size: 24px; color: rgb(0, 120, 140);'>🌡️ </span>"
42 18 : "<span style='font-family: Digital-7; font-size: 30px; color: rgb(0, 120, 140);'>" +
43 36 : QString::number(temp) + "</span>"
44 : "<span style='font-family: Calculator; font-size: 30px; color: rgb(0, 120, 140);'> °C</span>");
45 9 : label->setContentsMargins(5, 0, 0, 0);
46 9 : label->setAlignment(Qt::AlignLeft);
47 9 : }
48 :
49 9 : void Temperature::set_temperature(int temp)
50 : {
51 9 : temperature = temp;
52 9 : int sections_color = static_cast<int>((temp / 80.0) * nb_sections);
53 63 : for (int i = 0; i < nb_sections; ++i) {
54 54 : if (i < sections_color) {
55 26 : QColor sectionColor;
56 26 : if (temp < 60) {
57 26 : int green = std::min(255, 80 + (i * (50 / nb_sections))); //from dim cyan to regular cyan
58 26 : int blue = std::min(255, 100 + (i * (50 / nb_sections))); //dim blueincrease to bright blue
59 26 : sectionColor.setRgb(0, green, blue);
60 : } else {
61 0 : int red = (i * (200 / nb_sections)); //increase red component
62 0 : int green = std::min(255, 60 + (i * (20 / nb_sections))); //decrease green
63 0 : int blue = std::max(0, 80 - (i * (50 / nb_sections)));
64 0 : sectionColor.setRgb(red, green, blue);
65 : }
66 26 : sections[i]->setStyleSheet(QString("background-color: %1").arg(sectionColor.name()));
67 : } else {
68 28 : QColor inactive(22, 32, 60); //dim cyan color
69 28 : sections[i]->setStyleSheet(QString("background-color: %1").arg(inactive.name()));
70 : }
71 : }
72 9 : set_text(temp);
73 9 : }
74 :
75 1 : int Temperature::get_nbsections()
76 : {
77 1 : return nb_sections;
78 : }
79 :
80 1 : int Temperature::get_temperature()
81 : {
82 1 : return temperature;
83 : }
84 :
85 1 : QVector<QWidget*> Temperature::get_sections()
86 : {
87 1 : return sections;
88 : }
89 :
90 1 : QHBoxLayout* Temperature::get_layout()
91 : {
92 1 : return layout;
93 : }
94 :
95 1 : QVBoxLayout* Temperature::get_mainlayout()
96 : {
97 1 : return main_layout;
98 : }
99 :
100 1 : QLabel* Temperature::get_label()
101 : {
102 1 : return label;
103 : }
|