#include "speedcheck.h"
#include "ui_speedcheck.h"
#include "copythread.h"
#include "datasource/cpuload.h"
#include "datasource/networkspeed.h"
#include <QCoreApplication>
#include <QMessageBox>
#include <QNetworkInterface>
#include <QFile>
#include <QFileDialog>
#include <QDebug>
#include <QTimer>
SpeedCheck::SpeedCheck(QString fileName)
: _ui(new Ui::MainWindow),
_thread(NULL),
_doQuit(false),
_fileName(fileName)
{
_ui->setupUi(this);
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface iface, interfaces) {
if ((iface.flags().testFlag(QNetworkInterface::IsUp)) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack)) {
_ui->comboBox->addItem(iface.name());
}
}
connect(_ui->btnStart, SIGNAL(clicked(bool)), this, SLOT(startClicked(bool)));
connect(_ui->btnQuit, SIGNAL(clicked(bool)), this, SLOT(quitClicked(bool)));
connect(_ui->btnselect, SIGNAL(clicked(bool)), this, SLOT(selectClicked(bool)));
_ui->filelabel->setText("Selected File: " + _fileName);
_timer.setInterval(200);
connect(&_timer, SIGNAL(timeout()), this, SLOT(updateTimer()));
qsrand((uint)QCoreApplication::applicationPid());
}
SpeedCheck::~SpeedCheck()
{
}
void SpeedCheck::logMessage(QString message)
{
// qDebug() << message;
_ui->statusBar->showMessage(message);
}
void SpeedCheck::selectClicked(bool) {
_fileName = QFileDialog::getOpenFileName(this, tr("Select benchmark file"));
_ui->filelabel->setText("Selected File: " + _fileName);
}
void SpeedCheck::startClicked(bool)
{
QFile *file = new QFile(_fileName);
if (!file->open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, trUtf8("Error"), trUtf8("Could not open %1 for reading.").arg(_fileName));
return;
}
_ui->picCpu->setDataSource(new CpuLoad());
_ui->picSpeed->setDataSource(new NetworkSpeed(_ui->comboBox->currentText()));
_ui->btnStart->setDisabled(true);
_ui->btnselect->setDisabled(true);
_ui->comboBox->setDisabled(true);
delete _thread;
_thread = new CopyThread(file, this);
connect(_thread, SIGNAL(logMessage(QString)), this, SLOT(logMessage(QString)), Qt::QueuedConnection);
connect(_thread, SIGNAL(finished()), this, SLOT(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();
}
_ui->btnStart->setEnabled(true);
_ui->btnselect->setEnabled(true);
_ui->comboBox->setEnabled(true);
}
void SpeedCheck::updateTimer()
{
_ui->picCpu->readNextValue();
_ui->picSpeed->readNextValue();
}