Debian Codegraph @

kysdk-qtwidgets/src/kdialog.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 "kdialog.h"
24 #include "accessinfohelper.h"
25 #include "parmscontroller.h"
26 #include "ukuistylehelper/ukui-decoration-manager.h"
27 #include "ukuistylehelper/ukui-shell-manager.h"
28 #include <QApplication>
29 #include <QBoxLayout>
30 #include <QScreen>
31 #include "xatom-helper.h"
32
33 namespace kdk
34 {
35 class KDialogPrivate : public QObject
36 {
37 Q_OBJECT
38 Q_DECLARE_PUBLIC(KDialog)
39 public:
40 KDialogPrivate(KDialog *parent);
41
42 private:
43 KDialog *q_ptr;
44 KIconBar *m_pIconBar;
45 KWindowButtonBar *m_pWindowButtonBar;
46 QHBoxLayout *m_pTitleLayout;
47 QVBoxLayout *m_pMainLayout;
48 QWidget *m_pMainWidget;
49 QString m_IconName;
50 };
51
52 KDialog::KDialog(QWidget *parent)
53 : QDialog(parent)
54 , d_ptr(new KDialogPrivate(this))
55 {
56 Q_D(KDialog);
57 setAttribute(Qt::WA_AlwaysShowToolTips);
58 setFocusPolicy(Qt::ClickFocus);
59 setObjectName("Kdialog");
60
61 QString platform = QGuiApplication::platformName();
62 if (platform.startsWith(QLatin1String("xcb"), Qt::CaseInsensitive)) {
63 MotifWmHints hints;
64 hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
65 hints.functions = MWM_FUNC_ALL;
66 hints.decorations = MWM_DECOR_BORDER;
67 XAtomHelper::getInstance()->setWindowMotifHint(this->winId(), hints);
68 }
69
70 connect(d->m_pWindowButtonBar->minimumButton(), &QPushButton::clicked, this, &KDialog::showMinimized);
71 connect(d->m_pWindowButtonBar->maximumButton(), &QPushButton::clicked, this, [=]() {
72 if (this->isMaximized())
73 showNormal();
74 else
75 showMaximized();
76 });
77 connect(d->m_pWindowButtonBar->closeButton(), &QPushButton::clicked, this, &KDialog::close);
78 connect(d->m_pWindowButtonBar, &KWindowButtonBar::doubleClick, this, [=]() {
79 if (this->isMaximized())
80 showNormal();
81 else
82 showMaximized();
83 });
84 connect(d->m_pIconBar, &KIconBar::doubleClick, this, [=]() {
85 if (this->isMaximized())
86 showNormal();
87 else
88 showMaximized();
89 });
90
91 changeIconStyle();
92 connect(m_gsetting,&QGSettings::changed,this,[=](QString key){
93 if(key == "styleName" || key == "iconThemeName" || key == "widgetThemeName")
94 {
95 changeTheme();
96 }
97 if(key == "iconThemeName")
98 {
99 changeIconStyle();
100 }
101 });
102 changeTheme();
103 connect(Parmscontroller::self(), &Parmscontroller::modeChanged, this, [=]() {
104 updateGeometry();
105 });
106 installEventFilter(this);
107 resize(600, 480);
108 }
109
110 KDialog::~KDialog()
111 {
112 }
113
114 void KDialog::setWindowIcon(const QIcon &icon)
115 {
116 Q_D(KDialog);
117 d->m_pIconBar->setIcon(icon);
118 d->m_IconName = icon.name();
119 QWidget::setWindowIcon(icon);
120 }
121
122 void KDialog::setWindowIcon(const QString &iconName)
123 {
124 Q_D(KDialog);
125 d->m_IconName = iconName;
126 d->m_pIconBar->setIcon(iconName);
127 QWidget::setWindowIcon(QIcon::fromTheme(iconName));
128 }
129
130 void KDialog::setWindowTitle(const QString &title)
131 {
132 Q_D(KDialog);
133 QVariant value = this->property("isAboutDialog");
134 if (value.isNull() || !value.toBool()) {
135 d->m_pIconBar->setWidgetName(title);
136 }
137 QDialog::setWindowTitle(title);
138 }
139
140 QPushButton *KDialog::maximumButton()
141 {
142 Q_D(KDialog);
143 return d->m_pWindowButtonBar->maximumButton();
144 }
145
146 QPushButton *KDialog::minimumButton()
147 {
148 Q_D(KDialog);
149 return d->m_pWindowButtonBar->minimumButton();
150 }
151
152 QPushButton *KDialog::closeButton()
153 {
154 Q_D(KDialog);
155 return d->m_pWindowButtonBar->closeButton();
156 }
157
158 KMenuButton *KDialog::menuButton()
159 {
160 Q_D(KDialog);
161 return d->m_pWindowButtonBar->menuButton();
162 }
163
164 QWidget *KDialog::mainWidget()
165 {
166 Q_D(KDialog);
167 if (d->m_pMainWidget)
168 return d->m_pMainWidget;
169 }
170
171 KWindowButtonBar *KDialog::windowButtonBar()
172 {
173 Q_D(KDialog);
174 return d->m_pWindowButtonBar;
175 }
176
177 KIconBar *KDialog::iconBar()
178 {
179 Q_D(KDialog);
180 return d->m_pIconBar;
181 }
182
183 bool KDialog::eventFilter(QObject *target, QEvent *event)
184 {
185 Q_D(KDialog);
186 if (target == this && (event->type() == QEvent::WindowStateChange || event->type() == QEvent::Show)) {
187 if (event->type() == QEvent::Show) {
188 MotifWmHints hints;
189 hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
190 hints.functions = MWM_FUNC_ALL;
191 hints.decorations = MWM_DECOR_BORDER;
192 XAtomHelper::getInstance()->setWindowMotifHint(this->winId(), hints);
193
194 QWidget *parentWindow = qobject_cast<QWidget *>(this->parent());
195
196 while (parentWindow && parentWindow->parent())
197 parentWindow = qobject_cast<QWidget *>(parentWindow->parent());
198
199 if (parentWindow && parentWindow->isVisible()) {
200 const QPoint parentWindowCenter = parentWindow->geometry().center();
201 const QPoint dialogCenter = this->geometry().center();
202 const QPoint moveOffset = parentWindowCenter - dialogCenter;
203 move(pos() + moveOffset);
204 } else {
205 QScreen *screen = QGuiApplication::primaryScreen();
206 int screenWidth = screen->geometry().width();
207 int screenHeight = screen->geometry().height();
208
209 int messageBox_X = (screenWidth - this->width()) / 2;
210 int messageBox_Y = (screenHeight - this->height()) / 2;
211
212 this->move(messageBox_X, messageBox_Y);
213 }
214 }
215 if (this->isMaximized())
216 d->m_pWindowButtonBar->setMaximumButtonState(MaximumButtonState::Restore);
217 else
218 d->m_pWindowButtonBar->setMaximumButtonState(MaximumButtonState::Maximum);
219 }
220 if (target == this && (event->type() == QEvent::WindowActivate || event->type() == QEvent::WindowDeactivate)) {
221 changeTheme();
222 }
223 QString platform = QGuiApplication::platformName();
224 if (platform.startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
225 if ((event->type() == QEvent::PlatformSurface || event->type() == QEvent::Show || event->type() == QEvent::UpdateRequest || event->type() == QEvent::ChildAdded || event->type() == QEvent::ChildPolished)) {
226 #ifdef USE_UKUI_SHELL_PLUGIN
227 this->setProperty("ukui_surface_no_titlebar", true);
228 #else
229 #ifdef USE_UKUI_PROTOCOL
230 if (UkuiShellManager::getInstance()->ukuishellReady())
231 UkuiShellManager::getInstance()->removeHeaderBar(this->windowHandle());
232 else
233 UKUIDecorationManager::getInstance()->removeHeaderBar(this->windowHandle());
234 #else
235 UKUIDecorationManager::getInstance()->removeHeaderBar(this->windowHandle());
236 #endif
237 #endif
238 }
239 }
240 if (target == this && (event->type() == QEvent::Close || event->type() == QEvent::Hide)) {
241 d->m_pWindowButtonBar->closeButton()->setAttribute(Qt::WA_UnderMouse, false);
242 QHoverEvent hoverEvent(QEvent::HoverLeave, QPoint(10, 10), QPoint(0, 0));
243 QCoreApplication::sendEvent(d->m_pWindowButtonBar->closeButton(), &hoverEvent);
244 }
245 return QDialog::eventFilter(target, event);
246 }
247
248 void KDialog::changeTheme()
249 {
250 Q_D(KDialog);
251 initThemeStyle();
252 QPalette p = this->palette();
253 QColor color = ThemeController::getCustomColorFromDT("base-active");
254 p.setColor(QPalette::Window, color);
255 this->setPalette(p);
256 if (!d->m_IconName.isEmpty())
257 this->setWindowIcon(d->m_IconName);
258 }
259
260 void KDialog::changeIconStyle()
261 {
262 Q_D(KDialog);
263 initThemeStyle();
264 setWindowIcon(d->m_IconName);
265 }
266
267 QBoxLayout *KDialog::mainLayout()
268 {
269 Q_D(KDialog);
270 return d->m_pMainLayout;
271 }
272
273 KDialogPrivate::KDialogPrivate(KDialog *parent)
274 : q_ptr(parent)
275 {
276 Q_Q(KDialog);
277 m_pMainLayout = new QVBoxLayout(q);
278 m_pTitleLayout = new QHBoxLayout();
279 m_pTitleLayout->setContentsMargins(0, 0, 0, 0);
280 m_pTitleLayout->setSpacing(0);
281 m_pTitleLayout = new QHBoxLayout;
282 m_pIconBar = new KIconBar(q);
283 m_pWindowButtonBar = new KWindowButtonBar(q);
284 m_pTitleLayout->addWidget(m_pIconBar);
285 m_pTitleLayout->addWidget(m_pWindowButtonBar);
286
287 m_pMainWidget = new QWidget(q);
288 m_pMainLayout->setSpacing(0);
289 m_pMainLayout->setContentsMargins(0, 0, 0, 0);
290 m_pMainLayout->addLayout(m_pTitleLayout, Qt::AlignTop);
291 m_pMainLayout->addWidget(m_pMainWidget);
292
293 // 暂时先默认只显示关闭按钮
294 m_pWindowButtonBar->maximumButton()->hide();
295 m_pWindowButtonBar->menuButton()->hide();
296 m_pWindowButtonBar->minimumButton()->hide();
297
298 connect(m_pWindowButtonBar->minimumButton(), &QPushButton::clicked, q, &KDialog::showMinimized);
299 connect(m_pWindowButtonBar->closeButton(), &QPushButton::clicked, q, &KDialog::close);
300 setParent(parent);
301
302 KDK_ALL_INFO_FORMAT(m_pIconBar,"");
303 KDK_ALL_INFO_FORMAT(m_pWindowButtonBar,"");
304 KDK_ALL_INFO_FORMAT(m_pMainWidget,"");
305 }
306 }
307
308 #include "kdialog.moc"
309 #include "moc_kdialog.cpp"