reservedtimepopup.cpp 1.86 KB
#include "reservedtimepopup.h"
#include "ui_reservedtimepopup.h"

#include <QDebug>
#include <QKeyEvent>

#include "stringer.h"

ReservedTimePopup::ReservedTimePopup(QWidget *parent, QDateTime target) :
    QWidget(parent),
    ui(new Ui::ReservedTimePopup),
    target(target)
{
    ui->setupUi(this);

    setAttribute(Qt::WA_DeleteOnClose);

    int remainingTime = QDateTime::currentDateTime().msecsTo(target);
    ui->remainingTime->setMaximum(remainingTime);
    ui->remainingTime->setValue(remainingTime);

    connect(&checkTimeTimer, SIGNAL(timeout()), SLOT(checkTime()));
    checkTimeTimer.start(100);

    checkTime();

    setFocus();
}

ReservedTimePopup::~ReservedTimePopup()
{
    delete ui;
}

void ReservedTimePopup::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        ui->cancelButton->setFocus();
        break;
    case 0x01000031:    // Push
        break;
    case 0x01000030:    // Turn right
        ui->cancelButton->setFocus();
        break;
    }
}

void ReservedTimePopup::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000032:    // Turn left
        ui->cancelButton->setFocus();
        break;
    case 0x01000031:    // Push
        ui->cancelButton->click();
        break;
    case 0x01000030:    // Turn right
        ui->cancelButton->setFocus();
        break;
    }
}

void ReservedTimePopup::checkTime()
{
    qint64 remaining = QDateTime::currentDateTime().msecsTo(target);
    if (remaining > 0)
    {
        ui->timeLabel->setText(Stringer::remainingTime(remaining));
        ui->remainingTime->setValue(remaining);
    }
    else
    {
        emit timeout();
        close();
    }
}

void ReservedTimePopup::on_cancelButton_clicked()
{
    QWidget *focused = focusWidget();
    if (focused)
        focused->clearFocus();

    emit canceled();
    close();
}