configpanelbutton.cpp 3.12 KB
#include "configpanelbutton.h"
#include "ui_configpanelbutton.h"

#include <QPixmap>
#include <QPainter>
#include <QDebug>
#include <QFocusEvent>

#include "soundplayer.h"

ConfigPanelButton::ConfigPanelButton(QWidget *parent, uint16_t btn_id) :
    QWidget(parent),
    ui(new Ui::ConfigPanelButton)
{
    ui->setupUi(this);

    btnid = btn_id;

    showingFavoriteButton = false;
    ui->favoriteButton->hide();

    textRect = QRect(20, 0, 556, 65);
    valueRect = QRect(556, 0, 265, 65);
//    connect(ui->pushButton, SIGNAL(pressed()), SIGNAL(pressed()));
//    connect(ui->pushButton, SIGNAL(released()), SIGNAL(released()));
//    connect(ui->pushButton, SIGNAL(clicked()), SIGNAL(clicked()));

//    connect(ui->favoriteButton,SIGNAL(clicked(bool)),SIGNAL(checkButtonClicked(bool)));

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

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

bool ConfigPanelButton::isFavoriteFocused()
{
    return (focusWidget() == ui->favoriteButton);
}

void ConfigPanelButton::setText(const QString &text)
{
    if (text_ == text)
        return;

    text_ = text;

    updateIcon();
}

void ConfigPanelButton::setValue(const QString &value)
{
    if (value_ == value)
        return;

    value_ = value;

    updateIcon();
}

void ConfigPanelButton::showFavoriteButton()
{
    if (showingFavoriteButton)
        return;

    showingFavoriteButton = true;
    ui->favoriteButton->show();

    textRect = QRect(20 + 77, 0, 556 - 77, 65);
    updateIcon();
}

void ConfigPanelButton::hideFavoriteButton()
{
    if (!showingFavoriteButton)
        return;

    showingFavoriteButton = false;
    ui->favoriteButton->hide();

    textRect = QRect(20, 0, 556, 65);
    updateIcon();
}

void ConfigPanelButton::updateIcon()
{
    QPixmap pixmap(ui->pushButton->size());
    pixmap.fill(Qt::transparent);

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

    QPainter painter(&pixmap);
    painter.setFont(font);
    painter.setPen(Qt::white);
    painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text_);
    painter.setPen(Qt::gray);
    painter.drawText(valueRect, Qt::AlignCenter, value_);

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

bool ConfigPanelButton::isFavoriteChecked(){
    return ui->favoriteButton->isChecked();
}

void ConfigPanelButton::setFavoriteCheck(bool checked){
    ui->favoriteButton->setChecked(checked);
}

void ConfigPanelButton::focusInEvent(QFocusEvent *event)
{
    switch (event->reason())
    {
    case Qt::TabFocusReason:
        ui->pushButton->setFocus();
        break;
    case Qt::BacktabFocusReason:
        ui->favoriteButton->setFocus();
        break;
    }
}

void ConfigPanelButton::on_favoriteButton_clicked(bool checked)
{
    emit checkButtonClicked(btnid,checked);
}



void ConfigPanelButton::on_pushButton_clicked()
{
    emit clicked(btnid);
}



void ConfigPanelButton::on_pushButton_released()
{
    emit released(btnid);
}


void ConfigPanelButton::on_pushButton_pressed()
{
    emit pressed(btnid);
}