| 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 "kballontip.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QBoxLayout> |
| 26 |
#include <QColor> |
| 27 |
#include <QDebug> |
| 28 |
#include <QGraphicsDropShadowEffect> |
| 29 |
#include <QHBoxLayout> |
| 30 |
#include <QIcon> |
| 31 |
#include <QLabel> |
| 32 |
#include <QPainter> |
| 33 |
#include <QPainterPath> |
| 34 |
#include <QPalette> |
| 35 |
#include <QStyle> |
| 36 |
#include <QStyleOption> |
| 37 |
#include <QTimer> |
| 38 |
#include <kwindoweffects.h> |
| 39 |
#include "accessinfohelper.h" |
| 40 |
|
| 41 |
namespace kdk |
| 42 |
{ |
| 43 |
class KBallonTipPrivate : public QObject, public ThemeController |
| 44 |
{ |
| 45 |
Q_DECLARE_PUBLIC(KBallonTip) |
| 46 |
Q_OBJECT |
| 47 |
|
| 48 |
public: |
| 49 |
KBallonTipPrivate(KBallonTip *parent); |
| 50 |
void adjustStyle(); |
| 51 |
void setAccessInfo(); |
| 52 |
|
| 53 |
protected: |
| 54 |
void changeTheme(); |
| 55 |
|
| 56 |
private: |
| 57 |
KBallonTip *q_ptr; |
| 58 |
QLabel *m_pContentLabel; |
| 59 |
QLabel *m_pIconLabel; |
| 60 |
TipType m_type; |
| 61 |
QIcon m_icon; |
| 62 |
QPixmap m_variablePixmap; |
| 63 |
QPixmap m_pixmap; |
| 64 |
QColor m_color; |
| 65 |
QHBoxLayout *m_pLayout; |
| 66 |
QVBoxLayout *m_pVLayout; |
| 67 |
int m_time; |
| 68 |
QGraphicsDropShadowEffect *m_pShadow_effect; |
| 69 |
QTimer *m_pTimer; |
| 70 |
int m_flashState; |
| 71 |
}; |
| 72 |
|
| 73 |
KBallonTip::KBallonTip(QWidget *parent) |
| 74 |
: QWidget(parent) |
| 75 |
, d_ptr(new KBallonTipPrivate(this)) |
| 76 |
{ |
| 77 |
Q_D(KBallonTip); |
| 78 |
setAttribute(Qt::WA_TranslucentBackground); |
| 79 |
setWindowFlag(Qt::ToolTip); |
| 80 |
setFixedWidth(544); |
| 81 |
d->m_pVLayout = new QVBoxLayout(this); |
| 82 |
d->m_pLayout = new QHBoxLayout(); |
| 83 |
d->m_pLayout->setSpacing(12); |
| 84 |
this->setContentsMargins(24, 18, 24, 18); |
| 85 |
d->m_pContentLabel = new QLabel(this); |
| 86 |
// d->m_pContentLabel->setWordWrap(true); |
| 87 |
// 设置具体阴影 |
| 88 |
d->m_pShadow_effect = new QGraphicsDropShadowEffect(this); |
| 89 |
d->m_pShadow_effect->setOffset(0, 0); |
| 90 |
// 阴影颜色 |
| 91 |
d->m_pShadow_effect->setColor(ThemeController::getCustomColorFromDT("text-disable")); |
| 92 |
// 阴影半径 |
| 93 |
if (ThemeController::themeMode() == LightTheme) |
| 94 |
d->m_pShadow_effect->setBlurRadius(15); |
| 95 |
else |
| 96 |
d->m_pShadow_effect->setBlurRadius(0); |
| 97 |
this->setGraphicsEffect(d->m_pShadow_effect); |
| 98 |
|
| 99 |
d->m_pIconLabel = new QLabel(this); |
| 100 |
QVBoxLayout *vLayout = new QVBoxLayout; |
| 101 |
vLayout->addWidget(d->m_pIconLabel); |
| 102 |
// vLayout->addStretch(); |
| 103 |
d->m_pLayout->addLayout(vLayout); |
| 104 |
d->m_pLayout->addWidget(d->m_pContentLabel); |
| 105 |
d->m_pLayout->setAlignment(Qt::AlignVCenter); |
| 106 |
d->m_type = TipType::Nothing; |
| 107 |
d->m_pVLayout->addStretch(); |
| 108 |
d->m_pVLayout->addLayout(d->m_pLayout); |
| 109 |
d->m_pVLayout->addStretch(); |
| 110 |
|
| 111 |
d->adjustStyle(); |
| 112 |
d->m_pVLayout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize); |
| 113 |
if (ThemeController::systemFontSize() > 11) |
| 114 |
vLayout->setContentsMargins(0, (ThemeController::systemFontSize() - 11), 0, 0); |
| 115 |
else |
| 116 |
vLayout->setContentsMargins(0, 0, 0, 0); |
| 117 |
|
| 118 |
connect(d->m_gsetting,&QGSettings::changed,this,[=](QString key){ |
| 119 |
if (key.contains("systemFontSize")) |
| 120 |
{ |
| 121 |
if (ThemeController::systemFontSize() > 11) |
| 122 |
vLayout->setContentsMargins(0, (ThemeController::systemFontSize() - 11), 0, 0); |
| 123 |
else |
| 124 |
vLayout->setContentsMargins(0, 0, 0, 0); |
| 125 |
} |
| 126 |
if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName") |
| 127 |
{ |
| 128 |
if(key == "styleName") |
| 129 |
{ |
| 130 |
if (ThemeController::themeMode() == LightTheme) |
| 131 |
d->m_pShadow_effect->setBlurRadius(15); |
| 132 |
else |
| 133 |
d->m_pShadow_effect->setBlurRadius(0); |
| 134 |
} |
| 135 |
d->changeTheme(); |
| 136 |
} |
| 137 |
}); |
| 138 |
|
| 139 |
connect(d->m_pTimer, &QTimer::timeout, this, [=]() { |
| 140 |
if (d->m_flashState < 7) |
| 141 |
d->m_flashState++; |
| 142 |
else |
| 143 |
d->m_flashState = 0; |
| 144 |
if (ThemeController::themeMode() == LightTheme) { |
| 145 |
d->m_pIconLabel->setPixmap(QIcon::fromTheme(QString("ukui-loading-%1.symbolic").arg(d->m_flashState)).pixmap(22, 22)); |
| 146 |
} else { |
| 147 |
d->m_pIconLabel->setPixmap(ThemeController::drawColoredPixmap(QIcon::fromTheme(QString("ukui-loading-%1.symbolic").arg(d->m_flashState)).pixmap(22, 22), ThemeController::getCustomColorFromDT("kwhite"))); |
| 148 |
} |
| 149 |
}); |
| 150 |
|
| 151 |
d->setAccessInfo(); |
| 152 |
} |
| 153 |
|
| 154 |
KBallonTip::KBallonTip(const QString &content, const TipType &type, QWidget *parent) |
| 155 |
: KBallonTip(parent) |
| 156 |
{ |
| 157 |
Q_D(KBallonTip); |
| 158 |
d->m_pContentLabel->setText(content); |
| 159 |
d->m_type = type; |
| 160 |
d->adjustStyle(); |
| 161 |
setAttribute(Qt::WA_TranslucentBackground); |
| 162 |
} |
| 163 |
|
| 164 |
void KBallonTip::showInfo() |
| 165 |
{ |
| 166 |
Q_D(KBallonTip); |
| 167 |
|
| 168 |
show(); |
| 169 |
QTimer *timer = new QTimer(this); |
| 170 |
timer->start(d->m_time); |
| 171 |
timer->setSingleShot(true); |
| 172 |
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeupDestroy())); |
| 173 |
} |
| 174 |
|
| 175 |
void KBallonTip::setTipType(const TipType &type) |
| 176 |
{ |
| 177 |
Q_D(KBallonTip); |
| 178 |
d->m_type = type; |
| 179 |
d->adjustStyle(); |
| 180 |
} |
| 181 |
|
| 182 |
TipType KBallonTip::tipType() |
| 183 |
{ |
| 184 |
Q_D(KBallonTip); |
| 185 |
return d->m_type; |
| 186 |
} |
| 187 |
|
| 188 |
void KBallonTip::setText(const QString &text) |
| 189 |
{ |
| 190 |
Q_D(KBallonTip); |
| 191 |
d->m_pContentLabel->setText(text); |
| 192 |
} |
| 193 |
|
| 194 |
QString KBallonTip::text() |
| 195 |
{ |
| 196 |
Q_D(KBallonTip); |
| 197 |
return d->m_pContentLabel->text(); |
| 198 |
} |
| 199 |
|
| 200 |
void KBallonTip::setContentsMargins(int left, int top, int right, int bottom) |
| 201 |
{ |
| 202 |
Q_D(KBallonTip); |
| 203 |
d->m_pLayout->setContentsMargins(left, top, right, bottom); |
| 204 |
repaint(); |
| 205 |
} |
| 206 |
|
| 207 |
void KBallonTip::setContentsMargins(const QMargins &margins) |
| 208 |
{ |
| 209 |
Q_D(KBallonTip); |
| 210 |
d->m_pLayout->setContentsMargins(margins); |
| 211 |
repaint(); |
| 212 |
} |
| 213 |
|
| 214 |
void KBallonTip::setTipTime(int my_time) |
| 215 |
{ |
| 216 |
Q_D(KBallonTip); |
| 217 |
d->m_time = my_time; |
| 218 |
} |
| 219 |
|
| 220 |
QHBoxLayout *KBallonTip::hBoxLayout() |
| 221 |
{ |
| 222 |
Q_D(KBallonTip); |
| 223 |
return d->m_pLayout; |
| 224 |
} |
| 225 |
|
| 226 |
void KBallonTip::setVariableIcon(QIcon icon) |
| 227 |
{ |
| 228 |
Q_D(KBallonTip); |
| 229 |
d->m_variablePixmap = icon.pixmap(22, 22); |
| 230 |
update(); |
| 231 |
} |
| 232 |
|
| 233 |
QPixmap KBallonTip::variableIcon() |
| 234 |
{ |
| 235 |
Q_D(KBallonTip); |
| 236 |
return d->m_variablePixmap; |
| 237 |
} |
| 238 |
|
| 239 |
void KBallonTip::onTimeupDestroy() |
| 240 |
{ |
| 241 |
this->close(); |
| 242 |
} |
| 243 |
|
| 244 |
void KBallonTip::paintEvent(QPaintEvent *event) |
| 245 |
{ |
| 246 |
Q_D(KBallonTip); |
| 247 |
// bug 173714 173165 |
| 248 |
adjustSize(); |
| 249 |
|
| 250 |
d->m_pShadow_effect->setColor(ThemeController::getCustomColorFromDT("text-disable")); |
| 251 |
QPainter painter(this); |
| 252 |
painter.setRenderHint(QPainter::Antialiasing); // 反锯齿; |
| 253 |
|
| 254 |
auto color = ThemeController::getCustomColorFromDT("kline-window-inactive"); |
| 255 |
QPen pen; |
| 256 |
pen.setColor(color); |
| 257 |
pen.setWidthF(0.2); |
| 258 |
painter.setPen(pen); |
| 259 |
painter.setBrush(ThemeController::getCustomColorFromDT("window-active")); |
| 260 |
QRect rect = this->rect(); |
| 261 |
if (!mask().isNull()) |
| 262 |
clearMask(); |
| 263 |
painter.setBrush(ThemeController::getCustomColorFromDT("window-active")); |
| 264 |
int radius = ThemeController::getRadiusFromDT("kradius-normal"); |
| 265 |
if (radius == -1) |
| 266 |
radius = 6; |
| 267 |
painter.drawRoundedRect(rect.adjusted(8, 8, -8, -8), radius, radius); |
| 268 |
if (!d->m_variablePixmap.isNull()) { |
| 269 |
if (ThemeController::isPixmapPureColor(d->m_variablePixmap)) { |
| 270 |
if (ThemeController::themeMode() == LightTheme) |
| 271 |
d->m_pixmap = d->m_variablePixmap; |
| 272 |
else |
| 273 |
d->m_pixmap = ThemeController::drawColoredPixmap(d->m_variablePixmap, Qt::white); |
| 274 |
} |
| 275 |
} |
| 276 |
d->adjustStyle(); |
| 277 |
} |
| 278 |
|
| 279 |
KBallonTipPrivate::KBallonTipPrivate(KBallonTip *parent) |
| 280 |
: q_ptr(parent) |
| 281 |
{ |
| 282 |
Q_Q(KBallonTip); |
| 283 |
m_pTimer = new QTimer(this); |
| 284 |
m_pTimer->setInterval(100); |
| 285 |
m_time = 1000; |
| 286 |
setParent(parent); |
| 287 |
connect(m_gsetting, &QGSettings::changed, this, [=]() { |
| 288 |
changeTheme(); |
| 289 |
}); |
| 290 |
} |
| 291 |
|
| 292 |
void KBallonTipPrivate::adjustStyle() |
| 293 |
{ |
| 294 |
Q_Q(KBallonTip); |
| 295 |
switch (m_type) { |
| 296 |
case TipType::Nothing: |
| 297 |
m_icon = QIcon(); |
| 298 |
m_pIconLabel->setPixmap(m_icon.pixmap(22, 22)); |
| 299 |
m_pIconLabel->hide(); |
| 300 |
if (m_pTimer->isActive()) |
| 301 |
m_pTimer->stop(); |
| 302 |
break; |
| 303 |
case TipType::Normal: |
| 304 |
// m_icon = QIcon::fromTheme("ukui-dialog-success"); |
| 305 |
m_icon = QIcon::fromTheme("ukui-dialog-success", QIcon::fromTheme("emblem-default")); |
| 306 |
m_pIconLabel->setPixmap(m_icon.pixmap(22, 22)); |
| 307 |
m_pIconLabel->show(); |
| 308 |
if (m_pTimer->isActive()) |
| 309 |
m_pTimer->stop(); |
| 310 |
break; |
| 311 |
case TipType::Info:; |
| 312 |
m_icon = QIcon::fromTheme("dialog-info"); |
| 313 |
m_pIconLabel->setPixmap(m_icon.pixmap(22, 22)); |
| 314 |
m_pIconLabel->show(); |
| 315 |
if (m_pTimer->isActive()) |
| 316 |
m_pTimer->stop(); |
| 317 |
break; |
| 318 |
case TipType::Warning: |
| 319 |
m_icon = QIcon::fromTheme("dialog-warning"); |
| 320 |
m_pIconLabel->setPixmap(m_icon.pixmap(22, 22)); |
| 321 |
m_pIconLabel->show(); |
| 322 |
if (m_pTimer->isActive()) |
| 323 |
m_pTimer->stop(); |
| 324 |
break; |
| 325 |
case TipType::Error: |
| 326 |
m_icon = QIcon::fromTheme("dialog-error"); |
| 327 |
m_pIconLabel->setPixmap(m_icon.pixmap(22, 22)); |
| 328 |
m_pIconLabel->show(); |
| 329 |
if (m_pTimer->isActive()) |
| 330 |
m_pTimer->stop(); |
| 331 |
break; |
| 332 |
case TipType::Variable: |
| 333 |
m_pIconLabel->setPixmap(m_pixmap); |
| 334 |
m_pIconLabel->show(); |
| 335 |
if (m_pTimer->isActive()) |
| 336 |
m_pTimer->stop(); |
| 337 |
break; |
| 338 |
case TipType::Loading: |
| 339 |
if (!m_pTimer->isActive()) { |
| 340 |
m_pTimer->start(); |
| 341 |
m_pIconLabel->show(); |
| 342 |
} |
| 343 |
break; |
| 344 |
default: |
| 345 |
break; |
| 346 |
} |
| 347 |
m_pIconLabel->setFixedSize(22, 22); |
| 348 |
// m_pContentLabel->setAlignment(Qt::AlignLeft); |
| 349 |
} |
| 350 |
|
| 351 |
void KBallonTipPrivate::setAccessInfo() |
| 352 |
{ |
| 353 |
Q_Q(KBallonTip); |
| 354 |
KDK_ALL_INFO_FORMAT(m_pContentLabel,""); |
| 355 |
KDK_ALL_INFO_FORMAT(m_pIconLabel,""); |
| 356 |
KDK_OBJ_INFO_FORMAT(m_pShadow_effect); |
| 357 |
} |
| 358 |
|
| 359 |
void KBallonTipPrivate::changeTheme() |
| 360 |
{ |
| 361 |
Q_Q(KBallonTip); |
| 362 |
m_pShadow_effect->setOffset(0, 0); |
| 363 |
// 阴影颜色 |
| 364 |
m_pShadow_effect->setColor(ThemeController::getCustomColorFromDT("text-disable")); |
| 365 |
// 阴影半径 |
| 366 |
if (ThemeController::themeMode() == LightTheme) { |
| 367 |
m_pShadow_effect->setBlurRadius(15); |
| 368 |
} else |
| 369 |
m_pShadow_effect->setBlurRadius(0); |
| 370 |
q->setGraphicsEffect(m_pShadow_effect); |
| 371 |
} |
| 372 |
|
| 373 |
} |
| 374 |
|
| 375 |
#include "kballontip.moc" |
| 376 |
#include "moc_kballontip.cpp" |