onAppendText() only appends text.

onReplaceLast() replaces the last line in the text edit window.
This commit is contained in:
Gerhard Hoffmann 2023-08-06 14:14:47 +02:00
parent a8994423f4
commit 1fd2269753
4 changed files with 53 additions and 20 deletions

View File

@ -12,10 +12,10 @@ MainWindow::MainWindow(Worker *worker, QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, ui(new Ui::MainWindow) , ui(new Ui::MainWindow)
, m_worker(worker) , m_worker(worker)
, m_width(52) { , m_width(70) {
ui->setupUi(this); ui->setupUi(this);
ui->updateProgress->setRange(1, 100); ui->updateProgress->setRange(0, 100);
ui->updateProgress->reset(); ui->updateProgress->reset();
QStringList lst; QStringList lst;
@ -57,6 +57,7 @@ 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(replaceLast(QString, QString)), this, SLOT(onReplaceLast(QString,QString)));
ui->updateStatus->setText(lst.join('\n')); ui->updateStatus->setText(lst.join('\n'));
ui->updateStatus->setEnabled(true); ui->updateStatus->setEnabled(true);
@ -71,7 +72,11 @@ MainWindow::~MainWindow() {
void MainWindow::customEvent(QEvent *event) { void MainWindow::customEvent(QEvent *event) {
if (event->type() == ProgressEvent::type()) { if (event->type() == ProgressEvent::type()) {
int progress = ((ProgressEvent *)(event))->progressPercent(); int progress = ((ProgressEvent *)(event))->progressPercent();
ui->updateProgress->setValue(progress); if (progress > 0) {
ui->updateProgress->setValue(progress);
} else {
ui->updateProgress->reset();
}
} }
} }
@ -100,22 +105,36 @@ void MainWindow::onQuit() {
void MainWindow::onAppendText(QString text, QString suffix) { void MainWindow::onAppendText(QString text, QString suffix) {
QString editText = ui->updateStatus->toPlainText(); QString editText = ui->updateStatus->toPlainText();
if (suffix.size() > 0) {
editText += QString("\n").leftJustified(m_width-3, '=');
editText += " ";
editText += (QString("\n") + text).leftJustified(m_width - (2 + suffix.size()) ) + suffix;
} else {
editText += text.leftJustified(m_width-9);
}
QStringList lines = editText.split('\n'); QStringList lines = editText.split('\n');
for (int i=0; i<lines.size(); ++i) { for (int i=0; i<lines.size(); ++i) {
qCritical() << lines.at(i); qDebug() << lines.at(i);
} qDebug() << ""; qDebug() << "";
ui->updateStatus->setPlainText(editText);
ui->updateStatus->setEnabled(true);
}
void MainWindow::onReplaceLast(QString text, QString suffix) {
QString editText = ui->updateStatus->toPlainText();
QStringList lines = editText.split('\n');
if (lines.size() > 0) {
lines.removeLast();
lines += text.leftJustified(m_width-10) + suffix;
} }
if (suffix.contains("[SUCCESS]")) {
editText += QString("\n").leftJustified(m_width-3, '='); for (int i=0; i<lines.size(); ++i) {
editText += QString("\n").leftJustified(m_width-12) + " [SUCCESS]"; qDebug() << lines.at(i);
} else } qDebug() << ""; qDebug() << "";
if (suffix.contains(" [fail]")) {
editText += QString("\n").leftJustified(m_width-3, '='); ui->updateStatus->setText(lines.join('\n'));
editText += QString("\n").leftJustified(m_width-9) + " [fail]";
} else {
editText += text.leftJustified(m_width-9) + suffix;
ui->updateStatus->setPlainText(editText);
}
ui->updateStatus->setText(editText);
ui->updateStatus->setEnabled(true); ui->updateStatus->setEnabled(true);
} }

View File

@ -21,7 +21,8 @@ public:
~MainWindow(); ~MainWindow();
public slots: public slots:
void onAppendText(QString, QString); void onAppendText(QString, QString suffix = "");
void onReplaceLast(QString, QString);
void onShowErrorMessage(QString, QString); void onShowErrorMessage(QString, QString);
void onStopStartTimer(); void onStopStartTimer();
void onRestartExitTimer(); void onRestartExitTimer();

View File

@ -26,7 +26,7 @@
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]");
QString const Worker::UPDATE_STEP_FAIL(" [fail]"); QString const Worker::UPDATE_STEP_FAIL(" [FAIL]");
QString const Worker::UPDATE_STEP_SUCCESS(" [SUCCESS]"); QString const Worker::UPDATE_STEP_SUCCESS(" [SUCCESS]");
Worker::Worker(hwinf *hw, Worker::Worker(hwinf *hw,
@ -130,6 +130,18 @@ void Worker::privateUpdate() {
QPushButton *start = qobject_cast<QPushButton *>(QObject::sender()); QPushButton *start = qobject_cast<QPushButton *>(QObject::sender());
start->setEnabled(false); start->setEnabled(false);
//emit appendText("\nConnecting backend ...");
//
//for (int i=0;i <= 100; ++i) {
// setProgress(i);
// QThread::msleep(100);
//}
//emit replaceLast("Connecting backend ...", UPDATE_STEP_OK);
//emit appendText(QString("UPDATE "), UPDATE_STEP_SUCCESS);
//emit appendText(QString("UPDATE "), UPDATE_STEP_FAIL);
// setProgress(0);
// return;
emit stopStartTimer(); emit stopStartTimer();
emit disableExit(); emit disableExit();
m_updateProcessRunning = true; m_updateProcessRunning = true;
@ -211,7 +223,7 @@ void Worker::privateUpdate() {
progress = 90; progress = 90;
setProgress(progress); setProgress(progress);
m_ismasClient.setProgressInPercent(progress); m_ismasClient.setProgressInPercent(progress);
emit appendText(QString(""), UPDATE_STEP_SUCCESS); emit appendText(QString("Update process "), UPDATE_STEP_SUCCESS);
IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT, IsmasClient::sendRequestReceiveResponse(IsmasClient::APISM::DB_PORT,
QString("#M=APISM#C=CMD_EVENT#J=") + QString("#M=APISM#C=CMD_EVENT#J=") +

View File

@ -180,7 +180,8 @@ public:
//} //}
signals: signals:
void appendText(QString, QString); void appendText(QString, QString suffix = "");
void replaceLast(QString, QString);
void showErrorMessage(QString title, QString description); void showErrorMessage(QString title, QString description);
void stopStartTimer(); void stopStartTimer();
void restartExitTimer(); void restartExitTimer();