Files
shutdown2/mainwindow.cpp
2019-10-14 15:19:21 +02:00

142 lines
3.6 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTime>
#include <QStyle>
#include <QDebug>
#include <QKeyEvent>
#include <QLineEdit>
class QNetworkReply;
namespace shutdown2 {
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
setFixedSize(size());
#if defined(Q_OS_WIN)
setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
#endif
mSettings = new QSettings(QSettings::IniFormat, QSettings::UserScope, "RogoSoftware", "EasyShutdown2");
restoreGeometry(mSettings->value("geometry", saveGeometry()).toByteArray());
move(mSettings->value("pos", pos()).toPoint());
QFile css(":style.css");
if (css.open(QFile::ReadOnly | QFile::Text))
mCss = css.readAll();
setStyleSheet(mCss);
// init timers
QApplication::setEffectEnabled(Qt::UI_AnimateCombo, false);
ui->cb_timers->setValidator(new QIntValidator(0, 99999, this));
ui->cb_timers->setAutoCompletion(false);
ui->cb_timers->lineEdit()->setAlignment(Qt::AlignRight);
ui->cb_timers->addItem("0");
ui->cb_timers->addItem("11");
ui->cb_timers->addItem("22");
ui->cb_timers->addItem("30");
ui->cb_timers->addItem("45");
ui->cb_timers->addItem("60");
ui->cb_timers->addItem("90");
ui->cb_timers->addItem("120");
ui->cb_timers->addItem("240");
// init state machine
initStateMachine();
connect(&mTimer, &QTimer::timeout, this, &MainWindow::updateTime);
}
MainWindow::~MainWindow()
{
delete ui;
delete mSettings;
}
void MainWindow::initStateMachine()
{
// states
QState* on = new QState();
on->assignProperty(ui->btn_startstop, "on", false);
on->assignProperty(ui->lbl_timer, "on", false);
on->assignProperty(ui->btn_startstop, "text", "Stop");
QState* off = new QState();
off->assignProperty(ui->btn_startstop, "on", true);
off->assignProperty(ui->lbl_timer, "on", true);
off->assignProperty(ui->btn_startstop, "text", "Start");
// transistions
on->addTransition(ui->btn_startstop, SIGNAL(clicked()), off);
off->addTransition(ui->btn_startstop, SIGNAL(clicked()), on);
// init
mStateMachine.addState(on);
mStateMachine.addState(off);
mStateMachine.setInitialState(off);
mStateMachine.start();
}
void MainWindow::on_btn_startstop_clicked()
{
timerToggled();
setStyleSheet(mCss);
}
void MainWindow::shutdown()
{
// TODO: shutdown
}
void MainWindow::updateTime()
{
if (mTimeLeft == 0) {
ui->lbl_timer->setText("0:00:00");
shutdown();
mTimer.stop();
return;
}
QString h = QString::number(mTimeLeft / 3600);
QString m = QString::number((mTimeLeft / 60) % 60);
QString s = QString::number(mTimeLeft % 60);
if (s.length() == 1) s = "0" + s;
if (m.length() == 1) m = "0" + m;
ui->lbl_timer->setText(h + ":" + m + ":" + s);
mTimeLeft--;
}
void MainWindow::timerToggled() {
if (ui->btn_startstop->property("on").toBool()) {
int time = ui->cb_timers->currentText().toInt();
time *= 60; // convert to seconds
mTimeLeft = time;
updateTime();
mTimer.start(1000);
ui->stackedWidget->setCurrentIndex(1);
} else {
mTimeLeft = -1;
mTimer.stop();
ui->lbl_timer->setText("--:--:--");
ui->stackedWidget->setCurrentIndex(0);
}
}
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();
}
}
}