added timer

This commit is contained in:
2019-10-03 20:03:05 +02:00
parent 6319b6fee8
commit 77e61ec029
4 changed files with 236 additions and 31 deletions

View File

@@ -6,6 +6,7 @@
#include <QStyle>
#include <QDebug>
#include <QKeyEvent>
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<QRadioButton*>()) {
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();
}
}
}