diff --git a/headers/backend/linbobackend.h b/headers/backend/linbobackend.h index 65434b2..ffeff6a 100644 --- a/headers/backend/linbobackend.h +++ b/headers/backend/linbobackend.h @@ -46,7 +46,7 @@ class LinboBackend : public QObject Q_PROPERTY(LinboBackend::LinboState state READ getState NOTIFY stateChanged) Q_PROPERTY(LinboOs* currentOs READ getCurrentOs WRITE setCurrentOs NOTIFY currentOsChanged) Q_PROPERTY(double autostartTimeoutProgress READ getAutostartTimeoutProgress NOTIFY autostartTimeoutProgressChanged) - Q_PROPERTY(int autostartTimeoutRemainingSeconds READ getAutostartTimeoutRemainingSeconds NOTIFY autostartTimeoutProgressChanged) + //Q_PROPERTY(int autostartTimeoutRemainingSeconds READ getAutostartTimeoutRemainingSeconds NOTIFY autostartTimeoutProgressChanged) public: explicit LinboBackend(QObject *parent = nullptr); @@ -147,6 +147,7 @@ private slots: signals: void stateChanged(LinboBackend::LinboState state); void currentOsChanged(LinboOs* os); + void autostartTimeoutProgressChanged(); }; diff --git a/headers/backend/linbologger.h b/headers/backend/linbologger.h index 2fafbc7..8a3c47f 100644 --- a/headers/backend/linbologger.h +++ b/headers/backend/linbologger.h @@ -29,7 +29,7 @@ public: const LinboLog& getLatestLog(); - QString logTypeToString(LinboLogType logType); + static QString logTypeToString(LinboLogType logType); private: diff --git a/headers/backend/linboos.h b/headers/backend/linboos.h index 7261175..6f6b2b1 100644 --- a/headers/backend/linboos.h +++ b/headers/backend/linboos.h @@ -65,7 +65,7 @@ public: } } - LinboOsStartAction startActionFromString(const QString& name) const { + static LinboOsStartAction startActionFromString(const QString& name) { if(name == "start") return StartOs; else if(name == "sync") diff --git a/headers/linbostartactions.h b/headers/linbostartactions.h index e20c025..2bc94a3 100644 --- a/headers/linbostartactions.h +++ b/headers/linbostartactions.h @@ -47,6 +47,7 @@ private slots: void handleCurrentOsChanged(LinboOs* newOs); void handleLinboStateChanged(LinboBackend::LinboState newState); void handleLatestLogChanged(const LinboLogger::LinboLog& latestLog); + void handleAutostartTimeoutProgressChanged(); signals: void selectedOsChanged(); diff --git a/headers/qmodernprogressbar.h b/headers/qmodernprogressbar.h index ac9ed23..2061fbc 100644 --- a/headers/qmodernprogressbar.h +++ b/headers/qmodernprogressbar.h @@ -17,6 +17,7 @@ public: void setIndeterminate(bool indeterminate); bool getIndeterminate(); + void setReversed(bool reversed); protected: QTimer* refreshTimer; @@ -26,7 +27,10 @@ protected: private: bool indeterminate; + bool reversed; int preIndeterminateValue; + int preIndeterminateMinimum; + int preIndeterminateMaximum; protected slots: void updateIndeterminate(); diff --git a/sources/backend/linbobackend.cpp b/sources/backend/linbobackend.cpp index 60c0707..5f478dd 100644 --- a/sources/backend/linbobackend.cpp +++ b/sources/backend/linbobackend.cpp @@ -27,11 +27,13 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent) this->setState(Initializing); this->logger = new LinboLogger(this); this->config = new LinboConfig(this); - this->autostartTimer = new QTimer(this); + this->autostartTimer = new QTimer(); this->autostartTimer->setSingleShot(true); - this->autostartRemainingTimeRefreshTimer = new QTimer(this); - this->autostartRemainingTimeRefreshTimer->setSingleShot(true); + connect(this->autostartTimer, SIGNAL(timeout()), this, SLOT(handleAutostartTimerTimeout())); + this->autostartRemainingTimeRefreshTimer = new QTimer(); + this->autostartRemainingTimeRefreshTimer->setSingleShot(false); this->autostartRemainingTimeRefreshTimer->setInterval(10); + connect(this->autostartRemainingTimeRefreshTimer, SIGNAL(timeout()), this, SIGNAL(autostartTimeoutProgressChanged())); this->autostartElapsedTimer = new QElapsedTimer(); this->currentOs = nullptr; @@ -127,7 +129,7 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent) this->config->setCacheSize(this->executeCommand(true, "size")); // Harddisk Size - QRegExp *removePartition = new QRegExp("[0-9]{1,2}") ; + QRegExp *removePartition = new QRegExp("[0-9]{1,2}"); QString hd = this->config->getCache(); // e.g. turn /dev/sda1 into /dev/sda hd.remove( *removePartition ); @@ -152,12 +154,43 @@ LinboBackend::LinboBackend(QObject *parent) : QObject(parent) // -------------------- void LinboBackend::executeAutostart() { + if(this->currentOs->getDefaultAction() == LinboOs::UnknownAction) { + this->setState(Idle); + return; + } + + this->setState(Autostarting); + this->logger->log("Beginning autostart timeout for " + this->currentOs->getName(), LinboLogger::LinboGuiInfo); this->autostartTimer->setInterval(this->currentOs->getAutostartTimeout() * 1000); + this->autostartTimer->start(); this->autostartElapsedTimer->restart(); + this->autostartRemainingTimeRefreshTimer->start(); } -void handleAutostartTimerTimeout() { +void LinboBackend::handleAutostartTimerTimeout() { + this->autostartElapsedTimer->invalidate(); + this->autostartRemainingTimeRefreshTimer->stop(); + this->logger->log("Executing autostart for " + this->currentOs->getName(), LinboLogger::LinboGuiInfo); + LinboOs::LinboOsStartAction defaultAction = this->currentOs->getDefaultAction(); + bool wasSuccessfull = false; + switch (defaultAction) { + case LinboOs::StartOs: + wasSuccessfull = this->startCurrentOs(); + break; + case LinboOs::SyncOs: + wasSuccessfull = this->syncCurrentOs(); + break; + case LinboOs::ReinstallOs: + wasSuccessfull = this->reinstallCurrentOs(); + break; + default: break; + } + + if(wasSuccessfull) + this->logger->log("Executed autostart successfully!", LinboLogger::LinboGuiInfo); + else + this->logger->log("An error occured when executing autostart for " + this->currentOs->getName(), LinboLogger::LinboGuiError); } void LinboBackend::shutdown() { @@ -171,13 +204,11 @@ void LinboBackend::reboot() { bool LinboBackend::startCurrentOs() { LinboOs* os = this->currentOs; - if(os == nullptr || this->state != Idle || !this->currentOs->getStartbutton()) + if(os == nullptr || (this->state != Idle && this->state != Autostarting) || !os->getStartbutton()) return false; this->setState(Starting); - return true; - this->executeCommand( false, "start", @@ -195,7 +226,7 @@ bool LinboBackend::startCurrentOs() { bool LinboBackend::syncCurrentOs() { LinboOs* os = this->currentOs; - if(os == nullptr || this->state != Idle || !this->currentOs->getSyncbutton()) + if(os == nullptr || (this->state != Idle && this->state != Autostarting) || !os->getSyncbutton()) return false; this->setState(Syncing); @@ -220,7 +251,7 @@ bool LinboBackend::syncCurrentOs() { bool LinboBackend::reinstallCurrentOs() { LinboOs* os = this->currentOs; - if(os == nullptr || this->state != Idle || !this->currentOs->getNewbutton()) + if(os == nullptr || (this->state != Idle && this->state != Autostarting) || !os->getNewbutton()) return false; this->setState(Reinstalling); @@ -329,11 +360,11 @@ void LinboBackend::setCurrentOs(LinboOs* os) { double LinboBackend::getAutostartTimeoutProgress() { - return this->autostartElapsedTimer->elapsed() / this->autostartTimer->interval(); + return this->autostartTimer->isActive() ? double(this->autostartElapsedTimer->elapsed()) / double(this->autostartTimer->interval()) : 1; } int LinboBackend::getAutostartTimeoutRemainingSeconds() { - return (this->autostartTimer->interval() - this->autostartElapsedTimer->elapsed()) / 1000; + return this->autostartTimer->isActive() ? (this->autostartTimer->interval() - this->autostartElapsedTimer->elapsed()) / 1000 : 0; } // ----------- @@ -348,10 +379,10 @@ QString LinboBackend::executeCommand(bool waitForFinished, QString command, QStr // clear old output this->synchronosProcess->readAll(); - synchronosProcess->start(command, commandArgs); - synchronosProcess->waitForStarted(); + this->synchronosProcess->start(command, commandArgs); + this->synchronosProcess->waitForStarted(); - while( !synchronosProcess->waitForFinished(10000) ) {} + this->synchronosProcess->waitForFinished(10000); return this->synchronosProcess->readAllStandardOutput(); } else { diff --git a/sources/linbostartactions.cpp b/sources/linbostartactions.cpp index ab74d4b..ccd6720 100644 --- a/sources/linbostartactions.cpp +++ b/sources/linbostartactions.cpp @@ -5,6 +5,7 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q this->backend = backend; connect(this->backend, SIGNAL(currentOsChanged(LinboOs*)), this, SLOT(handleCurrentOsChanged(LinboOs*))); connect(this->backend, SIGNAL(stateChanged(LinboBackend::LinboState)), this, SLOT(handleLinboStateChanged(LinboBackend::LinboState))); + connect(this->backend, SIGNAL(autostartTimeoutProgressChanged()), this, SLOT(handleAutostartTimeoutProgressChanged())); connect(this->backend->getLogger(), SIGNAL(latestLogChanged(const LinboLogger::LinboLog&)), this, SLOT(handleLatestLogChanged(const LinboLogger::LinboLog&))); this->stackView = new QModernStackedWidget(this); @@ -28,6 +29,7 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q // Progress bar this->progressBarWidget = new QWidget(); this->progressBar = new QModernProgressBar(this->progressBarWidget); + this->progressBar->setRange(0,1000); this->progressBar->setIndeterminate(true); this->logLabel = new QLabel("", this->progressBarWidget); @@ -37,9 +39,10 @@ LinboStartActions::LinboStartActions(LinboBackend* backend, QWidget *parent) : Q this->logLabel->setFont(this->logFont); this->stackView->addWidget(this->progressBarWidget); + this->stackView->setCurrentWidget(nullptr); connect(this->stackView, SIGNAL(currentChanged(int)), this, SLOT(resizeAndPositionAllItems())); - this->stackView->setCurrentWidget(this->buttonWidget); + this->handleLinboStateChanged(this->backend->getState()); } void LinboStartActions::resizeAndPositionAllItems() { @@ -172,20 +175,37 @@ void LinboStartActions::handleCurrentOsChanged(LinboOs* newOs) { } void LinboStartActions::handleLinboStateChanged(LinboBackend::LinboState newState) { + + QWidget* currentWidget = nullptr; + switch (newState) { + case LinboBackend::Autostarting: + qDebug() << "Linbo state is: autostart"; + this->progressBar->setIndeterminate(false); + this->progressBar->setReversed(true); + this->progressBar->setValue(0); + currentWidget = this->progressBarWidget; + break; case LinboBackend::Idle: - this->stackView->setCurrentWidgetAnimated(this->buttonWidget); + currentWidget = this->buttonWidget; break; case LinboBackend::Starting: case LinboBackend::Syncing: case LinboBackend::Reinstalling: - this->stackView->setCurrentWidgetAnimated(this->progressBarWidget); + this->progressBar->setIndeterminate(true); + this->progressBar->setReversed(false); + currentWidget = this->progressBarWidget; break; default: break; } + + if(this->inited) + this->stackView->setCurrentWidgetAnimated(currentWidget); + else + this->stackView->setCurrentWidget(currentWidget); } @@ -204,3 +224,25 @@ void LinboStartActions::handleLatestLogChanged(const LinboLogger::LinboLog& late this->logLabel->setStyleSheet("QLabel { color : " + logColor + "; }"); this->logLabel->setText(latestLog.message); } + +void LinboStartActions::handleAutostartTimeoutProgressChanged() { + if(this->backend->getState() != LinboBackend::Autostarting) + return; + + this->progressBar->setValue(1000 - this->backend->getAutostartTimeoutProgress() * 1000); + + QString actionString; + switch (this->backend->getCurrentOs()->getDefaultAction()) { + case LinboOs::SyncOs: + actionString = "Syncing and starting"; + break; + case LinboOs::ReinstallOs: + actionString = "Reinstalling and starting"; + break; + default: + actionString = "Starting"; + break; + } + + this->logLabel->setText(actionString + " " + this->backend->getCurrentOs()->getName() + " in " + QString::number(this->backend->getAutostartTimeoutRemainingSeconds()) + " seconds."); +} diff --git a/sources/qmodernprogressbar.cpp b/sources/qmodernprogressbar.cpp index 928ec8e..b3d0d04 100644 --- a/sources/qmodernprogressbar.cpp +++ b/sources/qmodernprogressbar.cpp @@ -26,6 +26,8 @@ void QModernProgressBar::setIndeterminate(bool indeterminate) { if(this->indeterminate) { this->preIndeterminateValue = this->value(); + this->preIndeterminateMinimum = this->minimum(); + this->preIndeterminateMaximum = this->maximum(); // finer steps, so the Animation is fluid this->setMinimum(0); this->setMaximum(1000); @@ -33,13 +35,21 @@ void QModernProgressBar::setIndeterminate(bool indeterminate) { } else { // reset minimum and maximum - this->setMinimum(0); - this->setMaximum(100); + this->setMinimum(this->preIndeterminateMinimum); + this->setMaximum(this->preIndeterminateMaximum); this->setValue(this->preIndeterminateValue); this->indeterminateAnimtion->stop(); } } +void QModernProgressBar::setReversed(bool reversed) { + if(this->reversed == reversed) + return; + + this->reversed = reversed; + this->update(); +} + bool QModernProgressBar::getIndeterminate() { return this->indeterminate; } @@ -54,18 +64,34 @@ void QModernProgressBar::paintEvent(QPaintEvent *e) { // background painter.fillRect(e->rect(), QColor("#c7c7c7")); + double from = 0; + double to = 0; + // progress if(this->indeterminate) { int maximum = this->maximum() / 2; if(this->value() <= maximum) // for the first half -> fill from left - painter.fillRect(QRect(0,0, e->rect().width() * double(double(this->value()) / double(maximum)), e->rect().height()), QColor(0,0,0)); - else + to = double(double(this->value()) / double(maximum)); + else { // for the second half -> empty from right - painter.fillRect(QRect(e->rect().width() * double(double(this->value()- (maximum)) / double(maximum)),0, e->rect().width(), e->rect().height()), QColor(0,0,0)); + from = double(double(this->value()- (maximum)) / double(maximum)); + to = 1; + } } - else - painter.fillRect(QRect(0,0, e->rect().width() * float(float(this->value()) / float(this->maximum())), e->rect().height()), QColor(0,0,0)); + else { + to = double(double(this->value()) / double(this->maximum())); + } + + if(this->reversed) { + // if reversed -> reverse and swap from and to + double tmp = 1 - from; + from = 1 - to; + to = tmp; + + } + + painter.fillRect(QRect(e->rect().width() * from, 0, e->rect().width() * to, e->rect().height()), QColor(0,0,0)); painter.end(); diff --git a/sources/qmodernstackedwidget.cpp b/sources/qmodernstackedwidget.cpp index 623858f..628ee5e 100644 --- a/sources/qmodernstackedwidget.cpp +++ b/sources/qmodernstackedwidget.cpp @@ -11,7 +11,7 @@ QModernStackedWidget::QModernStackedWidget(QWidget* parent) : QStackedWidget(par } void QModernStackedWidget::setCurrentWidgetAnimated(QWidget* widget) { - if(widget == nullptr) + if(widget == nullptr || widget == this->currentWidget()) return; this->newWidget = widget;