-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
362 lines (312 loc) · 11.4 KB
/
mainwindow.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <powersetting.h>
#include <QDesktopServices>
#include <QUrl>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//Initializing auto_window
secWindow = new Auto();
//If secWindow -> baseWindow, so this -> show...
connect(secWindow, &Auto::baseWindow, this, &MainWindow::show);
connect(secWindow, &Auto::butt2clicked, this, &MainWindow::SetAutoMode);
thirdWindow = new InformativeWindow();
connect(thirdWindow, &InformativeWindow::baseWindow, this, &MainWindow::show);
timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(timerAlarm()));
timer->start(1000);
timer2 = new QTimer();
connect(timer2, SIGNAL(timeout()), this, SLOT(timerAlarmAuto()));
timer2->start(1000);
fourthWindow = new Dialog();
connect(fourthWindow, &Dialog::baseWindow, this, &MainWindow::show);
fiveWindow = new UserInfo();
connect(fiveWindow, &UserInfo::baseWindow, this, &MainWindow::show);
_displayBrightness.ucDisplayPolicy = (UCHAR)ui->horizontalSlider->value();
_displayBrightness.ucACBrightness = (UCHAR)ui->horizontalSlider->value(); //for testing purposes
_displayBrightness.ucDCBrightness = (UCHAR)ui->horizontalSlider->value();
DWORD ret = NULL;
DWORD disp_nOutBufferSize = sizeof(_displayBrightness);
HANDLE h = CreateFile(L"\\\\.\\LCD", GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, 0, nullptr);
DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (DISPLAY_BRIGHTNESS*)&_displayBrightness, disp_nOutBufferSize, nullptr, 0, &ret, nullptr);
PowerGetActiveScheme(NULL, CurrentPowerPolicy);
if(**CurrentPowerPolicy == GUID_MIN_POWER_SAVINGS)
{
ui->label_9->setText("Hight perfomance Mode");
}
else if(**CurrentPowerPolicy == GUID_MAX_POWER_SAVINGS)
{
ui->label_9->setText("Power Saving Mode");
}
else if (**CurrentPowerPolicy == GUID_TYPICAL_POWER_SAVINGS)
{
ui->label_9->setText("Balance Mode");
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_6_clicked()
{
secWindow->show();
this->close();
}
void MainWindow::on_pushButton_4_clicked()
{
fourthWindow->show();
this->close();
}
void MainWindow::on_toolButton_clicked()
{
thirdWindow->show();
this->close();
}
void MainWindow::on_pushButton_2_clicked()
{
QMessageBox::StandardButton ans = QMessageBox::question(this, "PowerControl", "Усі не збережені данні будуть знищені.\n"
"Ви впевнені, що бажаєте вийти?", QMessageBox::Yes | QMessageBox::No);
if(ans == QMessageBox::Yes) {
qDebug() << "Exit" << endl;
this->close();
}
}
bool MainWindow::on_horizontalSlider_valueChanged(int value)
{
_displayBrightness.ucDisplayPolicy = (UCHAR)value;
_displayBrightness.ucACBrightness = (UCHAR)value; //for testing purposes
_displayBrightness.ucDCBrightness = (UCHAR)value;
DWORD ret = NULL;
DWORD nOutBufferSize = sizeof(_displayBrightness);
HANDLE h = CreateFile(L"\\\\.\\LCD",
GENERIC_READ | GENERIC_WRITE,
0,
nullptr,
OPEN_EXISTING,
0, nullptr);
if (h == INVALID_HANDLE_VALUE) {
//Does not reach here
return false;
}
if (!DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (DISPLAY_BRIGHTNESS*)&_displayBrightness, nOutBufferSize, nullptr, 0, &ret,
nullptr))
{
// GetLastError() returns error code 6 - Invalid handle
return false;
}
else {
return true;
}
}
void MainWindow::timerAlarm()
{
PowerGetActiveScheme(NULL, CurrentPowerPolicy);
if(**CurrentPowerPolicy == GUID_MIN_POWER_SAVINGS)
{
qDebug() << "MinPowSav";
mode = 1;
}
else if(**CurrentPowerPolicy == GUID_MAX_POWER_SAVINGS)
{
qDebug() << "MaxPowSav";
mode = 3;
}
else if (**CurrentPowerPolicy == GUID_TYPICAL_POWER_SAVINGS)
{
qDebug() << "Typ";
mode = 2;
}
CallNtPowerInformation(SystemBatteryState, nullptr, 0, &batStats, 512);
if(batStats.BatteryPresent != TRUE)
{
QMessageBox::critical(this, "Помилка пошуку.", "Неможливо знайти жодної батареї.", QMessageBox::Ok);
this->close();
}
QString text = QString::number(batStats.RemainingCapacity);
//QMessageBox::information(this, "Title", text, QMessageBox::Ok);
DWORD cap = batStats.RemainingCapacity;
double Percent = ((double)cap/(double)batStats.MaxCapacity);
string on = "background-image: url(:/img/100.png);";
string off = "background-image: url(:/img/b100.png);";
if(Percent < 0.20 && Percent >= 0)
{
on = "background-image: url(:/img/0.png);";
off = "background-image: url(:/img/b0.png);";
}
else if(Percent < 0.4 && Percent >= 0.20)
{
on = "background-image: url(:/img/20.png);";
off = "background-image: url(:/img/b20.png);";
}
else if(Percent < 0.6 && Percent >= 0.4)
{
on = "background-image: url(:/img/40.png);";
off = "background-image: url(:/img/b40.png);";
}
else if(Percent < 0.8 && Percent >= 0.6)
{
on = "background-image: url(:/img/60.png);";
off = "background-image: url(:/img/b60.png);";
}
else if(Percent < 1 && Percent >= 0.8)
{
on = "background-image: url(:/img/80.png);";
off = "background-image: url(:/img/b80.png);";
}
else if(Percent == 1)
{
on = "background-image: url(:/img/100.png);";
off = "background-image: url(:/img/b100.png);";
}
if(batStats.AcOnLine == TRUE)
{
ui->info->clear();
ui->frame->styleSheet().clear();
ui->frame->setStyleSheet(QString::fromStdString(on));
ui->frame->show();
ui->info->setText("Поточний стан: відбувається заряджання батареї від зовнішньої мережі.\n"
"Приблизна ємність, що є у данний момент на батареї:" + text + " mAh\n" + QString::number(Percent*100) + "%");
QFont f("MS Shell dlg 2", 12);
ui->info->setFont(f);
ui->info->show();
}
else if(batStats.AcOnLine == FALSE)
{
ui->info->clear();
ui->frame->styleSheet().clear();
ui->frame->setStyleSheet(QString::fromStdString(off));
ui->frame->show();
ui->info->setText("Поточний стан: живлення за рахунок батареї.\n"
"Приблизна ємність, що залишилась на батареї:" + text + " mAh\n" + QString::number(Percent*100) + "%\n"
"Час, що залишився на батареї: " + QString::number(batStats.EstimatedTime/60) + " хвилин.");
QFont f("MS Shell dlg 2", 12);
ui->info->setFont(f);
ui->info->show();
}
}
void MainWindow::on_action_3_triggered() //Default
{
ui->horizontalSlider->setValue(50);
PowerRestoreDefaultPowerSchemes();
secWindow->setDef();
}
void MainWindow::on_action_4_triggered()
{
this->hide();
secWindow = new Auto(this);
secWindow->show();
}
void MainWindow::on_pushButton_3_clicked() //default
{
PowerRestoreDefaultPowerSchemes();
ui->horizontalSlider->setValue(50);
}
void MainWindow::on_pushButton_clicked()
{
QMessageBox::StandardButton ans = QMessageBox::question(this, "PowerControl", "Усі не збережені данні будуть знищені.\n"
"Ви впевнені, що бажаєте вийти?", QMessageBox::Yes | QMessageBox::No);
if(ans == QMessageBox::Yes) {
qDebug() << "Exit" << endl;
this->close();
}
}
void MainWindow::on_action_5_triggered()
{
QMessageBox::StandardButton ans = QMessageBox::question(this, "PowerControl", "Усі не збережені данні будуть знищені.\n"
"Ви впевнені, що бажаєте вийти?", QMessageBox::Yes | QMessageBox::No);
if(ans == QMessageBox::Yes) {
qDebug() << "Exit" << endl;
this->close();
}
}
void MainWindow::on_action_triggered()
{
QString path = QCoreApplication:: applicationDirPath();
QDesktopServices::openUrl(QUrl(path + "/instructions.pdf"));
}
void MainWindow::on_pushButton_10_clicked()
{
QMessageBox::StandardButton ans = QMessageBox::question(this, "PowerControl", "Усі не збережені данні будуть знищені.\n"
"Ви впевнені, що бажаєте вийти?", QMessageBox::Yes | QMessageBox::No);
if(ans == QMessageBox::Yes) {
qDebug() << "Exit" << endl;
this->close();
}
}
void MainWindow::on_pushButton_7_clicked()
{
PowerSetActiveScheme(NULL, &GUID_MAX_POWER_SAVINGS);
ui->horizontalSlider->setValue(0);
qDebug() << "PowerSaving Activated";
ui->label_9->clear();
ui->label_9->setText("Power Saving Mode");
ui->label_9->show();
}
void MainWindow::on_pushButton_8_clicked()
{
PowerSetActiveScheme(NULL, &GUID_TYPICAL_POWER_SAVINGS);
ui->horizontalSlider->setValue(60);
qDebug() << "Typ Scheme Activated";
ui->label_9->clear();
ui->label_9->setText("Balance Mode");
ui->label_9->show();
}
void MainWindow::on_pushButton_9_clicked()
{
PowerSetActiveScheme(NULL, &GUID_MIN_POWER_SAVINGS);
ui->horizontalSlider->setValue(100);
qDebug() << "High Scheme Activated";
ui->label_9->clear();
ui->label_9->setText("Hight perfomance Mode");
ui->label_9->show();
}
void MainWindow::SetAutoMode()
{
this->show();
int elect = secWindow->getElect();
int batt = secWindow->getBatt();
int perc = secWindow->getProc();
int low = secWindow->getMode();
if(perc == -2)
{
if(batStats.Charging == TRUE && batStats.AcOnLine == TRUE)
{
if(elect == 0)
on_pushButton_9_clicked();
if(elect == 1)
on_pushButton_8_clicked();
if(elect == 2)
on_pushButton_7_clicked();
}
if(batStats.Charging == FALSE && batStats.AcOnLine == FALSE)
{
if(batt == 0)
on_pushButton_9_clicked();
if(batt == 1)
on_pushButton_8_clicked();
if(batt == 2)
on_pushButton_7_clicked();
}
}
else if(perc > 0)
{
DWORD cap = batStats.RemainingCapacity;
double Percent = ((double)cap/(double)batStats.MaxCapacity);
if( Percent <= perc)
{ if(low == 0)
on_pushButton_9_clicked();
if(low == 1)
on_pushButton_8_clicked();
if(low == 2)
on_pushButton_7_clicked();
}
}
}
void MainWindow::on_pushButton_5_clicked()
{
ui->formGroupBox->hide();
ui->label->setText("<html><head/><body><p align=\"center\"><span style=\" font-size:16pt;\">Дякуємо за ваш відгук. Його буде розглянуто, очікуйте відповіді.</span></p></body></html>");
}