favoritenamepopup.cpp 2.46 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);

    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;
}

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::on_okButton_clicked()
{
    ok();
}

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

void FavoriteNamePopup::onEncoderLeft()
{

}

void FavoriteNamePopup::onEncoderRight()
{

}

void FavoriteNamePopup::onEncoderClicked(QWidget *clicked)
{

}