File: kbadge.cpp

package info (click to toggle)
ukui-panel 4.0.0.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,148 kB
  • sloc: cpp: 28,423; xml: 476; sh: 51; makefile: 18
file content (205 lines) | stat: -rw-r--r-- 4,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2022, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: Nicole <buxiaoqing@kylinos.cn>
 *
 */

#include "kbadge.h"
#include <QPainter>
#include <QRect>
#include <QFont>
#include <QFontMetrics>
#include <QApplication>
#include <QDebug>

class KBadgePrivate:public QObject
{
    Q_OBJECT
    Q_DECLARE_PUBLIC(KBadge)

public:
    KBadgePrivate(KBadge* parent);

private:
    KBadge* q_ptr;
    QColor m_color;
    int m_value;
    int m_fontSize;
    bool m_isShowValue;
    QRect m_rect;
    QSize m_size;
};

KBadgePrivate::KBadgePrivate(KBadge *parent)
    :q_ptr(parent)
{
    Q_Q(KBadge);
    m_value = -1;
    m_color = q->palette().color(QPalette::Highlight);
    m_fontSize = 8;
    m_isShowValue = true;
    m_rect = QRect(0,0,40,30);
}

KBadge::KBadge(QWidget *parent)
    :QToolButton(parent),
      d_ptr(new KBadgePrivate(this))

{
    Q_D(KBadge);

}

QRect KBadge::geometry()
{
    Q_D(KBadge);
    return d->m_rect;
}

void KBadge::setGeometry(QRect rect)
{
    Q_D(KBadge);
    d->m_rect = rect;
}

void KBadge::setValue(int value)
{
    Q_D(KBadge);
    d->m_value = value;
}

void KBadge::setValueVisiable(bool flag)
{
    Q_D(KBadge);
    d->m_isShowValue = flag;
}

bool KBadge::isValueVisiable() const
{
    Q_D(const KBadge);
    return d->m_isShowValue;
}

int KBadge::value()
{
    Q_D(KBadge);
    return d->m_value;
}

QColor KBadge::color()
{
    Q_D(KBadge);
    return d->m_color;
}

void KBadge::setColor(const QColor &color)
{
    Q_D(KBadge);
    d->m_color = color;
}

int KBadge::fontSize()
{
    Q_D(KBadge);
    return d->m_fontSize;
}

void KBadge::setFontSize(int size)
{
    Q_D(KBadge);
    if(size<1 ||size >100)
        return;
    d->m_fontSize = size;
    updateSize();
}

QSize KBadge::updateSize()
{
    Q_D(KBadge);
    QFont font(QApplication::font());
    font.setPixelSize(d->m_fontSize);
    QFontMetrics fm(font);
    int height = fm.height();
    if (height < 14 ) {
        height = 14;
    }
    int width;
    if(d->m_value <1 ||!d->m_isShowValue)
    {
        this->setVisible(false);
    }
    else if(d->m_value >= 1 && d->m_value < 100)
    {
        width = fm.width(QString::number(d->m_value)) + 10;
        width = width > height ? width:height;
    }
    else
    {
        width = fm.width(QString::number(999)) + 10;
        width = width > height ? width:height;
    }
    this->setFixedSize(QSize(width,height));
    //qDebug()<<"=======update kbadge size"<<width<<height;

    return QSize(width, height);

}
void KBadge::paintEvent(QPaintEvent *event)
{
    Q_D(KBadge);
    QSize size = updateSize();
    int height = size.height();
    int width = size.width();

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(Qt::NoPen);
    painter.setBrush(d->m_color);
    QRect tmpRect(0, 0, width, height);
    painter.drawRoundedRect(tmpRect, height/2, height/2);
    //文字颜色固定
    painter.setPen(QToolButton::palette().color(QPalette::Light));
    if(d->m_value >= 1 && d->m_value<100 && d->m_isShowValue)
    {
        QFont font(QApplication::font());
        font.setPixelSize(d->m_fontSize);
        if (font.family() != QString("Noto Sans CJK SC")) {
            font.setBold(true);
        }
        painter.setFont(font);
        painter.drawText(tmpRect,Qt::AlignCenter,QString::number(d->m_value));
    }

    if(d->m_value >= 100 && d->m_value < INTMAX_MAX && d->m_isShowValue)
    {
        QFont font(QApplication::font());
        font.setPixelSize(d->m_fontSize);
        if (font.family() != QString("Noto Sans CJK SC")) {
            font.setBold(true);
        }
        painter.setFont(font);
        painter.drawText(tmpRect,Qt::AlignCenter,QString("99+"));
    }
}

void KBadge::resizeEvent(QResizeEvent *event)
{
    QToolButton::resizeEvent(event);
    repaint();
}

#include "kbadge.moc"