-
Notifications
You must be signed in to change notification settings - Fork 6
/
computerview.cpp
169 lines (135 loc) · 4.89 KB
/
computerview.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "computerview.h"
#include "abstractrpm.h"
#include <Wt/WApplication>
#include <Wt/WBoxLayout>
#include <Wt/WGridLayout>
#include <Wt/WText>
#include <Wt/WImage>
#include <Wt/WPushButton>
#include <Wt/WFileResource>
#include <Wt/WLabel>
#include <Wt/WTextArea>
#include "util.h"
void ComputerView::btn_atx_force_off_clicked()
{
sig_atxForceOff();
}
void ComputerView::btn_atx_force_on_clicked()
{
sig_atxForceOn();
}
void ComputerView::btn_atx_reset_clicked()
{
sig_atxReset();
}
void ComputerView::btn_pw_switch_press_clicked()
{
sig_pwSwitchPress();
}
void ComputerView::btn_pw_switch_force_off_clicked()
{
sig_pwSwitchForceOff();
}
Wt::WFileResource *ComputerView::getImg(const Wt::WString &name)
{
std::string path = getExeDirectory() + "/../imgs/" + name.toUTF8();
std::string mime = "image/png";
size_t ext_found = name.toUTF8().find_last_of('.');
if (ext_found != std::string::npos)
mime = "image/" + name.toUTF8().substr(ext_found + 1);
return new Wt::WFileResource(mime, path);
}
void ComputerView::setPowerLedStatus(bool status)
{
if (status)
_img_led->setImageLink(_ico_led_on_file.get());
else
_img_led->setImageLink(_ico_led_off_file.get());
}
ComputerView::ComputerView(Wt::WApplication *app, const Wt::WString &computerName,
bool writeAccess, Wt::WContainerWidget *parent) :
Wt::WContainerWidget(parent),
app(app), _computerName(computerName), _img_led(NULL)
{
Wt::WBoxLayout *layout = new Wt::WBoxLayout(Wt::WBoxLayout::TopToBottom, this);
Wt::WText *_title = new Wt::WText(computerName);
_title->setStyleClass("ComputerName");
app->styleSheet().addRule(".ComputerName", "font-size: 36px; text-transform: capitalize;");
layout->addWidget(_title, 0, Wt::AlignCenter);
/* create the buttons and connect them to their slots */
_btn_atx_force_off = new Wt::WPushButton("Force off");
_btn_atx_force_on = new Wt::WPushButton("Force on");
_btn_atx_reset = new Wt::WPushButton("Reset");
_btn_pw_switch_press = new Wt::WPushButton("Press");
_btn_pw_switch_force_off = new Wt::WPushButton("Force off");
_btn_atx_force_off->setEnabled(writeAccess);
_btn_atx_force_on->setEnabled(writeAccess);
_btn_atx_reset->setEnabled(writeAccess);
_btn_pw_switch_press->setEnabled(writeAccess);
_btn_pw_switch_force_off->setEnabled(writeAccess);
_btn_atx_force_off->clicked().connect(this, &ComputerView::btn_atx_force_off_clicked);
_btn_atx_force_on->clicked().connect(this, &ComputerView::btn_atx_force_on_clicked);
_btn_atx_reset->clicked().connect(this, &ComputerView::btn_atx_reset_clicked);
_btn_pw_switch_press->clicked().connect(this, &ComputerView::btn_pw_switch_press_clicked);
_btn_pw_switch_force_off->clicked().connect(this, &ComputerView::btn_pw_switch_force_off_clicked);
/* preload all the images */
_ico_led_on_file.reset(getImg("green_light.png"));
_ico_led_off_file.reset(getImg("off_light.png"));
_ico_ping_file.reset(getImg("ping.png"));
_ico_atx_pwr_file.reset(getImg("atx_power.png"));
_ico_pwr_switch_file.reset(getImg("power-button.png"));
/* create the LED */
_img_led = new Wt::WImage();
_img_led->setHeight(Wt::WLength(16));
setPowerLedStatus(false);
Wt::WGridLayout *grid = new Wt::WGridLayout();
grid->addWidget(_img_led, 0, 0, Wt::AlignCenter);
grid->addWidget(new Wt::WText("Power Led state"), 0, 1);
grid->addWidget(new Wt::WLabel(""), 0, 5);
grid->addWidget(new Wt::WImage(_ico_ping_file.get()), 1, 0, Wt::AlignCenter);
grid->addWidget(new Wt::WText("Ping"), 1, 1);
_ping_txt = new Wt::WText("N/A");
grid->addWidget(_ping_txt, 1, 2, 0, 0, Wt::AlignCenter);
grid->addWidget(new Wt::WImage(_ico_atx_pwr_file.get()), 2, 0, Wt::AlignCenter);
grid->addWidget(new Wt::WText("ATX power"), 2, 1);
grid->addWidget(_btn_atx_force_off, 2, 2);
grid->addWidget(_btn_atx_force_on, 2, 3);
grid->addWidget(_btn_atx_reset, 2, 4);
grid->addWidget(new Wt::WImage(_ico_pwr_switch_file.get()), 3, 0, Wt::AlignCenter);
grid->addWidget(new Wt::WText("Power switch"), 3, 1);
grid->addWidget(_btn_pw_switch_press, 3, 2);
grid->addWidget(_btn_pw_switch_force_off, 3, 3);
grid->setColumnStretch(5, 1);
layout->addLayout(grid);
layout->addSpacing(10);
Wt::WLabel *label = new Wt::WLabel("Logs:");
layout->addWidget(label);
_logs_edit = new Wt::WTextArea("");
_logs_edit->setHeight(150);
_logs_edit->setMaximumSize(Wt::WLength::Auto, 150);
label->setBuddy(_logs_edit);
layout->addWidget(_logs_edit);
layout->addSpacing(10);
}
void ComputerView::powerLedStatusChanged(bool status)
{
setPowerLedStatus(status);
app->triggerUpdate();
}
void ComputerView::consoleDataAdded(const Wt::WString &data)
{
Wt::WString logs = data + _logs_edit->valueText();
logs = logs.toUTF8().substr(0, 1000); /* limit to 1k */
_logs_edit->setValueText(logs);
app->triggerUpdate();
}
void ComputerView::setPingDelay(double delay)
{
Wt::WString text;
if (delay < 0)
text = "Timeout";
else
text = Wt::WString("{1} ms").arg(floatToString(delay, 2));
_ping_txt->setText(text);
app->triggerUpdate();
}