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); dryRun);
MainWindow mw(&worker); MainWindow mw(&worker);
worker.setMainWindow(&mw);
mw.setWindowFlags(Qt::Window | Qt::FramelessWindowHint); mw.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
mw.setWindowState(Qt::WindowFullScreen); mw.setWindowState(Qt::WindowFullScreen);
mw.show(); mw.show();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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