| 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 "kbadge.h" |
| 24 |
#include "themeController.h" |
| 25 |
#include <QApplication> |
| 26 |
#include <QFont> |
| 27 |
#include <QFontMetrics> |
| 28 |
#include <QPainter> |
| 29 |
#include <QRect> |
| 30 |
|
| 31 |
namespace kdk |
| 32 |
{ |
| 33 |
class KBadgePrivate : public QObject, public ThemeController |
| 34 |
{ |
| 35 |
Q_OBJECT |
| 36 |
Q_DECLARE_PUBLIC(KBadge) |
| 37 |
|
| 38 |
public: |
| 39 |
KBadgePrivate(KBadge *parent); |
| 40 |
|
| 41 |
private: |
| 42 |
KBadge *q_ptr; |
| 43 |
QColor m_color; |
| 44 |
int m_value; |
| 45 |
int m_fontSize; |
| 46 |
bool m_isShowValue; |
| 47 |
bool m_useCustomColor; |
| 48 |
}; |
| 49 |
|
| 50 |
KBadgePrivate::KBadgePrivate(KBadge *parent) |
| 51 |
: q_ptr(parent) |
| 52 |
{ |
| 53 |
Q_Q(KBadge); |
| 54 |
m_value = -1; |
| 55 |
m_color = ThemeController::getCustomColorFromDT("highlight-active"); |
| 56 |
m_fontSize = 14; |
| 57 |
m_isShowValue = true; |
| 58 |
m_useCustomColor = false; |
| 59 |
setParent(parent); |
| 60 |
} |
| 61 |
|
| 62 |
KBadge::KBadge(QWidget *parent) |
| 63 |
: QWidget(parent) |
| 64 |
, d_ptr(new KBadgePrivate(this)) |
| 65 |
{ |
| 66 |
Q_D(KBadge); |
| 67 |
setMinimumSize(40, 30); |
| 68 |
} |
| 69 |
|
| 70 |
void KBadge::setValue(int value) |
| 71 |
{ |
| 72 |
Q_D(KBadge); |
| 73 |
d->m_value = value; |
| 74 |
} |
| 75 |
|
| 76 |
void KBadge::setValueVisiable(bool flag) |
| 77 |
{ |
| 78 |
Q_D(KBadge); |
| 79 |
d->m_isShowValue = flag; |
| 80 |
} |
| 81 |
|
| 82 |
bool KBadge::isValueVisiable() const |
| 83 |
{ |
| 84 |
Q_D(const KBadge); |
| 85 |
return d->m_isShowValue; |
| 86 |
} |
| 87 |
|
| 88 |
int KBadge::value() |
| 89 |
{ |
| 90 |
Q_D(KBadge); |
| 91 |
return d->m_value; |
| 92 |
} |
| 93 |
|
| 94 |
QColor KBadge::color() |
| 95 |
{ |
| 96 |
Q_D(KBadge); |
| 97 |
return d->m_color; |
| 98 |
} |
| 99 |
|
| 100 |
void KBadge::setColor(const QColor &color) |
| 101 |
{ |
| 102 |
Q_D(KBadge); |
| 103 |
d->m_useCustomColor = true; |
| 104 |
d->m_color = color; |
| 105 |
} |
| 106 |
|
| 107 |
int KBadge::fontSize() |
| 108 |
{ |
| 109 |
Q_D(KBadge); |
| 110 |
return d->m_fontSize; |
| 111 |
} |
| 112 |
|
| 113 |
void KBadge::setFontSize(int size) |
| 114 |
{ |
| 115 |
Q_D(KBadge); |
| 116 |
if (size < 1 || size > 100) |
| 117 |
return; |
| 118 |
d->m_fontSize = size; |
| 119 |
} |
| 120 |
|
| 121 |
void KBadge::paintEvent(QPaintEvent *event) |
| 122 |
{ |
| 123 |
Q_D(KBadge); |
| 124 |
QFont font(QApplication::font()); |
| 125 |
font.setPixelSize(d->m_fontSize); |
| 126 |
QFontMetrics fm(font); |
| 127 |
int height = fm.height(); |
| 128 |
int width; |
| 129 |
if (d->m_value < 1 || !d->m_isShowValue) { |
| 130 |
width = 10; |
| 131 |
height = 10; |
| 132 |
} else if (d->m_value >= 1 && d->m_value < 1000) { |
| 133 |
width = fm.horizontalAdvance(QString::number(d->m_value)) + 10; |
| 134 |
width = width > height ? width : height; |
| 135 |
} else { |
| 136 |
width = fm.horizontalAdvance(QString::number(999)) + 10; |
| 137 |
width = width > height ? width : height; |
| 138 |
} |
| 139 |
QPainter painter(this); |
| 140 |
painter.setRenderHint(QPainter::Antialiasing); |
| 141 |
painter.setPen(Qt::NoPen); |
| 142 |
if (d->m_useCustomColor) |
| 143 |
painter.setBrush(d->m_color); |
| 144 |
else |
| 145 |
painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active")); |
| 146 |
QRect tmpRect(rect().center().x() - width / 2, rect().center().y() - height / 2, width, height); |
| 147 |
painter.drawRoundedRect(tmpRect, height / 2, height / 2); |
| 148 |
// 文字颜色固定 |
| 149 |
painter.setPen(ThemeController::getCustomColorFromDT("light-active")); |
| 150 |
if (d->m_value >= 1 && d->m_value < 1000 && d->m_isShowValue) { |
| 151 |
QFont font(QApplication::font()); |
| 152 |
font.setPixelSize(d->m_fontSize); |
| 153 |
painter.setFont(font); |
| 154 |
painter.drawText(tmpRect, Qt::AlignCenter, QString::number(d->m_value)); |
| 155 |
} |
| 156 |
|
| 157 |
if (d->m_value >= 1000 && d->m_value < INTMAX_MAX && d->m_isShowValue) { |
| 158 |
painter.setBrush(ThemeController::getCustomColorFromDT("light-active")); |
| 159 |
QPointF pointf(rect().center().x(), rect().center().y()); |
| 160 |
painter.drawEllipse(pointf, qreal(1.5), qreal(1.5)); |
| 161 |
QPointF leff(pointf.x() - 10, pointf.y()); |
| 162 |
QPointF rightf(pointf.x() + 10, pointf.y()); |
| 163 |
painter.drawEllipse(leff, qreal(1.5), qreal(1.5)); |
| 164 |
painter.drawEllipse(rightf, qreal(1.5), qreal(1.5)); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
void KBadge::resizeEvent(QResizeEvent *event) |
| 169 |
{ |
| 170 |
QWidget::resizeEvent(event); |
| 171 |
repaint(); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
#include "kbadge.moc" |
| 176 |
#include "moc_kbadge.cpp" |