| 1 |
/* |
| 2 |
* libkysdk-qtwidgets's Library |
| 3 |
* |
| 4 |
* Copyright (C) 2023, KylinSoft Co., Ltd. |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 3 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this library. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* Authors: Zhen Sun <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kborderlessbutton.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QPainter> |
| 26 |
#include <QDebug> |
| 27 |
#include <QStyleOptionButton> |
| 28 |
|
| 29 |
namespace kdk |
| 30 |
{ |
| 31 |
class KBorderlessButtonPrivate : public QObject, public ThemeController |
| 32 |
{ |
| 33 |
Q_DECLARE_PUBLIC(KBorderlessButton) |
| 34 |
Q_OBJECT |
| 35 |
public: |
| 36 |
KBorderlessButtonPrivate(KBorderlessButton *parent) |
| 37 |
: q_ptr(parent) |
| 38 |
, m_hoverd(false) |
| 39 |
{ |
| 40 |
setParent(parent); |
| 41 |
} |
| 42 |
|
| 43 |
protected: |
| 44 |
void changeTheme(); |
| 45 |
|
| 46 |
private: |
| 47 |
KBorderlessButton *q_ptr; |
| 48 |
bool m_hoverd; |
| 49 |
}; |
| 50 |
|
| 51 |
KBorderlessButton::KBorderlessButton(QWidget *parent) |
| 52 |
: QPushButton(parent) |
| 53 |
, d_ptr(new KBorderlessButtonPrivate(this)) |
| 54 |
{ |
| 55 |
Q_D(KBorderlessButton); |
| 56 |
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); |
| 57 |
setFocusPolicy(Qt::NoFocus); |
| 58 |
} |
| 59 |
|
| 60 |
KBorderlessButton::KBorderlessButton(const QString &text, QWidget *parent) |
| 61 |
: KBorderlessButton(parent) |
| 62 |
{ |
| 63 |
setText(text); |
| 64 |
} |
| 65 |
|
| 66 |
KBorderlessButton::KBorderlessButton(const QIcon &icon, const QString &text, QWidget *parent) |
| 67 |
: KBorderlessButton(parent) |
| 68 |
{ |
| 69 |
setIcon(icon); |
| 70 |
setText(text); |
| 71 |
} |
| 72 |
|
| 73 |
KBorderlessButton::KBorderlessButton(const QIcon &icon, QWidget *parent) |
| 74 |
: KBorderlessButton(parent) |
| 75 |
{ |
| 76 |
setIcon(icon); |
| 77 |
} |
| 78 |
|
| 79 |
KBorderlessButton::~KBorderlessButton() |
| 80 |
{ |
| 81 |
} |
| 82 |
|
| 83 |
void KBorderlessButton::setIcon(const QIcon &icon) |
| 84 |
{ |
| 85 |
Q_D(KBorderlessButton); |
| 86 |
QPushButton::setIcon(icon); |
| 87 |
d->changeTheme(); |
| 88 |
} |
| 89 |
|
| 90 |
bool KBorderlessButton::eventFilter(QObject *watched, QEvent *event) |
| 91 |
{ |
| 92 |
Q_D(KBorderlessButton); |
| 93 |
return QPushButton::eventFilter(watched, event); |
| 94 |
} |
| 95 |
|
| 96 |
void KBorderlessButton::paintEvent(QPaintEvent *event) |
| 97 |
{ |
| 98 |
Q_D(KBorderlessButton); |
| 99 |
QStyleOptionButton option; |
| 100 |
initStyleOption(&option); |
| 101 |
QPainter p(this); |
| 102 |
QColor fontColor; |
| 103 |
QColor highlight = ThemeController::getCustomColorFromDT("highlight-active"); |
| 104 |
QColor startColor; |
| 105 |
QColor endColor; |
| 106 |
if (!option.state.testFlag(QStyle::State_Enabled)) { |
| 107 |
fontColor = ThemeController::getCustomColorFromDT("kfont-primary-disable"); |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
if (option.state.testFlag(QStyle::State_MouseOver)) |
| 112 |
{ |
| 113 |
if (option.state.testFlag(QStyle::State_Sunken)) |
| 114 |
{ |
| 115 |
ThemeController::getGradientFromDT("kbrand-click",startColor,endColor); |
| 116 |
fontColor = startColor; |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
ThemeController::getGradientFromDT("kbrand-hover",startColor,endColor); |
| 121 |
fontColor = startColor; |
| 122 |
} |
| 123 |
} else { |
| 124 |
if (!this->icon().isNull()) { |
| 125 |
fontColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 126 |
} else { |
| 127 |
fontColor = highlight; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
p.setBrush(Qt::NoBrush); |
| 133 |
p.setRenderHint(QPainter::Antialiasing); |
| 134 |
p.setRenderHint(QPainter::TextAntialiasing); |
| 135 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
| 136 |
QPen pen; |
| 137 |
pen.setCapStyle(Qt::RoundCap); |
| 138 |
pen.setJoinStyle(Qt::RoundJoin); |
| 139 |
pen.setWidth(1); |
| 140 |
pen.setColor(fontColor); |
| 141 |
p.setPen(pen); |
| 142 |
QPoint point; |
| 143 |
// bug 170516 修正1px |
| 144 |
QRect ir = option.rect.adjusted(0, 0, -1, 0); |
| 145 |
uint tf = Qt::AlignVCenter; |
| 146 |
if (!option.icon.isNull()) { |
| 147 |
// Center both icon and text |
| 148 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal |
| 149 |
: QIcon::Disabled; |
| 150 |
|
| 151 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 152 |
mode = QIcon::Active; |
| 153 |
QIcon::State state = QIcon::Off; |
| 154 |
if (option.state & QStyle::State_On) |
| 155 |
state = QIcon::On; |
| 156 |
|
| 157 |
QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 158 |
pixmap = ThemeController::drawColoredPixmap(pixmap, fontColor); |
| 159 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 160 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 161 |
|
| 162 |
if (!option.text.isEmpty()) |
| 163 |
w += option.fontMetrics.boundingRect(option.rect, tf, option.text).width() + 2; |
| 164 |
|
| 165 |
point = QPoint(ir.x() + ir.width() / 2 - w / 2, |
| 166 |
ir.y() + ir.height() / 2 - h / 2); |
| 167 |
|
| 168 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 169 |
|
| 170 |
if (option.direction == Qt::RightToLeft) |
| 171 |
point.rx() += w; |
| 172 |
|
| 173 |
if (option.direction == Qt::RightToLeft) |
| 174 |
ir.translate(-point.x() - 2, 0); |
| 175 |
else |
| 176 |
ir.translate(point.x() + w + 4, 0); |
| 177 |
// left-align text if there is |
| 178 |
if (!option.text.isEmpty()) |
| 179 |
tf |= Qt::AlignLeft; |
| 180 |
QFontMetrics fm = option.fontMetrics; |
| 181 |
int width = option.iconSize.width(); |
| 182 |
int height = option.iconSize.height(); |
| 183 |
if (fm.horizontalAdvance(option.text) > option.rect.width() - width - 4) { |
| 184 |
QRect rect(option.rect.x(), (option.rect.height() - height) / 2, width, height); |
| 185 |
p.drawPixmap(rect, pixmap); |
| 186 |
QRect rect1(width + 4, 0, option.rect.width() - width - 4, option.rect.height()); |
| 187 |
QString elidedText1 = fm.elidedText(option.text, Qt::ElideRight, option.rect.width() - width - 4); |
| 188 |
p.drawText(rect1, tf, elidedText1); |
| 189 |
if (toolTip().isNull()) |
| 190 |
setToolTip(option.text); |
| 191 |
} else { |
| 192 |
p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 193 |
p.drawText(ir, tf, option.text); |
| 194 |
} |
| 195 |
} else { |
| 196 |
tf |= Qt::AlignHCenter; |
| 197 |
QFontMetrics fm = option.fontMetrics; |
| 198 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, option.rect.width()); |
| 199 |
p.drawText(option.rect, tf, elidedText); |
| 200 |
if (fm.horizontalAdvance(option.text) > option.rect.width()) |
| 201 |
setToolTip(option.text); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
QSize KBorderlessButton::sizeHint() const |
| 206 |
{ |
| 207 |
QSize size = QPushButton::sizeHint(); |
| 208 |
QFontMetrics fm(this->font()); |
| 209 |
if (this->icon().isNull()) |
| 210 |
size.setWidth(fm.horizontalAdvance(this->text()) + 2); |
| 211 |
else { |
| 212 |
int width = fm.horizontalAdvance(this->text()) + this->iconSize().width() + 4; |
| 213 |
size.setWidth(width); |
| 214 |
} |
| 215 |
|
| 216 |
return size; |
| 217 |
} |
| 218 |
|
| 219 |
void KBorderlessButtonPrivate::changeTheme() |
| 220 |
{ |
| 221 |
Q_Q(KBorderlessButton); |
| 222 |
initThemeStyle(); |
| 223 |
} |
| 224 |
} |
| 225 |
#include "kborderlessbutton.moc" |
| 226 |
#include "moc_kborderlessbutton.cpp" |