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

#include <QPainter>
#include <QtDebug>

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

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

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

    listButtons(CookProgram::listAuto());
}

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

    listButtons(CookProgram::listManual());
}

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();

    foreach (CookRecord r, record)
        newButton(r);

    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)
        return;

    listAuto();
}

void ProgrammingWindow::on_manualButton_toggled(bool checked)
{
    if (!checked)
        return;

    listManual();
}

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

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

void ProgrammingWindow::on_helpButton_clicked()
{

}