Files
shutdown2/mainwindow.cpp

158 lines
4.1 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", "shutdown2");
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");
ui->cb_timers->installEventFilter(this);
// 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()
{
system("shutdown -s -f -t 0");
}
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);
ui->btn_startstop->setFocus();
} else {
mTimeLeft = -1;
mTimer.stop();
ui->lbl_timer->setText("--:--:--");
ui->stackedWidget->setCurrentIndex(0);
ui->cb_timers->setFocus();
}
}
void MainWindow::keyPressEvent(QKeyEvent *e)
{
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
ui->btn_startstop->click(); // this is needed this way to trigger statemachine transition
e->accept();
}
}
bool MainWindow::eventFilter(QObject *watched, QEvent *e)
{
if (e->type() == QEvent::KeyPress) {
if (QKeyEvent* ke = dynamic_cast<QKeyEvent*>(e)) {
if (ke->key() == Qt::Key_Space) {
ui->btn_startstop->click(); // this is needed this way to trigger statemachine transition
e->accept();
return true;
}
}
}
return QObject::eventFilter(watched, e);
}
}