-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfowidget.cpp
85 lines (71 loc) · 1.98 KB
/
infowidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "infowidget.h"
#include "core.h"
InfoWidget::InfoWidget(QWidget* parent) : QFrame(parent)
{
// grey transparent background
this->setAutoFillBackground(true);
this->setPalette(QPalette(QColor(0x40, 0x40, 0x40, 0xE9)));
// dark border around widget
this->setFrameShadow(QFrame::Raised);
this->setFrameShape(QFrame::Panel);
this->setLineWidth(2);
init_gui();
this->setFocusProxy(nullptr);
}
void InfoWidget::init_gui()
{
avg_speed.setAlignment(Qt::AlignCenter);
cars.setAlignment(Qt::AlignCenter);
lanes.setAlignment(Qt::AlignCenter);
length.setAlignment(Qt::AlignCenter);
time.setAlignment(Qt::AlignCenter);
main_layout.addWidget(&avg_speed);
main_layout.addWidget(&cars);
main_layout.addWidget(&time);
main_layout.addWidget(&length);
main_layout.addWidget(&lanes);
translate();
update_data();
this->setLayout(&main_layout);
}
void InfoWidget::translate()
{
avg_speed.setToolTip(tr("Average Speed"));
cars.setToolTip(tr("Amount Of Cars"));
lanes.setToolTip(tr("Street Lanes"));
length.setToolTip(tr("Street Length"));
time.setToolTip(tr("Elapsed Time"));
update_data();
}
void InfoWidget::update_data()
{
avg_speed.setText(tr("Avg. Speed: ") + QString::number(Core::get_avg_speed(), 'g', 4));
cars.setText(tr("Cars: ") + QString::number(Core::get_car_amount()));
lanes.setText(tr("Lanes: ") + QString::number(Core::get_lanes()));
length.setText(tr("Length: ") + QString::number(Core::get_length()));
time.setText(tr("Time: ") + QString::number(Core::get_time()));
}
void InfoWidget::enable_focus()
{
// allow focus to all children
for(QObject* a : this->children())
{
auto b = dynamic_cast<QWidget*>(a);
if(b)
b->setFocusPolicy(Qt::StrongFocus);
}
// enable own focus
this->setFocusPolicy(Qt::StrongFocus);
}
void InfoWidget::disable_focus()
{
// revoke focus of all children
for(QObject* a : this->children())
{
auto b = dynamic_cast<QWidget*>(a);
if(b)
b->setFocusPolicy(Qt::NoFocus);
}
// disable own focus
this->setFocusPolicy(Qt::NoFocus);
}