Debian Codegraph @

kysdk-qtwidgets/src/kfilewidget.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 "kfilewidget.h"
24 #include "themeController.h"
25 #include <QDebug>
26 #include <QEvent>
27 #include <QFontMetrics>
28 #include <QHBoxLayout>
29 #include <QIcon>
30 #include <QLabel>
31 #include <QPaintEvent>
32 #include <QPainter>
33 #include <QPainterPath>
34 #include <QVBoxLayout>
35 #include "accessinfohelper.h"
36
37 namespace kdk
38 {
39
40 class KFileWidgetPrivate : public QObject, public ThemeController
41 {
42 Q_OBJECT
43 Q_DECLARE_PUBLIC(KFileWidget)
44 public:
45 KFileWidgetPrivate(KFileWidget *parent);
46
47 protected:
48 void changeTheme();
49
50 private:
51 KFileWidget *q_ptr;
52 QHBoxLayout *m_pHLayout;
53 QVBoxLayout *m_pVLayout;
54 QLabel *m_pIconLabel;
55 QLabel *m_pMainLabel;
56 QLabel *m_pSubLabel;
57 QColor m_buttonBkgColor;
58 QColor m_iconColor;
59 QRect m_buttonRect;
60 int m_bottomRightRadius;
61 int m_topLeftRadius;
62 int m_bottomLeftRadius;
63 int m_topRightRadius;
64 };
65
66 KFileWidget::KFileWidget(QWidget *parent)
67 : QWidget(parent)
68 , d_ptr(new KFileWidgetPrivate(this))
69 {
70 Q_D(KFileWidget);
71 installEventFilter(this);
72 setMouseTracking(true);
73 d->m_pMainLabel->installEventFilter(this);
74 setFixedHeight(56);
75 }
76
77 void KFileWidget::setMainText(QString str)
78 {
79 Q_D(KFileWidget);
80 d->m_pMainLabel->setText(str);
81 }
82
83 void KFileWidget::setSubText(QString str)
84 {
85 Q_D(KFileWidget);
86 d->m_pSubLabel->setText(str);
87 }
88
89 void KFileWidget::setIcon(QIcon icon)
90 {
91 Q_D(KFileWidget);
92 d->m_pIconLabel->setFixedSize(32, 32);
93 d->m_pIconLabel->setPixmap(icon.pixmap(32, 32));
94 }
95
96 void KFileWidget::setBorderRadius(int radius)
97 {
98 Q_D(KFileWidget);
99 d->m_bottomRightRadius = radius;
100 d->m_topLeftRadius = radius;
101 d->m_bottomLeftRadius = radius;
102 d->m_topRightRadius = radius;
103 }
104
105 void KFileWidget::setBorderRadius(int bottomLeft, int topLeft, int topRight, int bottomRight)
106 {
107 Q_D(KFileWidget);
108 d->m_bottomRightRadius = bottomRight;
109 d->m_topLeftRadius = topLeft;
110 d->m_bottomLeftRadius = bottomLeft;
111 d->m_topRightRadius = topRight;
112 }
113
114 void KFileWidget::paintEvent(QPaintEvent *event)
115 {
116 Q_D(KFileWidget);
117 if (d->m_pSubLabel->text().isNull())
118 d->m_pSubLabel->setVisible(false);
119 d->m_buttonRect = QRect(rect().width() - 17, 0, 16, 16);
120 QPainter painter(this);
121 painter.setRenderHint(QPainter::Antialiasing);
122 painter.setRenderHint(QPainter::SmoothPixmapTransform);
123 painter.setBrush(ThemeController::getCustomColorFromDT("window-active"));
124 if (ThemeController::themeMode() == LightTheme) {
125 QColor color(38, 38, 38);
126 color.setAlphaF(0.15);
127 painter.setPen(color);
128 } else {
129 QColor color(255, 255, 255);
130 color.setAlphaF(0.15);
131 painter.setPen(color);
132 }
133
134 QPainterPath path;
135 QRect rectPath = rect().adjusted(0, 2, -2, 0);
136
137 path.moveTo(rectPath.topRight() - QPoint(d->m_topRightRadius, 0)); // 右上
138 path.lineTo(rectPath.topLeft() + QPointF(d->m_topLeftRadius, 0)); // 上方线
139 path.quadTo(rectPath.topLeft(), rectPath.topLeft() + QPointF(0, d->m_topLeftRadius)); // 圆角
140 path.lineTo(rectPath.bottomLeft() + QPointF(0, -d->m_bottomLeftRadius)); // 左方线
141 path.quadTo(rectPath.bottomLeft(), rectPath.bottomLeft() + QPointF(d->m_bottomLeftRadius, 0)); // 圆角
142 path.lineTo(rectPath.bottomRight() - QPointF(d->m_bottomRightRadius, 0)); // 下方线
143 path.quadTo(rectPath.bottomRight(), rectPath.bottomRight() + QPointF(0, -d->m_bottomRightRadius)); // 圆角
144 path.lineTo(rectPath.topRight() + QPointF(0, d->m_topRightRadius)); // 右方线
145 path.quadTo(rectPath.topRight(), rectPath.topRight() - QPointF(d->m_topRightRadius, 0)); // 圆角
146
147 if (!isEnabled()) {
148 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("button-disable");
149 d->m_iconColor = ThemeController::getCustomColorFromDT("highlightedtext-active");
150 }
151 painter.drawPath(path);
152 painter.setBrush(d->m_buttonBkgColor);
153 painter.drawEllipse(d->m_buttonRect);
154
155 d->m_pHLayout->setContentsMargins(8, 8, d->m_buttonRect.width(), 8);
156 QIcon icon(QIcon::fromTheme("application-exit-symbolic"));
157 QPixmap pixmap = icon.pixmap(d->m_buttonRect.size());
158 painter.drawPixmap(d->m_buttonRect, ThemeController::drawColoredPixmap(pixmap, d->m_iconColor));
159 }
160
161 bool KFileWidget::eventFilter(QObject *watched, QEvent *event)
162 {
163 Q_D(KFileWidget);
164
165 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
166 switch (event->type()) {
167 case QEvent::Enter:
168 case QEvent::MouseMove: {
169 if (d->m_buttonRect.contains(mouseEvent->pos())) {
170 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
171 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("kred1");
172 } else {
173 if (ThemeController::themeMode() == LightTheme) {
174 d->m_iconColor = ThemeController::getCustomColorFromDT("kgray-17");
175 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("kwhite");
176 } else {
177 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
178 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("window-active");
179 }
180 }
181 update();
182 break;
183 }
184 case QEvent::MouseButtonPress: {
185 if (d->m_buttonRect.contains(mouseEvent->pos())) {
186 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
187 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("error-click");
188 } else {
189 if (ThemeController::themeMode() == LightTheme) {
190 d->m_iconColor = ThemeController::getCustomColorFromDT("kgray-17");
191 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("kwhite");
192 } else {
193 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
194 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("window-active");
195 }
196 }
197
198 update();
199 break;
200 }
201 case QEvent::MouseButtonRelease: {
202 if (d->m_buttonRect.contains(mouseEvent->pos())) {
203 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
204 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("kred1");
205 if (isEnabled())
206 emit clicked();
207 } else {
208 if (ThemeController::themeMode() == LightTheme) {
209 d->m_iconColor = ThemeController::getCustomColorFromDT("kgray-17");
210 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("kwhite");
211 } else {
212 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
213 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("window-active");
214 }
215 }
216 update();
217 break;
218 }
219 case QEvent::Leave: {
220 if (ThemeController::themeMode() == LightTheme) {
221 d->m_iconColor = ThemeController::getCustomColorFromDT("kgray-17");
222 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("kwhite");
223 } else {
224 d->m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
225 d->m_buttonBkgColor = ThemeController::getCustomColorFromDT("window-active");
226 }
227 update();
228 break;
229 }
230 default:
231 break;
232 }
233 return QWidget::eventFilter(watched, event);
234 }
235
236 KFileWidgetPrivate::KFileWidgetPrivate(KFileWidget *parent)
237 : q_ptr(parent)
238 {
239 Q_Q(KFileWidget);
240 int radius = ThemeController::getRadiusFromDT("kradius-normal");
241 if (radius == -1) {
242 m_bottomRightRadius = 6;
243 m_topLeftRadius = 6;
244 m_bottomLeftRadius = 6;
245 m_topRightRadius = 6;
246 } else {
247 m_bottomRightRadius = radius;
248 m_topLeftRadius = radius;
249 m_bottomLeftRadius = radius;
250 m_topRightRadius = radius;
251 }
252 m_pMainLabel = new QLabel();
253 m_pSubLabel = new QLabel();
254
255 m_pVLayout = new QVBoxLayout();
256 m_pVLayout->setSpacing(0);
257 m_pVLayout->addStretch();
258 m_pVLayout->addWidget(m_pMainLabel);
259 m_pVLayout->addWidget(m_pSubLabel);
260 m_pVLayout->addStretch();
261 m_pVLayout->setContentsMargins(0, 0, 0, 0);
262
263 m_pIconLabel = new QLabel();
264 m_pHLayout = new QHBoxLayout(q);
265
266 m_pHLayout->setContentsMargins(0, 0, 0, 0);
267 m_pHLayout->setSpacing(0);
268 m_pHLayout->addWidget(m_pIconLabel);
269 m_pHLayout->addSpacing(8);
270 m_pHLayout->addLayout(m_pVLayout);
271 q->setContentsMargins(0, 0, 0, 0);
272
273 changeTheme();
274 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
275 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
276 {
277 changeTheme();
278 }
279 });
280 setParent(parent);
281
282 KDK_ALL_INFO_FORMAT(m_pIconLabel,"");
283 KDK_ALL_INFO_FORMAT(m_pMainLabel,"");
284 KDK_ALL_INFO_FORMAT(m_pSubLabel,"");
285 }
286
287 void KFileWidgetPrivate::changeTheme()
288 {
289 Q_Q(KFileWidget);
290 initThemeStyle();
291
292 QPalette subPalette = q->palette();
293 QColor color = ThemeController::getCustomColorFromDT("windowtext-disable");
294 subPalette.setColor(QPalette::Text, color);
295 m_pSubLabel->setPalette(subPalette);
296
297 if (ThemeController::themeMode() == LightTheme) {
298 m_iconColor = ThemeController::getCustomColorFromDT("kgray-17");
299 m_buttonBkgColor = ThemeController::getCustomColorFromDT("kwhite");
300 } else {
301 m_iconColor = ThemeController::getCustomColorFromDT("kwhite");
302 m_buttonBkgColor = ThemeController::getCustomColorFromDT("window-active");
303 }
304 }
305
306 }
307
308 #include "kfilewidget.moc"
309 #include "moc_kfilewidget.cpp"