Debian Codegraph @

kysdk-qtwidgets/src/kpixmapcontainer.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: Zhen Sun <[email protected]>
20 *
21 */
22
23 #include "kpixmapcontainer.h"
24 #include "themeController.h"
25 #include <QApplication>
26 #include <QDebug>
27 #include <QLabel>
28 #include <QPainter>
29 #include <QVBoxLayout>
30
31 const static int margin = 10;
32 namespace kdk
33 {
34
35 class KPixmapContainerPrivate : public QObject
36 {
37 Q_OBJECT
38 Q_DECLARE_PUBLIC(KPixmapContainer)
39
40 public:
41 KPixmapContainerPrivate(KPixmapContainer *parent);
42
43 private:
44 KPixmapContainer *q_ptr;
45 QPixmap m_pixmap;
46 QColor m_color;
47 int m_fontSize;
48 int m_value;
49 bool m_isShowValue;
50 };
51
52 KPixmapContainer::KPixmapContainer(QWidget *parent)
53 : QWidget(parent)
54 , d_ptr(new KPixmapContainerPrivate(this))
55 {
56 Q_D(KPixmapContainer);
57 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
58 }
59
60 int KPixmapContainer::value() const
61 {
62 Q_D(const KPixmapContainer);
63 return d->m_value;
64 }
65
66 void KPixmapContainer::setValue(int value)
67 {
68 Q_D(KPixmapContainer);
69 d->m_value = value;
70 }
71
72 void KPixmapContainer::setValueVisiable(bool flag)
73 {
74 Q_D(KPixmapContainer);
75 d->m_isShowValue = flag;
76 }
77
78 bool KPixmapContainer::isValueVisiable() const
79 {
80 Q_D(const KPixmapContainer);
81 return d->m_isShowValue;
82 }
83
84 void KPixmapContainer::setPixmap(const QPixmap &pixmap)
85 {
86 Q_D(KPixmapContainer);
87 d->m_pixmap = pixmap;
88 QSize size = QSize(d->m_pixmap.size().width() + 20, d->m_pixmap.size().height() + 20);
89 this->setFixedSize(size);
90 update();
91 }
92
93 QPixmap KPixmapContainer::pixmap() const
94 {
95 Q_D(const KPixmapContainer);
96 if (!d->m_pixmap.isNull())
97 return d->m_pixmap;
98 else
99 return QPixmap();
100 }
101
102 void KPixmapContainer::clearValue()
103 {
104 Q_D(KPixmapContainer);
105 d->m_value = 0;
106 }
107
108 QColor KPixmapContainer::color()
109 {
110 Q_D(KPixmapContainer);
111 return d->m_color;
112 }
113
114 void KPixmapContainer::setColor(const QColor &color)
115 {
116 Q_D(KPixmapContainer);
117 d->m_color = color;
118 }
119
120 int KPixmapContainer::fontSize()
121 {
122 Q_D(KPixmapContainer);
123 return d->m_fontSize;
124 }
125
126 void KPixmapContainer::setFontSize(int size)
127 {
128 Q_D(KPixmapContainer);
129 if (size < 1 || size > 100)
130 return;
131 d->m_fontSize = size;
132 update();
133 }
134
135 void KPixmapContainer::paintEvent(QPaintEvent *event)
136 {
137 Q_D(KPixmapContainer);
138 QWidget::paintEvent(event);
139 QPainter p(this);
140 p.setPen(Qt::NoPen);
141 p.drawRect(this->rect());
142
143 QFont font(QApplication::font());
144 font.setPixelSize(d->m_fontSize);
145 QFontMetrics fm(font);
146 int height = fm.height();
147 int width;
148 if (d->m_value < 1 || !d->m_isShowValue) {
149 width = 10;
150 height = 10;
151 } else if (d->m_value >= 1 && d->m_value < 1000) {
152 width = fm.horizontalAdvance(QString::number(d->m_value));
153 width = width > height ? width : height;
154 } else {
155 width = fm.horizontalAdvance(QString::number(999));
156 width = width > height ? width : height;
157 }
158 QPainter painter(this);
159 painter.setRenderHint(QPainter::Antialiasing);
160 painter.setPen(Qt::NoPen);
161
162 d->m_color = ThemeController::getCustomColorFromDT("highlight-active");
163
164 painter.setBrush(d->m_color);
165
166 if (ThemeController::themeMode() == LightTheme)
167 painter.drawPixmap(margin, margin, d->m_pixmap.width(), d->m_pixmap.height(), d->m_pixmap);
168 else {
169 if (!d->m_pixmap.isNull()) {
170 if (ThemeController::isPixmapPureColor(d->m_pixmap))
171 painter.drawPixmap(margin, margin, d->m_pixmap.width(), d->m_pixmap.height(), ThemeController::drawSymbolicColoredPixmap(d->m_pixmap));
172 }
173 }
174 QRect tmpRect(this->rect().topRight().x() - width / 2 - margin, this->rect().topRight().y() - height / 2 + margin, width, height);
175 painter.drawRoundedRect(tmpRect, height / 2, height / 2);
176 painter.setPen(ThemeController::getCustomColorFromDT("kwhite"));
177 if (d->m_value >= 1 && d->m_value < 1000 && d->m_isShowValue) {
178 QFont font(QApplication::font());
179 font.setPixelSize(d->m_fontSize);
180 painter.setFont(font);
181 painter.drawText(tmpRect, Qt::AlignCenter, QString::number(d->m_value));
182 }
183
184 if (d->m_value >= 1000 && d->m_value < INTMAX_MAX && d->m_isShowValue) {
185 painter.setBrush(ThemeController::getCustomColorFromDT("kwhite"));
186 QPointF pointf(tmpRect.center().x(), tmpRect.center().y());
187 painter.drawEllipse(pointf, qreal(1.0), qreal(1.0));
188 QPointF leff(pointf.x() - 5, pointf.y());
189 QPointF rightf(pointf.x() + 5, pointf.y());
190 painter.drawEllipse(leff, qreal(1.0), qreal(1.0));
191 painter.drawEllipse(rightf, qreal(1.0), qreal(1.0));
192 }
193 }
194
195 KPixmapContainerPrivate::KPixmapContainerPrivate(KPixmapContainer *parent)
196 : q_ptr(parent)
197 {
198 Q_Q(KPixmapContainer);
199 m_value = -1;
200 m_isShowValue = true;
201 m_color = ThemeController::getCustomColorFromDT("kbrand1");
202 m_fontSize = 10;
203 setParent(parent);
204 }
205
206 }
207
208 #include "kpixmapcontainer.moc"
209 #include "moc_kpixmapcontainer.cpp"