| 1 |
/* |
| 2 |
* Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co.,Ltd. |
| 3 |
* |
| 4 |
* Author: Ji XiangLong <[email protected]> |
| 5 |
* |
| 6 |
* Maintainer: WangYu <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "toolbutton.h" |
| 23 |
#include <QPainter> |
| 24 |
#include <QStylePainter> |
| 25 |
#include <QSvgRenderer> |
| 26 |
#include <QStyleOptionButton> |
| 27 |
#include <qtoolbutton.h> |
| 28 |
#include <DGuiApplicationHelper> |
| 29 |
|
| 30 |
ToolButton::ToolButton(QWidget *parent) : DPushButton(parent) |
| 31 |
{ |
| 32 |
setMinimumSize(200, 11); |
| 33 |
} |
| 34 |
|
| 35 |
void ToolButton::setText(const QString &text) |
| 36 |
{ |
| 37 |
//控件设置需要绘制的文字 |
| 38 |
m_text = text; |
| 39 |
update(); |
| 40 |
} |
| 41 |
|
| 42 |
void ToolButton::setLIcon(const QIcon &icon) |
| 43 |
{ |
| 44 |
//控件设置需要绘制的图片 |
| 45 |
m_Licon = icon; |
| 46 |
update(); |
| 47 |
} |
| 48 |
|
| 49 |
void ToolButton::setRIcon(const QIcon &icon) |
| 50 |
{ |
| 51 |
//控件设置需要绘制的图片 |
| 52 |
m_Ricon = icon; |
| 53 |
} |
| 54 |
|
| 55 |
void ToolButton::setRWIcon(const QString &path) |
| 56 |
{ |
| 57 |
m_RiconWhite = QIcon::fromTheme(path); |
| 58 |
} |
| 59 |
|
| 60 |
void ToolButton::paintEvent(QPaintEvent *e) |
| 61 |
{ |
| 62 |
Q_UNUSED(e) |
| 63 |
|
| 64 |
QStylePainter p(this); |
| 65 |
QStyleOptionButton option; |
| 66 |
initStyleOption(&option); |
| 67 |
|
| 68 |
QPainter painter(this); |
| 69 |
//绘制背景 |
| 70 |
painter.save(); |
| 71 |
//非禁用状态绘制背景 |
| 72 |
if (option.state & QStyle::State_Enabled) { |
| 73 |
if (option.state & QStyle::State_MouseOver) { |
| 74 |
painter.setPen(Qt::NoPen); |
| 75 |
//获取系统主题颜色 |
| 76 |
QColor hovertColor(option.palette.highlight().color()); |
| 77 |
painter.setBrush(hovertColor); |
| 78 |
painter.drawRect(this->rect()); |
| 79 |
} |
| 80 |
} |
| 81 |
painter.restore(); |
| 82 |
|
| 83 |
if (Dtk::Gui::DGuiApplicationHelper::instance()->themeType() == 1) { |
| 84 |
//控件禁用样式 |
| 85 |
if (!(option.state & QStyle::State_Enabled)) { |
| 86 |
painter.setPen(QColor("#9C9C9C")); |
| 87 |
} |
| 88 |
|
| 89 |
//鼠标悬停画笔颜色 |
| 90 |
else if (option.state & QStyle::State_MouseOver) { |
| 91 |
painter.setPen(QColor(Qt::white)); |
| 92 |
} |
| 93 |
|
| 94 |
//鼠标按下画笔颜色 |
| 95 |
else if (option.state & QStyle::State_Sunken) { |
| 96 |
painter.setPen(QColor("#99cdff")); |
| 97 |
} else { |
| 98 |
painter.setPen(QColor("#343434")); |
| 99 |
} |
| 100 |
|
| 101 |
} else { |
| 102 |
|
| 103 |
//控件禁用样式 |
| 104 |
if (!(option.state & QStyle::State_Enabled)) { |
| 105 |
painter.setPen(QColor("#8D8D8D")); |
| 106 |
} |
| 107 |
|
| 108 |
//鼠标悬停画笔颜色 |
| 109 |
else if (option.state & QStyle::State_MouseOver) { |
| 110 |
painter.setPen(QColor(Qt::white)); |
| 111 |
} |
| 112 |
|
| 113 |
//鼠标按下画笔颜色 |
| 114 |
else if (option.state & QStyle::State_Sunken) { |
| 115 |
painter.setPen(QColor("#99cdff")); |
| 116 |
} else { |
| 117 |
painter.setPen(QColor(Qt::white)); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// 绘制图片 |
| 122 |
painter.drawPixmap(QRect(10, -6, 32, 32), m_Licon.pixmap(QSize(32, 32))); |
| 123 |
|
| 124 |
//绘制文字 |
| 125 |
painter.save(); |
| 126 |
QFont ft; |
| 127 |
ft.setPixelSize(14); |
| 128 |
painter.setFont(ft); |
| 129 |
painter.drawText(QPoint(40, 22), m_text); |
| 130 |
painter.restore(); |
| 131 |
|
| 132 |
// 绘制图片 |
| 133 |
if (option.state & QStyle::State_MouseOver) { |
| 134 |
painter.drawPixmap(QRect(130, 10, 16, 16), m_RiconWhite.pixmap(QSize(16, 16))); |
| 135 |
} else { |
| 136 |
painter.drawPixmap(QRect(130, 10, 16, 16), m_Ricon.pixmap(QSize(16, 16))); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void ToolButton::focusInEvent(QFocusEvent *e) |
| 141 |
{ |
| 142 |
emit focusStatusChanged(true); |
| 143 |
DPushButton::focusInEvent(e); |
| 144 |
} |
| 145 |
|
| 146 |
void ToolButton::focusOutEvent(QFocusEvent *e) |
| 147 |
{ |
| 148 |
emit focusStatusChanged(false); |
| 149 |
DPushButton::focusOutEvent(e); |
| 150 |
} |