//
// Created by HW on 2023/07/26.
//
#define TEXT_SIZE 7
#include "MyButton.h"
MyButton::MyButton(ButtonStruct &buttonStruct,int width,int height,QList<MyButton *> *buttons,QWidget *parent) : QPushButton(parent) {
    this->buttons=buttons;
    this->width2=width;
    this->height2=height;
    this->text=buttonStruct.text;
    this->logo=buttonStruct.image;
	this->logo_cover = buttonStruct.image_cover;
    this->setStyleSheet("background-color:#333332;/*border:none;*/");
}

MyButton::~MyButton() {
    delete logo;
	delete logo_cover;
}

void MyButton::paintEvent(QPaintEvent *e) {
    QPushButton::paintEvent(e);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    int logo_width=width()/3;
    int logo_x=width()/3;
    int logo_y=(height()-logo_width)/3;
    setContentsMargins(0,0,0,0);
    QFont ft;
    ft.setPixelSize(TEXT_SIZE);
    int text_x=(width()-TEXT_SIZE*text.length()*scale*2)/2;
    int text_y=(logo_width+logo_y+10);
    QRectF logo_rect(logo_x, logo_y, logo_width, logo_width);
    QRectF text_rect(text_x,text_y, TEXT_SIZE*text.length()*scale*2,TEXT_SIZE*scale*2);
    painter.setPen(QColor("#FFFFFF"));
    painter.drawImage(logo_rect, *logo);
    painter.drawText(text_rect, Qt::AlignCenter, text);
    if(checked){
        QRectF rect(0, this->height()-6, this->width(), 5);
        painter.fillRect(rect, QColor("#FFFFFF"));
    }

}
QSize MyButton::sizeHint() const {
    return QSize(width2,height2);
}

void MyButton::enterEvent(QEvent *event) {
    QWidget::enterEvent(event);
    this->setStyleSheet("background-color:#646464;border:none;");
}

void MyButton::leaveEvent(QEvent *event) {
    QWidget::leaveEvent(event);
    this->setStyleSheet("background-color:#333332;border:none;");
}

void MyButton::mousePressEvent(QMouseEvent *event) {
    QPushButton::mousePressEvent(event);
}

void MyButton::mouseReleaseEvent(QMouseEvent *event) {
    QPushButton::mouseReleaseEvent(event);
    for(auto button:*buttons){
        button->setChecked(false);
        button->update();
    }
    if(checked)
        checked=false;
    else
        checked=true;
}

