Debian Codegraph @

kysdk-qtwidgets/src/kbubblewidget.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 "kbubblewidget.h"
24 #include "themeController.h"
25 #include <KWindowEffects>
26 #include <QHBoxLayout>
27 #include <QPainter>
28 #include <QPainterPath>
29 #include <QRegion>
30 #include "kshadowhelper.h"
31 #include <QGraphicsDropShadowEffect>
32 #include <QDebug>
33 #include "kysdk-ukuiwindowhelper/ukuiwindowhelper.h"
34
35 namespace kdk
36 {
37
38 class KBubbleWidgetPrivate : public QObject, public ThemeController
39 {
40 Q_OBJECT
41 Q_DECLARE_PUBLIC(KBubbleWidget)
42
43 public:
44 KBubbleWidgetPrivate(KBubbleWidget *parent);
45
46 private:
47 KBubbleWidget *q_ptr;
48
49 int m_startX;
50 int m_startY;
51 int m_tailWidth;
52 int m_tailHeight;
53 TailDirection m_tailDirection;
54 TailLocation m_tailLocation;
55 int m_topLeftRadius;
56 int m_topRightRadius;
57 int m_bottomLeftRadius;
58 int m_bottomRightRadius;
59 bool m_enableBlur;
60 bool m_isHighlightBackground;
61 qreal m_opacity;
62 QGraphicsDropShadowEffect *m_shadowEffect;
63 QRegion m_region;
64 UkuiWindowHelper *m_ukuiWindowHelper;
65 };
66
67 void KBubbleWidget::setTailPosition(TailDirection dirType, TailLocation locType)
68 {
69 Q_D(KBubbleWidget);
70 d->m_tailDirection = dirType;
71 d->m_tailLocation = locType;
72 this->update();
73 }
74
75 TailDirection KBubbleWidget ::tailDirection()
76 {
77 Q_D(KBubbleWidget);
78 return d->m_tailDirection;
79 }
80
81 TailLocation KBubbleWidget::tailLocation()
82 {
83 Q_D(KBubbleWidget);
84 return d->m_tailLocation;
85 }
86
87 void KBubbleWidget::setBorderRadius(int bottomLeft, int topLeft, int topRight, int bottomRight)
88 {
89 Q_D(KBubbleWidget);
90 d->m_bottomRightRadius = bottomRight;
91 d->m_topLeftRadius = topLeft;
92 d->m_bottomLeftRadius = bottomLeft;
93 d->m_topRightRadius = topRight;
94 }
95
96 void KBubbleWidget::setBorderRadius(int radius)
97 {
98 Q_D(KBubbleWidget);
99 d->m_topLeftRadius = radius;
100 d->m_bottomLeftRadius = radius;
101 d->m_bottomRightRadius = radius;
102 d->m_topRightRadius = radius;
103 }
104
105 void KBubbleWidget::setEnableBlur(bool flag)
106 {
107 Q_D(KBubbleWidget);
108 d->m_enableBlur = flag;
109 }
110
111 bool KBubbleWidget::enableBlur()
112 {
113 Q_D(KBubbleWidget);
114 return d->m_enableBlur;
115 }
116
117 void KBubbleWidget::setOpacity(qreal opacity)
118 {
119 Q_D(KBubbleWidget);
120 if (opacity < 0 || opacity > 1)
121 return;
122 d->m_opacity = opacity;
123 }
124
125 qreal KBubbleWidget::opacity()
126 {
127 Q_D(KBubbleWidget);
128 return d->m_opacity;
129 }
130
131 void KBubbleWidget::setHighlightBackground(bool flag)
132 {
133 Q_D(KBubbleWidget);
134 d->m_isHighlightBackground = flag;
135 update();
136 }
137
138 KBubbleWidget::KBubbleWidget(QWidget *parent)
139 : QWidget(parent)
140 , d_ptr(new KBubbleWidgetPrivate(this))
141 {
142 Q_D(KBubbleWidget);
143
144 setWindowFlags(Qt::FramelessWindowHint);
145 setAttribute(Qt::WA_TranslucentBackground);
146 // effects::KShadowHelper::self()->setWidget(this);
147 d->m_shadowEffect = new QGraphicsDropShadowEffect(this);
148 d->m_shadowEffect->setBlurRadius(12);
149 d->m_shadowEffect->setColor(ThemeController::getCustomColorFromDT("text-disable"));
150 d->m_shadowEffect->setOffset(0, 0);
151 setGraphicsEffect(d->m_shadowEffect);
152
153 d->m_ukuiWindowHelper = new UkuiWindowHelper(this);
154
155 }
156
157 void KBubbleWidget::setTailSize(const QSize &size)
158 {
159 Q_D(KBubbleWidget);
160 d->m_tailWidth = size.width();
161 d->m_tailHeight = size.height();
162 }
163
164 QSize KBubbleWidget::tailSize()
165 {
166 Q_D(KBubbleWidget);
167 QSize size;
168 size.setWidth(d->m_tailWidth);
169 size.setHeight(d->m_tailHeight);
170 return size;
171 }
172
173 void KBubbleWidget ::paintEvent(QPaintEvent *)
174 {
175 Q_D(KBubbleWidget);
176 QPainter painter(this);
177 painter.setPen(Qt::NoPen);
178 painter.setBrush(Qt::NoBrush);
179 painter.drawRoundedRect(rect(), 0, 0);
180 QPolygon trianglePolygon;
181 QPainterPath path;
182 QRect targetRect = this->rect()/*.adjusted(8,8,-8,-8)*/;
183
184 switch (d->m_tailDirection) {
185 case TopDirection:
186 targetRect = targetRect.adjusted(8, d->m_tailHeight, -8, -8);
187
188 if (d->m_tailLocation == LeftLocation)
189 d->m_startX = 18;
190 else if (d->m_tailLocation == MiddleLocation)
191 d->m_startX = (targetRect.width() - d->m_tailWidth * 2) / 2;
192 else if (d->m_tailLocation == RightLocation)
193 d->m_startX = targetRect.width() - d->m_tailHeight * 2 - d->m_tailWidth - 19;
194 d->m_startY = 0;
195 trianglePolygon << QPoint(d->m_startX + d->m_tailHeight, d->m_tailHeight);
196 trianglePolygon << QPoint(d->m_startX + d->m_tailWidth / 2 + d->m_tailHeight, 0);
197 trianglePolygon << QPoint(d->m_startX + d->m_tailWidth + d->m_tailHeight, d->m_tailHeight);
198 setContentsMargins(4, 4 + d->m_tailHeight, 4, 4);
199 break;
200
201 case LeftDirection:
202 targetRect = targetRect.adjusted(d->m_tailHeight, 8, -8, -8);
203
204 if (d->m_tailLocation == LeftLocation)
205 d->m_startY = d->m_tailHeight + 18;
206 else if (d->m_tailLocation == MiddleLocation)
207 d->m_startY = targetRect.height() / 2 - d->m_tailWidth / 2;
208 else if (d->m_tailLocation == RightLocation)
209 d->m_startY = d->m_tailHeight + static_cast<int>((targetRect.height() - d->m_tailHeight * 2 - d->m_tailWidth)) - 19;
210 d->m_startX = 0;
211 trianglePolygon << QPoint(d->m_startX + d->m_tailHeight, d->m_startY);
212 trianglePolygon << QPoint(d->m_startX, d->m_startY + d->m_tailWidth / 2);
213 trianglePolygon << QPoint(d->m_startX + d->m_tailHeight, d->m_startY + d->m_tailWidth);
214 setContentsMargins(4 + d->m_tailHeight, 4, 4, 4);
215 break;
216
217 case RightDirection:
218 targetRect = targetRect.adjusted(8, 8, -d->m_tailHeight, -8);
219
220 if (d->m_tailLocation == LeftLocation)
221 d->m_startY = d->m_tailHeight + 18;
222 else if (d->m_tailLocation == MiddleLocation)
223 d->m_startY = targetRect.height() / 2 - d->m_tailWidth / 2;
224 else if (d->m_tailLocation == RightLocation)
225 d->m_startY = d->m_tailHeight + static_cast<int>((targetRect.height() - d->m_tailHeight * 2 - d->m_tailWidth)) - 19;
226 d->m_startX = targetRect.width();
227 trianglePolygon << QPoint(d->m_startX - 1, d->m_startY);
228 trianglePolygon << QPoint(d->m_startX + d->m_tailHeight - 1, d->m_startY + d->m_tailWidth / 2);
229 trianglePolygon << QPoint(d->m_startX - 1, d->m_startY + d->m_tailWidth);
230 setContentsMargins(4, 4, 4 + d->m_tailHeight, 4);
231 break;
232 case BottomDirection:
233 targetRect = targetRect.adjusted(8, 8, -8, -d->m_tailHeight);
234
235 if (d->m_tailLocation == LeftLocation)
236 d->m_startX = 18;
237 else if (d->m_tailLocation == MiddleLocation)
238 d->m_startX = (this->rect().width() - d->m_tailWidth * 2) / 2;
239 else if (d->m_tailLocation == RightLocation)
240 d->m_startX = this->rect().width() - d->m_tailHeight * 2 - d->m_tailWidth - 19;
241 d->m_startY = this->rect().height();
242 trianglePolygon << QPoint(d->m_startX + d->m_tailHeight, d->m_startY - d->m_tailHeight - 1);
243 trianglePolygon << QPoint(d->m_startX + d->m_tailWidth / 2 + d->m_tailHeight, d->m_startY - 1);
244 trianglePolygon << QPoint(d->m_startX + d->m_tailWidth + d->m_tailHeight, d->m_startY - d->m_tailHeight - 1);
245 setContentsMargins(4, 4, 4, 4 + d->m_tailHeight);
246 break;
247 case None:
248 break;
249 }
250
251 path.moveTo(targetRect.topRight() - QPoint(d->m_topRightRadius, 0)); // 右上
252 path.lineTo(targetRect.topLeft() + QPointF(d->m_topLeftRadius, 0)); // 上方线
253 path.quadTo(targetRect.topLeft(), targetRect.topLeft() + QPointF(0, d->m_topLeftRadius)); // 圆角
254 path.lineTo(targetRect.bottomLeft() + QPointF(0, -d->m_bottomLeftRadius)); // 左方线
255 path.quadTo(targetRect.bottomLeft(), targetRect.bottomLeft() + QPointF(d->m_bottomLeftRadius, 0)); // 圆角
256 path.lineTo(targetRect.bottomRight() - QPointF(d->m_bottomRightRadius, 0)); // 下方线
257 path.quadTo(targetRect.bottomRight(), targetRect.bottomRight() + QPointF(0, -d->m_bottomRightRadius)); // 圆角
258 path.lineTo(targetRect.topRight() + QPointF(0, d->m_topRightRadius)); // 右方线
259 path.quadTo(targetRect.topRight(), targetRect.topRight() - QPointF(d->m_topRightRadius, 0)); // 圆角
260
261 path.addPolygon(trianglePolygon);
262 path = path.simplified(); // 合并subpath,取消交集部分的线
263
264 painter.setRenderHints(QPainter::Antialiasing);
265
266 if (d->m_enableBlur && !d->m_isHighlightBackground && isActiveWindow()) {
267 // 开启毛玻璃时
268 d->m_region = QRegion(path.toFillPolygon().toPolygon());
269 #ifdef USE_UKUI_SHELL_PLUGIN
270 d->m_ukuiWindowHelper->setBlurEffect(d->m_region, 0, true);
271 #else
272 KWindowEffects::enableBlurBehind(this->winId(), true, d->m_region);
273 #endif
274 this->setMask(rect());
275 painter.setOpacity(d->m_opacity);
276 painter.setBrush(Qt::NoBrush);
277 } else {
278 // 未开启毛玻璃时
279 painter.setOpacity(1);
280 #ifdef USE_UKUI_SHELL_PLUGIN
281 d->m_ukuiWindowHelper->setBlurEffect(d->m_region, 0, false);
282 #else
283 KWindowEffects::enableBlurBehind(this->winId(), false, d->m_region);
284 #endif
285 if (d->m_isHighlightBackground)
286 painter.setBrush(ThemeController::getCustomColorFromDT("highlight-active"));
287 else
288 painter.setBrush(ThemeController::getCustomColorFromDT("kmenu"));//ThemeController::getCustomColorFromDT("base-active"));
289 }
290 QPen pen;
291 pen.setJoinStyle(Qt::RoundJoin);
292 pen.setWidthF(1);
293 pen.setColor(Qt::transparent);
294 painter.translate(0.5, 0.5);
295 painter.setPen(pen);
296
297 painter.drawPath(path);
298
299 }
300
301 KBubbleWidgetPrivate::KBubbleWidgetPrivate(KBubbleWidget *parent)
302 : q_ptr(parent)
303 , m_tailHeight(8)
304 , m_tailWidth(16)
305 , m_enableBlur(false)
306 , m_opacity(0.5)
307 , m_isHighlightBackground(false)
308 , m_tailLocation(TailLocation::LeftLocation)
309 , m_tailDirection(TailDirection::BottomDirection)
310 {
311 Q_Q(KBubbleWidget);
312 int radius = ThemeController::getRadiusFromDT("kradius-menu");
313 if (radius == -1) {
314 m_bottomLeftRadius = 8;
315 m_topLeftRadius = 8;
316 m_bottomRightRadius = 8;
317 m_topRightRadius = 8;
318 } else {
319 m_bottomLeftRadius = radius;
320 m_topLeftRadius = radius;
321 m_bottomRightRadius = radius;
322 m_topRightRadius = radius;
323 }
324 }
325
326 }
327 #include "kbubblewidget.moc"
328 #include "moc_kbubblewidget.cpp"