#include "programmedcookpanelbutton.h" #include "ui_programmedcookpanelbutton.h" #include #include "soundplayer.h" ProgrammedCookPanelButton::ProgrammedCookPanelButton(CookRecord record, QWidget *parent) : QWidget(parent), record(record), ui(new Ui::ProgrammedCookPanelButton), rendered(false), longPressEnabled(false) { ui->setupUi(this); setText(record.name); foreach (QPushButton *button, findChildren()) connect(button, &QPushButton::pressed, SoundPlayer::playClick); longPressedTimer.setSingleShot(true); longPressedTimer.setInterval(3000); connect(&longPressedTimer, SIGNAL(timeout()), SLOT(emitLongPressed())); } ProgrammedCookPanelButton::~ProgrammedCookPanelButton() { delete ui; } void ProgrammedCookPanelButton::setText(QString text) { ui->pushButton->setText(text); } void ProgrammedCookPanelButton::showInfo() { if (!rendered) { QPixmap p = CookHistory::render(record); label = new QLabel(this); label->setPixmap(p); label->setGeometry((width() - p.width()) / 2, 65, p.width(), p.height()); } label->show(); setMinimumHeight(ui->pushButton->height() + label->height()); } void ProgrammedCookPanelButton::hideInfo() { label->hide(); setMinimumHeight(ui->pushButton->height()); } void ProgrammedCookPanelButton::setLongPressEnabled(bool enabled) { longPressEnabled = enabled; } QPushButton *ProgrammedCookPanelButton::bar() { return ui->pushButton; } QPushButton *ProgrammedCookPanelButton::infoButton() { return ui->showInfoButton; } QPushButton *ProgrammedCookPanelButton::deleteButton() { return ui->deleteButton; } void ProgrammedCookPanelButton::setEnabled(bool enabled) { ui->pushButton->setEnabled(enabled); ui->showInfoButton->setEnabled(enabled); ui->deleteButton->setEnabled(enabled); } void ProgrammedCookPanelButton::keyPressEvent(QKeyEvent *event) { switch (event->key()) { case 0x01000032: // Turn left event->ignore(); break; case 0x01000031: // Push pushed = focusWidget(); if (pushed == ui->pushButton) on_pushButton_pressed(); break; case 0x01000030: // Turn right event->ignore(); break; } } void ProgrammedCookPanelButton::keyReleaseEvent(QKeyEvent *event) { switch (event->key()) { case 0x01000032: // Turn left event->ignore(); break; case 0x01000031: // Push if (pushed == ui->pushButton) on_pushButton_released(); if (focusWidget() == pushed) onEncoderClicked(pushed); pushed = NULL; break; case 0x01000030: // Turn right event->ignore(); break; } } void ProgrammedCookPanelButton::onEncoderLeft() { } void ProgrammedCookPanelButton::onEncoderRight() { } void ProgrammedCookPanelButton::onEncoderClicked(QWidget *clicked) { QPushButton *b = qobject_cast(clicked); if (b) b->click(); } void ProgrammedCookPanelButton::emitLongPressed() { emitted = true; emit longPressed(this); } void ProgrammedCookPanelButton::on_pushButton_pressed() { longPressedTimer.start(); emitted = false; } void ProgrammedCookPanelButton::on_pushButton_released() { longPressedTimer.stop(); } void ProgrammedCookPanelButton::on_pushButton_clicked() { if (longPressEnabled && emitted) return; emit clicked(this); } void ProgrammedCookPanelButton::on_showInfoButton_clicked() { emit infoClicked(this); } void ProgrammedCookPanelButton::on_deleteButton_clicked() { emit deleteClicked(this); }