favoritenamepopup.cpp 3.64 KB
#include "favoritenamepopup.h"
#include "ui_favoritenamepopup.h"

#include <QKeyEvent>

#include "soundplayer.h"
#include "primewindow.h"
#include "mainwindow.h"

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

    connect(ui->keyboard, SIGNAL(onOkKeyClicked()), ui->keyboard, SLOT(focusOutKeyboard()));
    connect(ui->keyboard, SIGNAL(onCancelKeyClicked()), ui->keyboard, SLOT(focusOutKeyboard()));
    connect(ui->keyboard, SIGNAL(onCancelKeyClicked()), SLOT(revert()));

    ui->lineEdit->installEventFilter(this);

    ui->lineEdit->setFocus();
    ui->lineEdit->selectAll();

    connect(ui->okButton, &QPushButton::pressed, SoundPlayer::playClick);
    connect(ui->cancelButton, &QPushButton::pressed, SoundPlayer::playClick);
}

FavoriteNamePopup::FavoriteNamePopup(QWidget *parent, ManualCookSetting setting) : FavoriteNamePopup(parent)
{
    type = Manual;
    manualSetting = setting;
}

FavoriteNamePopup::FavoriteNamePopup(QWidget *parent, AutoCookSetting setting) : FavoriteNamePopup(parent)
{
    type = Auto;
    autoSetting = setting;
}

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

bool FavoriteNamePopup::eventFilter(QObject */*watched*/, QEvent *event)
{
    switch (event->type())
    {
    case QEvent::FocusIn:
        lastLine = ui->lineEdit->text();
        break;
    default:
        break;
    }

    return false;
}

void FavoriteNamePopup::keyPressEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000030:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        pushed = focusWidget();
        break;
    case 0x01000032:    // Turn right
        onEncoderRight();
        break;
    }
}

void FavoriteNamePopup::keyReleaseEvent(QKeyEvent *event)
{
    switch (event->key())
    {
    case 0x01000030:    // Turn left
        onEncoderLeft();
        break;
    case 0x01000031:    // Push
        if (focusWidget() == pushed)
            onEncoderClicked(pushed);

        pushed = NULL;
        break;
    case 0x01000032:    // Turn right
        onEncoderRight();
        break;
    }
}

void FavoriteNamePopup::ok()
{
    int id;
    switch (type)
    {
    case Manual:
        id = CookHistory::addFavorite(manualSetting, ui->lineEdit->text());
        break;
    case Auto:
        id = CookHistory::addFavorite(autoSetting, ui->lineEdit->text());
        break;
    default:
        return;
    }

    PrimeWindow *w = new PrimeWindow(MainWindow::getInstance());
    w->setWindowModality(Qt::WindowModal);
    w->listFavorites();
    w->focusFavorite(id);
    w->showFullScreen();
    w->raise();

    MainWindow::jump(w);
}

void FavoriteNamePopup::cancel()
{
    deleteLater();
}

void FavoriteNamePopup::revert()
{
    ui->lineEdit->setText(lastLine);

    if (focusWidget() == ui->lineEdit)
        ui->lineEdit->selectAll();
}

void FavoriteNamePopup::on_okButton_clicked()
{
    ok();
}

void FavoriteNamePopup::on_cancelButton_clicked()
{
    cancel();
}

void FavoriteNamePopup::onEncoderLeft()
{
    if (focusWidget() == ui->lineEdit)
        ui->cancelButton->setFocus();
    else
        focusPreviousChild();
}

void FavoriteNamePopup::onEncoderRight()
{
    if (focusWidget() == ui->cancelButton)
    {
        ui->lineEdit->setFocus();
        ui->lineEdit->selectAll();
    }
    else
        focusNextChild();
}

void FavoriteNamePopup::onEncoderClicked(QWidget *clicked)
{
    QPushButton *b = qobject_cast<QPushButton *>(clicked);
    if (b)
        b->click();
    else
    {
        QLineEdit *e = qobject_cast<QLineEdit *>(clicked);
        if (e)
            ui->keyboard->focusInKeyboard();
    }
}