OfficeAssistant_Win10/OfficeAssistant_msvc/MyButton.cpp

98 lines
2.8 KiB
C++
Raw Permalink Normal View History

2023-08-18 19:09:55 +08:00
//
2023-08-03 16:16:02 +08:00
// Created by HW on 2023/07/26.
//
#include "MyButton.h"
2023-08-13 12:16:22 +08:00
#include "config.h"
2023-08-03 16:16:02 +08:00
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->op = buttonStruct.op;
2023-08-18 19:09:55 +08:00
this->orig_name = buttonStruct.orig_name;
this->func = buttonStruct.func;
2023-08-03 16:16:02 +08:00
this->url = buttonStruct.url;
this->path = buttonStruct.path;
this->initial_position = buttonStruct.initial_position;
2023-08-03 16:16:02 +08:00
this->logo=buttonStruct.image;
this->logo_cover = buttonStruct.image_cover;
this->background_color = buttonStruct.background_color;
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;
}
MyButton::~MyButton() {
}
void MyButton::paintEvent(QPaintEvent *e) {
QPushButton::paintEvent(e);
2023-08-13 12:16:22 +08:00
QRectF rect(0, 0, this->width(), this->height());
QColor qColor;
qColor.setNamedColor(background_color);
QPainter painter(this);
painter.fillRect(rect, qColor);
2023-08-03 16:16:02 +08:00
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);
2023-08-18 19:09:55 +08:00
//别问我为啥乘3我也不知道
2023-08-03 16:16:02 +08:00
int text_x=(width()-TEXT_SIZE*text.length()*scale*3)/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*3,TEXT_SIZE*scale*3);
if(checked){
painter.setPen(text_cover_color);
painter.drawImage(logo_rect, *logo_cover);
painter.drawText(text_rect, Qt::AlignCenter, text);
}
else {
painter.setPen(text_color);
painter.drawImage(logo_rect, *logo);
painter.drawText(text_rect, Qt::AlignCenter, text);
}
}
QSize MyButton::sizeHint() const {
return QSize(width2,height2);
}
void MyButton::enterEvent(QEvent *event) {
QWidget::enterEvent(event);
checked = true;
update();
}
void MyButton::leaveEvent(QEvent *event) {
QWidget::leaveEvent(event);
checked = false;
update();
}
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;
2023-08-18 19:09:55 +08:00
emit clicked1(orig_name,op, func, url, path, initial_position);
2023-08-03 16:16:02 +08:00
}