2023-07-26 19:48:14 +08:00
|
|
|
|
//
|
|
|
|
|
// Created by HW on 2023/07/26.
|
|
|
|
|
//
|
2023-07-27 13:44:54 +08:00
|
|
|
|
#define TEXT_SIZE 7
|
2023-07-26 19:48:14 +08:00
|
|
|
|
#include "MyButton.h"
|
2023-07-30 01:06:42 +08:00
|
|
|
|
MyButton::MyButton(ButtonStruct &buttonStruct,int width,int height,QList<MyButton *> *buttons,QWidget *parent) : QPushButton(parent) {
|
2023-07-27 13:44:54 +08:00
|
|
|
|
this->buttons=buttons;
|
2023-07-26 19:48:14 +08:00
|
|
|
|
this->width2=width;
|
|
|
|
|
this->height2=height;
|
2023-07-30 01:06:42 +08:00
|
|
|
|
this->text=buttonStruct.text;
|
2023-07-30 10:26:41 +08:00
|
|
|
|
this->url = buttonStruct.url;
|
2023-07-30 01:06:42 +08:00
|
|
|
|
this->logo=buttonStruct.image;
|
|
|
|
|
this->logo_cover = buttonStruct.image_cover;
|
2023-08-01 09:04:53 +08:00
|
|
|
|
QString style = "background-color:";
|
|
|
|
|
style += buttonStruct.background_color;
|
|
|
|
|
style += ";border:none;";
|
|
|
|
|
this->setStyleSheet(style);
|
|
|
|
|
this->text_color = buttonStruct.text_color;
|
|
|
|
|
this->text_cover_color = buttonStruct.text_cover_color;
|
|
|
|
|
this->checked = false;
|
2023-07-26 19:48:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MyButton::~MyButton() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyButton::paintEvent(QPaintEvent *e) {
|
|
|
|
|
QPushButton::paintEvent(e);
|
|
|
|
|
QPainter painter(this);
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
2023-07-27 13:44:54 +08:00
|
|
|
|
int logo_width=width()/3;
|
|
|
|
|
int logo_x=width()/3;
|
|
|
|
|
int logo_y=(height()-logo_width)/3;
|
|
|
|
|
setContentsMargins(0,0,0,0);
|
2023-07-26 19:48:14 +08:00
|
|
|
|
QFont ft;
|
2023-07-27 13:44:54 +08:00
|
|
|
|
ft.setPixelSize(TEXT_SIZE);
|
2023-08-02 07:18:10 +08:00
|
|
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊɶ<CEAA><C9B6>3<EFBFBD><33><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2>֪<EFBFBD><D6AA>
|
|
|
|
|
int text_x=(width()-TEXT_SIZE*text.length()*scale*3)/2;
|
2023-07-27 13:44:54 +08:00
|
|
|
|
int text_y=(logo_width+logo_y+10);
|
|
|
|
|
QRectF logo_rect(logo_x, logo_y, logo_width, logo_width);
|
2023-08-02 07:18:10 +08:00
|
|
|
|
QRectF text_rect(text_x,text_y, TEXT_SIZE*text.length()*scale*3,TEXT_SIZE*scale*3);
|
2023-07-30 13:57:54 +08:00
|
|
|
|
|
2023-07-27 13:44:54 +08:00
|
|
|
|
if(checked){
|
2023-08-01 09:04:53 +08:00
|
|
|
|
painter.setPen(text_cover_color);
|
2023-07-30 13:57:54 +08:00
|
|
|
|
painter.drawImage(logo_rect, *logo_cover);
|
|
|
|
|
painter.drawText(text_rect, Qt::AlignCenter, text);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-08-01 09:04:53 +08:00
|
|
|
|
painter.setPen(text_color);
|
2023-07-30 13:57:54 +08:00
|
|
|
|
painter.drawImage(logo_rect, *logo);
|
|
|
|
|
painter.drawText(text_rect, Qt::AlignCenter, text);
|
|
|
|
|
}
|
2023-07-27 13:44:54 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
QSize MyButton::sizeHint() const {
|
|
|
|
|
return QSize(width2,height2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyButton::enterEvent(QEvent *event) {
|
|
|
|
|
QWidget::enterEvent(event);
|
2023-08-01 09:04:53 +08:00
|
|
|
|
checked = true;
|
|
|
|
|
update();
|
2023-07-27 13:44:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyButton::leaveEvent(QEvent *event) {
|
|
|
|
|
QWidget::leaveEvent(event);
|
2023-08-01 09:04:53 +08:00
|
|
|
|
checked = false;
|
|
|
|
|
update();
|
2023-07-27 13:44:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MyButton::mousePressEvent(QMouseEvent *event) {
|
|
|
|
|
QPushButton::mousePressEvent(event);
|
2023-07-26 19:48:14 +08:00
|
|
|
|
}
|
2023-07-27 13:44:54 +08:00
|
|
|
|
|
|
|
|
|
void MyButton::mouseReleaseEvent(QMouseEvent *event) {
|
|
|
|
|
QPushButton::mouseReleaseEvent(event);
|
|
|
|
|
for(auto button:*buttons){
|
|
|
|
|
button->setChecked(false);
|
|
|
|
|
button->update();
|
|
|
|
|
}
|
|
|
|
|
if(checked)
|
|
|
|
|
checked=false;
|
|
|
|
|
else
|
|
|
|
|
checked=true;
|
2023-07-30 10:26:41 +08:00
|
|
|
|
emit clicked1(text, url);
|
2023-07-27 13:44:54 +08:00
|
|
|
|
}
|
|
|
|
|
|