diff --git a/mainwindow.cpp b/mainwindow.cpp index 78f9b61..3537b9f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -6,6 +6,7 @@ #include #include +#include class QNetworkReply; namespace shutdown2 { @@ -30,6 +31,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi // init state machine initStateMachine(); + connect(&mTimer, &QTimer::timeout, this, &MainWindow::updateTime); } MainWindow::~MainWindow() @@ -56,21 +58,72 @@ void MainWindow::initStateMachine() off->addTransition(ui->btn_startstop, SIGNAL(clicked()), on); // init - mState.addState(on); - mState.addState(off); - mState.setInitialState(off); - mState.start(); + mStateMachine.addState(on); + mStateMachine.addState(off); + mStateMachine.setInitialState(off); + mStateMachine.start(); } void MainWindow::on_btn_startstop_clicked() { - triggerCssUpdate(); -} - -void MainWindow::triggerCssUpdate() -{ - qDebug() << QTime::currentTime() << "updating CSS"; // rogo: delete + timerToggled(); setStyleSheet(mCss); } +void MainWindow::shutdown() +{ + // TODO: shutdown +} + +void MainWindow::updateTime() +{ + if (mTimeLeft == 0) { + shutdown(); + mTimer.stop(); + return; + } + + QString m = QString::number(mTimeLeft / 60); + QString s = QString::number(mTimeLeft % 60); + + if (s.length() == 1) s = "0" + s; + + ui->lbl_timer->setText(m + ":" + s); + mTimeLeft--; +} + +void MainWindow::timerToggled() { + if (ui->btn_startstop->property("on").toBool()) { + + int time = 0; + foreach (QRadioButton* r, ui->groupBox->findChildren()) { + if (r->isChecked()) { + time = r->property("value").toInt(); + break; + } + } + + if (time == -1) + time = ui->spinBox->value(); + + time *= 60; // convert to seconds + mTimeLeft = time; + updateTime(); + mTimer.start(1000); + } else { + mTimeLeft = -2; + mTimer.stop(); + ui->lbl_timer->setText("-- : --"); + } +} + +void MainWindow::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return) { + ui->btn_startstop->click(); // this is needed this way to trigger statemachine transition + event->accept(); + } +} + + } diff --git a/mainwindow.h b/mainwindow.h index 77f9c89..c5ae4cb 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -4,6 +4,7 @@ #include #include #include +#include namespace Ui { class MainWindow; @@ -21,16 +22,22 @@ public: private slots: void on_btn_startstop_clicked(); + void updateTime(); private: Ui::MainWindow *ui; QSettings* mSettings = nullptr; - QStateMachine mState; + QStateMachine mStateMachine; QByteArray mCss; + QTimer mTimer; + int mTimeLeft = -1; // in seconds void initStateMachine(); void triggerCssUpdate(); + void keyPressEvent(QKeyEvent *event) override; + void timerToggled(); + void shutdown(); }; } diff --git a/mainwindow.ui b/mainwindow.ui index 00a7ee1..fb72b3b 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -16,6 +16,9 @@ 0 + + Qt::StrongFocus + Qt::NoContextMenu @@ -194,6 +197,9 @@ 90 min + + 90 + @@ -234,6 +240,9 @@ 22 min + + 22 + @@ -259,7 +268,7 @@ 0 - + 0 @@ -274,6 +283,9 @@ 60 min + + 60 + @@ -314,6 +326,9 @@ 11 min + + 11 + @@ -354,6 +369,9 @@ 45 min + + 45 + @@ -394,6 +412,9 @@ 30 min + + 30 + @@ -434,6 +455,9 @@ 120 min + + 120 + @@ -474,6 +498,9 @@ + + -1 + @@ -538,6 +565,9 @@ true + + 0 + @@ -558,7 +588,7 @@ r_22 r_30 r_45 - r_74 + r_60 r_90 r_120 r_custom diff --git a/shutdown2.pro.user b/shutdown2.pro.user index d90bdb9..2c0c045 100644 --- a/shutdown2.pro.user +++ b/shutdown2.pro.user @@ -1,9 +1,10 @@ + EnvironmentId - {1042e94e-a121-467f-86f8-548b0504a650} + {2eab4085-7443-4062-b54e-64c18ac87c35} ProjectExplorer.Project.ActiveTarget @@ -38,37 +39,39 @@ 1 true false - 1 + 0 true true - 1 + 0 8 true 1 true true true - true + false ProjectExplorer.Project.PluginSettings - + + -fno-delayed-template-parsing + true ProjectExplorer.Project.Target.0 - Desktop Qt 5.12.2 Clang 64bit - Desktop Qt 5.12.2 Clang 64bit - {3623dadc-96df-4513-a832-6bb4de7f7933} + Desktop Qt 5.12.2 MinGW 64-bit + Desktop Qt 5.12.2 MinGW 64-bit + qt.qt5.5122.win64_mingw73_kit 0 0 0 - /home/rogo/workspace/shutdown2/build-shutdown2-Desktop_Qt_5_12_2_Clang_64bit-Debug + L:/workspace/shutdown2/build-shutdown2-Desktop_Qt_5_12_2_MinGW_64_bit-Debug true @@ -123,7 +126,119 @@ 2 true - 1 + + L:/workspace/build-yremote-Desktop_Qt_5_12_2_MinGW_64_bit-Release + + + true + qmake + + QtProjectManager.QMakeBuildStep + false + + false + false + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Release + Release + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + + L:/workspace/build-yremote-Desktop_Qt_5_12_2_MinGW_64_bit-Profile + + + true + qmake + + QtProjectManager.QMakeBuildStep + true + + false + true + true + + + true + Make + + Qt4ProjectManager.MakeStep + + false + + + false + + 2 + Build + + ProjectExplorer.BuildSteps.Build + + + + true + Make + + Qt4ProjectManager.MakeStep + + true + clean + + false + + 1 + Clean + + ProjectExplorer.BuildSteps.Clean + + 2 + false + + Profile + Profile + Qt4ProjectManager.Qt4BuildConfiguration + 0 + true + + 3 0 @@ -166,11 +281,11 @@ 1 25 - 0 + 1 true false true - /usr/bin/valgrind + valgrind 0 1 @@ -190,19 +305,19 @@ 2 - - Custom Executable + shutdown2 - ProjectExplorer.CustomExecutableRunConfiguration + Qt4ProjectManager.Qt4RunConfiguration:L:/workspace/shutdown2/shutdown2.pro 3768 false true + true false false true - + L:/workspace/shutdown2/build-shutdown2-Desktop_Qt_5_12_2_MinGW_64_bit-Debug 1 @@ -213,10 +328,10 @@ ProjectExplorer.Project.Updater.FileVersion - 21 + 22 Version - 21 + 22