| 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 "ktag.h" |
| 24 |
#include "ktoolbutton.h" |
| 25 |
#include "parmscontroller.h" |
| 26 |
#include "themeController.h" |
| 27 |
#include <QApplication> |
| 28 |
#include <QDebug> |
| 29 |
#include <QFontMetrics> |
| 30 |
#include <QPainter> |
| 31 |
#include <QPalette> |
| 32 |
#include <QStyleOption> |
| 33 |
#include <QToolButton> |
| 34 |
#include "accessinfohelper.h" |
| 35 |
|
| 36 |
namespace kdk |
| 37 |
{ |
| 38 |
class KTagPrivate : public QObject, public ThemeController |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
Q_DECLARE_PUBLIC(KTag) |
| 42 |
|
| 43 |
public: |
| 44 |
KTagPrivate(KTag *parent); |
| 45 |
void changeTheme(); |
| 46 |
void updateDeleteBtnColor(); |
| 47 |
|
| 48 |
private: |
| 49 |
KTag *q_ptr; |
| 50 |
QString m_text; |
| 51 |
KToolButton *m_pDeleteBtn; |
| 52 |
TagStyle m_style; |
| 53 |
QColor m_backgroundColor; |
| 54 |
bool m_isClosable; |
| 55 |
bool m_isBackgroundFlag; |
| 56 |
bool m_isTranslucent; |
| 57 |
}; |
| 58 |
|
| 59 |
KTag::KTag(QWidget *parent) |
| 60 |
: QPushButton(parent) |
| 61 |
, d_ptr(new KTagPrivate(this)) |
| 62 |
{ |
| 63 |
Q_D(KTag); |
| 64 |
setClosable(false); |
| 65 |
d->changeTheme(); |
| 66 |
connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=](bool flag) { |
| 67 |
updateGeometry(); |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
void KTag::setClosable(bool flag) |
| 72 |
{ |
| 73 |
Q_D(KTag); |
| 74 |
d->m_isClosable = flag; |
| 75 |
if (flag) |
| 76 |
setMinimumWidth(88); |
| 77 |
d->updateDeleteBtnColor(); |
| 78 |
update(); |
| 79 |
} |
| 80 |
|
| 81 |
bool KTag::closable() |
| 82 |
{ |
| 83 |
Q_D(KTag); |
| 84 |
return d->m_isClosable; |
| 85 |
} |
| 86 |
|
| 87 |
void KTag::setText(const QString &text) |
| 88 |
{ |
| 89 |
Q_D(KTag); |
| 90 |
d->m_text = text; |
| 91 |
update(); |
| 92 |
} |
| 93 |
|
| 94 |
void KTag::setTagStyle(TagStyle style) |
| 95 |
{ |
| 96 |
Q_D(KTag); |
| 97 |
d->m_style = style; |
| 98 |
d->changeTheme(); |
| 99 |
} |
| 100 |
|
| 101 |
TagStyle KTag::tagStyle() |
| 102 |
{ |
| 103 |
Q_D(KTag); |
| 104 |
d->updateDeleteBtnColor(); |
| 105 |
return d->m_style; |
| 106 |
} |
| 107 |
|
| 108 |
void KTag::setBackgroundColor(QColor color) |
| 109 |
{ |
| 110 |
Q_D(KTag); |
| 111 |
d->m_isBackgroundFlag = true; |
| 112 |
d->m_backgroundColor = color; |
| 113 |
d->updateDeleteBtnColor(); |
| 114 |
} |
| 115 |
|
| 116 |
void KTag::setTranslucent(bool flag) |
| 117 |
{ |
| 118 |
Q_D(KTag); |
| 119 |
d->m_isTranslucent = flag; |
| 120 |
if (flag) |
| 121 |
d->updateDeleteBtnColor(); |
| 122 |
} |
| 123 |
|
| 124 |
bool KTag::translucent() |
| 125 |
{ |
| 126 |
Q_D(KTag); |
| 127 |
return d->m_isTranslucent; |
| 128 |
} |
| 129 |
|
| 130 |
QString KTag::text() |
| 131 |
{ |
| 132 |
Q_D(KTag); |
| 133 |
return d->m_text; |
| 134 |
} |
| 135 |
|
| 136 |
void KTag::paintEvent(QPaintEvent *event) |
| 137 |
{ |
| 138 |
Q_D(KTag); |
| 139 |
QStyleOptionButton option; |
| 140 |
initStyleOption(&option); |
| 141 |
QColor bkgColor; |
| 142 |
QColor highColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 143 |
QColor iconColor; |
| 144 |
QColor fontColor; |
| 145 |
QRect iconRect; |
| 146 |
QRect textRect; |
| 147 |
if (!option.state.testFlag(QStyle::State_Enabled)) { |
| 148 |
if(d->m_style == IconTag) |
| 149 |
bkgColor = ThemeController::getCustomColorFromDT("button-disable"); |
| 150 |
else |
| 151 |
bkgColor = ThemeController::getCustomColorFromDT("kbrand-disable"); |
| 152 |
iconColor = ThemeController::getCustomColorFromDT("highlightedtext-disable"); |
| 153 |
fontColor = ThemeController::getCustomColorFromDT("highlightedtext-disable"); |
| 154 |
} else { |
| 155 |
if (d->m_isTranslucent || d->m_isBackgroundFlag) { |
| 156 |
iconColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 157 |
fontColor = ThemeController::getCustomColorFromDT("highlight-active"); |
| 158 |
} else { |
| 159 |
iconColor = Qt::white; |
| 160 |
fontColor = Qt::white; |
| 161 |
} |
| 162 |
|
| 163 |
if (!d->m_isBackgroundFlag) { |
| 164 |
bkgColor = highColor; |
| 165 |
if (option.state.testFlag(QStyle::State_MouseOver)) { |
| 166 |
if (option.state.testFlag(QStyle::State_Sunken)) { |
| 167 |
if (d->m_isTranslucent) |
| 168 |
bkgColor.setAlphaF(0.45); |
| 169 |
else |
| 170 |
bkgColor = ThemeController::mixColor(bkgColor, ThemeController::getCustomColorFromDT("brighttext-active"), 0.2); |
| 171 |
} else { |
| 172 |
if (d->m_isTranslucent) |
| 173 |
bkgColor.setAlphaF(0.3); |
| 174 |
else |
| 175 |
bkgColor = ThemeController::mixColor(bkgColor, ThemeController::getCustomColorFromDT("brighttext-active"), 0.05); |
| 176 |
} |
| 177 |
} else { |
| 178 |
if (d->m_isTranslucent) |
| 179 |
bkgColor.setAlphaF(0.15); |
| 180 |
} |
| 181 |
} else { |
| 182 |
bkgColor = d->m_backgroundColor; |
| 183 |
} |
| 184 |
} |
| 185 |
if (closable()) { |
| 186 |
QRect rect = option.rect.adjusted(8, 0, -(d->m_pDeleteBtn->width()), 0); |
| 187 |
int drawLength = option.iconSize.width() + 4 + fontMetrics().horizontalAdvance((d->m_text)); |
| 188 |
|
| 189 |
if(!option.icon.isNull()) |
| 190 |
{ |
| 191 |
if (rect.width() > drawLength) { |
| 192 |
iconRect = QRect(rect.center().x() - drawLength / 2, |
| 193 |
rect.center().y() - option.iconSize.height() / 2, |
| 194 |
option.iconSize.width(), |
| 195 |
option.iconSize.height()); |
| 196 |
textRect = QRect(iconRect.right() + 4, |
| 197 |
0, |
| 198 |
fontMetrics().horizontalAdvance(d->m_text), |
| 199 |
rect.height()); |
| 200 |
} else { |
| 201 |
iconRect = QRect(rect.x() + 4, |
| 202 |
rect.center().y() - option.iconSize.height() / 2, |
| 203 |
option.iconSize.width(), |
| 204 |
option.iconSize.height()); |
| 205 |
textRect = QRect(iconRect.right() + 4, |
| 206 |
0, |
| 207 |
rect.width() - 4 - iconRect.width(), |
| 208 |
rect.height()); |
| 209 |
} |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
if (rect.width() > drawLength) { |
| 214 |
textRect = QRect(4, |
| 215 |
0, |
| 216 |
rect.width() - 4, |
| 217 |
rect.height()); |
| 218 |
} else { |
| 219 |
textRect = QRect( 4, |
| 220 |
0, |
| 221 |
rect.width() - 4, |
| 222 |
rect.height()); |
| 223 |
} |
| 224 |
} |
| 225 |
} else { |
| 226 |
QRect rect = option.rect.adjusted(8, 0, 0, 0); |
| 227 |
int drawLength = option.iconSize.width() + 4 + fontMetrics().horizontalAdvance((d->m_text)); |
| 228 |
if(!option.icon.isNull()) |
| 229 |
{ |
| 230 |
if (rect.width() > drawLength) { |
| 231 |
iconRect = QRect(rect.center().x() - drawLength / 2, |
| 232 |
rect.center().y() - option.iconSize.height() / 2, |
| 233 |
option.iconSize.width(), |
| 234 |
option.iconSize.height()); |
| 235 |
textRect = QRect(iconRect.right() + 4, |
| 236 |
0, |
| 237 |
fontMetrics().horizontalAdvance(d->m_text), |
| 238 |
rect.height()); |
| 239 |
} else { |
| 240 |
iconRect = QRect(rect.x() + 4, |
| 241 |
rect.center().y() - option.iconSize.height() / 2, |
| 242 |
option.iconSize.width(), |
| 243 |
option.iconSize.height()); |
| 244 |
textRect = QRect(iconRect.right() + 4, |
| 245 |
0, |
| 246 |
rect.width() - 4 - iconRect.width(), |
| 247 |
rect.height()); |
| 248 |
} |
| 249 |
} |
| 250 |
else |
| 251 |
{ |
| 252 |
if (rect.width() > drawLength) { |
| 253 |
textRect = QRect(4, |
| 254 |
0, |
| 255 |
rect.width() - 4, |
| 256 |
rect.height()); |
| 257 |
} else { |
| 258 |
textRect = QRect( 4, |
| 259 |
0, |
| 260 |
rect.width() - 4, |
| 261 |
rect.height()); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
QPainter painter(this); |
| 266 |
painter.setRenderHint(QPainter::Antialiasing); |
| 267 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 268 |
if (painter.pen().width() == 1) |
| 269 |
painter.translate(0.5, 0.5); |
| 270 |
} |
| 271 |
QRect rect = this->rect(); |
| 272 |
if (d->m_isClosable) { |
| 273 |
d->m_pDeleteBtn->show(); |
| 274 |
d->m_pDeleteBtn->move(width() - d->m_pDeleteBtn->width(), (height() - d->m_pDeleteBtn->height()) / 2 + 2); |
| 275 |
rect.adjust(0, 0, -16, 0); |
| 276 |
} else { |
| 277 |
d->m_pDeleteBtn->hide(); |
| 278 |
} |
| 279 |
QColor color = ThemeController::getCustomColorFromDT("highlight-active"); |
| 280 |
QFontMetrics fm = fontMetrics(); |
| 281 |
switch (d->m_style) { |
| 282 |
case HighlightTag: |
| 283 |
|
| 284 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 285 |
QColor backColor = color; |
| 286 |
backColor.setAlpha(38); |
| 287 |
painter.setBrush(backColor); |
| 288 |
painter.setPen(color); |
| 289 |
painter.drawRoundedRect(this->rect(), 0, 0); |
| 290 |
} else { |
| 291 |
painter.setBrush(color); |
| 292 |
painter.setPen(Qt::PenStyle::NoPen); |
| 293 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 294 |
if (radius == -1) |
| 295 |
radius = 6; |
| 296 |
painter.drawRoundedRect(this->rect(), radius, radius); |
| 297 |
painter.setPen(QColor("#FFFFFF")); |
| 298 |
} |
| 299 |
break; |
| 300 |
case BoderTag: |
| 301 |
painter.setBrush(QColor(Qt::transparent)); |
| 302 |
painter.setPen(color); |
| 303 |
if (ThemeController::widgetTheme() == ClassicTheme) |
| 304 |
painter.drawRoundedRect(this->rect(), 0, 0); |
| 305 |
else { |
| 306 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 307 |
if (radius == -1) |
| 308 |
radius = 6; |
| 309 |
painter.drawRoundedRect(this->rect(), radius, radius); |
| 310 |
} |
| 311 |
break; |
| 312 |
case BaseBoderTag: { |
| 313 |
QColor backColor = color; |
| 314 |
backColor.setAlpha(38); |
| 315 |
painter.setBrush(backColor); |
| 316 |
painter.setPen(color); |
| 317 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 318 |
painter.drawRoundedRect(this->rect(), 0, 0); |
| 319 |
} else { |
| 320 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 321 |
if (radius == -1) |
| 322 |
radius = 6; |
| 323 |
painter.drawRoundedRect(this->rect(), radius, radius); |
| 324 |
} |
| 325 |
break; |
| 326 |
} |
| 327 |
case GrayTag: |
| 328 |
|
| 329 |
if (ThemeController::widgetTheme() == ClassicTheme) { |
| 330 |
painter.setBrush(QColor(Qt::transparent)); |
| 331 |
painter.setPen(color); |
| 332 |
painter.drawRoundedRect(this->rect(), 0, 0); |
| 333 |
} else { |
| 334 |
painter.setPen(Qt::PenStyle::NoPen); |
| 335 |
painter.setBrush(ThemeController::getCustomColorFromDT("kcomponent-normal")); |
| 336 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 337 |
if (radius == -1) |
| 338 |
radius = 6; |
| 339 |
painter.drawRoundedRect(this->rect(), radius, radius); |
| 340 |
painter.setPen(ThemeController::getCustomColorFromDT("text-active")); |
| 341 |
} |
| 342 |
break; |
| 343 |
case IconTag: { |
| 344 |
QPixmap pixmap = option.icon.pixmap(option.iconSize); |
| 345 |
painter.setPen(Qt::NoPen); |
| 346 |
painter.setBrush(bkgColor); |
| 347 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 348 |
if (radius == -1) |
| 349 |
radius = 6; |
| 350 |
painter.drawRoundedRect(this->rect(), radius, radius); |
| 351 |
painter.drawPixmap(iconRect, ThemeController::drawColoredPixmap(pixmap, iconColor)); |
| 352 |
painter.setPen(fontColor); |
| 353 |
break; |
| 354 |
} |
| 355 |
default: |
| 356 |
break; |
| 357 |
} |
| 358 |
if (fm.horizontalAdvance(d->m_text) > textRect.width()) { |
| 359 |
QString str = fm.elidedText(d->m_text,Qt::TextElideMode::ElideRight,textRect.width()); |
| 360 |
setToolTip(d->m_text); |
| 361 |
painter.drawText(textRect,Qt::AlignCenter,str); |
| 362 |
} else { |
| 363 |
setToolTip(""); |
| 364 |
painter.drawText(textRect, Qt::AlignCenter, d->m_text); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
QSize KTag::sizeHint() const |
| 369 |
{ |
| 370 |
auto size = QPushButton::sizeHint(); |
| 371 |
size.setHeight(Parmscontroller::parm(Parmscontroller::Parm::PM_TagHeight)); |
| 372 |
return size; |
| 373 |
} |
| 374 |
|
| 375 |
KTagPrivate::KTagPrivate(KTag *parent) |
| 376 |
: q_ptr(parent) |
| 377 |
, m_isClosable(false) |
| 378 |
, m_text("") |
| 379 |
, m_style(HighlightTag) |
| 380 |
, m_isBackgroundFlag(false) |
| 381 |
, m_isTranslucent(false) |
| 382 |
{ |
| 383 |
Q_Q(KTag); |
| 384 |
m_pDeleteBtn = new KToolButton(q); |
| 385 |
m_pDeleteBtn->setIconSize(QSize(16, 16)); |
| 386 |
connect(m_pDeleteBtn, &KToolButton::clicked, q, &KTag::close); |
| 387 |
m_pDeleteBtn->hide(); |
| 388 |
QPalette btnPalette; |
| 389 |
btnPalette.setBrush(QPalette::Active, QPalette::Button, Qt::transparent); |
| 390 |
btnPalette.setBrush(QPalette::Inactive, QPalette::Button, Qt::transparent); |
| 391 |
btnPalette.setBrush(QPalette::Disabled, QPalette::Button, Qt::transparent); |
| 392 |
|
| 393 |
btnPalette.setBrush(QPalette::Active, QPalette::Highlight, Qt::transparent); |
| 394 |
btnPalette.setBrush(QPalette::Inactive, QPalette::Highlight, Qt::transparent); |
| 395 |
btnPalette.setBrush(QPalette::Disabled, QPalette::Highlight, Qt::transparent); |
| 396 |
m_pDeleteBtn->setAutoFillBackground(true); |
| 397 |
m_pDeleteBtn->setPalette(btnPalette); |
| 398 |
m_pDeleteBtn->setFocusPolicy(Qt::NoFocus); |
| 399 |
m_pDeleteBtn->setCursor(Qt::ArrowCursor); |
| 400 |
QIcon icon = QIcon::fromTheme("application-exit-symbolic"); |
| 401 |
m_pDeleteBtn->setIcon(icon); |
| 402 |
connect(m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 403 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 404 |
{ |
| 405 |
changeTheme(); |
| 406 |
} |
| 407 |
}); |
| 408 |
|
| 409 |
KDK_ALL_INFO_FORMAT(m_pDeleteBtn,""); |
| 410 |
} |
| 411 |
|
| 412 |
void KTagPrivate::changeTheme() |
| 413 |
{ |
| 414 |
Q_Q(KTag); |
| 415 |
|
| 416 |
initThemeStyle(); |
| 417 |
updateDeleteBtnColor(); |
| 418 |
} |
| 419 |
|
| 420 |
void KTagPrivate::updateDeleteBtnColor() |
| 421 |
{ |
| 422 |
Q_Q(KTag); |
| 423 |
|
| 424 |
switch (m_style) { |
| 425 |
case HighlightTag: |
| 426 |
if(q->isEnabled()) |
| 427 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("kfont-white")); |
| 428 |
else |
| 429 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("kfont-white-disable")); |
| 430 |
break; |
| 431 |
case BoderTag: |
| 432 |
case BaseBoderTag: |
| 433 |
if(q->isEnabled()) |
| 434 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("kbrand-normal")); |
| 435 |
else |
| 436 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("kbrand-disable")); |
| 437 |
break; |
| 438 |
case GrayTag: |
| 439 |
if(q->isEnabled()) |
| 440 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("kfont-primary")); |
| 441 |
else |
| 442 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("kfont-primary-disable")); |
| 443 |
break; |
| 444 |
break; |
| 445 |
case IconTag: |
| 446 |
if (m_isTranslucent || m_isBackgroundFlag) |
| 447 |
m_pDeleteBtn->setIconColor(true, ThemeController::getCustomColorFromDT("highlight-active")); // m_backgroundColor |
| 448 |
else |
| 449 |
m_pDeleteBtn->setIconColor(true, Qt::white); |
| 450 |
break; |
| 451 |
default: |
| 452 |
break; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
#include "ktag.moc" |
| 458 |
#include "moc_ktag.cpp" |