summaryrefslogtreecommitdiffstats
path: root/src/speedcheck.cpp
blob: 303f3e31f4103288abaafa83fedaa90069c56321 (plain) (blame)
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
#include "speedcheck.h"
#include "ui_speedcheck.h"
#include "copythread.h"
#include "datasource/cpuload.h"
#include "datasource/networkspeed.h"
#include "copythread.h"

#include <QCoreApplication>
#include <QMessageBox>
#include <QFile>
#include <QDebug>
#include <QTimer>

SpeedCheck::SpeedCheck(QString fileName, bool autoStart)
	: _ui(new Ui::MainWindow),
	  _thread(NULL),
	  _doQuit(false),
	  _fileName(fileName)
{
	_ui->setupUi(this);
	connect(_ui->btnStart, &QPushButton::clicked, this, &SpeedCheck::startClicked);
	connect(_ui->btnQuit, &QPushButton::clicked, this, &SpeedCheck::quitClicked);
	_timer.setInterval(200);
	connect(&_timer, &QTimer::timeout, this, &SpeedCheck::updateTimer);
	if (autoStart) {
		QTimer::singleShot(1, [this]() {
			this->startClicked(true);
		});
	}
}

SpeedCheck::~SpeedCheck()
{

}

void SpeedCheck::logMessage(CopyThread::LogMessageId, QString message)
{
	qDebug() << message;
	_ui->statusBar->showMessage(message);
}

void SpeedCheck::startClicked(bool)
{
	QFile *file = new QFile(_fileName);
	if (!file->open(QIODevice::ReadOnly)) {
		QMessageBox::critical(this, tr("Error"), tr("Could not open %1 for reading.").arg(_fileName));
		return;
	}
	_ui->picCpu->setDataSource(new CpuLoad());
	_ui->picSpeed->setDataSource(new NetworkSpeed());
	_ui->btnStart->setDisabled(true);
	delete _thread;
	_thread = new CopyThread(file, this);
	connect(_thread, &CopyThread::logMessage, this, &SpeedCheck::logMessage, Qt::QueuedConnection);
	connect(_thread, &CopyThread::finished, this, &SpeedCheck::testFinished, Qt::QueuedConnection);
	_timer.start();
	_thread->start();
}

void SpeedCheck::quitClicked(bool)
{
	if (_thread != NULL && _thread->isRunning()) {
		_doQuit = true;
		_thread->stop();
	}
	if (_thread == NULL || !_thread->isRunning()) {
		QCoreApplication::instance()->quit();
	}
}

void SpeedCheck::testFinished()
{
	_timer.stop();
	delete _thread;
	_thread = NULL;
	if (_doQuit) {
		QCoreApplication::instance()->quit();
		return;
	}
	_ui->btnStart->setEnabled(true);
}

void SpeedCheck::updateTimer()
{
	_ui->picCpu->readNextValue();
	_ui->picSpeed->readNextValue();
}