multicookautowindow.cpp 3.54 KB
#include "multicookautowindow.h"
#include "ui_multicookautowindow.h"

#include <QKeyEvent>

#include "soundplayer.h"
#include "confirmpopup.h"
#include "manualviewerdlg.h"

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

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    book = Q_NULLPTR;

    setFocus();
}

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

void MultiCookAutoWindow::setType(Define::CookType type)
{
    ui->cookTypeIcon->setPixmap(Define::icon(type));
}

void MultiCookAutoWindow::setBook(MultiCookBook *book)
{
    this->book = book;

    QSignalMapper *sm = new QSignalMapper(this);
    connect(sm, SIGNAL(mapped(int)), SLOT(select(int)));

    QFont font = this->font();
    font.setPointSize(12);
    font.setBold(true);
    font.setWeight(75);

    QLatin1String stylesheet("QPushButton {\n"
    "border-image: url(:/images/button/288.png);\n"
    "}\n"
    "QPushButton::pressed, QPushButton:focus {\n"
    "border-image: url(:/images/button/288_ov.png);\n"
    "}");

    QList<QString> list = book->names();

    QWidget *last = this;
    for (int idx = 0; idx < list.size(); idx++)
    {
        int x = 14 + (idx % 3) * 353;
        int y = 738 + (idx / 3) * 96;

        QPushButton *pb = new QPushButton(this);
        pb->setGeometry(QRect(x, y, 345, 84));
        pb->setFont(font);
        pb->setStyleSheet(stylesheet);
        pb->setText(list.at(idx));

        sm->setMapping(pb, idx);
        connect(pb, SIGNAL(clicked()), sm, SLOT(map()));

        setTabOrder(last, pb);

        last = pb;
    }

    setTabOrder(last, ui->backButton);
    setTabOrder(ui->backButton, ui->helpButton);

    foreach (QPushButton *button, findChildren<QPushButton *>())
        connect(button, &QPushButton::pressed, SoundPlayer::playClick);
}

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

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

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

void MultiCookAutoWindow::onEncoderLeft()
{
    focusPreviousChild();
}

void MultiCookAutoWindow::onEncoderRight()
{
    focusNextChild();
}

void MultiCookAutoWindow::onEncoderClicked(QWidget *clicked)
{
    QPushButton *b = qobject_cast<QPushButton *>(clicked);
    if (b)
        b->click();
}

void MultiCookAutoWindow::select(int idx)
{
    selectedIndex = idx;

    setFocus();

    ConfirmPopup *p = new ConfirmPopup(this, tr("다중 요리 목록에 추가하시겠습니까?"));
    p->showFullScreen();

    connect(p, SIGNAL(accepted()), SLOT(confirm()));
}

void MultiCookAutoWindow::confirm()
{
    MultiAutoCook *cook = book->cook(selectedIndex);
    if (cook == Q_NULLPTR)
        return;

    emit selected(cook);
}

void MultiCookAutoWindow::on_backButton_clicked()
{
    close();
}

void MultiCookAutoWindow::on_helpButton_clicked()
{
    ManualViewerDlg* dlg = new ManualViewerDlg(this);
    dlg->showFullScreen();
    dlg->raise();
}