Debian Codegraph @

kysdk-qtwidgets/src/klabel.cpp

raw
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: Zhenyu Wang <[email protected]>
20 *
21 */
22
23 #include "klabel.h"
24 #include "themeController.h"
25 #include <QApplication>
26 #include <QDebug>
27 #include <QMouseEvent>
28 #include <QPainter>
29 #include <QPainterPath>
30 #include <QRect>
31 #include <QTextDocument>
32
33 namespace kdk
34 {
35
36 static const int spacing = 4;
37 class KLabelPrivate : public QObject, public ThemeController
38 {
39 Q_OBJECT
40 Q_DECLARE_PUBLIC(KLabel)
41 public:
42 KLabelPrivate(KLabel *parent);
43
44 protected:
45 void changeTheme();
46
47 private:
48 KLabel *q_ptr;
49 KLabelType m_labelType;
50 QPixmap m_pixmap;
51 QString m_text;
52 QColor m_backgroundColor;
53 QColor m_fontColor;
54 QIcon m_icon;
55 QPalette::ColorRole m_fontColorRole;
56 bool m_dataHightColorFlag;
57 bool m_isBackgroundFlag;
58 int m_topLeftRadius;
59 int m_topRightRadius;
60 int m_bottomLeftRadius;
61 int m_bottomRightRadius;
62 int diffPointSize;
63 int m_pixmapWidth;
64 int m_pixmapHeight;
65 bool m_isHighlightBackgroundFlag;
66 bool m_wordWrap;
67 bool m_fontColorFlag;
68 bool m_isIcon;
69 bool m_fontColorRoleFlag;
70 bool m_isTristate;
71 bool m_clicked;
72 };
73
74 KLabel::KLabel(QWidget *parent)
75 : QLabel(parent)
76 , d_ptr(new KLabelPrivate(this))
77 {
78 Q_D(KLabel);
79 int pointSize = QApplication::font().pointSize();
80
81 connect(d->m_gsetting, &QGSettings::changed, this, [=](const QString &key) {
82 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
83 {
84 d->changeTheme();
85 }
86 if(key == "systemFontSize" || key == "systemFont")
87 {
88 if(this->font().pointSize() != pointSize || d->diffPointSize != 0)
89 {
90 int now = this->font().pointSize();
91 if(now != QApplication::font().pointSize() || d->diffPointSize != 0)
92 {
93 if(d->diffPointSize == 0)
94 d->diffPointSize = now - pointSize;
95 QFont font = this->font();
96 font.setPointSize(d->diffPointSize + QApplication::font().pointSize());
97 setFont(font);
98 }
99 }
100 }
101 });
102 }
103
104 void KLabel::setLabelType(KLabelType type)
105 {
106 Q_D(KLabel);
107 d->m_labelType = type;
108 }
109
110 void KLabel::setDataHightColor(bool flag)
111 {
112 Q_D(KLabel);
113 d->m_dataHightColorFlag = flag;
114 }
115
116 void KLabel::setPixmap(const QPixmap &pixmap)
117 {
118 Q_D(KLabel);
119 d->m_isIcon = false;
120 if(pixmap.devicePixelRatio() != 1)
121 d->m_pixmap = pixmap.scaled(pixmap.size() / pixmap.devicePixelRatio() , Qt::KeepAspectRatio , Qt::SmoothTransformation);
122 else
123 d->m_pixmap = pixmap;
124 update();
125 }
126
127 void KLabel::setPixmap(QIcon icon, int width, int height)
128 {
129 Q_D(KLabel);
130 d->m_isIcon = true;
131 d->m_pixmapWidth = width;
132 d->m_pixmapHeight = height;
133 d->m_icon = icon;
134 update();
135 }
136
137 const QPixmap *KLabel::pixmap() const
138 {
139 Q_D(const KLabel);
140 return &d->m_pixmap;
141 }
142
143 void KLabel::setText(const QString &str)
144 {
145 Q_D(KLabel);
146 QLabel::setText(str);
147 }
148
149 QString KLabel::text()
150 {
151 Q_D(KLabel);
152 return QLabel::text();
153 }
154
155 void KLabel::setBackgroundColor(bool flag, QColor color)
156 {
157 Q_D(KLabel);
158 if (d->m_isHighlightBackgroundFlag)
159 d->m_isHighlightBackgroundFlag = false;
160 d->m_isBackgroundFlag = flag;
161 d->m_backgroundColor = color;
162 update();
163 }
164
165 void KLabel::setHighlightBackground(bool flag)
166 {
167 Q_D(KLabel);
168 if (d->m_isBackgroundFlag)
169 d->m_isBackgroundFlag = false;
170 d->m_isHighlightBackgroundFlag = flag;
171 update();
172 }
173
174 void KLabel::setBorderRadius(int bottomLeft, int topLeft, int topRight, int bottomRight)
175 {
176 Q_D(KLabel);
177 d->m_bottomRightRadius = bottomRight;
178 d->m_topLeftRadius = topLeft;
179 d->m_bottomLeftRadius = bottomLeft;
180 d->m_topRightRadius = topRight;
181 }
182
183 void KLabel::setBorderRadius(int radius)
184 {
185 Q_D(KLabel);
186 d->m_bottomRightRadius = radius;
187 d->m_topLeftRadius = radius;
188 d->m_bottomLeftRadius = radius;
189 d->m_topRightRadius = radius;
190 }
191
192 void KLabel::setWordWrap(bool flag)
193 {
194 Q_D(KLabel);
195 d->m_wordWrap = flag;
196 QLabel::setWordWrap(true);
197 }
198
199 bool KLabel::wordWrap()
200 {
201 Q_D(KLabel);
202 return d->m_wordWrap;
203 }
204
205 void KLabel::setFontColor(QColor color)
206 {
207 Q_D(KLabel);
208 d->m_fontColor = color;
209 d->m_fontColorFlag = true;
210 d->m_fontColorRoleFlag = false;
211 update();
212 }
213
214 void KLabel::setFontColorRole(QPalette::ColorRole role)
215 {
216 Q_D(KLabel);
217 d->m_fontColorRoleFlag = true;
218 d->m_fontColorFlag = false;
219 d->m_fontColorRole = role;
220 update();
221 }
222
223 void KLabel::setTristate(bool flag)
224 {
225 Q_D(KLabel);
226 d->m_isTristate = flag;
227 if (d->m_isTristate) {
228 QColor textColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
229 setFontColor(textColor);
230 } else {
231 d->m_pixmap = ThemeController::drawColoredPixmap(d->m_pixmap, ThemeController::getCustomColorFromDT("highlight-active"));
232 if (d->m_fontColorRoleFlag)
233 setFontColorRole(d->m_fontColorRole);
234 else if (d->m_fontColorRoleFlag)
235 setFontColorRole(d->m_fontColorRole);
236 else if (d->m_dataHightColorFlag)
237 setFontColorRole(QPalette::Highlight);
238 else
239 setFontColorRole(QPalette::Text);
240 }
241 }
242
243 bool KLabel::isTristate()
244 {
245 Q_D(KLabel);
246 return d->m_isTristate;
247 }
248
249 void KLabel::paintEvent(QPaintEvent *event)
250 {
251 Q_D(KLabel);
252 QPainter painter(this);
253 painter.setRenderHint(QPainter::SmoothPixmapTransform);
254 painter.setRenderHint(QPainter::Antialiasing);
255 QColor bkgColor;
256 QMargins margins = contentsMargins();
257 QRect drawRect = rect().adjusted(margins.left(),margins.top(),-margins.right(),-margins.bottom());
258 QRect iconRect;
259 QRect textRect;
260
261 painter.setPen(Qt::NoPen);
262 QString str = text();
263
264 // 绘制背景
265 QPainterPath path;
266 path.moveTo(drawRect.topRight() - QPoint(d->m_topRightRadius, 0)); // 右上
267 path.lineTo(drawRect.topLeft() + QPointF(d->m_topLeftRadius, 0)); // 上方线
268 path.quadTo(drawRect.topLeft(), drawRect.topLeft() + QPointF(0, d->m_topLeftRadius)); // 圆角
269 path.lineTo(drawRect.bottomLeft() + QPointF(0, -d->m_bottomLeftRadius)); // 左方线
270 path.quadTo(drawRect.bottomLeft(), drawRect.bottomLeft() + QPointF(d->m_bottomLeftRadius, 0)); // 圆角
271 path.lineTo(drawRect.bottomRight() - QPointF(d->m_bottomRightRadius, 0)); // 下方线
272 path.quadTo(drawRect.bottomRight(), drawRect.bottomRight() + QPointF(0, -d->m_bottomRightRadius)); // 圆角
273 path.lineTo(drawRect.topRight() + QPointF(0, d->m_topRightRadius)); // 右方线
274 path.quadTo(drawRect.topRight(), drawRect.topRight() - QPointF(d->m_topRightRadius, 0)); // 圆角
275
276 switch (d->m_labelType) {
277 case DataType:
278 bkgColor = ThemeController::getCustomColorFromDT("highlight-active");
279 bkgColor.setAlphaF(0.15);
280 if (d->m_isBackgroundFlag) {
281 painter.setBrush(d->m_backgroundColor);
282 painter.setPen(Qt::NoPen);
283 } else {
284 painter.setBrush(bkgColor);
285 painter.setPen(Qt::NoPen);
286 }
287 painter.drawPath(path);
288 break;
289 case NormalType:
290 if (d->m_isBackgroundFlag)
291 painter.setBrush(d->m_backgroundColor);
292 else if (d->m_isHighlightBackgroundFlag)
293 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
294 else
295 painter.setBrush(Qt::NoBrush);
296 painter.setPen(Qt::NoPen);
297 painter.drawPath(path);
298 break;
299 case CircleType:
300 if (d->m_isBackgroundFlag)
301 painter.setBrush(d->m_backgroundColor);
302 else if (d->m_isHighlightBackgroundFlag)
303 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
304 else
305 painter.setBrush(ThemeController::getCustomColorFromDT("button-active"));
306 painter.setPen(Qt::NoPen);
307 painter.drawEllipse(rect());
308 break;
309 default:
310 break;
311 }
312
313 // 绘制图标和文字
314 if (d->m_isIcon)
315 {
316 d->m_pixmap = d->m_icon.pixmap(d->m_pixmapWidth, d->m_pixmapHeight);
317 if ((d->m_pixmap.size().width() != d->m_pixmapWidth || d->m_pixmap.size().height() != d->m_pixmapHeight) && !d->m_pixmap.isNull())
318 d->m_pixmap = d->m_pixmap.scaled(d->m_pixmapWidth, d->m_pixmapHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
319 }
320 if (!wordWrap()) {
321 int drawLength;
322 if (!text().isEmpty() && !d->m_pixmap.isNull())
323 drawLength = d->m_pixmap.width() + spacing + fontMetrics().horizontalAdvance(text());
324 else if (!text().isEmpty() && d->m_pixmap.isNull())
325 drawLength = fontMetrics().horizontalAdvance(text());
326 else if (text().isEmpty() && !d->m_pixmap.isNull())
327 drawLength = d->m_pixmap.width();
328
329 if (!text().isEmpty() && !d->m_pixmap.isNull()) {
330 iconRect = QRect(drawRect.x() ,
331 drawRect.height()/2 - d->m_pixmap.height() / 2 + 1,
332 d->m_pixmap.width(),
333 d->m_pixmap.height());
334 textRect = QRect(iconRect.right() + spacing,
335 drawRect.top(),
336 drawRect.width() - spacing - iconRect.width(),
337 drawRect.height());
338 int drawLength = d->m_pixmap.width() + spacing + fontMetrics().horizontalAdvance(text());
339 if (drawRect.width() >= drawLength) {
340 setToolTip("");
341 } else {
342 setToolTip(str);
343 str = fontMetrics().elidedText(str, Qt::ElideRight, textRect.width());
344 }
345 } else {
346 if (text().isEmpty()) {
347 iconRect = QRect(drawRect.width()/2 - drawLength / 2 ,
348 drawRect.height()/2 - d->m_pixmap.height() / 2 ,
349 d->m_pixmap.width(),
350 d->m_pixmap.height());
351 } else {
352 textRect = QRect(drawRect.x(),
353 drawRect.top(),
354 drawRect.width(),
355 drawRect.height());
356 if (drawRect.width() >= drawLength) {
357 setToolTip("");
358 } else {
359 setToolTip(str);
360 str = fontMetrics().elidedText(str, Qt::ElideRight, textRect.width());
361 }
362 }
363 }
364
365 if (d->m_dataHightColorFlag) {
366 d->m_pixmap = ThemeController::drawColoredPixmap(d->m_pixmap, ThemeController::getCustomColorFromDT("highlight-active"));
367 if (d->m_fontColorFlag)
368 painter.setPen(d->m_fontColor);
369 else if (d->m_fontColorRoleFlag)
370 painter.setPen(palette().color(d->m_fontColorRole));
371 else
372 painter.setPen(ThemeController::getCustomColorFromDT("highlight-active"));
373 } else if (d->m_isBackgroundFlag || d->m_isHighlightBackgroundFlag) {
374 d->m_pixmap = ThemeController::drawSymbolicColoredPixmap(d->m_pixmap);
375 if (d->m_fontColorFlag)
376 painter.setPen(d->m_fontColor);
377 else if (d->m_fontColorRoleFlag)
378 painter.setPen(palette().color(d->m_fontColorRole));
379 else
380 painter.setPen(ThemeController::getCustomColorFromDT("text-active"));
381 } else {
382 if (d->m_fontColorFlag)
383 painter.setPen(d->m_fontColor);
384 else if (d->m_fontColorRoleFlag)
385 painter.setPen(palette().color(d->m_fontColorRole));
386 else
387 painter.setPen(ThemeController::getCustomColorFromDT("text-active"));
388 if (!d->m_pixmap.isNull()) {
389 if (ThemeController::isPixmapPureColor(d->m_pixmap)) {
390 if (ThemeController::themeMode() == DarkTheme)
391 d->m_pixmap = ThemeController::drawColoredPixmap(d->m_pixmap, Qt::white);
392 else
393 d->m_pixmap = ThemeController::drawColoredPixmap(d->m_pixmap, ThemeController::getCustomColorFromDT("windowtext-active"));
394 }
395 }
396 }
397
398 if (alignment() == Qt::AlignCenter || alignment() == Qt::AlignHCenter) {
399 if (!text().isEmpty() && !d->m_pixmap.isNull()) {
400 if (drawRect.width() >= drawLength)
401 iconRect.moveLeft(drawRect.width() / 2 - drawLength / 2);
402 else
403 iconRect.moveLeft(drawRect.left());
404 } else {
405 if (text().isEmpty())
406 iconRect.moveLeft(drawRect.width() / 2 - drawLength / 2);
407 }
408 }
409 else if(alignment() == Qt::AlignRight)
410 iconRect.moveLeft(drawRect.width() - fontMetrics().horizontalAdvance(str) - spacing - iconRect.width());
411 painter.drawText(textRect, alignment() | Qt::AlignVCenter, str);
412 painter.drawPixmap(iconRect, d->m_pixmap);
413 } else {
414 QRect contentRect = drawRect.adjusted(margins.left(), margins.top(), -margins.right(), -margins.bottom());
415 if (!d->m_pixmap.isNull()) {
416 int iconX = contentRect.x();
417 int iconY = contentRect.y() + (contentRect.height() - d->m_pixmap.height()) / 2;
418 if (alignment() & Qt::AlignHCenter) {
419 iconX = contentRect.x() + (contentRect.width() - d->m_pixmap.width()) / 2;
420 } else if (alignment() & Qt::AlignRight) {
421 iconX = contentRect.right() - d->m_pixmap.width();
422 }
423
424 iconRect = QRect(iconX, iconY, d->m_pixmap.width(), d->m_pixmap.height());
425
426 if (alignment() & Qt::AlignLeft) {
427 contentRect.setLeft(iconRect.right() + spacing);
428 } else if (alignment() & Qt::AlignRight) {
429 contentRect.setRight(iconRect.left() - spacing);
430 }
431 painter.drawPixmap(iconRect, d->m_pixmap);
432 }
433
434 if (!text().isEmpty()) {
435 QTextDocument doc;
436 doc.setDefaultFont(font());
437 doc.setPlainText(text());
438
439 QTextOption textOption;
440 textOption.setWrapMode(QTextOption::WordWrap);
441 textOption.setAlignment(alignment());
442 doc.setDefaultTextOption(textOption);
443 doc.setTextWidth(contentRect.width());
444
445 qreal textHeight = doc.size().height();
446 qreal yPos = static_cast<qreal>(contentRect.y());
447
448 if (alignment() & Qt::AlignBottom) {
449 yPos = static_cast<qreal>(contentRect.bottom()) - textHeight;
450 } else if (alignment() & Qt::AlignVCenter) {
451 yPos = static_cast<qreal>(contentRect.y()) + (static_cast<qreal>(contentRect.height()) - textHeight) / 2;
452 }
453
454 yPos = qMax(static_cast<qreal>(contentRect.y()),
455 qMin(yPos, static_cast<qreal>(contentRect.bottom()) - textHeight));
456 painter.save();
457 painter.translate(contentRect.x(), static_cast<int>(yPos));
458 painter.setClipRect(QRect(0, 0, contentRect.width(), contentRect.height()));
459 doc.drawContents(&painter, QRectF(0, 0, contentRect.width(), contentRect.height()));
460 painter.restore();
461 }
462 }
463 }
464
465 void KLabel::resizeEvent(QResizeEvent *event)
466 {
467 Q_D(KLabel);
468 int height = d->m_pixmapHeight > fontMetrics().height() ? d->m_pixmapHeight : fontMetrics().height();
469 if (height > size().height()) {
470 setMinimumHeight(height);
471 }
472 QLabel::resizeEvent(event);
473 }
474
475 void KLabel::enterEvent(QEnterEvent *event)
476 {
477 Q_D(KLabel);
478 if (d->m_isTristate) {
479 QColor textColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
480 QColor highlight = ThemeController::getCustomColorFromDT("highlight-active");
481 QColor mix = ThemeController::getCustomColorFromDT("brighttext-active");
482 textColor = ThemeController::mixColor(highlight, mix, 0.05);
483 setFontColor(textColor);
484 }
485
486 Q_EMIT enterWidget();
487 QLabel::enterEvent(event);
488 }
489
490 void KLabel::leaveEvent(QEvent *event)
491 {
492 Q_D(KLabel);
493 if (d->m_isTristate) {
494 QColor textColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
495 setFontColor(textColor);
496 }
497 Q_EMIT leaveWidget();
498
499 QLabel::leaveEvent(event);
500 }
501
502 void KLabel::mousePressEvent(QMouseEvent *event)
503 {
504 Q_D(KLabel);
505 if (event->button() == Qt::LeftButton && d->m_isTristate) {
506 QColor textColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
507 QColor highlight = ThemeController::getCustomColorFromDT("highlight-active");
508 QColor mix = ThemeController::getCustomColorFromDT("brighttext-active");
509 textColor = ThemeController::mixColor(highlight, mix, 0.2);
510 setFontColor(textColor);
511 }
512 QLabel::mousePressEvent(event);
513 }
514
515 void KLabel::mouseReleaseEvent(QMouseEvent *event)
516 {
517 Q_D(KLabel);
518 if (event->button() == Qt::LeftButton && d->m_isTristate) {
519 QColor textColor = ThemeController::getCustomColorFromDT("placeholdertext-active");
520 QColor highlight = ThemeController::getCustomColorFromDT("highlight-active");
521 QColor mix = ThemeController::getCustomColorFromDT("brighttext-active");
522 textColor = ThemeController::mixColor(highlight, mix, 0.05);
523 setFontColor(textColor);
524 }
525 d->m_clicked = !d->m_clicked;
526 Q_EMIT clicked(d->m_clicked);
527 QLabel::mouseReleaseEvent(event);
528 }
529
530 void KLabel::mouseDoubleClickEvent(QMouseEvent *event)
531 {
532 Q_EMIT doubleClicked();
533 QLabel::mouseDoubleClickEvent(event);
534 }
535
536 QSize KLabel::sizeHint() const
537 {
538 Q_D(const KLabel);
539 return QLabel::sizeHint();
540 }
541
542 KLabelPrivate::KLabelPrivate(KLabel *parent)
543 : q_ptr(parent)
544 , m_isBackgroundFlag(false)
545 , m_dataHightColorFlag(false)
546 , m_bottomRightRadius(6)
547 , m_topLeftRadius(6)
548 , m_bottomLeftRadius(6)
549 , m_topRightRadius(6)
550 , diffPointSize(0)
551 , m_pixmapWidth(16)
552 , m_pixmapHeight(16)
553 , m_isHighlightBackgroundFlag(false)
554 , m_wordWrap(false)
555 , m_fontColorFlag(false)
556 , m_isIcon(false)
557 , m_fontColorRoleFlag(false)
558 , m_isTristate(false)
559 , m_clicked(false)
560 {
561 Q_Q(KLabel);
562 m_labelType = NormalType;
563 }
564
565 void KLabelPrivate::changeTheme()
566 {
567 Q_Q(KLabel);
568 initThemeStyle();
569 }
570
571 }
572
573 #include "klabel.moc"
574 #include "moc_klabel.cpp"