2023-07-31 16:55:36 +02:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include "worker.h"
|
2023-08-02 15:29:10 +02:00
|
|
|
#include "utils.h"
|
2023-08-05 18:50:50 +02:00
|
|
|
#include "progress_event.h"
|
2023-07-31 16:55:36 +02:00
|
|
|
|
|
|
|
#include <QDateTime>
|
2023-08-02 15:29:10 +02:00
|
|
|
#include <QMessageBox>
|
2023-08-02 15:44:16 +02:00
|
|
|
#include <QDebug>
|
2023-07-31 16:55:36 +02:00
|
|
|
|
|
|
|
MainWindow::MainWindow(Worker *worker, QWidget *parent)
|
|
|
|
: QMainWindow(parent)
|
|
|
|
, ui(new Ui::MainWindow)
|
2023-08-02 15:29:10 +02:00
|
|
|
, m_worker(worker)
|
2023-08-06 20:44:26 +02:00
|
|
|
, m_width(70)
|
|
|
|
, m_progressRunning(false)
|
|
|
|
, m_progressValue(0) {
|
2023-07-31 16:55:36 +02:00
|
|
|
ui->setupUi(this);
|
|
|
|
|
2023-08-06 14:14:47 +02:00
|
|
|
ui->updateProgress->setRange(0, 100);
|
2023-08-02 15:29:10 +02:00
|
|
|
ui->updateProgress->reset();
|
|
|
|
|
2023-08-04 13:44:16 +02:00
|
|
|
QStringList lst;
|
|
|
|
QString start = QDateTime::currentDateTime().toString(Qt::ISODate);
|
|
|
|
lst << QString("Start: ") + start.leftJustified(m_width-10);
|
|
|
|
lst << QString("").leftJustified(m_width-3, '=');
|
|
|
|
lst << QString("Machine number : %1 ").arg(m_worker->machineNr()).leftJustified(m_width-3);
|
|
|
|
lst << QString("Customer number : %1 ").arg(m_worker->customerNr()).leftJustified(m_width-3);
|
|
|
|
lst << QString("Zone number : %1 (%2)").arg(m_worker->zoneNr()).arg(Utils::zoneName(m_worker->zoneNr())).leftJustified(m_width-3);
|
|
|
|
lst << QString("").leftJustified(m_width-3, '=');
|
|
|
|
|
|
|
|
ui->updateStatus->setText(lst.join('\n'));
|
|
|
|
ui->updateStatus->setEnabled(true);
|
|
|
|
|
2023-08-02 15:29:10 +02:00
|
|
|
m_startTimer = new QTimer(this);
|
2023-08-09 15:08:22 +02:00
|
|
|
connect(m_startTimer, SIGNAL(timeout()), m_worker, SLOT(update()));
|
2023-08-02 15:29:10 +02:00
|
|
|
m_startTimer->setSingleShot(true);
|
2023-08-11 11:05:02 +02:00
|
|
|
m_startTimer->start(1000);
|
2023-07-31 16:55:36 +02:00
|
|
|
|
2023-08-02 15:29:10 +02:00
|
|
|
m_exitTimer = new QTimer(this);
|
|
|
|
connect(m_exitTimer, SIGNAL(timeout()), ui->exit, SLOT(click()));
|
|
|
|
m_exitTimer->setSingleShot(true);
|
|
|
|
m_exitTimer->start(1800 * 1000);
|
|
|
|
|
|
|
|
connect(ui->exit, SIGNAL(clicked()), this, SLOT(onQuit()));
|
2023-08-02 17:51:35 +02:00
|
|
|
connect(m_worker, SIGNAL(disableExit()), this, SLOT(onDisableExit()));
|
|
|
|
connect(m_worker, SIGNAL(enableExit()), this, SLOT(onEnableExit()));
|
2023-08-02 15:29:10 +02:00
|
|
|
connect(m_worker, SIGNAL(stopStartTimer()), this, SLOT(onStopStartTimer()));
|
|
|
|
connect(m_worker, SIGNAL(restartExitTimer()), this, SLOT(onRestartExitTimer()));
|
|
|
|
connect(m_worker, SIGNAL(appendText(QString, QString)), this, SLOT(onAppendText(QString, QString)));
|
|
|
|
connect(m_worker, SIGNAL(showErrorMessage(QString,QString)), this, SLOT(onShowErrorMessage(QString,QString)));
|
2023-08-06 14:14:47 +02:00
|
|
|
connect(m_worker, SIGNAL(replaceLast(QString, QString)), this, SLOT(onReplaceLast(QString,QString)));
|
2023-07-31 16:55:36 +02:00
|
|
|
|
|
|
|
ui->updateStatus->setText(lst.join('\n'));
|
|
|
|
ui->updateStatus->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainWindow::~MainWindow() {
|
2023-08-02 15:29:10 +02:00
|
|
|
delete m_startTimer;
|
|
|
|
delete m_exitTimer;
|
2023-07-31 16:55:36 +02:00
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2023-08-05 18:50:50 +02:00
|
|
|
void MainWindow::customEvent(QEvent *event) {
|
|
|
|
if (event->type() == ProgressEvent::type()) {
|
2023-08-06 20:44:26 +02:00
|
|
|
ProgressEvent *pevent = (ProgressEvent *)event;
|
|
|
|
int const progress = pevent->progressPercent();
|
|
|
|
QObject const *sender = pevent->sender();
|
|
|
|
if (sender == this) {
|
|
|
|
switch(progress) {
|
|
|
|
case 0: {
|
|
|
|
ui->updateProgress->reset();
|
|
|
|
} break;
|
|
|
|
case START_PROGRESS_LOOP: {
|
|
|
|
m_progressRunning = true;
|
|
|
|
ui->updateProgress->reset();
|
|
|
|
m_progressValue = 10;
|
|
|
|
QApplication::postEvent(this, new ProgressEvent(this, m_progressValue));
|
|
|
|
} break;
|
|
|
|
case STOP_PROGRESS_LOOP: {
|
|
|
|
m_progressRunning = false;
|
|
|
|
m_progressValue -= 10;
|
|
|
|
m_worker->setProgress(m_progressValue/10);
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
if (m_progressRunning) {
|
|
|
|
m_progressValue = progress;
|
|
|
|
ui->updateProgress->setValue(progress/10);
|
|
|
|
QApplication::postEvent(this, new ProgressEvent(this, progress+10));
|
2023-08-11 11:05:33 +02:00
|
|
|
QThread::msleep(500);
|
2023-08-06 20:44:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
if (sender == m_worker) {
|
|
|
|
switch(progress) {
|
|
|
|
case 0: {
|
|
|
|
ui->updateProgress->reset();
|
|
|
|
} break;
|
|
|
|
case START_PROGRESS_LOOP: {
|
|
|
|
QApplication::postEvent(this, new ProgressEvent(this, START_PROGRESS_LOOP));
|
|
|
|
} break;
|
|
|
|
case STOP_PROGRESS_LOOP: {
|
|
|
|
QApplication::postEvent(this, new ProgressEvent(this, STOP_PROGRESS_LOOP));
|
|
|
|
} break;
|
|
|
|
default:{
|
|
|
|
ui->updateProgress->setValue(progress);
|
|
|
|
}}
|
2023-08-06 14:14:47 +02:00
|
|
|
} else {
|
2023-08-06 20:44:26 +02:00
|
|
|
qCritical() << "!!! UNKNOWN SENDER !!!";
|
2023-08-06 14:14:47 +02:00
|
|
|
}
|
2023-08-05 18:50:50 +02:00
|
|
|
}
|
2023-08-11 11:05:33 +02:00
|
|
|
|
|
|
|
QThread::yieldCurrentThread();
|
2023-08-05 18:50:50 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 15:29:10 +02:00
|
|
|
void MainWindow::onStopStartTimer() {
|
|
|
|
m_startTimer->stop();
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:51:35 +02:00
|
|
|
void MainWindow::onDisableExit() {
|
|
|
|
ui->exit->setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::onEnableExit() {
|
|
|
|
ui->exit->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2023-08-02 15:29:10 +02:00
|
|
|
void MainWindow::onRestartExitTimer() {
|
|
|
|
m_exitTimer->stop();
|
2023-08-09 15:08:22 +02:00
|
|
|
m_exitTimer->start(60 * 1000);
|
2023-08-02 15:29:10 +02:00
|
|
|
}
|
2023-07-31 16:55:36 +02:00
|
|
|
|
2023-08-02 15:29:10 +02:00
|
|
|
void MainWindow::onQuit() {
|
2023-08-04 13:45:56 +02:00
|
|
|
m_exitTimer->stop();
|
|
|
|
qCritical() << QString("ON QUIT: EXIT CODE %1").arg(m_worker->returnCode());
|
2023-08-03 10:14:09 +02:00
|
|
|
qApp->exit(m_worker->returnCode());
|
2023-08-02 15:29:10 +02:00
|
|
|
}
|
2023-07-31 16:55:36 +02:00
|
|
|
|
2023-08-02 15:29:10 +02:00
|
|
|
void MainWindow::onAppendText(QString text, QString suffix) {
|
|
|
|
QString editText = ui->updateStatus->toPlainText();
|
2023-08-07 13:59:44 +02:00
|
|
|
if (!suffix.isNull() && suffix.size() > 0) {
|
|
|
|
qInfo() << "TEXT" << text << "SUFFIX" << suffix;
|
|
|
|
if (suffix == Worker::UPDATE_STEP_SUCCESS || suffix == Worker::UPDATE_STEP_FAIL) {
|
|
|
|
editText += QString("\n").leftJustified(m_width-3, '=');
|
|
|
|
editText += " ";
|
|
|
|
}
|
2023-08-06 14:14:47 +02:00
|
|
|
editText += (QString("\n") + text).leftJustified(m_width - (2 + suffix.size()) ) + suffix;
|
2023-08-03 09:06:50 +02:00
|
|
|
} else {
|
2023-08-06 14:14:47 +02:00
|
|
|
editText += text.leftJustified(m_width-9);
|
2023-08-02 15:29:10 +02:00
|
|
|
}
|
2023-08-06 14:14:47 +02:00
|
|
|
|
2023-08-09 15:08:22 +02:00
|
|
|
QStringList const lines = editText.split('\n');
|
2023-08-06 14:14:47 +02:00
|
|
|
for (int i=0; i<lines.size(); ++i) {
|
2023-08-07 14:01:51 +02:00
|
|
|
qInfo() << lines.at(i);
|
|
|
|
} qInfo() << ""; qInfo() << "";
|
2023-08-06 14:14:47 +02:00
|
|
|
|
2023-08-07 14:01:51 +02:00
|
|
|
ui->updateStatus->setPlainText(editText.trimmed());
|
2023-08-06 14:14:47 +02:00
|
|
|
ui->updateStatus->setEnabled(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::onReplaceLast(QString text, QString suffix) {
|
2023-08-07 14:01:51 +02:00
|
|
|
qInfo() << "REPL TEXT" << text << "SUFFIX" << suffix;
|
|
|
|
|
2023-08-06 14:14:47 +02:00
|
|
|
QString editText = ui->updateStatus->toPlainText();
|
|
|
|
QStringList lines = editText.split('\n');
|
|
|
|
if (lines.size() > 0) {
|
|
|
|
lines.removeLast();
|
2023-08-07 14:01:51 +02:00
|
|
|
if (!suffix.isNull() && suffix.size() > 0 && suffix != "\n") {
|
|
|
|
lines += text.leftJustified(m_width-10) + suffix;
|
|
|
|
} else {
|
|
|
|
lines += text.leftJustified(m_width-10);
|
|
|
|
}
|
2023-08-06 14:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<lines.size(); ++i) {
|
2023-08-07 14:01:51 +02:00
|
|
|
qInfo() << lines.at(i);
|
|
|
|
} qInfo() << ""; qInfo() << "";
|
2023-08-06 14:14:47 +02:00
|
|
|
|
2023-08-07 14:01:51 +02:00
|
|
|
ui->updateStatus->setText(lines.join('\n').trimmed());
|
2023-08-02 15:29:10 +02:00
|
|
|
ui->updateStatus->setEnabled(true);
|
|
|
|
}
|
|
|
|
void MainWindow::onShowErrorMessage(QString title, QString text) {
|
2023-08-07 14:01:51 +02:00
|
|
|
text = text.leftJustified(50, ' ');
|
|
|
|
QMessageBox msgBox(QMessageBox::NoIcon, title,
|
|
|
|
text, QMessageBox::Ok,
|
|
|
|
nullptr, Qt::FramelessWindowHint);
|
|
|
|
msgBox.setDefaultButton(QMessageBox::Ok);
|
|
|
|
msgBox.setStyleSheet("QDialog {background-color: red;}"
|
|
|
|
"QPushButton {background-color: blue;}");
|
|
|
|
QTimer *t = new QTimer(this);
|
|
|
|
connect(t, SIGNAL(timeout()), msgBox.defaultButton(), SLOT(click()));
|
|
|
|
t->setSingleShot(true);
|
|
|
|
t->start(5 * 1000);
|
|
|
|
|
|
|
|
if(msgBox.exec() == QMessageBox::Ok) {
|
|
|
|
// do something
|
|
|
|
} else {
|
|
|
|
// do something else
|
|
|
|
}
|
|
|
|
disconnect(t, SIGNAL(timeout()), msgBox.defaultButton(), SLOT(click()));
|
2023-07-31 16:55:36 +02:00
|
|
|
}
|