programmingwindow.cpp 8.2 KB
#include "programmingwindow.h"
#include "ui_programmingwindow.h"

#include <QPainter>
#include <QtDebug>
#include <QKeyEvent>

#include "programmingmanualwindow.h"
#include "programmingselectionwindow.h"
#include "cookprogram.h"
#include "soundplayer.h"
#include "confirmpopup.h"
#include "programmingnamepopup.h"

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

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

    setupUi();

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

    setFocus();
}

ProgrammingWindow::~ProgrammingWindow()
{
    CookProgram::discard();
    delete ui;
}

void ProgrammingWindow::listAuto()
{
    if (!ui->autoButton->isChecked())
    {
        ui->autoButton->blockSignals(true);
        ui->autoButton->setChecked(true);
        ui->autoButton->blockSignals(false);
    }

    if (ui->manualButton->isChecked())
        ui->manualButton->setChecked(false);

    listButtons(CookProgram::listAuto());
}

void ProgrammingWindow::listManual()
{
    if (!ui->manualButton->isChecked())
    {
        ui->manualButton->blockSignals(true);
        ui->manualButton->setChecked(true);
        ui->manualButton->blockSignals(false);
    }

    if (ui->autoButton->isChecked())
        ui->autoButton->setChecked(false);

    listButtons(CookProgram::listManual());
}

void ProgrammingWindow::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 ProgrammingWindow::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 ProgrammingWindow::setupUi()
{
    ui->verticalScrollLayout->setAlignment(Qt::AlignTop);

    QFont font = ui->addButton->font();
    font.setPixelSize(30);

    int textWidth = QFontMetrics(font).width(tr("추가하기"));

    QPixmap iconPix(":/images/etc/bar_icon_03.png");

    QPixmap pixmap(QSize(iconPix.width() + 20 + textWidth, ui->addButton->height()));
    pixmap.fill(Qt::transparent);

    QRect textRect(iconPix.width() + 20, 0, textWidth, pixmap.height());

    QPainter painter(&pixmap);
    painter.setFont(font);
    painter.setPen(Qt::white);
    painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, tr("추가하기"));
    painter.drawPixmap(0, (pixmap.height() - iconPix.height()) / 2, iconPix);

    QIcon icon(pixmap);
    ui->addButton->setIcon(icon);
    ui->addButton->setIconSize(pixmap.size());
    ui->addButton->hide();
}

void ProgrammingWindow::updateView()
{
    if (ui->autoButton->isChecked())
        listAuto();
    else if (ui->manualButton->isChecked())
        listManual();
}

void ProgrammingWindow::listButtons(QList<CookRecord> record)
{
    clear();

    ui->addButton->show();

    setTabOrder(this, ui->autoButton);
    setTabOrder(ui->autoButton, ui->manualButton);
    setTabOrder(ui->manualButton, ui->addButton);

    QWidget *last = ui->addButton;
    foreach (CookRecord r, record)
    {
        CookPanelButton *b = newButton(r);
        setTabOrder(last, b->bar());
        setTabOrder(b->bar(), b->infoButton());
        setTabOrder(b->infoButton(), b->deleteButton());
        last = b->deleteButton();
    }

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

    ui->scrollAreaWidgetContents->adjustSize();
}

void ProgrammingWindow::clear()
{
    lastInfoDisplayed = NULL;
    while (!list.isEmpty())
        list.takeFirst()->deleteLater();
}

CookPanelButton *ProgrammingWindow::newButton(CookRecord record)
{
    CookPanelButton *button = new CookPanelButton(record, this);
    connect(button, SIGNAL(infoClicked(CookPanelButton*)), SLOT(onInfoButtonClicked(CookPanelButton*)));
    connect(button, SIGNAL(deleteClicked(CookPanelButton*)), SLOT(onDeleteButtonClicked(CookPanelButton*)));
    connect(button, SIGNAL(longPressed(CookPanelButton*)), SLOT(onLongPressed(CookPanelButton*)));

    button->setLongPressEnabled(true);

    ui->verticalScrollLayout->addWidget(button);
    list.append(button);

    return button;
}

void ProgrammingWindow::back()
{
    CookProgram::discard();
    close();
}

void ProgrammingWindow::save()
{
    CookProgram::save();
}

void ProgrammingWindow::onInfoButtonClicked(CookPanelButton *panelButton)
{
    if (lastInfoDisplayed)
    {
        if (panelButton == lastInfoDisplayed)
        {
            lastInfoDisplayed->hideInfo();
            lastInfoDisplayed = NULL;

            ui->scrollAreaWidgetContents->adjustSize();
        }
        else
        {
            lastInfoDisplayed->hideInfo();
            lastInfoDisplayed = panelButton;
            lastInfoDisplayed->showInfo();

            ui->scrollAreaWidgetContents->adjustSize();
            ui->scrollArea->ensureWidgetVisible(lastInfoDisplayed);
        }
    }
    else
    {
        lastInfoDisplayed = panelButton;
        lastInfoDisplayed->showInfo();

        ui->scrollAreaWidgetContents->adjustSize();
        ui->scrollArea->ensureWidgetVisible(lastInfoDisplayed);
    }
}

void ProgrammingWindow::onDeleteButtonClicked(CookPanelButton *panelButton)
{
    if (panelButton == lastInfoDisplayed)
        lastInfoDisplayed = NULL;

    CookProgram::remove(panelButton->record);

    list.removeAll(panelButton);
    panelButton->deleteLater();
}

void ProgrammingWindow::onLongPressed(CookPanelButton *panelButton)
{
    panelButton->setEnabled(false);

    ProgrammingNamePopup *p = new ProgrammingNamePopup(this, panelButton->record);
    connect(p, SIGNAL(changed()), SLOT(updateView()));
    connect(p, SIGNAL(destroyed(QObject*)), panelButton, SLOT(setEnabled()));
    p->showFullScreen();
}

void ProgrammingWindow::on_addButton_clicked()
{
    ProgrammingSelectionWindow *w = new ProgrammingSelectionWindow(this);

    if (ui->autoButton->isChecked())
        w->setCookTypeEnabled(true);
    else if (ui->manualButton->isChecked())
        w->setModeEnabled(true);

    w->setWindowModality(Qt::WindowModal);
    w->showFullScreen();
    w->raise();

    connect(w, SIGNAL(added()), SLOT(updateView()));
}

void ProgrammingWindow::on_autoButton_toggled(bool checked)
{
    if (checked)
        listAuto();
    else
    {
        if (!ui->manualButton->isChecked())
        {
            ui->autoButton->blockSignals(true);
            ui->autoButton->setChecked(true);
            ui->autoButton->blockSignals(false);
        }
    }
}

void ProgrammingWindow::on_manualButton_toggled(bool checked)
{
    if (checked)
        listManual();
    else
    {
        if (!ui->autoButton->isChecked())
        {
            ui->manualButton->blockSignals(true);
            ui->manualButton->setChecked(true);
            ui->manualButton->blockSignals(false);
        }
    }
}

void ProgrammingWindow::on_backButton_clicked()
{
    if (CookProgram::isModified())
    {
        ConfirmPopup *p = new ConfirmPopup(this, tr("저장하지 않고 돌아가시겠습니까?"));
        connect(p, SIGNAL(accepted()), SLOT(back()));
        p->showFullScreen();
    }
    else
        close();
}

void ProgrammingWindow::on_saveButton_clicked()
{
    ConfirmPopup *p = new ConfirmPopup(this, tr("저장하시겠습니까?"));
    connect(p, SIGNAL(accepted()), SLOT(save()));
    p->showFullScreen();
}

void ProgrammingWindow::on_helpButton_clicked()
{

}

void ProgrammingWindow::onEncoderLeft()
{
    focusPreviousChild();
    ui->scrollArea->ensureWidgetVisible(focusWidget());
}

void ProgrammingWindow::onEncoderRight()
{
    focusNextChild();
    ui->scrollArea->ensureWidgetVisible(focusWidget());
}

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