-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetCPUWidget.cpp
More file actions
158 lines (147 loc) · 5.91 KB
/
GetCPUWidget.cpp
File metadata and controls
158 lines (147 loc) · 5.91 KB
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
/***************************************************************************
* Copyright (C) 2019 by Viktor Dózsa *
* luke2135@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 3 of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses>. *
***************************************************************************/
#include "GetCPUWidget.h"
#include "ui_GetCPUWidget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);ui->lineEdit->clear();this->setMouseTracking(true);
pThreadCPU = new TestCPUThread();ui->lineEdit->setReadOnly(true);
if (!pThreadCPU)
{
QMessageBox::information(this, tr("Error"), tr("Can't start new thread to measure CPU load on this system. Fatal error."));
close();
}
sleepProcess = new QProcess(this);
if (!sleepProcess)
{
QMessageBox::information(this, tr("Error"), tr("Can't start new process... Sleep mode disabled."));bSleepOff = true;
}
connect(this, SIGNAL(startTest()), pThreadCPU, SLOT(threadStart()), Qt::QueuedConnection);bSleepOff = false;
connect(this, SIGNAL(stopTest()), pThreadCPU, SLOT(threadStop()), Qt::QueuedConnection);
connect(pThreadCPU, SIGNAL(sendMessage(QString)), this, SLOT(thread_receiveMessage(QString)), Qt::QueuedConnection);
connect(pThreadCPU, SIGNAL(sendLCDValue(int)), this, SLOT(thread_receiveLCDValue(int)), Qt::QueuedConnection);
connect(ui->spinBox, SIGNAL(valueChanged(int)), this, SLOT(spinBox_valueChanged(int)), Qt::QueuedConnection);
ui->lcdNumber->setStyleSheet("background: black");setWindowTitle(tr("Are we going to Standby?"));
ui->horizontalSlider->setStyleSheet("background: black");ui->horizontalSlider_2->setStyleSheet("background: black");
qi64MsecondsPassed = qi64Msec = key = 0;emit startTest();timer.start();iCountdown = ui->spinBox->value();ui->exitButton->setFocusPolicy(Qt::StrongFocus);
ui->lineEdit->setFocus();
}
void Widget::sleep( int millisecondsToWait )
{
QTime dieTime = QTime::currentTime().addMSecs( millisecondsToWait );
while( QTime::currentTime() < dieTime )
{
QCoreApplication::processEvents( QEventLoop::AllEvents, 100 );
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_exitButton_clicked()
{
if (sleepProcess->state() == QProcess::Running)
sleepProcess->kill(); //if it's running, we won't arrive here anyway
delete sleepProcess;
if (pThreadCPU->isRunning())
{
emit stopTest();
while (pThreadCPU->isRunning())
sleep(250);
delete pThreadCPU;
close();
}
else
{
delete pThreadCPU;
close();
}
}
void Widget::thread_receiveMessage(QString s)
{
ui->lineEdit->setText(s);
}
void Widget::thread_receiveLCDValue(int iVal)
{
if (iVal < 25)
ui->lcdNumber->setStyleSheet("background: black; color: lightgray");
else if (iVal < 50)
ui->lcdNumber->setStyleSheet("background: black; color: lightblue");
else if (iVal < 75)
ui->lcdNumber->setStyleSheet("background: black; color: lightgreen");
else
ui->lcdNumber->setStyleSheet("background: black; color: red");
ui->lcdNumber->display(iVal);
if (iVal >= ui->spinBoxCPUThreshold->value())
{
iCountdown = ui->spinBox->value();ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
}
if (iVal < ui->spinBoxCPUThreshold->value())
{
qi64Msec = timer.elapsed();
if ( qi64Msec > qi64MsecondsPassed + 1000)
{
qi64MsecondsPassed = qi64Msec;
if (iCountdown > 0)
{
if (iCountdown == 1)
ui->lineEdit->setText(tr("Entering standy in %1 second.").arg(iCountdown));
else
ui->lineEdit->setText(tr("Entering standy in %1 seconds.").arg(iCountdown));
iCountdown--;
}
else if (!bSleepOff)
{
args.clear();iCountdown = ui->spinBox->value();ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
//sleepProcess->start("sleep.bat", args);
args << "powrprof.dll" << ",SetSuspendState" << "0,1,0";sleepProcess->start("rundll32.exe", args);
}
}
}
}
void Widget::spinBox_valueChanged(int iVal)
{
//qDebug() << "new Value: " << iVal;
iCountdown = iVal;ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
//ui->lineEdit->setText("mouse moved!");
iCountdown = ui->spinBox->value();ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
}
void Widget::mousePressEvent(QMouseEvent *event)
{
//ui->lineEdit->setText("mouse pressed!");
iCountdown = ui->spinBox->value();ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
}
void Widget::keyPressEvent(QKeyEvent *event)
{
if (event->key() != key)
{
key = event->key();
iCountdown = ui->spinBox->value();ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
}
if (ui->exitButton->hasFocus() && ( key == Qt::Key_Enter || Qt::Key_Return ))
emit ui->exitButton->clicked();
}
void Widget::keyReleaseEvent(QKeyEvent *event)
{
key = 0;
iCountdown = ui->spinBox->value();ui->lineEdit->clear();timer.restart();qi64MsecondsPassed = qi64Msec = 0;
}