| 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: Xiangwei Liu <[email protected]> |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "kpushbutton.h" |
| 24 |
#include "accessinfohelper.h" |
| 25 |
#include "themeController.h" |
| 26 |
#include <QApplication> |
| 27 |
#include <QColor> |
| 28 |
#include <QDebug> |
| 29 |
#include <QGraphicsDropShadowEffect> |
| 30 |
#include <QMenu> |
| 31 |
#include <QPainter> |
| 32 |
#include <QPainterPath> |
| 33 |
#include <QScreen> |
| 34 |
#include <QStyleOptionButton> |
| 35 |
#include <QTimer> |
| 36 |
|
| 37 |
namespace kdk |
| 38 |
{ |
| 39 |
static int margin = 5; // 左右边距 |
| 40 |
static int spacing = 4; // 图标文本间距 |
| 41 |
|
| 42 |
QStringList iconList{ |
| 43 |
"ukui-priority-symbolic", |
| 44 |
"ukui-priority-low-symbolic", |
| 45 |
"ukui-priority-middling-symbolic", |
| 46 |
"ukui-priority-high-symbolic" |
| 47 |
}; |
| 48 |
|
| 49 |
class KPushButtonPrivate : public QObject, public ThemeController |
| 50 |
{ |
| 51 |
Q_DECLARE_PUBLIC(KPushButton) |
| 52 |
Q_OBJECT |
| 53 |
public: |
| 54 |
KPushButtonPrivate(KPushButton *parent) |
| 55 |
: q_ptr(parent) |
| 56 |
{ |
| 57 |
setParent(parent); |
| 58 |
} |
| 59 |
|
| 60 |
void changeTheme(); |
| 61 |
|
| 62 |
void updateColor(QPainter &painter, QStyleOptionButton &option, QColor &backgroundColor, QColor &highlight, QColor &mix, QColor &fontColor, QColor &pixmapColor); |
| 63 |
void drawBackground(QPainter &painter, QStyleOptionButton &option, QColor &backgroundColor); |
| 64 |
void updateArrowDirection(QPixmap &arrowPixmap, QPoint pos, QPoint desktopPos, QPoint globalPos, QIcon::Mode mode, QIcon::State state); |
| 65 |
void updatePixmap(QPixmap &arrowPixmap, QPixmap &pixmap, QStyleOptionButton &option, QColor &highlight, QColor &fontColor, QColor &pixmapColor, QIcon::Mode mode, QIcon::State state); |
| 66 |
|
| 67 |
void updateGrayColor(QPainter &painter, QStyleOptionButton &option, QColor &backgroundColor, QColor &pixmapColor); |
| 68 |
|
| 69 |
void drawContent(QPainter &painter, QStyleOptionButton &option, QColor &fontColor, QRect &arrowRect, QRect &textRect, QRect &iconRect, QFontMetrics fm, QPixmap &pixmap, QPixmap &arrowPixmap, uint tf, QPoint iconPosition, int iconWidth, int iconHeight, int textWidth); |
| 70 |
|
| 71 |
private: |
| 72 |
KPushButton *q_ptr; |
| 73 |
int m_radius; |
| 74 |
int m_bottomLeft; |
| 75 |
int m_topLeft; |
| 76 |
int m_topRight; |
| 77 |
int m_bottomRight; |
| 78 |
int m_flashState; |
| 79 |
QColor m_backgroundColor; |
| 80 |
bool m_useCustomColor; |
| 81 |
bool m_hasOneParam; |
| 82 |
bool m_isTranslucent; |
| 83 |
bool m_isIconHighlight; |
| 84 |
QColor m_iconColor; |
| 85 |
bool m_useCustomIconColor; |
| 86 |
bool m_isBackgroundColorHighlight; |
| 87 |
bool m_isTranslucentFlag; |
| 88 |
bool m_hasArrow; |
| 89 |
bool m_isLoading; |
| 90 |
KPushButton::ArrowDirection m_arrowDirection; |
| 91 |
KPushButton::ButtonType m_buttonType; |
| 92 |
QGraphicsDropShadowEffect *m_pShadowEffect; |
| 93 |
QTimer *m_pTimer; |
| 94 |
QIcon m_icon; |
| 95 |
QLinearGradient m_linearGradient ; |
| 96 |
QColor m_borderColor; |
| 97 |
}; |
| 98 |
|
| 99 |
KPushButton::KPushButton(QWidget *parent) |
| 100 |
: QPushButton(parent) |
| 101 |
, d_ptr(new KPushButtonPrivate(this)) |
| 102 |
{ |
| 103 |
Q_D(KPushButton); |
| 104 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 105 |
if (radius == -1) { |
| 106 |
d->m_radius = 6; |
| 107 |
d->m_bottomLeft = 6; |
| 108 |
d->m_topLeft = 6; |
| 109 |
d->m_topRight = 6; |
| 110 |
d->m_bottomRight = 6; |
| 111 |
} else { |
| 112 |
d->m_radius = radius; |
| 113 |
d->m_bottomLeft = radius; |
| 114 |
d->m_topLeft = radius; |
| 115 |
d->m_topRight = radius; |
| 116 |
d->m_bottomRight = radius; |
| 117 |
} |
| 118 |
d->m_flashState = 0; |
| 119 |
d->m_backgroundColor = ThemeController::getCustomColorFromDT("button-active"); |
| 120 |
d->m_useCustomColor = false; |
| 121 |
d->m_hasOneParam = true; |
| 122 |
d->m_buttonType = NormalType; |
| 123 |
d->m_isTranslucent = false; |
| 124 |
d->m_isIconHighlight = false; |
| 125 |
d->m_iconColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 126 |
d->m_useCustomIconColor = false; |
| 127 |
d->m_isBackgroundColorHighlight = false; |
| 128 |
d->m_isTranslucentFlag = false; |
| 129 |
d->m_hasArrow = false; |
| 130 |
d->m_isLoading = false; |
| 131 |
d->m_arrowDirection = ArrowBottom; |
| 132 |
d->m_pShadowEffect = new QGraphicsDropShadowEffect(this); |
| 133 |
|
| 134 |
KDK_OBJ_INFO_FORMAT(d->m_pShadowEffect); |
| 135 |
|
| 136 |
d->m_pTimer = new QTimer(this); |
| 137 |
d->m_pTimer->setInterval(100); |
| 138 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 139 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 140 |
{ |
| 141 |
d->changeTheme(); |
| 142 |
} |
| 143 |
}); |
| 144 |
connect(d->m_pTimer, &QTimer::timeout, this, [=]() { |
| 145 |
if (d->m_flashState < 7) |
| 146 |
d->m_flashState++; |
| 147 |
else |
| 148 |
d->m_flashState = 0; |
| 149 |
setIcon(QIcon::fromTheme(QString("ukui-loading-%1.symbolic").arg(d->m_flashState))); |
| 150 |
}); |
| 151 |
} |
| 152 |
|
| 153 |
KPushButton::~KPushButton() |
| 154 |
{ |
| 155 |
} |
| 156 |
|
| 157 |
void KPushButton::setBorderRadius(int radius) |
| 158 |
{ |
| 159 |
Q_D(KPushButton); |
| 160 |
d->m_hasOneParam = true; |
| 161 |
d->m_radius = radius; |
| 162 |
update(); |
| 163 |
} |
| 164 |
|
| 165 |
void KPushButton::setBorderRadius(int bottomLeft, int topLeft, int topRight, int bottomRight) |
| 166 |
{ |
| 167 |
Q_D(KPushButton); |
| 168 |
d->m_hasOneParam = false; |
| 169 |
d->m_bottomLeft = bottomLeft; |
| 170 |
d->m_topLeft = topLeft; |
| 171 |
d->m_topRight = topRight; |
| 172 |
d->m_bottomRight = bottomRight; |
| 173 |
update(); |
| 174 |
} |
| 175 |
|
| 176 |
int KPushButton::borderRadius() |
| 177 |
{ |
| 178 |
Q_D(KPushButton); |
| 179 |
return d->m_radius; |
| 180 |
} |
| 181 |
|
| 182 |
void KPushButton::setBackgroundColor(QColor color) |
| 183 |
{ |
| 184 |
Q_D(KPushButton); |
| 185 |
if (d->m_isBackgroundColorHighlight) |
| 186 |
d->m_isBackgroundColorHighlight = false; |
| 187 |
d->m_useCustomColor = true; |
| 188 |
d->m_backgroundColor = color; |
| 189 |
update(); |
| 190 |
} |
| 191 |
|
| 192 |
QColor KPushButton::backgroundColor() |
| 193 |
{ |
| 194 |
Q_D(KPushButton); |
| 195 |
return d->m_backgroundColor; |
| 196 |
} |
| 197 |
|
| 198 |
void KPushButton::setButtonType(ButtonType type) |
| 199 |
{ |
| 200 |
Q_D(KPushButton); |
| 201 |
if (type == KPushButton::ShadowType) { |
| 202 |
d->m_pShadowEffect->setOffset(0, 3); |
| 203 |
// 阴影颜色 |
| 204 |
QColor color(0, 0, 0); |
| 205 |
// 阴影半径 |
| 206 |
if (ThemeController::themeMode() == LightTheme) |
| 207 |
color.setAlphaF(0.25); |
| 208 |
else |
| 209 |
color.setAlphaF(0.45); |
| 210 |
d->m_pShadowEffect->setColor(color); |
| 211 |
d->m_pShadowEffect->setBlurRadius(8); |
| 212 |
setGraphicsEffect(d->m_pShadowEffect); |
| 213 |
} |
| 214 |
if (type == KPushButton::GrayType) |
| 215 |
setFocusPolicy(Qt::StrongFocus); |
| 216 |
else |
| 217 |
setFocusPolicy(Qt::NoFocus); |
| 218 |
d->m_buttonType = type; |
| 219 |
} |
| 220 |
|
| 221 |
KPushButton::ButtonType KPushButton::buttonType() |
| 222 |
{ |
| 223 |
Q_D(KPushButton); |
| 224 |
return d->m_buttonType; |
| 225 |
} |
| 226 |
|
| 227 |
void KPushButton::setTranslucent(bool flag) |
| 228 |
{ |
| 229 |
Q_D(KPushButton); |
| 230 |
d->m_isTranslucentFlag = flag; |
| 231 |
d->m_isTranslucent = flag; |
| 232 |
d->changeTheme(); |
| 233 |
} |
| 234 |
|
| 235 |
bool KPushButton::isTranslucent() |
| 236 |
{ |
| 237 |
Q_D(KPushButton); |
| 238 |
return d->m_isTranslucent; |
| 239 |
} |
| 240 |
|
| 241 |
void KPushButton::setIconHighlight(bool flag) |
| 242 |
{ |
| 243 |
Q_D(KPushButton); |
| 244 |
d->m_isIconHighlight = flag; |
| 245 |
} |
| 246 |
|
| 247 |
bool KPushButton::isIconHighlight() |
| 248 |
{ |
| 249 |
Q_D(KPushButton); |
| 250 |
return d->m_isIconHighlight; |
| 251 |
} |
| 252 |
|
| 253 |
void KPushButton::setIconColor(QColor color) |
| 254 |
{ |
| 255 |
Q_D(KPushButton); |
| 256 |
d->m_useCustomIconColor = true; |
| 257 |
d->m_iconColor = color; |
| 258 |
update(); |
| 259 |
} |
| 260 |
|
| 261 |
QColor KPushButton::IconColor() |
| 262 |
{ |
| 263 |
Q_D(KPushButton); |
| 264 |
return d->m_iconColor; |
| 265 |
} |
| 266 |
|
| 267 |
void KPushButton::setBackgroundColorHighlight(bool flag) |
| 268 |
{ |
| 269 |
Q_D(KPushButton); |
| 270 |
if (d->m_useCustomColor) |
| 271 |
d->m_useCustomColor = false; |
| 272 |
d->m_isBackgroundColorHighlight = flag; |
| 273 |
} |
| 274 |
|
| 275 |
bool KPushButton::isBackgroundColorHighlight() |
| 276 |
{ |
| 277 |
Q_D(KPushButton); |
| 278 |
return d->m_isBackgroundColorHighlight; |
| 279 |
} |
| 280 |
|
| 281 |
void KPushButton::setArrow(bool flag, KPushButton::ArrowDirection arrowDirection) |
| 282 |
{ |
| 283 |
Q_D(KPushButton); |
| 284 |
d->m_hasArrow = flag; |
| 285 |
d->m_arrowDirection = arrowDirection; |
| 286 |
} |
| 287 |
|
| 288 |
bool KPushButton::hasArrow() const |
| 289 |
{ |
| 290 |
Q_D(const KPushButton); |
| 291 |
return d->m_hasArrow; |
| 292 |
} |
| 293 |
|
| 294 |
void KPushButton::setLoaingStatus(bool flag) |
| 295 |
{ |
| 296 |
Q_D(KPushButton); |
| 297 |
if (!d->m_isLoading) { |
| 298 |
d->m_icon = icon(); |
| 299 |
} |
| 300 |
d->m_isLoading = flag; |
| 301 |
if (flag) { |
| 302 |
d->m_pTimer->start(); |
| 303 |
} else { |
| 304 |
d->m_pTimer->stop(); |
| 305 |
setIcon(d->m_icon); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
bool KPushButton::isLoading() |
| 310 |
{ |
| 311 |
Q_D(KPushButton); |
| 312 |
return d->m_isLoading; |
| 313 |
} |
| 314 |
|
| 315 |
bool KPushButton::eventFilter(QObject *watched, QEvent *event) |
| 316 |
{ |
| 317 |
Q_D(KPushButton); |
| 318 |
return QPushButton::eventFilter(watched, event); |
| 319 |
} |
| 320 |
|
| 321 |
void KPushButton::paintEvent(QPaintEvent *event) |
| 322 |
{ |
| 323 |
Q_D(KPushButton); |
| 324 |
d->m_linearGradient = QLinearGradient(width() / 2, y(), width() / 2, height() + y()); |
| 325 |
QStyleOptionButton option; |
| 326 |
initStyleOption(&option); |
| 327 |
QColor pixmapColor; |
| 328 |
|
| 329 |
QPainter painter(this); |
| 330 |
painter.setRenderHint(QPainter::Antialiasing); |
| 331 |
painter.setRenderHint(QPainter::TextAntialiasing); |
| 332 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 333 |
|
| 334 |
QColor fontColor = ThemeController::getCustomColorFromDT("buttontext-disable"); |
| 335 |
QColor mix = ThemeController::getCustomColorFromDT("brighttext-active"); |
| 336 |
// QColor highlight = ThemeController::getCustomColorFromDT("highlight-active"); |
| 337 |
QColor highlight = ThemeController::getCustomColorFromDT("kbrand-normal"); |
| 338 |
QColor backgroundColor; |
| 339 |
|
| 340 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 341 |
if (painter.pen().width() == 1) |
| 342 |
painter.translate(0.5, 0.5); |
| 343 |
} |
| 344 |
|
| 345 |
d->updateColor(painter, option, backgroundColor, highlight, mix, fontColor, pixmapColor); |
| 346 |
d->drawBackground(painter, option, backgroundColor); |
| 347 |
|
| 348 |
/*绘制图标和文字*/ |
| 349 |
QPen pen; |
| 350 |
pen.setColor(fontColor); |
| 351 |
painter.setBrush(Qt::NoBrush); |
| 352 |
painter.setPen(pen); |
| 353 |
QPoint point; |
| 354 |
QRect ir = option.rect; |
| 355 |
uint tf = Qt::AlignVCenter; |
| 356 |
QPixmap pixmap; |
| 357 |
QPixmap arrowPixmap; |
| 358 |
|
| 359 |
QIcon::Mode mode = option.state & QStyle::State_Enabled ? QIcon::Normal : QIcon::Disabled; |
| 360 |
if (mode == QIcon::Normal && option.state & QStyle::State_HasFocus) |
| 361 |
mode = QIcon::Active; |
| 362 |
QIcon::State state = QIcon::Off; |
| 363 |
if (option.state & QStyle::State_On) |
| 364 |
state = QIcon::On; |
| 365 |
QPoint pos; |
| 366 |
QPoint desktopPos(QGuiApplication::primaryScreen()->availableGeometry().width(), QGuiApplication::primaryScreen()->availableGeometry().height()); |
| 367 |
QPoint globalPos; |
| 368 |
|
| 369 |
d->updateArrowDirection(arrowPixmap, pos, desktopPos, globalPos, mode, state); |
| 370 |
d->updatePixmap(arrowPixmap, pixmap, option, highlight, fontColor, pixmapColor, mode, state); |
| 371 |
|
| 372 |
if (!option.icon.isNull()) { |
| 373 |
// QPixmap pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 374 |
// QImage image = pixmap.toImage(); |
| 375 |
// if(mode != QIcon::Disabled){ |
| 376 |
// if(d->m_isIconHighlight){ |
| 377 |
// pixmap = ThemeController::drawColoredPixmap(this->icon().pixmap(iconSize()),highlight); |
| 378 |
// }else if(d->m_useCustomIconColor){ |
| 379 |
// pixmap = ThemeController::drawColoredPixmap(this->icon().pixmap(iconSize()),d->m_iconColor); |
| 380 |
// }else{ |
| 381 |
// pixmap = ThemeController::drawColoredPixmap(this->icon().pixmap(iconSize()),fontColor); |
| 382 |
// } |
| 383 |
// } |
| 384 |
int w = pixmap.width() / pixmap.devicePixelRatio(); |
| 385 |
int h = pixmap.height() / pixmap.devicePixelRatio(); |
| 386 |
|
| 387 |
if (!option.text.isEmpty()) |
| 388 |
w += option.fontMetrics.boundingRect(option.rect, tf, option.text).width() + 2; |
| 389 |
|
| 390 |
point = QPoint(ir.x() + ir.width() / 2 - w / 2, |
| 391 |
ir.y() + ir.height() / 2 - h / 2); |
| 392 |
|
| 393 |
w = pixmap.width() / pixmap.devicePixelRatio(); |
| 394 |
|
| 395 |
if (option.direction == Qt::RightToLeft) |
| 396 |
point.rx() += w; |
| 397 |
// if(mode == QIcon::Disabled){ |
| 398 |
// qreal opacity = 0.35; |
| 399 |
// QPainter painter(&image); |
| 400 |
// painter.setOpacity(opacity); |
| 401 |
// painter.setCompositionMode(QPainter::CompositionMode_SourceIn); |
| 402 |
// painter.fillRect(image.rect(),fontColor); |
| 403 |
// painter.end(); |
| 404 |
// p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), QPixmap::fromImage(image)); |
| 405 |
// } |
| 406 |
// else |
| 407 |
// p.drawPixmap(this->style()->visualPos(option.direction, option.rect, point), pixmap); |
| 408 |
|
| 409 |
if (option.direction == Qt::RightToLeft) |
| 410 |
ir.translate(-point.x() - 2, 0); |
| 411 |
else |
| 412 |
ir.translate(point.x() + w + 4, 0); |
| 413 |
|
| 414 |
// left-align text if there is |
| 415 |
if (!option.text.isEmpty()) |
| 416 |
tf |= Qt::AlignLeft; |
| 417 |
} else { |
| 418 |
tf |= Qt::AlignHCenter; |
| 419 |
} |
| 420 |
|
| 421 |
if (hasArrow()) |
| 422 |
point.rx() -= arrowPixmap.width() / 2; |
| 423 |
|
| 424 |
QFontMetrics fm = option.fontMetrics; |
| 425 |
|
| 426 |
// if(!this->isEnabled()){ |
| 427 |
// qreal opacity = 0.35; |
| 428 |
// fontColor.setAlphaF(opacity); |
| 429 |
// QPen pen ; |
| 430 |
// pen.setColor(fontColor); |
| 431 |
// p.setPen(pen); |
| 432 |
// if(option.icon.isNull() && (fm.horizontalAdvance(option.text) > option.rect.width() - 10)) |
| 433 |
// { |
| 434 |
// QString elidedText = fm.elidedText(option.text,Qt::ElideRight,option.rect.width() - 10); |
| 435 |
// p.drawText(option.rect.adjusted(5,0,-5,0),tf,elidedText); |
| 436 |
// setToolTip(option.text); |
| 437 |
// } |
| 438 |
// else |
| 439 |
// p.drawText(ir,tf,option.text); |
| 440 |
|
| 441 |
// } |
| 442 |
// else |
| 443 |
// { |
| 444 |
// if(option.icon.isNull() && (fm.horizontalAdvance(option.text) > option.rect.width() - 10)){ |
| 445 |
// QString elidedText = fm.elidedText(option.text,Qt::ElideRight,option.rect.width() - 10); |
| 446 |
// p.drawText(option.rect.adjusted(5,0,-5,0),tf,elidedText); |
| 447 |
// setToolTip(option.text); |
| 448 |
// } |
| 449 |
// else |
| 450 |
// p.drawText(ir,tf,option.text); |
| 451 |
// } |
| 452 |
QPoint iconPosition = this->style()->visualPos(option.direction, option.rect, point); |
| 453 |
int iconWidth = option.iconSize.width(); |
| 454 |
int iconHeight = option.iconSize.height(); |
| 455 |
int textWidth = option.rect.width() - iconWidth * 2 - iconPosition.x(); |
| 456 |
QRect arrowRect; |
| 457 |
QRect textRect; |
| 458 |
QRect iconRect = QRect(iconPosition.x(), iconPosition.y(), iconWidth, iconHeight); |
| 459 |
d->drawContent(painter, option, fontColor, arrowRect, textRect, iconRect, fm, pixmap, arrowPixmap, tf, iconPosition, iconWidth, iconHeight, textWidth); |
| 460 |
} |
| 461 |
|
| 462 |
void KPushButtonPrivate::changeTheme() |
| 463 |
{ |
| 464 |
Q_Q(KPushButton); |
| 465 |
if (m_isTranslucentFlag) { |
| 466 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 467 |
m_isTranslucent = false; |
| 468 |
else |
| 469 |
m_isTranslucent = true; |
| 470 |
} else { |
| 471 |
m_isTranslucent = false; |
| 472 |
} |
| 473 |
initThemeStyle(); |
| 474 |
if (m_buttonType == KPushButton::ShadowType) { |
| 475 |
m_pShadowEffect->setOffset(0, 3); |
| 476 |
// 阴影颜色 |
| 477 |
QColor color(0, 0, 0); |
| 478 |
// 阴影半径 |
| 479 |
if (ThemeController::themeMode() == LightTheme) |
| 480 |
color.setAlphaF(0.25); |
| 481 |
else |
| 482 |
color.setAlphaF(0.45); |
| 483 |
m_pShadowEffect->setColor(color); |
| 484 |
m_pShadowEffect->setBlurRadius(8); |
| 485 |
q->setGraphicsEffect(m_pShadowEffect); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void KPushButtonPrivate::updateColor(QPainter &painter, QStyleOptionButton &option, QColor &backgroundColor, QColor &highlight, QColor &mix, QColor &fontColor, QColor &pixmapColor) |
| 490 |
{ |
| 491 |
Q_Q(KPushButton); |
| 492 |
QColor startColor; |
| 493 |
QColor endColor; |
| 494 |
if(!q->isDown()) |
| 495 |
{ |
| 496 |
if (m_buttonType == KPushButton::NormalType || m_buttonType == KPushButton::CircleType || m_buttonType == KPushButton::ShadowType) |
| 497 |
{ |
| 498 |
if (m_isTranslucent) |
| 499 |
{ |
| 500 |
/*判断使用用户设置的背景色、跟随系统高亮色或是使用默认的背景色*/ |
| 501 |
if (!q->isFlat()) { |
| 502 |
if (m_useCustomColor) |
| 503 |
{ |
| 504 |
backgroundColor = m_backgroundColor; |
| 505 |
m_linearGradient.setColorAt(0,m_backgroundColor); |
| 506 |
m_linearGradient.setColorAt(1,m_backgroundColor); |
| 507 |
} |
| 508 |
else if (m_isBackgroundColorHighlight) |
| 509 |
{ |
| 510 |
backgroundColor = highlight; |
| 511 |
m_linearGradient.setColorAt(0,highlight); |
| 512 |
m_linearGradient.setColorAt(1,highlight); |
| 513 |
} |
| 514 |
else if (m_buttonType == KPushButton::ShadowType) |
| 515 |
{ |
| 516 |
backgroundColor = ThemeController::getCustomColorFromDT("base-active"); |
| 517 |
} |
| 518 |
else |
| 519 |
{ |
| 520 |
ThemeController::getGradientFromDT("kcomponent-alpha-normal",startColor,endColor); |
| 521 |
m_linearGradient.setColorAt(0,startColor); |
| 522 |
m_linearGradient.setColorAt(1,endColor); |
| 523 |
} |
| 524 |
} else { |
| 525 |
backgroundColor = Qt::transparent; |
| 526 |
m_linearGradient.setColorAt(0,Qt::transparent); |
| 527 |
m_linearGradient.setColorAt(1,Qt::transparent); |
| 528 |
} |
| 529 |
|
| 530 |
if (!option.state.testFlag(QStyle::State_Enabled)) |
| 531 |
{ |
| 532 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-disable"); |
| 533 |
ThemeController::getGradientFromDT("kcomponent-alpha-disable",startColor,endColor); |
| 534 |
m_linearGradient.setColorAt(0,startColor); |
| 535 |
m_linearGradient.setColorAt(1,endColor); |
| 536 |
pixmapColor = fontColor; |
| 537 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-disable"); |
| 538 |
} |
| 539 |
else /*按钮处于可用(Enabled)状态*/ |
| 540 |
{ |
| 541 |
fontColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 542 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-normal"); |
| 543 |
pixmapColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 544 |
if (option.state.testFlag(QStyle::State_MouseOver)) /*鼠标在按钮上(hover状态)*/ |
| 545 |
{ |
| 546 |
if (m_buttonType == KPushButton::ShadowType) { |
| 547 |
backgroundColor = ThemeController::getCustomColorFromDT("kbrand-normal"); |
| 548 |
} |
| 549 |
if (option.state.testFlag(QStyle::State_Sunken)) /*按钮被选中(clicked)*/ |
| 550 |
{ |
| 551 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 552 |
if (m_useCustomColor || m_isBackgroundColorHighlight || m_buttonType == KPushButton::ShadowType) |
| 553 |
{ |
| 554 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active"); |
| 555 |
backgroundColor.setAlphaF(0.45); |
| 556 |
m_linearGradient.setColorAt(0,backgroundColor); |
| 557 |
m_linearGradient.setColorAt(1,backgroundColor); |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
fontColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 562 |
ThemeController::getGradientFromDT("kcomponent-alpha-click",startColor,endColor); |
| 563 |
m_linearGradient.setColorAt(0,startColor); |
| 564 |
m_linearGradient.setColorAt(1,endColor); |
| 565 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 566 |
} |
| 567 |
} |
| 568 |
else |
| 569 |
{ |
| 570 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 571 |
if (m_useCustomColor || m_isBackgroundColorHighlight) |
| 572 |
{ |
| 573 |
backgroundColor.setAlphaF(0.3); |
| 574 |
m_linearGradient.setColorAt(0,backgroundColor); |
| 575 |
m_linearGradient.setColorAt(1,backgroundColor); |
| 576 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active"); |
| 577 |
} |
| 578 |
else |
| 579 |
{ |
| 580 |
ThemeController::getGradientFromDT("kcomponent-alpha-hover",startColor,endColor); |
| 581 |
m_linearGradient.setColorAt(0,startColor); |
| 582 |
m_linearGradient.setColorAt(1,endColor); |
| 583 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 584 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
else |
| 589 |
{ |
| 590 |
if (m_useCustomColor || m_isBackgroundColorHighlight) |
| 591 |
{ |
| 592 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active"); |
| 593 |
backgroundColor.setAlphaF(0.15); |
| 594 |
} |
| 595 |
else if (m_buttonType == KPushButton::ShadowType) { |
| 596 |
if (ThemeController::themeMode() == DarkTheme) |
| 597 |
backgroundColor.setAlphaF(0.2); |
| 598 |
} |
| 599 |
} |
| 600 |
} |
| 601 |
} |
| 602 |
else |
| 603 |
{ |
| 604 |
/*判断使用用户设置的背景色或是使用默认的背景色*/ |
| 605 |
if (!q->isFlat()) { |
| 606 |
if (m_useCustomColor) { |
| 607 |
backgroundColor = m_backgroundColor; |
| 608 |
m_linearGradient.setColorAt(0,m_backgroundColor); |
| 609 |
m_linearGradient.setColorAt(1,m_backgroundColor); |
| 610 |
} else if (m_isBackgroundColorHighlight) { |
| 611 |
ThemeController::getGradientFromDT("kbrand-normal",startColor,endColor); |
| 612 |
m_linearGradient.setColorAt(0,startColor); |
| 613 |
m_linearGradient.setColorAt(1,endColor); |
| 614 |
} else { |
| 615 |
ThemeController::getGradientFromDT("kcomponent-normal",startColor,endColor); |
| 616 |
m_linearGradient.setColorAt(0,startColor); |
| 617 |
m_linearGradient.setColorAt(1,endColor); |
| 618 |
} |
| 619 |
} else { |
| 620 |
backgroundColor = Qt::transparent; |
| 621 |
m_linearGradient.setColorAt(0,Qt::transparent); |
| 622 |
m_linearGradient.setColorAt(1,Qt::transparent); |
| 623 |
} |
| 624 |
|
| 625 |
/*按钮处于不可用(Disabled)状态*/ |
| 626 |
if (!option.state.testFlag(QStyle::State_Enabled)) |
| 627 |
{ |
| 628 |
if(m_isBackgroundColorHighlight) |
| 629 |
{ |
| 630 |
fontColor = ThemeController::getCustomColorFromDT("kfont-white-disable"); |
| 631 |
ThemeController::getGradientFromDT("kbrand-disable",startColor,endColor); |
| 632 |
m_linearGradient.setColorAt(0,startColor); |
| 633 |
m_linearGradient.setColorAt(1,endColor); |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-disable"); |
| 638 |
pixmapColor = ThemeController::getCustomColorFromDT("buttontext-disable"); |
| 639 |
ThemeController::getGradientFromDT("kcomponent-disable",startColor,endColor); |
| 640 |
startColor = ThemeController::getCustomColorFromDT("kcomponent-disable"); |
| 641 |
m_linearGradient.setColorAt(0,startColor); |
| 642 |
m_linearGradient.setColorAt(1,endColor); |
| 643 |
} |
| 644 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-disable"); |
| 645 |
} |
| 646 |
else |
| 647 |
{ |
| 648 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-normal"); |
| 649 |
pixmapColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 650 |
if (m_useCustomColor || m_isBackgroundColorHighlight) |
| 651 |
{ |
| 652 |
if(m_isBackgroundColorHighlight) |
| 653 |
fontColor = ThemeController::getCustomColorFromDT("kfont-white"); |
| 654 |
else |
| 655 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 656 |
|
| 657 |
if (option.state.testFlag(QStyle::State_MouseOver)) /*鼠标在按钮上(hover状态)*/ |
| 658 |
{ |
| 659 |
if (option.state.testFlag(QStyle::State_Sunken)) /*按钮被选中(clicked)*/ |
| 660 |
{ |
| 661 |
if(m_isBackgroundColorHighlight) |
| 662 |
{ |
| 663 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 664 |
ThemeController::getGradientFromDT("kbrand-click",startColor,endColor); |
| 665 |
m_linearGradient.setColorAt(0,startColor); |
| 666 |
m_linearGradient.setColorAt(1,endColor); |
| 667 |
} |
| 668 |
else |
| 669 |
{ |
| 670 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 671 |
backgroundColor = ThemeController::mixColor(backgroundColor, mix, 0.2); |
| 672 |
m_linearGradient.setColorAt(0,backgroundColor); |
| 673 |
m_linearGradient.setColorAt(1,backgroundColor); |
| 674 |
} |
| 675 |
} |
| 676 |
else |
| 677 |
{ |
| 678 |
if(m_isBackgroundColorHighlight) |
| 679 |
{ |
| 680 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 681 |
ThemeController::getGradientFromDT("kbrand-hover",startColor,endColor); |
| 682 |
m_linearGradient.setColorAt(0,startColor); |
| 683 |
m_linearGradient.setColorAt(1,endColor); |
| 684 |
} |
| 685 |
else |
| 686 |
{ |
| 687 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 688 |
backgroundColor = ThemeController::mixColor(backgroundColor, mix, 0.05); |
| 689 |
m_linearGradient.setColorAt(0,backgroundColor); |
| 690 |
m_linearGradient.setColorAt(1,backgroundColor); |
| 691 |
} |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
else |
| 696 |
{ |
| 697 |
/*按钮处于可用(Enabled)状态*/ |
| 698 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 699 |
if (option.state.testFlag(QStyle::State_MouseOver)) /*鼠标在按钮上(hover状态)*/ |
| 700 |
{ |
| 701 |
if (option.state.testFlag(QStyle::State_Sunken)) /*按钮被选中(clicked)*/ |
| 702 |
{ |
| 703 |
ThemeController::getGradientFromDT("kcomponent-click",startColor,endColor); |
| 704 |
m_linearGradient.setColorAt(0,startColor); |
| 705 |
m_linearGradient.setColorAt(1,endColor); |
| 706 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 707 |
} |
| 708 |
else |
| 709 |
{ |
| 710 |
ThemeController::getGradientFromDT("kcomponent-hover",startColor,endColor); |
| 711 |
m_linearGradient.setColorAt(0,startColor); |
| 712 |
m_linearGradient.setColorAt(1,endColor); |
| 713 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
if (q->isChecked()) |
| 720 |
{ |
| 721 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active"); |
| 722 |
m_linearGradient.setColorAt(0,highlight); |
| 723 |
m_linearGradient.setColorAt(1,highlight); |
| 724 |
if (option.state.testFlag(QStyle::State_MouseOver)) /*鼠标在按钮上(hover状态)*/ |
| 725 |
{ |
| 726 |
if (option.state.testFlag(QStyle::State_Sunken)) /*按钮被选中(clicked)*/ |
| 727 |
{ |
| 728 |
ThemeController::getGradientFromDT("kbrand-click",startColor,endColor); |
| 729 |
m_linearGradient.setColorAt(0,startColor); |
| 730 |
m_linearGradient.setColorAt(1,endColor); |
| 731 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 732 |
} |
| 733 |
else |
| 734 |
{ |
| 735 |
ThemeController::getGradientFromDT("kbrand-hover",startColor,endColor); |
| 736 |
m_linearGradient.setColorAt(0,startColor); |
| 737 |
m_linearGradient.setColorAt(1,endColor); |
| 738 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-hover"); |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
} else if (m_buttonType == KPushButton::GrayType) { |
| 743 |
updateGrayColor(painter, option, backgroundColor, pixmapColor); |
| 744 |
} |
| 745 |
} |
| 746 |
else |
| 747 |
{ |
| 748 |
if (m_isTranslucent) |
| 749 |
{ |
| 750 |
if (m_useCustomColor) |
| 751 |
{ |
| 752 |
backgroundColor = m_backgroundColor; |
| 753 |
} |
| 754 |
else if (m_isBackgroundColorHighlight) |
| 755 |
{ |
| 756 |
backgroundColor = highlight; |
| 757 |
} |
| 758 |
else if (m_buttonType == KPushButton::ShadowType) |
| 759 |
{ |
| 760 |
backgroundColor = ThemeController::getCustomColorFromDT("base-active"); |
| 761 |
} |
| 762 |
else |
| 763 |
{ |
| 764 |
ThemeController::getGradientFromDT("kcomponent-alpha-normal",startColor,endColor); |
| 765 |
m_linearGradient.setColorAt(0,startColor); |
| 766 |
m_linearGradient.setColorAt(1,endColor); |
| 767 |
} |
| 768 |
|
| 769 |
m_borderColor =ThemeController::getCustomColorFromDT("kline-component-click"); |
| 770 |
if (m_useCustomColor || m_isBackgroundColorHighlight ||m_buttonType == KPushButton::ShadowType) |
| 771 |
{ |
| 772 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active"); |
| 773 |
backgroundColor.setAlphaF(0.45); |
| 774 |
m_linearGradient.setColorAt(0, backgroundColor); |
| 775 |
m_linearGradient.setColorAt(1, backgroundColor); |
| 776 |
} |
| 777 |
else |
| 778 |
{ |
| 779 |
fontColor = ThemeController::getCustomColorFromDT("kfont-primary"); |
| 780 |
ThemeController::getGradientFromDT("kcomponent-alpha-click",startColor, endColor); |
| 781 |
m_linearGradient.setColorAt(0, startColor); |
| 782 |
m_linearGradient.setColorAt(1, endColor); |
| 783 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 784 |
} |
| 785 |
} |
| 786 |
else |
| 787 |
{ |
| 788 |
if (m_useCustomColor || m_isBackgroundColorHighlight) |
| 789 |
{ |
| 790 |
if (m_useCustomColor) |
| 791 |
{ |
| 792 |
backgroundColor = m_backgroundColor; |
| 793 |
} |
| 794 |
else if (m_isBackgroundColorHighlight) |
| 795 |
{ |
| 796 |
backgroundColor = highlight; |
| 797 |
} |
| 798 |
|
| 799 |
if (m_isBackgroundColorHighlight) |
| 800 |
fontColor = ThemeController::getCustomColorFromDT("kfont-white"); |
| 801 |
else |
| 802 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 803 |
if (m_isBackgroundColorHighlight) |
| 804 |
{ |
| 805 |
m_borderColor =ThemeController::getCustomColorFromDT("kline-component-click"); |
| 806 |
ThemeController::getGradientFromDT("kbrand-click", startColor,endColor); |
| 807 |
m_linearGradient.setColorAt(0, startColor); |
| 808 |
m_linearGradient.setColorAt(1, endColor); |
| 809 |
} |
| 810 |
else |
| 811 |
{ |
| 812 |
m_borderColor =ThemeController::getCustomColorFromDT("kline-component-click"); |
| 813 |
backgroundColor =ThemeController::mixColor(backgroundColor, mix, 0.2); |
| 814 |
m_linearGradient.setColorAt(0, backgroundColor); |
| 815 |
m_linearGradient.setColorAt(1, backgroundColor); |
| 816 |
} |
| 817 |
} |
| 818 |
else |
| 819 |
{ |
| 820 |
fontColor = ThemeController::getCustomColorFromDT("buttontext-active"); |
| 821 |
ThemeController::getGradientFromDT("kcomponent-click", startColor,endColor); |
| 822 |
m_linearGradient.setColorAt(0, startColor); |
| 823 |
m_linearGradient.setColorAt(1, endColor); |
| 824 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 825 |
} |
| 826 |
} |
| 827 |
if (q->isChecked()) |
| 828 |
{ |
| 829 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-active"); |
| 830 |
ThemeController::getGradientFromDT("kbrand-click", startColor,endColor); |
| 831 |
m_linearGradient.setColorAt(0, startColor); |
| 832 |
m_linearGradient.setColorAt(1, endColor); |
| 833 |
m_borderColor = ThemeController::getCustomColorFromDT("kline-component-click"); |
| 834 |
} |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
void KPushButtonPrivate::drawBackground(QPainter &painter, QStyleOptionButton &option, QColor &backgroundColor) |
| 839 |
{ |
| 840 |
Q_Q(KPushButton); |
| 841 |
/*绘制背景色和rect*/ |
| 842 |
painter.save(); |
| 843 |
if (((m_buttonType == KPushButton::NormalType || m_buttonType == KPushButton::CircleType) && !m_useCustomColor) || q->isChecked()) { |
| 844 |
painter.setBrush(m_linearGradient); |
| 845 |
painter.setPen(m_borderColor); |
| 846 |
} else { |
| 847 |
painter.setBrush(backgroundColor); |
| 848 |
painter.setPen(Qt::NoPen); |
| 849 |
} |
| 850 |
|
| 851 |
switch (m_buttonType) { |
| 852 |
case KPushButton::NormalType: |
| 853 |
{ |
| 854 |
if (m_hasOneParam) |
| 855 |
{ |
| 856 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 857 |
painter.drawRoundedRect(option.rect.adjusted(0, 0, -1, -1), 0, 0); |
| 858 |
else |
| 859 |
painter.drawRoundedRect(option.rect.adjusted(0, 0, -1, -1), m_radius, m_radius); |
| 860 |
} |
| 861 |
else |
| 862 |
{ |
| 863 |
QPainterPath path; |
| 864 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 865 |
path.addRect(option.rect.adjusted(0, 0, -1, -1)); |
| 866 |
else { |
| 867 |
path.moveTo(option.rect.topLeft() + QPointF(0, m_topLeft)); |
| 868 |
path.lineTo(option.rect.bottomLeft() - QPointF(0, m_bottomLeft)); |
| 869 |
path.quadTo(option.rect.bottomLeft(), option.rect.bottomLeft() + QPointF(m_bottomLeft, 0)); |
| 870 |
path.lineTo(option.rect.bottomRight() - QPointF(m_bottomRight, 0)); |
| 871 |
path.quadTo(option.rect.bottomRight(), option.rect.bottomRight() - QPointF(0, m_bottomRight)); |
| 872 |
path.lineTo(option.rect.topRight() + QPointF(0, m_topRight)); |
| 873 |
path.quadTo(option.rect.topRight(), option.rect.topRight() - QPointF(m_topRight, 0)); |
| 874 |
path.lineTo(option.rect.topLeft() + QPointF(m_topLeft, 0)); |
| 875 |
path.quadTo(option.rect.topLeft(), option.rect.topLeft() + QPointF(0, m_topLeft)); |
| 876 |
} |
| 877 |
painter.drawPath(path); |
| 878 |
} |
| 879 |
painter.restore(); |
| 880 |
break; |
| 881 |
} |
| 882 |
case KPushButton::CircleType: |
| 883 |
painter.drawRoundedRect(option.rect.adjusted(0, 0, 0, 0), q->width() / 2, q->height() / 2); |
| 884 |
painter.restore(); |
| 885 |
break; |
| 886 |
case KPushButton::GrayType: { |
| 887 |
if (option.state.testFlag(QStyle::State_HasFocus)) { |
| 888 |
QPen pen; |
| 889 |
pen.setWidth(2); |
| 890 |
pen.setColor(ThemeController::getCustomColorFromDT("kbrand-normal")); |
| 891 |
painter.setPen(pen); |
| 892 |
} else { |
| 893 |
painter.setPen(Qt::NoPen); |
| 894 |
} |
| 895 |
if (m_hasOneParam) { |
| 896 |
painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), m_radius, m_radius); |
| 897 |
} else { |
| 898 |
QPainterPath path; |
| 899 |
|
| 900 |
path.moveTo(option.rect.topLeft() + QPointF(0, m_topLeft)); |
| 901 |
path.lineTo(option.rect.bottomLeft() - QPointF(0, m_bottomLeft)); |
| 902 |
path.quadTo(option.rect.bottomLeft(), option.rect.bottomLeft() + QPointF(m_bottomLeft, 0)); |
| 903 |
path.lineTo(option.rect.bottomRight() - QPointF(m_bottomRight, 0)); |
| 904 |
path.quadTo(option.rect.bottomRight(), option.rect.bottomRight() - QPointF(0, m_bottomRight)); |
| 905 |
path.lineTo(option.rect.topRight() + QPointF(0, m_topRight)); |
| 906 |
path.quadTo(option.rect.topRight(), option.rect.topRight() - QPointF(m_topRight, 0)); |
| 907 |
path.lineTo(option.rect.topLeft() + QPointF(m_topLeft, 0)); |
| 908 |
path.quadTo(option.rect.topLeft(), option.rect.topLeft() + QPointF(0, m_topLeft)); |
| 909 |
|
| 910 |
painter.setRenderHint(QPainter::Antialiasing); |
| 911 |
painter.setRenderHint(QPainter::TextAntialiasing); |
| 912 |
painter.setRenderHint(QPainter::SmoothPixmapTransform); |
| 913 |
|
| 914 |
painter.drawPath(path); |
| 915 |
} |
| 916 |
painter.restore(); |
| 917 |
break; |
| 918 |
} |
| 919 |
case KPushButton::ShadowType: { |
| 920 |
QColor penColor = Qt::white; |
| 921 |
if (!q->isEnabled()) { |
| 922 |
if (ThemeController::themeMode() == LightTheme) |
| 923 |
penColor.setAlphaF(0.05); |
| 924 |
else |
| 925 |
penColor.setAlphaF(0.1); |
| 926 |
} |
| 927 |
painter.setPen(penColor); |
| 928 |
painter.setBrush(backgroundColor); |
| 929 |
painter.drawEllipse(option.rect.adjusted(0, 0, -1, -1)); |
| 930 |
painter.restore(); |
| 931 |
} break; |
| 932 |
default: |
| 933 |
break; |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
void KPushButtonPrivate::updateArrowDirection(QPixmap &arrowPixmap, QPoint pos, QPoint desktopPos, QPoint globalPos, QIcon::Mode mode, QIcon::State state) |
| 938 |
{ |
| 939 |
Q_Q(KPushButton); |
| 940 |
if (q->hasArrow()) { |
| 941 |
switch (m_arrowDirection) { |
| 942 |
case KPushButton::ArrowTop: |
| 943 |
arrowPixmap = QIcon::fromTheme("ukui-up-symbolic").pixmap(QSize(16, 16), mode, state); |
| 944 |
if (q->menu()) { |
| 945 |
pos.setX(q->rect().left()); |
| 946 |
pos.setY(-q->menu()->height()); |
| 947 |
globalPos = q->mapToGlobal(pos); // 现在 |
| 948 |
if (globalPos.x() > (desktopPos.x() - q->menu()->width())) |
| 949 |
globalPos.setX(desktopPos.x() - q->menu()->width()); |
| 950 |
if (globalPos.x() < 0) |
| 951 |
globalPos.setX(0); |
| 952 |
if (globalPos.y() < 0) { |
| 953 |
pos.setY(q->rect().bottom()); |
| 954 |
globalPos.setY(q->mapToGlobal(pos).y()); |
| 955 |
} |
| 956 |
q->menu()->move(globalPos); |
| 957 |
} |
| 958 |
break; |
| 959 |
case KPushButton::ArrowBottom: |
| 960 |
arrowPixmap = QIcon::fromTheme("ukui-down-symbolic").pixmap(QSize(16, 16), mode, state); |
| 961 |
break; |
| 962 |
case KPushButton::ArrowLeft: |
| 963 |
arrowPixmap = QIcon::fromTheme("ukui-start-symbolic").pixmap(QSize(16, 16), mode, state); |
| 964 |
if (q->menu()) { |
| 965 |
pos.setX(q->rect().left() - q->menu()->width()); |
| 966 |
pos.setY(q->rect().center().y() / 2); |
| 967 |
globalPos = q->mapToGlobal(pos); |
| 968 |
if (globalPos.x() < 0) { |
| 969 |
pos.setY(q->rect().bottom()); |
| 970 |
globalPos.setX(0); |
| 971 |
globalPos.setY(q->mapToGlobal(pos).y()); |
| 972 |
} |
| 973 |
if (globalPos.y() > desktopPos.y() - q->menu()->height()) { |
| 974 |
globalPos.setY(desktopPos.y() - q->menu()->height()); |
| 975 |
} |
| 976 |
q->menu()->move(globalPos); |
| 977 |
} |
| 978 |
break; |
| 979 |
case KPushButton::ArrowRight: |
| 980 |
arrowPixmap = QIcon::fromTheme("ukui-end-symbolic").pixmap(QSize(16, 16), mode, state); |
| 981 |
if (q->menu()) { |
| 982 |
pos.setX(q->rect().right()); |
| 983 |
q->menu()->move(q->mapToGlobal(pos)); |
| 984 |
globalPos = q->mapToGlobal(pos); |
| 985 |
if (globalPos.x() > (desktopPos.x() - q->menu()->width())) { |
| 986 |
pos.setX(q->rect().left() - q->menu()->width()); |
| 987 |
globalPos.setX(q->mapToGlobal(pos).x()); |
| 988 |
} |
| 989 |
if (globalPos.y() > desktopPos.y() - q->menu()->height()) { |
| 990 |
globalPos.setY(desktopPos.y() - q->menu()->height()); |
| 991 |
} |
| 992 |
q->menu()->move(globalPos); |
| 993 |
} |
| 994 |
break; |
| 995 |
default: |
| 996 |
break; |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
if(arrowPixmap.size() != QSize(16,16)) |
| 1001 |
arrowPixmap = arrowPixmap.scaled(QSize(16,16),Qt::KeepAspectRatio,Qt::SmoothTransformation); |
| 1002 |
} |
| 1003 |
|
| 1004 |
void KPushButtonPrivate::updatePixmap(QPixmap &arrowPixmap, QPixmap &pixmap, QStyleOptionButton &option, QColor &highlight, QColor &fontColor, QColor &pixmapColor, QIcon::Mode mode, QIcon::State state) |
| 1005 |
{ |
| 1006 |
Q_Q(KPushButton); |
| 1007 |
pixmap = option.icon.pixmap(option.iconSize, mode, state); |
| 1008 |
if (mode != QIcon::Disabled) { |
| 1009 |
if (m_buttonType == KPushButton::NormalType || m_buttonType == KPushButton::CircleType || m_buttonType == KPushButton::ShadowType) { |
| 1010 |
if (m_isIconHighlight) { |
| 1011 |
pixmap = ThemeController::drawColoredPixmap(q->icon().pixmap(q->iconSize()), highlight); |
| 1012 |
arrowPixmap = ThemeController::drawColoredPixmap(arrowPixmap, highlight); |
| 1013 |
} else if (m_useCustomIconColor) { |
| 1014 |
pixmap = ThemeController::drawColoredPixmap(q->icon().pixmap(q->iconSize()), m_iconColor); |
| 1015 |
arrowPixmap = ThemeController::drawColoredPixmap(arrowPixmap, m_iconColor); |
| 1016 |
} else { |
| 1017 |
if (!pixmap.isNull()) { |
| 1018 |
if (iconList.contains(option.icon.name())) { |
| 1019 |
pixmap = q->icon().pixmap(q->iconSize()); |
| 1020 |
} else if (ThemeController::isPixmapPureColor(pixmap)) |
| 1021 |
pixmap = ThemeController::drawColoredPixmap( |
| 1022 |
q->icon().pixmap(q->iconSize()), fontColor); |
| 1023 |
} |
| 1024 |
|
| 1025 |
if (!arrowPixmap.isNull()) { |
| 1026 |
if (ThemeController::isPixmapPureColor(arrowPixmap)) |
| 1027 |
arrowPixmap = ThemeController::drawColoredPixmap(arrowPixmap, fontColor); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
} else { |
| 1031 |
pixmap = ThemeController::drawColoredPixmap(q->icon().pixmap(q->iconSize()), pixmapColor); |
| 1032 |
arrowPixmap = ThemeController::drawColoredPixmap(arrowPixmap, pixmapColor); |
| 1033 |
} |
| 1034 |
} else { |
| 1035 |
arrowPixmap = ThemeController::drawColoredPixmap(arrowPixmap, ThemeController::getCustomColorFromDT("buttontext-disable")); |
| 1036 |
if (ThemeController::isPixmapPureColor(pixmap) && !iconList.contains(option.icon.name())) |
| 1037 |
pixmap = ThemeController::drawColoredPixmap(pixmap, ThemeController::getCustomColorFromDT("buttontext-disable")); |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
void KPushButtonPrivate::updateGrayColor(QPainter &painter, QStyleOptionButton &option, QColor &backgroundColor, QColor &pixmapColor) |
| 1042 |
{ |
| 1043 |
Q_Q(KPushButton); |
| 1044 |
if (!m_isTranslucent) { |
| 1045 |
pixmapColor = ThemeController::getCustomColorFromDT("text-active"); |
| 1046 |
if (!q->isEnabled()) { |
| 1047 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-disable"); |
| 1048 |
pixmapColor = ThemeController::getCustomColorFromDT("text-disable"); |
| 1049 |
painter.setPen(Qt::NoPen); |
| 1050 |
} else { |
| 1051 |
if (q->isLoading()) { |
| 1052 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-normal"); |
| 1053 |
painter.setPen(Qt::NoPen); |
| 1054 |
} else { |
| 1055 |
if (option.state.testFlag(QStyle::State_MouseOver)) { |
| 1056 |
if (option.state.testFlag(QStyle::State_Sunken)) { |
| 1057 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-click"); |
| 1058 |
} else { |
| 1059 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-hover"); |
| 1060 |
} |
| 1061 |
} else { |
| 1062 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-normal"); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} else { |
| 1067 |
pixmapColor = ThemeController::getCustomColorFromDT("text-active"); |
| 1068 |
if (!q->isEnabled()) { |
| 1069 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-alpha-disable"); |
| 1070 |
pixmapColor = ThemeController::getCustomColorFromDT("text-disable"); |
| 1071 |
} else { |
| 1072 |
if (q->isLoading()) { |
| 1073 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-alpha-normal"); |
| 1074 |
painter.setPen(Qt::NoPen); |
| 1075 |
} else { |
| 1076 |
if (option.state.testFlag(QStyle::State_MouseOver)) { |
| 1077 |
if (option.state.testFlag(QStyle::State_Sunken)) { |
| 1078 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-alpha-click"); |
| 1079 |
} else { |
| 1080 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-alpha-hover"); |
| 1081 |
} |
| 1082 |
} else { |
| 1083 |
backgroundColor = ThemeController::getCustomColorFromDT("kcomponent-alpha-normal"); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
|
| 1090 |
void KPushButtonPrivate::drawContent(QPainter &painter, QStyleOptionButton &option, QColor &fontColor, QRect &arrowRect, QRect &textRect, QRect &iconRect, QFontMetrics fm, QPixmap &pixmap, QPixmap &arrowPixmap, uint tf, QPoint iconPosition, int iconWidth, int iconHeight, int textWidth) |
| 1091 |
{ |
| 1092 |
Q_Q(KPushButton); |
| 1093 |
if (m_isLoading) { |
| 1094 |
iconRect = QRect((q->rect().width() - iconWidth) / 2 + 1, iconPosition.y() - 1, iconWidth, iconHeight); |
| 1095 |
painter.drawPixmap(iconRect, pixmap); |
| 1096 |
} else { |
| 1097 |
if (q->hasArrow()) { |
| 1098 |
if (!q->isEnabled()) { |
| 1099 |
QPen pen; |
| 1100 |
pen.setColor(fontColor); |
| 1101 |
painter.setPen(pen); |
| 1102 |
if (option.icon.isNull()) { |
| 1103 |
textRect = QRect(margin, (option.rect.height() - fm.height()) / 2, option.rect.width() - iconWidth - margin*2 - spacing, fm.height()); |
| 1104 |
|
| 1105 |
if ((fm.horizontalAdvance(option.text) >= textRect.width())) { |
| 1106 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1107 |
|
| 1108 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, textRect.width()); |
| 1109 |
painter.drawText(textRect, tf, elidedText); |
| 1110 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1111 |
} else { |
| 1112 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1113 |
painter.drawText(textRect, tf, option.text); |
| 1114 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1115 |
} |
| 1116 |
|
| 1117 |
} else { |
| 1118 |
painter.drawPixmap(iconRect, pixmap); |
| 1119 |
textRect = QRect(iconPosition.x() + spacing + iconWidth, 0, textWidth - margin - spacing, option.rect.height()); |
| 1120 |
|
| 1121 |
if (fm.horizontalAdvance(option.text) > textRect.width()) { |
| 1122 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1123 |
|
| 1124 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, textRect.width()); |
| 1125 |
painter.drawText(textRect, tf, elidedText); |
| 1126 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1127 |
} else { |
| 1128 |
textRect = QRect(iconPosition.x() + spacing + iconWidth, 0, fm.horizontalAdvance(option.text), option.rect.height()); |
| 1129 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1130 |
|
| 1131 |
painter.drawText(textRect, tf, option.text); |
| 1132 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
} else { |
| 1136 |
if (option.icon.isNull()) { |
| 1137 |
textRect = QRect(margin, (option.rect.height() - fm.height()) / 2, option.rect.width() - iconWidth - margin*2 - spacing, fm.height()); |
| 1138 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1139 |
|
| 1140 |
if (fm.horizontalAdvance(option.text) > textRect.width()) { |
| 1141 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, textRect.width()); |
| 1142 |
painter.drawText(textRect, tf, elidedText); |
| 1143 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1144 |
} else { |
| 1145 |
painter.drawText(textRect, tf, option.text); |
| 1146 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1147 |
} |
| 1148 |
|
| 1149 |
} else { |
| 1150 |
textRect = QRect(iconPosition.x() + spacing + iconWidth, 0, textWidth - margin - spacing, option.rect.height()); |
| 1151 |
painter.drawPixmap(iconRect, pixmap); |
| 1152 |
|
| 1153 |
if (fm.horizontalAdvance(option.text) > textRect.width()) { |
| 1154 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1155 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, textRect.width()); |
| 1156 |
painter.drawText(textRect, tf, elidedText); |
| 1157 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1158 |
} else { |
| 1159 |
textRect = QRect(iconPosition.x() + spacing + iconWidth, 0, fm.horizontalAdvance(option.text), option.rect.height()); |
| 1160 |
arrowRect = QRect(textRect.right() + spacing, (option.rect.height() - arrowPixmap.width()) / 2, arrowPixmap.width(), arrowPixmap.height()); |
| 1161 |
painter.drawText(textRect, tf, option.text); |
| 1162 |
painter.drawPixmap(arrowRect, arrowPixmap); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
} |
| 1166 |
} else { |
| 1167 |
if (!q->isEnabled()) { |
| 1168 |
qreal opacity = 0.35; |
| 1169 |
fontColor.setAlphaF(opacity); |
| 1170 |
QPen pen; |
| 1171 |
pen.setColor(fontColor); |
| 1172 |
painter.setPen(pen); |
| 1173 |
if (option.icon.isNull()) { |
| 1174 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, option.rect.width() - 10); |
| 1175 |
if (fm.horizontalAdvance(option.text) > option.rect.width() - 10) { |
| 1176 |
painter.drawText(option.rect.adjusted(margin, 0, -margin, 0), tf, elidedText); |
| 1177 |
q->setToolTip(option.text); |
| 1178 |
} else { |
| 1179 |
painter.drawText(option.rect.adjusted(margin, 0, -margin, 0), tf, option.text); |
| 1180 |
q->setToolTip(""); |
| 1181 |
} |
| 1182 |
} else { |
| 1183 |
QString elidedText = option.text; |
| 1184 |
if (elidedText.isEmpty()) { |
| 1185 |
iconRect = QRect((q->rect().width() - iconWidth) / 2 + 1, iconPosition.y() - 1, iconWidth, iconHeight); |
| 1186 |
} else { |
| 1187 |
if ((fm.horizontalAdvance(option.text) > option.rect.width() - margin * 2 - spacing - iconWidth)) { |
| 1188 |
iconRect = QRect(margin, iconPosition.y(), iconWidth, iconHeight); |
| 1189 |
textRect = QRect(iconRect.right() + spacing, 0, option.rect.width() - margin * 2 - spacing - iconWidth, option.rect.height()); |
| 1190 |
elidedText = fm.elidedText(option.text, Qt::ElideRight, textRect.width()); |
| 1191 |
q->setToolTip(option.text); |
| 1192 |
} else { |
| 1193 |
textRect = QRect(iconRect.right() + spacing, 0, option.rect.width() - margin * 2 - spacing - iconWidth, option.rect.height()); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
painter.drawPixmap(iconRect, pixmap); |
| 1197 |
painter.drawText(textRect, tf, elidedText); |
| 1198 |
} |
| 1199 |
} else { |
| 1200 |
if (option.icon.isNull()) { |
| 1201 |
if (fm.horizontalAdvance(option.text) > option.rect.width() - 10) { |
| 1202 |
QString elidedText = fm.elidedText(option.text, Qt::ElideRight, option.rect.width() - 10); |
| 1203 |
painter.drawText(option.rect.adjusted(margin, 0, -margin, 0), tf, elidedText); |
| 1204 |
q->setToolTip(option.text); |
| 1205 |
} else { |
| 1206 |
painter.drawText(option.rect.adjusted(margin, 0, -margin, 0), tf, option.text); |
| 1207 |
q->setToolTip(""); |
| 1208 |
} |
| 1209 |
} else { |
| 1210 |
QString elidedText = option.text; |
| 1211 |
if (elidedText.isEmpty()) { |
| 1212 |
iconRect = QRect((q->rect().width() - iconWidth) / 2 + 1, iconPosition.y() - 1, iconWidth, iconHeight); |
| 1213 |
} else { |
| 1214 |
if ((fm.horizontalAdvance(option.text) > option.rect.width() - margin * 2 - spacing - iconWidth)) { |
| 1215 |
iconRect = QRect(margin, iconPosition.y(), iconWidth, iconHeight); |
| 1216 |
textRect = QRect(iconRect.right() + spacing, 0, option.rect.width() - margin * 2 - spacing - iconWidth, option.rect.height()); |
| 1217 |
elidedText = fm.elidedText(option.text, Qt::ElideRight, textRect.width()); |
| 1218 |
q->setToolTip(option.text); |
| 1219 |
} else { |
| 1220 |
textRect = QRect(iconRect.right() + spacing, 0, option.rect.width() - margin * 2 - spacing - iconWidth, option.rect.height()); |
| 1221 |
} |
| 1222 |
} |
| 1223 |
painter.drawPixmap(iconRect, pixmap); |
| 1224 |
painter.drawText(textRect, tf, elidedText); |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
} |
| 1232 |
#include "kpushbutton.moc" |
| 1233 |
#include "moc_kpushbutton.cpp" |