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

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

#include "manualcooksettingwidget.h"
#include "cookhistory.h"
#include "soundplayer.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);
}

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

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

    listButtons(CookHistory::listMostCooked());
}

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

    listButtons(CookHistory::listRecents());
}

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

    listButtons(CookHistory::listFavorites());
}

void PrimeWindow::focusFavorite(int id)
{
    foreach (CookPanelButton *b, list)
    {
        qDebug() << "Favorite ID" << b->record.id;
        if (b->record.id == id)
        {
//            b->setFocus();
            b->focusBar();
            break;
        }
    }
}

void PrimeWindow::on_mostCookedButton_toggled(bool checked)
{
    if (!checked)
        return;

    listButtons(CookHistory::listMostCooked());
}

void PrimeWindow::on_recentsButton_toggled(bool checked)
{
    if (!checked)
        return;

    listButtons(CookHistory::listRecents());
}

void PrimeWindow::on_favoritesButton_toggled(bool checked)
{
    if (!checked)
        return;

    listButtons(CookHistory::listFavorites());
}

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