Debian Codegraph @

kysdk-qtwidgets/src/ktitlewindow.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 "ktitlewindow.h"
24 #include "themeController.h"
25 #include "ukuistylehelper/ukui-decoration-manager.h"
26 #include "ukuistylehelper/ukui-shell-manager.h"
27 #include <QBoxLayout>
28 #include <QDebug>
29 #include <QEvent>
30 #include <QGuiApplication>
31 #include <QPainter>
32 #include <QPainterPath>
33 #include <QPushButton>
34 #include "xatom-helper.h"
35 namespace kdk
36 {
37
38 class TitleButton : public QPushButton
39 {
40 Q_OBJECT
41 public:
42 TitleButton(QWidget *widget = nullptr);
43
44 private:
45 QColor m_color;
46
47 protected:
48 void paintEvent(QPaintEvent *event);
49 };
50
51 class Q_DECL_HIDDEN KTitleWindowPrivate : public QObject, public ThemeController
52 {
53 Q_OBJECT
54 Q_DECLARE_PUBLIC(KTitleWindow)
55
56 public:
57 KTitleWindowPrivate(KTitleWindow *parent);
58
59 protected:
60 void changeTheme();
61
62 private:
63 KTitleWindow *q_ptr;
64 QWidget *m_pTitleWidget;
65 QWidget *m_pBaseWidget;
66 KPushButton *m_pAddBtn;
67 TitleButton *m_pColorBtn;
68 TitleButton *m_pMenuBtn;
69 QPushButton *m_pCloseBtn;
70 QHBoxLayout *m_pTitleWidgetLayou;
71 QVBoxLayout *m_pVLayout;
72 QColor m_titleColor;
73 };
74
75 KTitleWindow::KTitleWindow(QWidget *parent)
76 : QWidget(parent)
77 , d_ptr(new KTitleWindowPrivate(this))
78 {
79 Q_D(KTitleWindow);
80 installEventFilter(this);
81 MotifWmHints hints;
82 hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
83 hints.functions = MWM_FUNC_ALL;
84 hints.decorations = MWM_DECOR_BORDER;
85 XAtomHelper::getInstance()->setWindowMotifHint(this->winId(), hints);
86
87 setMinimumSize(390, 364);
88 }
89
90 KPushButton *KTitleWindow::addButton()
91 {
92 Q_D(KTitleWindow);
93 return d->m_pAddBtn;
94 }
95
96 QPushButton *KTitleWindow::colorButton()
97 {
98 Q_D(KTitleWindow);
99 return static_cast<QPushButton *>(d->m_pColorBtn);
100 }
101
102 QPushButton *KTitleWindow::menuButton()
103 {
104 Q_D(KTitleWindow);
105 return static_cast<QPushButton *>(d->m_pMenuBtn);
106 }
107
108 QWidget *KTitleWindow::baseWidget()
109 {
110 Q_D(KTitleWindow);
111 return d->m_pBaseWidget;
112 }
113
114 void KTitleWindow::setTitleColor(QColor color)
115 {
116 Q_D(KTitleWindow);
117 d->m_titleColor = color;
118 }
119
120 void KTitleWindow::resizeEvent(QResizeEvent *event)
121 {
122 Q_D(KTitleWindow);
123
124 QWidget::resizeEvent(event);
125 d->m_pTitleWidget->setFixedWidth(width());
126 repaint();
127 }
128
129 void KTitleWindow::paintEvent(QPaintEvent *event)
130 {
131 Q_D(KTitleWindow);
132
133 QPainter painter(this);
134 painter.setRenderHint(QPainter::Antialiasing);
135 painter.setRenderHint(QPainter::TextAntialiasing);
136 painter.setRenderHint(QPainter::SmoothPixmapTransform);
137 painter.setPen(Qt::NoPen);
138 painter.setBrush(ThemeController::getCustomColorFromDT("base-active"));
139 int radius = ThemeController::getRadiusFromDT("kradius-window");
140 if (radius == -1)
141 radius = 12;
142 painter.drawRoundedRect(this->rect(), radius, radius);
143
144 d->m_pAddBtn->setIconColor(d->m_titleColor);
145 d->m_pColorBtn->setIcon(ThemeController::drawColoredPixmap(d->m_pColorBtn->icon().pixmap(24, 24), Qt::white));
146 d->m_pMenuBtn->setIcon(ThemeController::drawColoredPixmap(d->m_pMenuBtn->icon().pixmap(24, 24), Qt::white));
147 d->m_pCloseBtn->setIcon(ThemeController::drawColoredPixmap(d->m_pCloseBtn->icon().pixmap(24, 24), Qt::white));
148
149 QPainterPath path1;
150 QRect rect = this->rect().adjusted(0, 0, 1, 1);
151 if (isActiveWindow()) {
152 if (!d->m_pTitleWidget->isVisible()) {
153 d->m_pTitleWidget->show();
154 d->m_pVLayout->setContentsMargins(8, 40, 8, 0);
155 }
156 path1.lineTo(rect.topLeft() + QPoint(12, 0));
157 path1.lineTo(rect.topRight() - QPoint(12, 0));
158 path1.quadTo(rect.topRight(), rect.topRight() + QPoint(0, 12));
159 path1.lineTo(rect.topRight() + QPoint(0, 40));
160 path1.lineTo(rect.topLeft() + QPoint(0, 40));
161 path1.lineTo(rect.topLeft() + QPoint(0, 12));
162 path1.quadTo(rect.topLeft(), rect.topLeft() + QPoint(12, 0));
163 } else {
164 if (d->m_pTitleWidget->isVisible()) {
165 d->m_pTitleWidget->hide();
166 d->m_pVLayout->setContentsMargins(8, 12, 8, 0);
167 }
168 path1.lineTo(rect.topLeft() + QPoint(12, 0));
169 path1.lineTo(rect.topRight() - QPoint(12, 0));
170 path1.quadTo(rect.topRight(), rect.topRight() + QPoint(0, 12));
171 path1.lineTo(rect.topRight() + QPoint(0, 12));
172 path1.lineTo(rect.topLeft() + QPoint(0, 12));
173 path1.lineTo(rect.topLeft() + QPoint(0, 12));
174 path1.quadTo(rect.topLeft(), rect.topLeft() + QPoint(12, 0));
175 }
176 painter.setBrush(d->m_titleColor);
177 painter.drawPath(path1);
178 }
179
180 bool KTitleWindow::eventFilter(QObject *watched, QEvent *event)
181 {
182 Q_D(KTitleWindow);
183 QString platform = QGuiApplication::platformName();
184 if (platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
185 if ((event->type() == QEvent::PlatformSurface || event->type() == QEvent::Show || event->type() == QEvent::UpdateRequest)) {
186 #ifdef USE_UKUI_PROTOCOL
187 if (UkuiShellManager::getInstance()->ukuishellReady())
188 UkuiShellManager::getInstance()->removeHeaderBar(this->windowHandle());
189 else
190 UKUIDecorationManager::getInstance()->removeHeaderBar(this->windowHandle());
191 #else
192 UKUIDecorationManager::getInstance()->removeHeaderBar(this->windowHandle());
193 #endif
194 }
195 }
196 return QWidget::eventFilter(watched, event);
197 }
198
199 KTitleWindowPrivate::KTitleWindowPrivate(KTitleWindow *parent)
200 : q_ptr(parent)
201 {
202 Q_Q(KTitleWindow);
203 m_titleColor = ThemeController::getCustomColorFromDT("highlight-active");
204 m_pTitleWidget = new QWidget(q);
205 m_pTitleWidget->setAttribute(Qt::WA_TranslucentBackground, true);
206 m_pTitleWidget->setContentsMargins(0, 0, 0, 0);
207 m_pTitleWidget->setFixedHeight(40);
208 m_pTitleWidgetLayou = new QHBoxLayout(m_pTitleWidget);
209
210 m_pAddBtn = new KPushButton();
211 m_pAddBtn->setProperty("useIconHighlightEffect", 0x2);
212 m_pAddBtn->setBackgroundColor(Qt::white);
213 m_pAddBtn->setFixedSize(24, 24);
214 m_pAddBtn->setBorderRadius(12);
215 m_pAddBtn->setIcon(QIcon::fromTheme("list-add-symbolic"));
216
217 m_pColorBtn = new TitleButton();
218 // m_pColorBtn->setProperty("useIconHighlightEffect", 0x2);
219 m_pColorBtn->setFlat(true);
220 m_pColorBtn->setFixedSize(24, 24);
221 m_pColorBtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
222
223 m_pMenuBtn = new TitleButton();
224 // m_pMenuBtn->setProperty("useIconHighlightEffect", 0x2);
225 m_pMenuBtn->setFlat(true);
226 m_pMenuBtn->setFixedSize(24, 24);
227 m_pMenuBtn->setIcon(QIcon::fromTheme("open-menu-symbolic"));
228
229 m_pCloseBtn = new QPushButton();
230 m_pCloseBtn->setProperty("isWindowButton", 0x02);
231 m_pCloseBtn->setProperty("useIconHighlightEffect", 0x08);
232 m_pCloseBtn->setFlat(true);
233 m_pCloseBtn->setFixedSize(24, 24);
234 m_pCloseBtn->setIcon(QIcon::fromTheme("window-close-symbolic"));
235 connect(m_pCloseBtn, &QPushButton::clicked, q, [=] {
236 q->close();
237 });
238
239 m_pBaseWidget = new QWidget();
240
241 m_pVLayout = new QVBoxLayout(q);
242
243 m_pTitleWidgetLayou->setSpacing(8);
244 m_pTitleWidgetLayou->setContentsMargins(8, 8, 8, 8);
245 m_pTitleWidgetLayou->addWidget(m_pAddBtn);
246 m_pTitleWidgetLayou->addStretch();
247 m_pTitleWidgetLayou->addWidget(m_pColorBtn);
248 m_pTitleWidgetLayou->addWidget(m_pMenuBtn);
249 m_pTitleWidgetLayou->addWidget(m_pCloseBtn);
250 m_pVLayout->addLayout(m_pTitleWidgetLayou);
251 m_pVLayout->addWidget(m_pBaseWidget);
252
253 m_pBaseWidget->setBackgroundRole(QPalette::Base);
254 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
255 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
256 {
257 changeTheme();
258 m_pBaseWidget->setBackgroundRole(QPalette::Base);
259 }
260 });
261 }
262
263 void KTitleWindowPrivate::changeTheme()
264 {
265 Q_Q(KTitleWindow);
266 initThemeStyle();
267 }
268
269 TitleButton::TitleButton(QWidget *widget)
270 {
271 }
272
273 void TitleButton::paintEvent(QPaintEvent *event)
274 {
275 QStyleOptionButton option;
276 initStyleOption(&option);
277 m_color = Qt::transparent;
278 QPainter painter(this);
279 painter.setRenderHint(QPainter::Antialiasing);
280 painter.setRenderHint(QPainter::TextAntialiasing);
281 painter.setRenderHint(QPainter::SmoothPixmapTransform);
282
283 if (option.state.testFlag(QStyle::State_MouseOver)) {
284 m_color = ThemeController::getCustomColorFromDT("brighttext-active");
285 if (option.state.testFlag(QStyle::State_Sunken)) {
286 if (ThemeController::themeMode() == LightTheme)
287 m_color.setAlphaF(0.28);
288 else
289 m_color.setAlphaF(0.15);
290 } else {
291 if (ThemeController::themeMode() == LightTheme)
292 m_color.setAlphaF(0.15);
293 else
294 m_color.setAlphaF(0.28);
295 }
296 }
297 painter.setBrush(m_color);
298 painter.setPen(Qt::NoPen);
299 painter.drawRoundedRect(this->rect(), 6, 6);
300
301 QRect rect = option.rect.adjusted(0, 0, 1, 1);
302 QRect iconRect(rect.center().x() - option.iconSize.width() / 2,
303 rect.center().y() - option.iconSize.height() / 2,
304 option.iconSize.width(),
305 option.iconSize.height());
306 painter.drawPixmap(iconRect, option.icon.pixmap(option.iconSize));
307 }
308
309 }
310
311 #include "ktitlewindow.moc"
312 #include "moc_ktitlewindow.cpp"