primewindow.cpp 8.12 KB
#include "primewindow.h"
#include "ui_primewindow.h"

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

#include "manualcooksettingwidget.h"
#include "cookhistory.h"
#include "soundplayer.h"
#include "configwindow.h"
#include "washwindow.h"
#include "mainwindow.h"

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

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

    ui->verticalScrollLayout->setAlignment(Qt::AlignTop);

    lastInfoDisplayed = NULL;

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

    setFocus();
}

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

void PrimeWindow::listMostCooked()
{
    if (!ui->mostCookedButton->isChecked())
    {
        ui->mostCookedButton->blockSignals(true);
        ui->mostCookedButton->setChecked(true);
        ui->mostCookedButton->blockSignals(false);
    }

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

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

    listButtons(CookHistory::listMostCooked());
}

void PrimeWindow::listRecents()
{
    if (!ui->recentsButton->isChecked())
    {
        ui->recentsButton->blockSignals(true);
        ui->recentsButton->setChecked(true);
        ui->recentsButton->blockSignals(false);
    }

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

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

    listButtons(CookHistory::listRecents());
}

void PrimeWindow::listFavorites()
{
    if (!ui->favoritesButton->isChecked())
    {
        ui->favoritesButton->blockSignals(true);
        ui->favoritesButton->setChecked(true);
        ui->favoritesButton->blockSignals(false);
    }

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

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

    listButtons(CookHistory::listFavorites());
}

void PrimeWindow::focusFavorite(int id)
{
    foreach (CookPanelButton *b, list)
    {
        if (b->record.id == id)
        {
            b->focusBar();
            break;
        }
    }
}

void PrimeWindow::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 PrimeWindow::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 PrimeWindow::on_mostCookedButton_toggled(bool checked)
{
    if (checked)
        listMostCooked();
    else
    {
        if (!ui->recentsButton->isChecked() && !ui->favoritesButton->isChecked())
        {
            ui->mostCookedButton->blockSignals(true);
            ui->mostCookedButton->setChecked(true);
            ui->mostCookedButton->blockSignals(false);
        }
    }
}

void PrimeWindow::on_recentsButton_toggled(bool checked)
{
    if (checked)
        listRecents();
    else
    {
        if (!ui->mostCookedButton->isChecked() && !ui->favoritesButton->isChecked())
        {
            ui->recentsButton->blockSignals(true);
            ui->recentsButton->setChecked(true);
            ui->recentsButton->blockSignals(false);
        }
    }
}

void PrimeWindow::on_favoritesButton_toggled(bool checked)
{
    if (checked)
        listFavorites();
    else
    {
        if (!ui->mostCookedButton->isChecked() && !ui->recentsButton->isChecked())
        {
            ui->favoritesButton->blockSignals(true);
            ui->favoritesButton->setChecked(true);
            ui->favoritesButton->blockSignals(false);
        }
    }
}

void PrimeWindow::listButtons(QList<CookRecord> records)
{
    clear();

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

    ui->scrollAreaWidgetContents->adjustSize();
}

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

CookPanelButton *PrimeWindow::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*)));

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

    return button;
}

void PrimeWindow::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 PrimeWindow::onDeleteButtonClicked(CookPanelButton *panelButton)
{
    if (panelButton == lastInfoDisplayed)
        lastInfoDisplayed = NULL;

    if (ui->mostCookedButton->isChecked())
        CookHistory::removeMostCooked(panelButton->record);
    else if (ui->recentsButton->isChecked())
        CookHistory::removeRecent(panelButton->record);
    else if (ui->favoritesButton->isChecked())
        CookHistory::removeFavorite(panelButton->record);

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


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

void PrimeWindow::on_configButton_clicked()
{
    ConfigWindow *w = new ConfigWindow(MainWindow::getInstance());
    w->setWindowModality(Qt::WindowModal);
    w->showFullScreen();
    w->raise();

    MainWindow::jump(w);
}

void PrimeWindow::on_washButton_clicked()
{
    WashWindow *w = new WashWindow(MainWindow::getInstance());
    w->setWindowModality(Qt::WindowModal);
    w->showFullScreen();
    w->raise();

    MainWindow::jump(w);
}

void PrimeWindow::on_helpButton_clicked()
{

}

void PrimeWindow::onEncoderLeft()
{
    QWidget *focused = focusWidget();
    if (focused == this || focused == NULL)
        ui->helpButton->setFocus();
    else if (list.size() > 0)
    {
        if (focused == ui->backButton)
            list.last()->focusDeleteButton();
        else if (focused == list.first()->bar())
            ui->favoritesButton->setFocus();
        else if (focused == ui->mostCookedButton)
            ui->helpButton->setFocus();
        else
            focusPreviousChild();

        ui->scrollArea->ensureWidgetVisible(focusWidget());
    }
    else
        focusPreviousChild();
}

void PrimeWindow::onEncoderRight()
{
    QWidget *focused = focusWidget();
    if (focused == this || focused == NULL)
        ui->mostCookedButton->setFocus();
    else if (list.size() > 0)
    {
        if (focused == ui->favoritesButton)
            list.first()->focusBar();
        else if (focused == list.last()->deleteButton())
            ui->backButton->setFocus();
        else if (focused == ui->helpButton)
            ui->mostCookedButton->setFocus();
        else
            focusNextChild();

        ui->scrollArea->ensureWidgetVisible(focusWidget());
    }
    else
        focusNextChild();
}

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