Debian Codegraph @

kysdk-qtwidgets/src/ktranslucentfloor.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 "ktranslucentfloor.h"
24 #include "kshadowhelper.h"
25 #include "themeController.h"
26 #include <QDebug>
27 #include <QPainter>
28 #include <QPainterPath>
29 #include <QRegion>
30 #include <kwindoweffects.h>
31 #include "kysdk-ukuiwindowhelper/ukuiwindowhelper.h"
32 #include <QEvent>
33 #include <QGuiApplication>
34 #include "ukuistylehelper/ukui-decoration-manager.h"
35 #include "ukuistylehelper/ukui-shell-manager.h"
36 #include "xatom-helper.h"
37
38 namespace kdk
39 {
40
41 class KTranslucentFloorPrivate : public QObject
42 {
43 Q_OBJECT
44 Q_DECLARE_PUBLIC(KTranslucentFloor)
45 public:
46 KTranslucentFloorPrivate(KTranslucentFloor *parent);
47
48 protected:
49 bool eventFilter(QObject *target, QEvent *event) override;
50
51 private:
52 KTranslucentFloor *q_ptr;
53 int m_radius;
54 bool m_shadowFlag;
55 bool m_enableBlur;
56 qreal m_opacity;
57 QRegion m_region;
58 UkuiWindowHelper *m_ukuiWindowHelper;
59 };
60
61 KTranslucentFloor::KTranslucentFloor(QWidget *parent)
62 : QFrame(parent)
63 , d_ptr(new KTranslucentFloorPrivate(this))
64 {
65 Q_D(KTranslucentFloor);
66 // setWindowFlags(Qt::FramelessWindowHint);
67 setAttribute(Qt::WA_TranslucentBackground, true);
68 d->m_ukuiWindowHelper = new UkuiWindowHelper(this);
69
70 QString platform = QGuiApplication::platformName();
71 if (platform.startsWith(QLatin1String("xcb"), Qt::CaseInsensitive)) {
72 MotifWmHints hints;
73 hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
74 hints.functions = MWM_FUNC_ALL;
75 hints.decorations = MWM_DECOR_BORDER;
76 XAtomHelper::getInstance()->setWindowMotifHint(this->winId(), hints);
77 }
78
79 }
80
81 void KTranslucentFloor::setBorderRadius(int radius)
82 {
83 Q_D(KTranslucentFloor);
84 d->m_radius = radius;
85 if (shadow()) {
86 effects::KShadowHelper::self()->setWidget(this, d->m_radius);
87 }
88 }
89
90 int KTranslucentFloor::borderRadius()
91 {
92 Q_D(KTranslucentFloor);
93 return d->m_radius;
94 }
95
96 void KTranslucentFloor::setShadow(bool flag)
97 {
98 Q_D(KTranslucentFloor);
99 d->m_shadowFlag = flag;
100 if (d->m_shadowFlag)
101 effects::KShadowHelper::self()->setWidget(this, d->m_radius);
102 }
103
104 bool KTranslucentFloor::shadow()
105 {
106 Q_D(KTranslucentFloor);
107 return d->m_shadowFlag;
108 }
109
110 void KTranslucentFloor::setEnableBlur(bool flag)
111 {
112 Q_D(KTranslucentFloor);
113 d->m_enableBlur = flag;
114 }
115
116 bool KTranslucentFloor::enableBlur()
117 {
118 Q_D(KTranslucentFloor);
119 return d->m_enableBlur;
120 }
121
122 void KTranslucentFloor::setOpacity(qreal opacity)
123 {
124 Q_D(KTranslucentFloor);
125 d->m_opacity = opacity;
126 }
127
128 qreal KTranslucentFloor::opacity()
129 {
130 Q_D(KTranslucentFloor);
131 return d->m_opacity;
132 }
133
134 void KTranslucentFloor::paintEvent(QPaintEvent *event)
135 {
136 Q_D(KTranslucentFloor);
137 QPainter painter(this);
138 painter.setRenderHints(QPainter::Antialiasing);
139
140 QPainterPath path;
141 QRect rect = this->rect();
142 path.addRoundedRect(rect, d->m_radius, d->m_radius);
143
144 if (d->m_enableBlur && isActiveWindow()) {
145 // 开启毛玻璃时
146 d->m_region =QRegion(path.toFillPolygon().toPolygon());
147 if(d->m_enableBlur)
148 {
149 #ifdef USE_UKUI_SHELL_PLUGIN
150 d->m_ukuiWindowHelper->setBlurEffect(d->m_region, 0, true);
151 #else
152 KWindowEffects::enableBlurBehind(this->winId(), true, d->m_region);
153 #endif
154 }
155 this->setMask(d->m_region);
156 painter.setOpacity(d->m_opacity);
157 painter.setBrush(Qt::NoBrush);
158 } else {
159 // 未开启毛玻璃时
160 #ifdef USE_UKUI_SHELL_PLUGIN
161 d->m_ukuiWindowHelper->setBlurEffect(d->m_region, 0, false);
162 #else
163 KWindowEffects::enableBlurBehind(this->winId(), false, d->m_region);
164 #endif
165 painter.setOpacity(1);
166 painter.setBrush(this->palette().color(this->backgroundRole()));
167 }
168 painter.setPen(Qt::NoPen);
169 painter.drawPath(path);
170 }
171
172 KTranslucentFloorPrivate::KTranslucentFloorPrivate(KTranslucentFloor *parent)
173 : q_ptr(parent)
174 , m_shadowFlag(true)
175 , m_enableBlur(true)
176 , m_opacity(0.5)
177 {
178 parent->installEventFilter(this);
179 m_radius = ThemeController::getRadiusFromDT("kradius-window");
180 if (m_radius == -1)
181 m_radius = 12;
182 }
183
184 bool KTranslucentFloorPrivate::eventFilter(QObject *target, QEvent *event)
185 {
186 QString platform = QGuiApplication::platformName();
187 if (platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive))
188 {
189 if ((event->type() == QEvent::PlatformSurface || event->type() == QEvent::Show || event->type() == QEvent::UpdateRequest))
190 {
191 #ifdef USE_UKUI_SHELL_PLUGIN
192 m_ukuiWindowHelper->removeTitleBar();
193 #else
194 #ifdef USE_UKUI_PROTOCOL
195 if (UkuiShellManager::getInstance()->ukuishellReady())
196 UkuiShellManager::getInstance()->removeHeaderBar(this->windowHandle());
197 else
198 UKUIDecorationManager::getInstance()->removeHeaderBar(this->windowHandle());
199 #else
200 UKUIDecorationManager::getInstance()->removeHeaderBar(this->windowHandle());
201 #endif
202 #endif
203 }
204 }
205 return QObject::eventFilter(target,event);
206 }
207
208 }
209
210 #include "ktranslucentfloor.moc"
211 #include "moc_ktranslucentfloor.cpp"