Send custom event from worker(-thread) to MainWindow in order to update

progress bar.
This commit is contained in:
Gerhard Hoffmann 2023-08-05 18:50:50 +02:00
parent b09ccfd4f5
commit cf9033e898
7 changed files with 44 additions and 24 deletions

View File

@ -145,6 +145,8 @@ int main(int argc, char *argv[]) {
dryRun);
MainWindow mw(&worker);
worker.setMainWindow(&mw);
mw.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
mw.setWindowState(Qt::WindowFullScreen);
mw.show();

View File

@ -2,6 +2,7 @@
#include "ui_mainwindow.h"
#include "worker.h"
#include "utils.h"
#include "progress_event.h"
#include <QDateTime>
#include <QMessageBox>
@ -56,7 +57,6 @@ MainWindow::MainWindow(Worker *worker, QWidget *parent)
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)));
connect(m_worker, SIGNAL(setProgress(quint8)), this, SLOT(onSetProgress(quint8)));
ui->updateStatus->setText(lst.join('\n'));
ui->updateStatus->setEnabled(true);
@ -68,6 +68,13 @@ MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::customEvent(QEvent *event) {
if (event->type() == ProgressEvent::type()) {
int progress = ((ProgressEvent *)(event))->progressPercent();
ui->updateProgress->setValue(progress);
}
}
void MainWindow::onStopStartTimer() {
m_startTimer->stop();
}
@ -91,11 +98,6 @@ void MainWindow::onQuit() {
qApp->exit(m_worker->returnCode());
}
void MainWindow::onSetProgress(quint8 v) {
qCritical() << "ON SET PROGRESS" << v;
ui->updateProgress->setValue(v);
}
void MainWindow::onAppendText(QString text, QString suffix) {
QString editText = ui->updateStatus->toPlainText();
QStringList lines = editText.split('\n');

View File

@ -13,6 +13,9 @@ QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
protected:
void customEvent(QEvent *event) override;
public:
MainWindow(Worker *worker, QWidget *parent = nullptr);
~MainWindow();
@ -20,7 +23,6 @@ public:
public slots:
void onAppendText(QString, QString);
void onShowErrorMessage(QString, QString);
void onSetProgress(quint8);
void onStopStartTimer();
void onRestartExitTimer();
void onEnableExit();

View File

@ -2,9 +2,9 @@
QEvent::Type ProgressEvent::customEventType = QEvent::None;
ProgressEvent::ProgressEvent()
ProgressEvent::ProgressEvent(int progressPercent)
: QEvent(ProgressEvent::type())
, m_progressPercent(0) {
, m_progressPercent(progressPercent) {
}
ProgressEvent::~ProgressEvent() {

View File

@ -7,7 +7,7 @@ class ProgressEvent : public QEvent {
int m_progressPercent;
public:
ProgressEvent();
explicit ProgressEvent(int progressPercent);
virtual ~ProgressEvent();
static QEvent::Type type();

View File

@ -21,6 +21,8 @@
#include "message_handler.h"
#include "plugins/interfaces.h"
#include "ismas/ismas_client.h"
#include "progress_event.h"
#include "mainwindow.h"
QString const Worker::UPDATE_STEP_OK(" [ ok]");
QString const Worker::UPDATE_STEP_DONE(" [done]");
@ -112,13 +114,19 @@ Worker::~Worker() {
}
}
void Worker::setProgress(int progress) {
if (m_mainWindow) {
QApplication::postEvent(m_mainWindow, new ProgressEvent(progress));
}
}
static std::once_flag once;
void Worker::update() {
// user should not start the update process several times
std::call_once(once, &Worker::privateUpdate, this);
}
void Worker::privateUpdate() {
// user should not start the update process several times
QPushButton *start = qobject_cast<QPushButton *>(QObject::sender());
start->setEnabled(false);
@ -148,7 +156,7 @@ void Worker::privateUpdate() {
emit appendText(QString(""), UPDATE_STEP_SUCCESS);
emit setProgress(100);
setProgress(100);
m_ismasClient.setProgressInPercent(100);
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") + m_ismasClient.updateOfPSAActivated());
@ -167,41 +175,41 @@ void Worker::privateUpdate() {
QString("#M=APISM#C=CMD_EVENT#J=") +
m_ismasClient.checkoutBranch(
m_updateStatus.m_statusDescription, ""));
emit setProgress(progress);
setProgress(progress);
qCritical() << "CHECKED OUT BRANCH";
if (backendConnected()) { qCritical() << "BACKEND CONNECTED";
progress = 20;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
if (updateTriggerSet()) { qCritical() << "UPDATE TRIGGER SET";
progress = 30;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
if (customerEnvironment()) { qCritical() << "CUSTOMER ENVIRONMENT";
progress = 40;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
if (filesToUpdate()) { qCritical() << "FILES TO UPDATE";
progress = 50;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
if (updateFiles(progress)) { qCritical() << "UPDATE FILES";
progress = 60;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
if (syncCustomerRepositoryAndFS()) { qCritical() << "SYNC REPOSITORY";
progress = 70;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
if (sendIsmasLastVersionNotification()) { qCritical() << "SEND LAST NOTIFICATION";
progress = 80;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
sentIsmasLastVersionNotification = true;
if (saveLogFile()) { qCritical() << "SAVE LOG FILE";
progress = 90;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
emit appendText(QString(""), UPDATE_STEP_SUCCESS);
@ -211,7 +219,7 @@ void Worker::privateUpdate() {
// mark update as activated -> this resets the WAIT button
progress = 100;
emit setProgress(progress);
setProgress(progress);
m_ismasClient.setProgressInPercent(progress);
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
@ -247,7 +255,7 @@ void Worker::privateUpdate() {
}
}
emit setProgress(100);
setProgress(100);
m_ismasClient.setProgressInPercent(100);
if (!sentIsmasLastVersionNotification) {

View File

@ -84,6 +84,7 @@ QString& operator<<(QString &str, UpdateStatus status);
#define ISMAS_UPDATE_REQUESTS (10)
class MainWindow;
class hwinf;
class Worker : public QObject {
Q_OBJECT
@ -124,6 +125,8 @@ class Worker : public QObject {
int m_displayIndex;
int m_returnCode;
MainWindow *m_mainWindow;
bool executeOpkgCommand(QString opkgCommand);
QString getOsVersion() const;
QString getATBQTVersion() const;
@ -135,6 +138,8 @@ class Worker : public QObject {
qint64 getFileSize(QString const &fileName) const;
void setProgress(int progress);
public:
static const QString UPDATE_STEP_OK;
static const QString UPDATE_STEP_DONE;
@ -153,6 +158,8 @@ public:
char const *baudrate = "115200");
~Worker();
void setMainWindow(MainWindow *mainWindow) { m_mainWindow = mainWindow; }
IsmasClient &getIsmasClient() { return m_ismasClient; }
IsmasClient const &getIsmasClient() const { return m_ismasClient; }
@ -175,7 +182,6 @@ public:
signals:
void appendText(QString, QString);
void showErrorMessage(QString title, QString description);
void setProgress(quint8);
void stopStartTimer();
void restartExitTimer();
void enableExit();