| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "suggestbutton.h" |
| 6 | |
| 7 | #include "widgetutil.h" |
| 8 | #include <QPainter> |
| 9 | #include <QDebug> |
| 10 | |
| 11 | SuggestButton::SuggestButton(DWidget *parent) : |
| 12 | DPushButton(parent), |
| 13 | israised(true) |
| 14 | { |
| 15 | // this->setStyleSheet(WidgetUtil::getQss(":/theme/light/SuggestButton.theme")); |
| 16 | // this->setFixedSize(QPixmap(":/theme/light/image/suggest_button_hover.svg").size()); |
| 17 | this->setFixedSize(310, 36); |
| 18 | m_filletradii = 8; |
| 19 | } |
| 20 | |
| 21 | //void SuggestButton::mSetText(QString text) |
| 22 | //{ |
| 23 | // m_text = text; |
| 24 | //} |
| 25 | |
| 26 | void SuggestButton::paintEvent(QPaintEvent *event) |
| 27 | { |
| 28 | DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 29 | QColor disablecolor, raisedcolor, disraisedcolor, textcolor; |
| 30 | if (themeType == DGuiApplicationHelper::LightType) { |
| 31 | disablecolor = QColor(0, 0, 0, 13); |
| 32 | raisedcolor = QColor("#E3E3E3"); |
| 33 | disraisedcolor = QColor("#00BFFF"); |
| 34 | textcolor = QColor("#414D68"); |
| 35 | } else if (themeType == DGuiApplicationHelper::DarkType) { |
| 36 | disablecolor = QColor(0, 0, 0, 13); |
| 37 | raisedcolor = QColor("#E3E3E3"); |
| 38 | disraisedcolor = QColor("#00BFFF"); |
| 39 | textcolor = QColor("#414D68"); |
| 40 | } |
| 41 | QPainter painter(this); |
| 42 | painter.setRenderHint(QPainter::Antialiasing); |
| 43 | if (!this->isEnabled()) { |
| 44 | painter.setBrush(QBrush(disablecolor)); |
| 45 | } else { |
| 46 | if (israised) |
| 47 | painter.setBrush(QBrush(raisedcolor)); |
| 48 | else |
| 49 | painter.setBrush(QBrush(disraisedcolor)); |
| 50 | } |
| 51 | painter.setPen(Qt::transparent); |
| 52 | QRect rect = this->rect(); |
| 53 | rect.setWidth(rect.width() - 1); |
| 54 | rect.setHeight(rect.height() - 1); |
| 55 | painter.drawRoundedRect(rect, m_filletradii, m_filletradii); |
| 56 | QFont qf = font(); |
| 57 | qf.setPixelSize(14); |
| 58 | painter.setFont(qf); |
| 59 | // painter.setPen(Qt::black); |
| 60 | painter.setPen(textcolor); |
| 61 | painter.drawText(0, 0, this->width(), this->height(), Qt::AlignCenter, text()); |
| 62 | // DPushButton::paintEvent(event); |
| 63 | } |
| 64 | |
| 65 | void SuggestButton::enterEvent(QEvent *e) |
| 66 | |
| 67 | { |
| 68 | israised = false; |
| 69 | repaint(); //重新绘制按钮 |
| 70 | } |
| 71 | |
| 72 | void SuggestButton::leaveEvent(QEvent *e) |
| 73 | { |
| 74 | israised = true; |
| 75 | repaint(); |
| 76 | } |
| 77 | |
| 78 | void SuggestButton::mouseEvent(QMouseEvent *e) |
| 79 | { |
| 80 | float w = this->width(); |
| 81 | float h = this->height(); |
| 82 | int x = e->x(); |
| 83 | int y = e->y(); |
| 84 | float k = h / w; //斜率 |
| 85 | if ( y > -k * x + h / 2 && |
| 86 | y >= k * x - h / 2 && |
| 87 | y <= k * x + h / 2 && |
| 88 | y <= -k * x + 3 * h / 2) { |
| 89 | israised = false; |
| 90 | } else { |
| 91 | israised = true; |
| 92 | } |
| 93 | repaint(); |
| 94 | } |