autocookselectionpopup.cpp 4.19 KB
#include "autocookselectionpopup.h"
#include "ui_autocookselectionpopup.h"

#include <QSignalMapper>
#include <QKeyEvent>

#include "soundplayer.h"

AutoCookSelectionPopup::AutoCookSelectionPopup(QWidget *parent, Define::CookType type) :
    QWidget(parent),
    ui(new Ui::AutoCookSelectionPopup),
    type(type), book(CookBook(type))
{
    ui->setupUi(this);

    QSignalMapper *m = new QSignalMapper(this);
    m->setMapping(ui->selection1, 0);
    m->setMapping(ui->selection2, 1);
    m->setMapping(ui->selection3, 2);
    m->setMapping(ui->selection4, 3);
    m->setMapping(ui->selection5, 4);
    m->setMapping(ui->selection6, 5);
    connect(m, SIGNAL(mapped(int)), SLOT(on_clicked(int)));

    connect(ui->selection1, SIGNAL(clicked()), m, SLOT(map()));
    connect(ui->selection2, SIGNAL(clicked()), m, SLOT(map()));
    connect(ui->selection3, SIGNAL(clicked()), m, SLOT(map()));
    connect(ui->selection4, SIGNAL(clicked()), m, SLOT(map()));
    connect(ui->selection5, SIGNAL(clicked()), m, SLOT(map()));
    connect(ui->selection6, SIGNAL(clicked()), m, SLOT(map()));

    ui->bullets->setBulletPixmap(":/images/auto_popup/bullet.png");
    ui->bullets->setCurrentBulletPixmap(":/images/auto_popup/bullet_selected.png");
    ui->bullets->setMaximum((book.list.size() - 1) / 6);

    list();

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

    setFocus();
}

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

void AutoCookSelectionPopup::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 AutoCookSelectionPopup::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 AutoCookSelectionPopup::onEncoderLeft()
{
    QWidget *focused = focusWidget();
    if (focused == NULL || focused == this || focused == ui->selection1)
        ui->next->setFocus();
    else
        focusPreviousChild();
}

void AutoCookSelectionPopup::onEncoderRight()
{
    QWidget *focused = focusWidget();
    if (focused == NULL || focused == this || focused == ui->next)
        ui->selection1->setFocus();
    else
        focusNextChild();
}

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

void AutoCookSelectionPopup::list()
{
    int startAt = ui->bullets->currentIndex() * 6;
    for (int i = 0; i < 6; i++)
    {
        QPushButton *b;
        switch (i)
        {
        case 0:
            b = ui->selection1;
            break;
        case 1:
            b = ui->selection2;
            break;
        case 2:
            b = ui->selection3;
            break;
        case 3:
            b = ui->selection4;
            break;
        case 4:
            b = ui->selection5;
            break;
        case 5:
            b = ui->selection6;
            break;
        }

        int idx = startAt + i;
        if (idx >= book.list.size())
            b->hide();
        else
        {
            b->show();
            b->setText(book.list.at(idx));
        }
    }
}

void AutoCookSelectionPopup::on_clicked(int index)
{
    index += ui->bullets->currentIndex() * 6;

    emit selected(book.get(index));
    close();
}

void AutoCookSelectionPopup::on_prev_clicked()
{
    ui->bullets->setCurrentIndex(ui->bullets->currentIndex() - 1);
    list();
}

void AutoCookSelectionPopup::on_next_clicked()
{
    ui->bullets->setCurrentIndex(ui->bullets->currentIndex() + 1);
    list();
}

void AutoCookSelectionPopup::on_upperClose_clicked()
{
    emit canceled();
    close();
}

void AutoCookSelectionPopup::on_lowerClose_clicked()
{
    emit canceled();
    close();
}