Debian Codegraph @

src/kernel/dguiapplicationhelper.cpp

raw
1 // SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dguiapplicationhelper.h"
6 #include "private/dguiapplicationhelper_p.h"
7 #include "dplatformhandle.h"
8 #include <DFontManager>
9 #include <DStandardPaths>
10
11 #include <QHash>
12 #include <QColor>
13 #include <QPalette>
14 #include <QWindow>
15 #include <QGuiApplication>
16 #include <QPointer>
17 #include <QPlatformSurfaceEvent>
18 #include <QDebug>
19 #include <QLocalServer>
20 #include <QLocalSocket>
21 #include <QLoggingCategory>
22 #include <QDir>
23 #include <QLockFile>
24 #include <QDirIterator>
25 #include <QDesktopServices>
26 #include <QLibraryInfo>
27 #include <QTimer>
28
29 #ifdef Q_OS_UNIX
30 #include <QDBusError>
31 #include <QDBusReply>
32 #include <QDBusInterface>
33 #include <QDBusPendingCall>
34 #include <QDBusConnection>
35 #include <QDBusConnectionInterface>
36 #include <QProcess>
37 #include <DConfig>
38 #endif
39 #include <QDir>
40 #include <QLockFile>
41 #include <QDirIterator>
42 #include <QDesktopServices>
43
44 #ifdef Q_OS_UNIX
45 #include <QDBusError>
46 #include <QDBusReply>
47 #include <QDBusInterface>
48 #include <QDBusPendingCall>
49 #include <QDBusConnection>
50 #include <QDBusConnectionInterface>
51 #include <QProcess>
52 #include <DPathBuf>
53 #endif
54
55 #include <private/qguiapplication_p.h>
56 #include <qpa/qplatformservices.h>
57 #include <qpa/qplatformintegration.h>
58 #include <qpa/qplatformtheme.h>
59
60 #ifdef Q_OS_LINUX
61 #include <unistd.h>
62 #endif
63
64 #ifdef Q_OS_UNIX
65 class EnvReplaceGuard
66 {
67 public:
68 EnvReplaceGuard(const int uid);
69 ~EnvReplaceGuard();
70
71 char *m_backupLogName;
72 char *m_backupHome;
73 bool initialized = false;
74 };
75
76 EnvReplaceGuard::EnvReplaceGuard(const int uid)
77 {
78 if (struct passwd *pwd = getpwuid(static_cast<__uid_t>(uid))) {
79 m_backupLogName = getenv("LOGNAME");
80 m_backupHome = getenv("HOME");
81
82 setenv("LOGNAME", pwd->pw_name, 1);
83 setenv("HOME", pwd->pw_dir, 1);
84 initialized = true;
85 }
86 }
87
88 EnvReplaceGuard::~EnvReplaceGuard()
89 {
90 if (initialized) {
91 setenv("LOGNAME", m_backupLogName, 1);
92 setenv("HOME", m_backupHome, 1);
93 }
94 }
95 #endif
96
97 DGUI_BEGIN_NAMESPACE
98
99 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
100 using HelperCreator = DGuiApplicationHelper::HelperCreator;
101 #else
102 using HelperCreator = DGuiApplicationHelper *(*)();
103 #endif
104
105 #ifdef QT_DEBUG
106 Q_LOGGING_CATEGORY(dgAppHelper, "dtk.dguihelper")
107 #else
108 Q_LOGGING_CATEGORY(dgAppHelper, "dtk.dguihelper", QtInfoMsg)
109 #endif
110
111 Q_GLOBAL_STATIC(QLocalServer, _d_singleServer)
112
113 static quint8 _d_singleServerVersion = 1;
114 Q_GLOBAL_STATIC(DFontManager, _globalFM)
115
116 #define WINDOW_THEME_KEY "_d_platform_theme"
117
118 #define APP_THEME_TYPE "themeType"
119 #define DTK_ENABLE_ANIMATIONS "enableDtkAnimations"
120 #define DTK_ANIMATIONS_ENV "D_DTK_DISABLE_ANIMATIONS"
121 Q_GLOBAL_STATIC_WITH_ARGS(DTK_CORE_NAMESPACE::DConfig, _d_dconfig, ("org.deepin.dtk.preference"));
122
123 /*!
124 @private
125 */
126 class _DGuiApplicationHelper
127 {
128 public:
129 #define INVALID_HELPER reinterpret_cast<DGuiApplicationHelper*>(1)
130 inline DGuiApplicationHelper *helper()
131 {
132 // 临时存储一个无效的指针值, 用于此处条件变量的竞争
133 if (m_helper.testAndSetRelaxed(nullptr, INVALID_HELPER)) {
134 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
135 m_helper.storeRelaxed(creator());
136 m_helper.loadRelaxed()->initialize();
137 }
138 return m_helper.loadRelaxed();
139 #else
140 m_helper.store(creator());
141 m_helper.load()->initialize();
142 }
143 return m_helper.load();
144 #endif
145 }
146
147 inline void clear()
148 {
149 if (m_helper != INVALID_HELPER)
150 delete m_helper.fetchAndStoreRelaxed(nullptr);
151 }
152
153 static DGuiApplicationHelper *defaultCreator()
154 {
155 return new DGuiApplicationHelper();
156 }
157
158 QAtomicPointer<DGuiApplicationHelper> m_helper;
159 static HelperCreator creator;
160 };
161
162 class LoadManualServiceWorker : public QThread
163 {
164 public:
165 explicit LoadManualServiceWorker(QObject *parent = nullptr);
166 ~LoadManualServiceWorker() override;
167 void checkManualServiceWakeUp();
168
169 protected:
170 void run() override;
171 };
172
173 LoadManualServiceWorker::LoadManualServiceWorker(QObject *parent)
174 : QThread(parent)
175 {
176 if (!parent)
177 connect(qApp, &QGuiApplication::aboutToQuit, this, std::bind(&LoadManualServiceWorker::exit, this, 0));
178 }
179
180 LoadManualServiceWorker::~LoadManualServiceWorker()
181 {
182 }
183
184 void LoadManualServiceWorker::run()
185 {
186 QDBusInterface("com.deepin.Manual.Search",
187 "/com/deepin/Manual/Search",
188 "com.deepin.Manual.Search");
189 }
190
191 void LoadManualServiceWorker::checkManualServiceWakeUp()
192 {
193 if (this->isRunning())
194 return;
195
196 start();
197 }
198
199 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
200 class Q_DECL_HIDDEN GuiApplicationEventFilter : public QObject
201 {
202 public:
203 explicit GuiApplicationEventFilter(DGuiApplicationHelperPrivate *transmitter,
204 QObject *parent = nullptr)
205 : QObject(parent)
206 , m_transmitter(transmitter)
207 {
208 }
209 virtual bool eventFilter(QObject *watched, QEvent *event) override
210 {
211 if (watched != qApp)
212 return QObject::eventFilter(watched, event);
213
214 switch(event->type()) {
215 case QEvent::ApplicationFontChange: {
216 const QFont font(qGuiApp->font());
217 m_transmitter->q_func()->fontChanged(font);
218 } break;
219 case QEvent::ApplicationPaletteChange: {
220 m_transmitter->onApplicationPaletteChanged();
221 break;
222 }
223 default:
224 break;
225 }
226 return QObject::eventFilter(watched, event);
227 }
228 private:
229 DGuiApplicationHelperPrivate *m_transmitter = nullptr;
230 };
231 #endif
232
233 HelperCreator _DGuiApplicationHelper::creator = _DGuiApplicationHelper::defaultCreator;
234 Q_GLOBAL_STATIC(_DGuiApplicationHelper, _globalHelper)
235
236 int DGuiApplicationHelperPrivate::waitTime = 3000;
237 DGuiApplicationHelper::Attributes DGuiApplicationHelperPrivate::attributes = DGuiApplicationHelper::UseInactiveColorGroup;
238 static const DGuiApplicationHelper::SizeMode InvalidSizeMode = static_cast<DGuiApplicationHelper::SizeMode>(-1);
239
240 DGuiApplicationHelperPrivate::DGuiApplicationHelperPrivate(DGuiApplicationHelper *qq)
241 : DObjectPrivate(qq)
242 , explicitSizeMode(InvalidSizeMode)
243 {
244
245 }
246
247 void DGuiApplicationHelperPrivate::init()
248 {
249 if (qGuiApp) {
250 initApplication(qGuiApp);
251 } else {
252 // 确保qAddPreRoutine只会被调用一次
253 static auto _ = [] {qAddPreRoutine(staticInitApplication); return true;}();
254 Q_UNUSED(_)
255 }
256 }
257
258 void DGuiApplicationHelperPrivate::initApplication(QGuiApplication *app)
259 {
260 D_Q(DGuiApplicationHelper);
261
262 if (!systemTheme) {
263 // 需要在QGuiApplication创建后再创建DPlatformTheme,否则DPlatformTheme无效.
264 // qGuiApp->platformFunction()会报警告,并返回nullptr.
265 systemTheme = new DPlatformTheme(0, q);
266 // 直接对应到系统级别的主题, 不再对外提供为某个单独程序设置主题的接口.
267 // 程序设置自身主题相关的东西皆可通过 setPaletteType 和 setApplicationPalette 实现.
268 appTheme = systemTheme;
269 }
270
271 // 跟随application销毁
272 qAddPostRoutine(staticCleanApplication);
273
274 // 转发程序自己变化的信号
275 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
276 app->installEventFilter(new GuiApplicationEventFilter(this, app));
277 #else
278 q->connect(app, &QGuiApplication::fontChanged, q, &DGuiApplicationHelper::fontChanged);
279 q->connect(app, &QGuiApplication::paletteChanged, q, [this] {
280 onApplicationPaletteChanged();
281 });
282 #endif
283
284 if (Q_UNLIKELY(!appTheme)) { // 此时说明appTheme可能已经被初始化为了systemtheme
285 if (QGuiApplicationPrivate::is_app_running) {
286 _q_initApplicationTheme();
287 } else {
288 // 延后初始化数据,因为在调用 clientLeader 前必须要保证QGuiApplication已经完全构造完成
289 q->metaObject()->invokeMethod(q, "_q_initApplicationTheme", Qt::QueuedConnection, Q_ARG(bool, true));
290 }
291 } else if (appTheme == systemTheme) {
292 // 此时appTheme等价于systemTheme, 可以直接信号链接
293 _q_initApplicationTheme();
294 }
295
296 systemSizeMode = static_cast<DGuiApplicationHelper::SizeMode>(systemTheme->sizeMode());
297 q->connect(systemTheme, SIGNAL(sizeModeChanged(int)), q, SLOT(_q_sizeModeChanged(int)));
298 }
299
300 void DGuiApplicationHelperPrivate::staticInitApplication()
301 {
302 if (!_globalHelper.exists())
303 return;
304
305 if (DGuiApplicationHelper *helper = _globalHelper->helper())
306 helper->d_func()->initApplication(qGuiApp);
307 }
308
309 void DGuiApplicationHelperPrivate::staticCleanApplication()
310 {
311 if (_globalHelper.exists())
312 _globalHelper->clear();
313 }
314
315 DPlatformTheme *DGuiApplicationHelperPrivate::initWindow(QWindow *window) const
316 {
317 DPlatformTheme *theme = new DPlatformTheme(window->winId(), q_func()->applicationTheme());
318 window->setProperty(WINDOW_THEME_KEY, QVariant::fromValue(theme));
319 theme->setParent(window); // 跟随窗口销毁
320
321 auto onWindowThemeChanged = [window, theme, this] {
322 // 如果程序自定义了调色板, 则没有必要再关心窗口自身平台主题的变化
323 // 需要注意的是, 这里的信号和事件可能会与 notifyAppThemeChanged 中的重复
324 // 但是不能因此而移除这里的通知, 当窗口自身所对应的平台主题发生变化时, 这里
325 // 的通知机制就有了用武之地.
326 if (Q_LIKELY(!isCustomPalette())) {
327 qGuiApp->postEvent(window, new QEvent(QEvent::ThemeChange));
328 }
329 };
330
331 window->connect(theme, &DPlatformTheme::themeNameChanged, window, onWindowThemeChanged);
332 window->connect(theme, &DPlatformTheme::activeColorChanged, window, onWindowThemeChanged);
333 window->connect(theme, &DPlatformTheme::paletteChanged, window, onWindowThemeChanged);
334
335 return theme;
336 }
337
338 void DGuiApplicationHelperPrivate::_q_initApplicationTheme(bool notifyChange)
339 {
340 if (!appTheme) {
341 // DPlatfromHandle::windowLeader依赖platformIntegration
342 Q_ASSERT(QGuiApplicationPrivate::platformIntegration());
343 appTheme = new DPlatformTheme(DPlatformHandle::windowLeader(), systemTheme);
344 }
345
346 QGuiApplication *app = qGuiApp;
347 auto onAppThemeChanged = [this] {
348 // 只有当程序未自定义调色板时才需要关心DPlatformTheme中themeName和palette的改变
349 if (!isCustomPalette())
350 notifyAppThemeChanged();
351 };
352 // 监听与程序主题相关的改变
353 QObject::connect(appTheme, &DPlatformTheme::themeNameChanged, app, onAppThemeChanged);
354 QObject::connect(appTheme, &DPlatformTheme::paletteChanged, app, onAppThemeChanged);
355 QTimer *timer = new QTimer(app);
356 timer->setInterval(100);
357 timer->setSingleShot(true);
358 QObject::connect(timer, &QTimer::timeout, timer, [this] {
359 if (!appPalette)
360 notifyAppThemeChanged();
361 });
362 QObject::connect(appTheme, &DPlatformTheme::activeColorChanged, app, [timer]{
363 timer->start();
364 });
365 QObject::connect(appTheme, &DPlatformTheme::darkActiveColorChanged, app, [timer] {
366 timer->start();
367 });
368
369 // appTheme在此之前可能由systemTheme所代替被使用,此时在创建appTheme
370 // 并初始化之后,应当发送信号通知程序主题的改变
371 if (notifyChange && appTheme->isValid()) {
372 notifyAppThemeChanged();
373 }
374 }
375
376 void DGuiApplicationHelperPrivate::notifyAppThemeChanged()
377 {
378 D_Q(DGuiApplicationHelper);
379 notifyAppThemeChangedByEvent();
380 QMetaObject::invokeMethod(q, [q] () {
381 // 通知主题类型发生变化, 此处可能存在误报的行为, 不过不应该对此做额外的约束
382 // 此信号的行为应当等价于 applicationPaletteChanged
383 Q_EMIT q->themeTypeChanged(q->themeType());
384 // 通知调色板对象的改变
385 Q_EMIT q->applicationPaletteChanged();
386 }, Qt::QueuedConnection);
387 }
388
389 void DGuiApplicationHelperPrivate::notifyAppThemeChangedByEvent()
390 {
391 QWindowSystemInterfacePrivate::ThemeChangeEvent event(nullptr);
392 // 此事件会促使QGuiApplication重新从QPlatformTheme中获取系统级别的QPalette.
393 // 而在deepin平台下, 系统级别的QPalette来源自 \a applicationPalette()
394 QGuiApplicationPrivate::processThemeChanged(&event);
395 }
396
397 void DGuiApplicationHelperPrivate::onApplicationPaletteChanged()
398 {
399 D_Q(DGuiApplicationHelper);
400 // 如果用户没有自定义颜色类型, 则应该通知程序的颜色类型发送变化
401 if (Q_LIKELY(!isCustomPalette())) {
402 Q_EMIT q->themeTypeChanged(q->toColorType(qGuiApp->palette()));
403 Q_EMIT q->applicationPaletteChanged();
404 } else {
405 qWarning() << "DGuiApplicationHelper: Don't use QGuiApplication::setPalette on DTK application.";
406 }
407 }
408
409 bool DGuiApplicationHelperPrivate::isCustomPalette() const
410 {
411 return appPalette || paletteType != DGuiApplicationHelper::UnknownType;
412 }
413
414 void DGuiApplicationHelperPrivate::setPaletteType(DGuiApplicationHelper::ColorType ct, bool emitSignal)
415 {
416 if (paletteType == ct)
417 return;
418
419 if (qGuiApp && qGuiApp->testAttribute(Qt::AA_SetPalette))
420 qWarning() << "DGuiApplicationHelper: Plase check 'QGuiApplication::setPalette',"
421 " Don't use it on DTK application.";
422
423 paletteType = ct;
424
425 if (!emitSignal) {
426 notifyAppThemeChangedByEvent();
427 return;
428 }
429
430 // 如果未固定调色板, 则paletteType的变化可能会导致调色板改变, 应当通知程序更新数据
431 if (!appPalette)
432 notifyAppThemeChanged();
433
434 D_Q(DGuiApplicationHelper);
435 Q_EMIT q->paletteTypeChanged(paletteType);
436 }
437
438 void DGuiApplicationHelperPrivate::initPaletteType() const
439 {
440 DCORE_USE_NAMESPACE
441 if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::DontSaveApplicationTheme))
442 return;
443
444 if (_d_dconfig.exists())
445 return;
446
447 auto applyThemeType = [this](bool emitSignal){
448 int ct = _d_dconfig->value(APP_THEME_TYPE, DGuiApplicationHelper::UnknownType).toInt();
449 if (ct > DGuiApplicationHelper::DarkType || ct < DGuiApplicationHelper::UnknownType)
450 ct = DGuiApplicationHelper::UnknownType;
451
452 const_cast<DGuiApplicationHelperPrivate *>(this)->setPaletteType(DGuiApplicationHelper::ColorType(ct), emitSignal);
453 };
454
455 applyThemeType(false);
456
457 QObject::connect(_d_dconfig, &DConfig::valueChanged, _d_dconfig, [applyThemeType](const QString &key){
458 if (key != APP_THEME_TYPE)
459 return;
460
461 applyThemeType(true);
462 });
463 }
464
465 void DGuiApplicationHelperPrivate::_q_sizeModeChanged(int mode)
466 {
467 D_Q(DGuiApplicationHelper);
468 qCInfo(dgAppHelper) << "Receiving that system size mode is set to [" << static_cast<DGuiApplicationHelper::SizeMode>(mode)
469 << "], and old system size mode is [" << systemSizeMode << "]";
470
471 const auto oldSizeMode = fetchSizeMode();
472 systemSizeMode = static_cast<DGuiApplicationHelper::SizeMode>(mode);
473 const auto currentSizeMode = fetchSizeMode();
474 if (oldSizeMode != currentSizeMode)
475 Q_EMIT q->sizeModeChanged(currentSizeMode);
476 }
477
478 DGuiApplicationHelper::SizeMode DGuiApplicationHelperPrivate::fetchSizeMode(bool *isSystemSizeMode) const
479 {
480 if (isSystemSizeMode)
481 *isSystemSizeMode = false;
482 // `setSizeMode` > `D_DTK_SIZEMODE` > `systemSizeMode`
483 if (explicitSizeMode != InvalidSizeMode)
484 return explicitSizeMode;
485
486 static const QString envSizeMode(qEnvironmentVariable("D_DTK_SIZEMODE"));
487 if (!envSizeMode.isEmpty()) {
488 bool ok = false;
489 const auto mode = envSizeMode.toInt(&ok);
490 if (ok)
491 return static_cast<DGuiApplicationHelper::SizeMode>(mode);
492 }
493
494 if (isSystemSizeMode)
495 *isSystemSizeMode = true;
496 return systemSizeMode;
497 }
498
499 /*!
500 \class Dtk::Gui::DGuiApplicationHelper
501 \inmodule dtkgui
502 \brief DGuiApplicationHelper 应用程序的 GUI ,如主题、调色板等.
503 */
504
505 /*!
506 \enum DGuiApplicationHelper::ColorType
507 DGuiApplicationHelper::ColorType 定义了主题类型.
508
509 \var DGuiApplicationHelper::ColorType DGuiApplicationHelper::UnknownType
510 未知主题(浅色主题或深色主题)
511
512 \var DGuiApplicationHelper::ColorType DGuiApplicationHelper::LightType
513 浅色主题
514
515 \var DGuiApplicationHelper::ColorType DGuiApplicationHelper::DarkType
516 深色主题
517 */
518
519 /*!
520 \enum DGuiApplicationHelper::Attribute
521 DGuiApplicationHelper::Attribute 定义了功能属性
522
523 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::UseInactiveColorGroup
524 如果开启,当窗口处于Inactive状态时就会使用QPalette::Inactive的颜色,否则窗口将没有任何颜色变化。
525
526 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::ColorCompositing
527 是否采用半透明样式的调色板。
528
529 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::ReadOnlyLimit
530 区分只读枚举。
531
532 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsDeepinPlatformTheme
533 获取当前是否使用deepin的platformtheme插件,platformtheme插件可以为Qt程序提供特定的控件样式,默认使用chameleon主题。
534
535 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsDXcbPlatform
536 获取当前使用的是不是dtk的xcb窗口插件,dxcb插件提供了窗口圆角和阴影功能。
537
538 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsXWindowPlatform
539 获取当前是否运行在X11环境中。
540
541 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsTableEnvironment
542 获取当前是否运行在deepin平板环境中,检测XDG_CURRENT_DESKTOP环境变量是不是tablet结尾。
543
544 \var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsDeepinEnvironment
545 获取当前是否运行在deepin桌面环境中,检测XDG_CURRENT_DESKTOP环境变量是不是deepin。
546 */
547
548 DGuiApplicationHelper::DGuiApplicationHelper()
549 : QObject(nullptr)
550 , DObject(*new DGuiApplicationHelperPrivate(this))
551 {
552
553 }
554
555 void DGuiApplicationHelper::initialize()
556 {
557 D_D(DGuiApplicationHelper);
558
559 d->init();
560 }
561
562 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
563 /*!
564 \brief 创建 DGuiApplicationHelper 对象.
565
566 \param creator 函数指针
567 \note 一定要先调用此函数,再使用 DGuiApplicationHelper::instance()
568 */
569 void DGuiApplicationHelper::registerInstanceCreator(DGuiApplicationHelper::HelperCreator creator)
570 {
571 if (creator == _DGuiApplicationHelper::creator)
572 return;
573
574 _DGuiApplicationHelper::creator = creator;
575
576 if (_globalHelper.exists()) {
577 _globalHelper->clear();
578 }
579 }
580 #endif
581
582 inline static int adjustColorValue(int base, qint8 increment, int max = 255)
583 {
584 return increment > 0 ? (max - base) * increment / 100.0 + base
585 : base * (1 + increment / 100.0);
586 }
587
588 /*!
589 \brief DGuiApplicationHelper::instance返回 DGuiApplicationHelper 对象
590 \return DGuiApplicationHelper对象
591 */
592 DGuiApplicationHelper *DGuiApplicationHelper::instance()
593 {
594 return _globalHelper->helper();
595 }
596
597 DGuiApplicationHelper::~DGuiApplicationHelper()
598 {
599 _globalHelper->m_helper = nullptr;
600 }
601
602 static inline QColor adjustHSLColor(const QColor &base, qint8 hueFloat, qint8 saturationFloat,
603 qint8 lightnessFloat, qint8 alphaFloat = 0)
604 {
605 if (Q_LIKELY(hueFloat || saturationFloat || lightnessFloat || alphaFloat)) {
606 // 按HSL格式调整
607 int H, S, L, A;
608 base.getHsl(&H, &S, &L, &A);
609
610 H = H > 0 ? adjustColorValue(H, hueFloat, 359) : H;
611 S = adjustColorValue(S, saturationFloat);
612 L = adjustColorValue(L, lightnessFloat);
613
614 return QColor::fromHsl(H, S, L, A);
615 }
616
617 return base;
618 }
619
620 static inline QColor adjustRGBColor(const QColor &base, qint8 redFloat, qint8 greenFloat,
621 qint8 blueFloat, qint8 alphaFloat = 0)
622 {
623 if (Q_LIKELY(redFloat || greenFloat || blueFloat || alphaFloat)) {
624 // 按RGB格式调整
625 int R, G, B, A;
626 base.getRgb(&R, &G, &B, &A);
627
628 R = adjustColorValue(R, redFloat);
629 G = adjustColorValue(G, greenFloat);
630 B = adjustColorValue(B, blueFloat);
631 A = adjustColorValue(A, alphaFloat);
632
633 return QColor(R, G, B, A);
634 }
635
636 return base;
637 }
638
639 /*!
640 \brief 调整颜色.
641
642 \note 取值范围均为 -100 ~ 100 ,当三原色参数为-100时,颜色为黑色,参数为100时,颜色为白色.
643 以透明度( alphaFloat )为例,当参数为负数时基础色的 alphaFloat 值减少,现象偏向透明, 参数为正数alphaFloat 值增加,现象偏不透明
644 \param base 基础色
645 \param hueFloat 色调
646 \param saturationFloat 饱和度
647 \param lightnessFloat 亮度
648 \param redFloat 红色
649 \param greenFloat 绿色
650 \param blueFloat 蓝色
651 \param alphaFloat Alpha通道(透明度)
652 \return 经过调整的颜色
653 */
654 QColor DGuiApplicationHelper::adjustColor(const QColor &base,
655 qint8 hueFloat, qint8 saturationFloat, qint8 lightnessFloat,
656 qint8 redFloat, qint8 greenFloat, qint8 blueFloat, qint8 alphaFloat)
657 {
658 if (Q_UNLIKELY(!base.isValid()))
659 return base;
660
661 // First do the adjust of spec of this color, Avoid precision loss caused by color spec conversion.
662 if (Q_UNLIKELY(base.spec() == QColor::Hsl)) {
663 QColor newColor = adjustHSLColor(base, hueFloat, saturationFloat, lightnessFloat, alphaFloat);
664 return adjustRGBColor(newColor, redFloat, greenFloat, blueFloat);
665 }
666
667 // For other specs, first do RGB adjust
668 QColor newColor = adjustRGBColor(base, redFloat, greenFloat, blueFloat, alphaFloat);
669 return adjustHSLColor(newColor, hueFloat, saturationFloat, lightnessFloat);
670 }
671
672 /*!
673 \brief 调整图片整体像素颜色.
674
675 \note 取值范围均为 -100 ~ 100 ,当三原色参数为-100时,颜色为黑色,参数为100时,颜色为白色.
676 以透明度( alphaFloat )为例,当参数为负数时基础色的 alphaFloat 值减少,现象偏向透明, 参数为正数alphaFloat 值增加,现象偏不透明
677 \param base 基础色
678 \param hueFloat 色调
679 \param saturationFloat 饱和度
680 \param lightnessFloat 亮度
681 \param redFloat 红色
682 \param greenFloat 绿色
683 \param blueFloat 蓝色
684 \param alphaFloat Alpha通道(透明度)
685 \return 经过调整的图片
686 */
687 QImage DGuiApplicationHelper::adjustColor(const QImage &base, qint8 hueFloat, qint8 saturationFloat, qint8 lightnessFloat, qint8 redFloat, qint8 greenFloat, qint8 blueFloat, qint8 alphaFloat)
688 {
689 if (base.isNull())
690 return base;
691
692 if (!hueFloat && !saturationFloat && !lightnessFloat && !redFloat
693 && !greenFloat && !blueFloat && !alphaFloat)
694 return base;
695
696 QImage dest = base;
697 for (int y = 0; y < dest.height(); ++y) {
698 const QRgb *rgb = reinterpret_cast<const QRgb *>(base.scanLine(y));
699 for (int x = 0; x < dest.width(); ++x) {
700 QColor base = QColor::fromRgba(rgb[x]);
701 if (base.alpha() == 0)
702 continue;
703 QColor color = adjustColor(base, hueFloat, saturationFloat, lightnessFloat,
704 redFloat, greenFloat, blueFloat, alphaFloat);
705 dest.setPixel(x, y, color.rgba());
706 }
707 }
708 return dest;
709 }
710
711 /*!
712 \brief 将两种颜色混合,合成新的颜色.
713
714 \param substrate 底层颜色
715 \param superstratum 上层颜色
716 \return 混合颜色
717 */
718 QColor DGuiApplicationHelper::blendColor(const QColor &substrate, const QColor &superstratum)
719 {
720 QColor c2 = superstratum.toRgb();
721
722 if (c2.alpha() >= 255)
723 return c2;
724
725 QColor c1 = substrate.toRgb();
726 qreal c1_weight = 1 - c2.alphaF();
727
728 int r = c1_weight * c1.red() + c2.alphaF() * c2.red();
729 int g = c1_weight * c1.green() + c2.alphaF() * c2.green();
730 int b = c1_weight * c1.blue() + c2.alphaF() * c2.blue();
731
732 return QColor(r, g, b, c1.alpha());
733 }
734
735 static QColor light_qpalette[QPalette::NColorRoles] {
736 QColor(0, 0, 0, 0.7 * 255), //WindowText
737 QColor("#e5e5e5"), //Button
738 QColor("#e6e6e6"), //Light
739 QColor("#e5e5e5"), //Midlight
740 QColor("#e3e3e3"), //Dark
741 QColor("#e4e4e4"), //Mid
742 QColor(0, 0, 0, 0.7 * 255), //Text
743 Qt::black, //BrightText
744 QColor(0, 0, 0, 0.7 * 255), //ButtonText
745 Qt::white, //Base
746 QColor("#f8f8f8"), //Window
747 QColor(0, 0, 0, 0.05 * 255), //Shadow
748 QColor("#0081ff"), //Highlight
749 Qt::white, //HighlightedText
750 QColor("#0082fa"), //Link
751 QColor("#ad4579"), //LinkVisited
752 QColor(0, 0, 0, 0.03 * 255), //AlternateBase
753 Qt::white, //NoRole
754 QColor(255, 255, 255, 0.8 * 255), //ToolTipBase
755 QColor(0, 0, 0, 0.85 * 255) //ToolTipText
756 };
757
758 static QColor dark_qpalette[QPalette::NColorRoles] {
759 QColor(255, 255, 255, 0.7 * 255), //WindowText
760 QColor("#444444"), //Button
761 QColor("#484848"), //Light
762 QColor("#474747"), //Midlight
763 QColor("#414141"), //Dark
764 QColor("#434343"), //Mid
765 QColor(255, 255, 255, 0.7 * 255), //Text
766 Qt::white, //BrightText
767 QColor(255, 255, 255, 0.7 * 255), //ButtonText
768 QColor("#282828"), //Base
769 QColor("#252525"), //Window
770 QColor(0, 0, 0, 0.05 * 255), //Shadow
771 QColor("#024CCA"), //Highlight
772 QColor("#F1F6FF"), //HighlightedText
773 QColor("#0082fa"), //Link
774 QColor("#ad4579"), //LinkVisited
775 QColor(0, 0, 0, 0.05 * 255), //AlternateBase
776 Qt::black, //NoRole
777 QColor(45, 45, 45, 0.8 * 255), //ToolTipBase
778 QColor(255, 255, 255, 0.85 * 255) //ToolTipText
779 };
780
781 static QColor light_dpalette[DPalette::NColorTypes] {
782 QColor(), //NoType
783 QColor(0, 0, 0, 255 * 0.03), //ItemBackground
784 QColor(0, 0, 0, 0.85 * 255), //TextTitle
785 QColor(0, 0, 0, 0.6 * 255), //TextTips
786 QColor("#FF5736"), //TextWarning
787 Qt::black, //TextLively
788 QColor("#0081FF"), //LightLively
789 QColor("#0081FF"), //DarkLively
790 QColor(0, 0, 0, 0.05 * 255), //FrameBorder
791 QColor(85, 85, 85, 0.4 * 255), //PlaceholderText
792 QColor(0, 0, 0, 0.1 * 255), //FrameShadowBorder
793 QColor(0, 0, 0, 0.1 * 255) //ObviousBackground
794 };
795
796 static QColor dark_dpalette[DPalette::NColorTypes] {
797 QColor(), //NoType
798 QColor(255, 255, 255, 255 * 0.05), //ItemBackground
799 QColor(255, 255, 255, 0.85 * 255), //TextTitle
800 QColor(255, 255, 255, 0.6 * 255), //TextTips
801 QColor("#E43F2E"), //TextWarning
802 Qt::white, //TextLively
803 QColor("#0059d2"), //LightLively
804 QColor("#0059d2"), //DarkLively
805 QColor(255, 255, 255, 0.1 * 255), //FrameBorder
806 QColor(192, 198, 212, 0.4 * 255), //PlaceholderText
807 QColor(0, 0, 0, 0.8 * 255), //FrameShadowBorder
808 QColor(255, 255, 255, 0.1 * 255) //ObviousBackground
809 };
810
811 /*!
812 \brief 根据主题获取标准调色板.
813
814 \param type 主题枚举值
815 \return 调色板
816 */
817 DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType type)
818 {
819 static const DPalette *light_palette = nullptr, *dark_palette = nullptr;
820 static const DPalette *alpha_light_palette = nullptr, *alpha_dark_palette = nullptr;
821 const bool allowCompositingColor = DGuiApplicationHelper::testAttribute(ColorCompositing);
822
823 if (type == LightType) {
824 if (Q_UNLIKELY(allowCompositingColor)) {
825 if (Q_LIKELY(alpha_light_palette)) {
826 return *alpha_light_palette;
827 }
828 }
829
830 if (Q_LIKELY(light_palette)) {
831 return *light_palette;
832 }
833 } else if (type == DarkType) {
834 if (Q_UNLIKELY(allowCompositingColor)) {
835 if (Q_LIKELY(alpha_dark_palette)) {
836 return *alpha_dark_palette;
837 }
838 }
839
840 if (Q_LIKELY(dark_palette)) {
841 return *dark_palette;
842 }
843 } else {
844 return DPalette();
845 }
846
847 DPalette *pa;
848 const QColor *qcolor_list, *dcolor_list;
849
850 if (type == DarkType) {
851 pa = new DPalette();
852
853 if (allowCompositingColor)
854 alpha_dark_palette = pa;
855 else
856 dark_palette = pa;
857
858 qcolor_list = dark_qpalette;
859 dcolor_list = dark_dpalette;
860 } else {
861 pa = new DPalette();
862
863 if (allowCompositingColor)
864 alpha_light_palette = pa;
865 else
866 light_palette = pa;
867
868 qcolor_list = light_qpalette;
869 dcolor_list = light_dpalette;
870 }
871
872 for (int i = 0; i < DPalette::NColorRoles; ++i) {
873 QPalette::ColorRole role = static_cast<QPalette::ColorRole>(i);
874 QColor color = qcolor_list[i];
875
876 #if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
877 if (role == QPalette::PlaceholderText) {
878 color = dcolor_list[DPalette::PlaceholderText];
879 }
880 #endif
881 // 处理半透明色
882 if (allowCompositingColor) {
883 switch (role) {
884 case QPalette::Window:
885 color = type == LightType ? adjustColor(color, 0, 0, 0, 0, 0, 0, -20) : adjustColor(color, 0, 0, -10, 0, 0, 0, -20);
886 break;
887 case QPalette::Base:
888 color = adjustColor(color, 0, 0, 0, 0, 0, 0, -20);
889 break;
890 case QPalette::WindowText:
891 case QPalette::Text:
892 color = adjustColor(color, 0, 0, type == LightType ? -20 : +20, 0, 0, 0, -20);
893 break;
894 case QPalette::ButtonText:
895 color = type == LightType ? adjustColor(color, 0, 0, -20, 0, 0, 0, -20) : adjustColor(color, 0, 0, +20, 0, 0, 0, 0);
896 break;
897 case QPalette::Button:
898 case QPalette::Light:
899 case QPalette::Mid:
900 case QPalette::Midlight:
901 case QPalette::Dark:
902 color = adjustColor(color, 0, 0, -20, 0, 0, 0, -40);
903 break;
904 default:
905 break;
906 }
907 }
908
909 pa->setColor(DPalette::Active, role, color);
910 generatePaletteColor(*pa, role, type);
911 }
912
913 for (int i = 0; i < DPalette::NColorTypes; ++i) {
914 DPalette::ColorType role = static_cast<DPalette::ColorType>(i);
915 QColor color = dcolor_list[i];
916
917 // 处理半透明色
918 if (allowCompositingColor) {
919 switch (role) {
920 case DPalette::ItemBackground:
921 color = adjustColor(color, 0, 0, 100, 0, 0, 0, type == LightType ? -80 : -90);
922 break;
923 case DPalette::TextTitle:
924 color = adjustColor(color, 0, 0, -20, 0, 0, 0, -20);
925 break;
926 case DPalette::TextTips:
927 color = type == LightType ? adjustColor(color, 0, 0, -40, 0, 0, 0, -40) : adjustColor(color, 0, 0, +40, 0, 0, 0, -50);
928 break;
929 default:
930 break;
931 }
932 }
933
934 pa->setColor(DPalette::Active, role, color);
935 generatePaletteColor(*pa, role, type);
936 }
937
938 return *const_cast<const DPalette*>(pa);
939 }
940
941 template<typename M>
942 static void generatePaletteColor_helper(DPalette &base, M role, DGuiApplicationHelper::ColorType type)
943 {
944 if (type == DGuiApplicationHelper::UnknownType) {
945 type = DGuiApplicationHelper::toColorType(base);
946 }
947
948 QColor disable_mask_color, inactive_mask_color;
949
950 if (type == DGuiApplicationHelper::DarkType) {
951 disable_mask_color = dark_qpalette[QPalette::Window];
952 inactive_mask_color = dark_qpalette[QPalette::Window];
953 disable_mask_color.setAlphaF(0.7);
954 inactive_mask_color.setAlphaF(0.6);
955 } else {
956 disable_mask_color = light_qpalette[QPalette::Window];
957 inactive_mask_color = light_qpalette[QPalette::Window];
958 disable_mask_color.setAlphaF(0.6);
959 inactive_mask_color.setAlphaF(0.4);
960 }
961
962 const QColor &color = base.color(QPalette::Normal, role);
963 base.setColor(QPalette::Disabled, role, DGuiApplicationHelper::blendColor(color, disable_mask_color));
964
965 QPalette::ColorRole qr = static_cast<QPalette::ColorRole>(role);
966 if (qr == QPalette::Text) {
967 // disable text color olny -60% alpha
968 base.setColor(QPalette::Disabled, role, DGuiApplicationHelper::adjustColor(color, 0, 0, 0, 0, 0, 0, -60));
969 }
970
971 if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::Attribute::UseInactiveColorGroup))
972 base.setColor(QPalette::Inactive, role, DGuiApplicationHelper::blendColor(color, inactive_mask_color));
973 else
974 base.setColor(QPalette::Inactive, role, color);
975 }
976
977 /*!
978 \brief 获取调色板颜色.
979
980 \param base 调色板
981 \param role 色码
982 \param type 主题枚举值
983 \sa QPalette::ColorRole
984 */
985 void DGuiApplicationHelper::generatePaletteColor(DPalette &base, QPalette::ColorRole role, DGuiApplicationHelper::ColorType type)
986 {
987 if (role == QPalette::Window) {
988 const QBrush &window = base.brush(QPalette::Normal, role);
989 base.setBrush(QPalette::Disabled, role, window);
990 base.setBrush(QPalette::Inactive, role, window);
991 return;
992 }
993 generatePaletteColor_helper(base, role, type);
994 }
995
996 /*!
997 \brief 加工调色板的颜色.
998 \overload
999
1000 一般我们只会为调色板的 QPalette::Normal 组设置颜色值, 但是
1001 控件中也需要使用其他组的颜色, 此函数使用一些固定规则为 \a base 填充 QPalette::Disabled
1002 和 QPalette::Inactive 分组的颜色. 不同的颜色类型会使用不同的加工规则, 如果为 LightType
1003 类型, 则将颜色的alpha通道调整为 0.6 后作为 QPalette::Disabled 类的颜色使用, 调整为 0.4 后
1004 作为 QPalette::Inactive 类的颜色使用. 如果为 DarkType 类型, 则将颜色的alpha通道调整为
1005 0.7 后作为 QPalette::Disabled 类的颜色使用, 调整为 0.6 后作为 QPalette::Inactive
1006 类的颜色使用.
1007
1008 \param base 被加工的调色板
1009 \param role 加工的项
1010 \param type 加工时所使用的颜色类型, 如果值为 UnknownType 将使用 toColorType 获取颜色类型
1011 */
1012 void DGuiApplicationHelper::generatePaletteColor(DPalette &base, DPalette::ColorType role, DGuiApplicationHelper::ColorType type)
1013 {
1014 generatePaletteColor_helper(base, role, type);
1015 }
1016
1017 /*!
1018 \brief 加工调色板的颜色.
1019 \overload
1020
1021 同 generatePaletteColor, 将直接调用 generatePaletteColor 加工
1022 所有类型的调色板颜色.
1023
1024 \param base 被加工的调色板
1025 \param type 加工时所使用的颜色类型, 如果值为 UnknownType 将使用 toColorType 获取颜色类型
1026 */
1027 void DGuiApplicationHelper::generatePalette(DPalette &base, ColorType type)
1028 {
1029 // 先判断调色板的色调
1030 if (type == UnknownType) {
1031 type = toColorType(base);
1032 }
1033
1034 for (int i = 0; i < QPalette::NColorRoles; ++i) {
1035 QPalette::ColorRole role = static_cast<QPalette::ColorRole>(i);
1036 generatePaletteColor(base, role, type);
1037 }
1038
1039 for (int i = 0; i < QPalette::NColorRoles; ++i) {
1040 DPalette::ColorType role = static_cast<DPalette::ColorType>(i);
1041 generatePaletteColor(base, role, type);
1042 }
1043 }
1044
1045 static inline QColor getActiveColor(const DPlatformTheme *theme, DGuiApplicationHelper::ColorType type)
1046 {
1047 QColor activeColor;
1048 if (type == DGuiApplicationHelper::DarkType) {
1049 activeColor = theme->darkActiveColor();
1050 if (!activeColor.isValid()) {
1051 activeColor = theme->activeColor();
1052 }
1053 } else {
1054 activeColor = theme->activeColor();
1055 }
1056
1057 return activeColor;
1058 }
1059 /*!
1060 \brief 获取调色板数据.
1061
1062 首先根据 DPlatformTheme::themeName 获取主题的颜色类型, 如果名称以
1063 "dark" 结尾则认为其颜色类型为 DarkType, 否则为 LightType.
1064 如果主题名称为空, 将使用其父主题的名称( DPlatformTheme::fallbackProperty ).
1065 根据颜色类型将使用 standardPalette 获取基础调色板数据, 在此基础上
1066 从 DPlatformTheme::fetchPalette 获取最终的调色板.
1067
1068 \param theme 平台主题对象
1069 \return 调色板数据
1070 */
1071 DPalette DGuiApplicationHelper::fetchPalette(const DPlatformTheme *theme)
1072 {
1073 DPalette base_palette;
1074 const QByteArray theme_name = theme->themeName();
1075 ColorType type = LightType;
1076
1077 // 深色主题
1078 if (theme_name.endsWith("dark")) {
1079 type = DarkType;
1080 }
1081
1082 bool ok = false;
1083 base_palette = theme->fetchPalette(standardPalette(type), &ok);
1084 const QColor &active_color = getActiveColor(theme, type);
1085
1086 if (active_color.isValid()) {
1087 base_palette.setColor(QPalette::Normal, QPalette::Highlight, active_color);
1088
1089 // ok为true时会整体处理palette的所有颜色
1090 if (!ok) {
1091 generatePaletteColor(base_palette, QPalette::Highlight, type);
1092 }
1093 }
1094
1095 if (ok) {
1096 generatePalette(base_palette, type);
1097 }
1098
1099 return base_palette;
1100 }
1101
1102 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
1103 /*!
1104 \brief 设置是否将调色板的颜色改为半透明模式.
1105
1106 一般用在主窗口背景为透明、模糊的程序中
1107 \param on 是否开启
1108 */
1109 void DGuiApplicationHelper::setUseInactiveColorGroup(bool on)
1110 {
1111 DGuiApplicationHelper::setAttribute(Attribute::UseInactiveColorGroup, on);
1112 }
1113
1114 /*!
1115 \brief 设置是否开启混合颜色.
1116
1117 \param on 是否开启
1118 */
1119 void DGuiApplicationHelper::setColorCompositingEnabled(bool on)
1120 {
1121 DGuiApplicationHelper::setAttribute(Attribute::ColorCompositing, on);
1122 }
1123 #endif
1124
1125 bool DGuiApplicationHelper::isXWindowPlatform()
1126 {
1127 return DGuiApplicationHelper::testAttribute(Attribute::IsXWindowPlatform);
1128 }
1129
1130 /*!
1131 \brief 用于判断当前桌面环境是否是平板电脑环境.
1132
1133 \return true 是平板电脑环境 false不是平板电脑环境
1134 */
1135 bool DGuiApplicationHelper::isTabletEnvironment()
1136 {
1137 return DGuiApplicationHelper::testAttribute(Attribute::IsTableEnvironment);
1138 }
1139
1140 /*!
1141 \brief isAnimationEnvironment 用于判断当前桌面环境是否是开启了动画等特效的环境
1142 \return true开启了 false没有开启
1143 */
1144 bool DGuiApplicationHelper::isSpecialEffectsEnvironment()
1145 {
1146 return DGuiApplicationHelper::testAttribute(Attribute::IsSpecialEffectsEnvironment);
1147 }
1148
1149 /*!
1150 \brief DGuiApplicationHelper::systemTheme.
1151
1152 返回系统级别的主题, 优先级低于 applicationTheme
1153 \return 平台主题对象
1154 \sa applicationTheme
1155 \sa windowTheme
1156 */
1157 DPlatformTheme *DGuiApplicationHelper::systemTheme() const
1158 {
1159 D_DC(DGuiApplicationHelper);
1160
1161 return d->systemTheme;
1162 }
1163
1164 /*!
1165 \brief DGuiApplicationHelper::applicationTheme.
1166
1167 同 systemTheme
1168 \return 平台主题对象
1169 \sa systemTheme
1170 \sa windowTheme
1171 */
1172 DPlatformTheme *DGuiApplicationHelper::applicationTheme() const
1173 {
1174 D_DC(DGuiApplicationHelper);
1175
1176 // 如果appTheme还未初始化,应当先初始化appTheme
1177 if (Q_UNLIKELY(!d->appTheme)) {
1178 // 初始程序级别的主题对象
1179 const_cast<DGuiApplicationHelperPrivate*>(d)->_q_initApplicationTheme(false);
1180 }
1181
1182 return d->appTheme;
1183 }
1184
1185 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
1186 /*!
1187 \brief 返回窗口级别的主题, 优先级高于 windowTheme 和 systemTheme.
1188
1189 \param window 主题对象对应的窗口
1190 \return 平台主题对象
1191 \sa applicationTheme()
1192 \warning 已废弃, 不再对外暴露为特定窗口设置主题的接口
1193 */
1194 DPlatformTheme *DGuiApplicationHelper::windowTheme(QWindow *window) const
1195 {
1196 DPlatformTheme *theme = qvariant_cast<DPlatformTheme*>(window->property(WINDOW_THEME_KEY));
1197
1198 if (theme) {
1199 return theme;
1200 }
1201
1202 D_DC(DGuiApplicationHelper);
1203 theme = d->initWindow(window);
1204
1205 return theme;
1206 }
1207 #endif
1208
1209 /*!
1210 \brief 返回应用程序调色板.
1211
1212 如果使用 setApplicationPalette 设置过一个有效的调色板, 将直接返回保存的调色板. 否则
1213 先计算调色板的ColorType, 再使用这个颜色类型通过 standardPalette 获取标准调色板. 计算
1214 ColorType的数据来源按优先级从高到低排列有以下几种方式:
1215 1. 如果使用 setThemeType 设置过一个有效的颜色类型, 将直接使用 themeType 的值.
1216 2. 如果为QGuiApplication设置过调色板(表现为 QGuiApplication::testAttribute(Qt::AA_SetPalette)
1217 为true), 则将使用 QGuiApplication::palette 通过 toColorType 获取颜色类型.
1218 3. 将根据 applicationTheme 的 DPlatformTheme::themeName 计算颜色类型.
1219 如果ColorType来源自第2种方式, 则会直接使用 QGuiApplication::palette 覆盖标准调色板中的
1220 QPalette 部分, 且程序不会再跟随系统的活动色自动更新调色板.
1221 \warning 不应该在DTK程序中使用QGuiApplication/QApplication::setPalette
1222 \return 应用程序调色板
1223 */
1224 DPalette DGuiApplicationHelper::applicationPalette(ColorType paletteType) const
1225 {
1226 D_DC(DGuiApplicationHelper);
1227
1228 if (d->appPalette) {
1229 return *d->appPalette;
1230 }
1231
1232 ColorType type = paletteType;
1233 bool aa_setPalette = qGuiApp && qGuiApp->testAttribute(Qt::AA_SetPalette);
1234 // 此时appTheme可能还未初始化, 因此先使用systemTheme, 待appTheme初始化之后会
1235 // 通知程序调色板发生改变
1236 auto theme = Q_LIKELY(d->appTheme) ? d->appTheme : d->systemTheme;
1237
1238 if (type == UnknownType) {
1239 if (aa_setPalette) {
1240 type = toColorType(qGuiApp->palette());
1241 } else {
1242 // 如果程序未自定义调色板, 则直接从平台主题中获取调色板数据
1243 return fetchPalette(theme);
1244 }
1245 }
1246
1247 // 如果程序自定义了palette的类型,将忽略 appTheme 中设置的调色板数据.
1248 DPalette pa = standardPalette(type);
1249
1250 if (aa_setPalette) {
1251 // 如果程序通过QGuiApplication::setPalette自定义了调色板, 则应当尊重程序的选择
1252 // 覆盖DPalette中的的QPalette数据
1253 pa.QPalette::operator =(qGuiApp->palette());
1254 } else {
1255 const QColor &active_color = getActiveColor(theme, type);
1256
1257 if (active_color.isValid()) {
1258 // 应用Active Color
1259 pa.setColor(QPalette::Normal, QPalette::Highlight, active_color);
1260 generatePaletteColor(pa, QPalette::Highlight, type);
1261 }
1262 }
1263
1264 return pa;
1265 }
1266
1267 DPalette DGuiApplicationHelper::applicationPalette() const
1268 {
1269 D_DC(DGuiApplicationHelper);
1270
1271 return applicationPalette(d->paletteType);
1272 }
1273
1274 /*!
1275 \brief DGuiApplicationHelper::setApplicationPalette.
1276
1277 自定义应用程序调色板, 如果没有为 QGuiApplication 设置过 QPalette, 则
1278 将触发 QGuiApplication::palette 的更新. 如果仅需要控制程序使用亮色还是暗色的
1279 调色板, 请使用 setThemeType.
1280 \note 主动设置调色板的操作会导致程序不再使用 DPlatformTheme 中调色板相关的数据, 也
1281 包括窗口级别的 windowTheme 所对应的 DPlatformTheme, 届时设置 DPlatformTheme
1282 的 themeName 和所有与 \a palette 相关的属性都不再生效.
1283 \warning 使用此方式设置的调色板将不会自动跟随活动色的变化
1284 \warning 如果使用过QGuiApplication::setPalette, 此方式可能不会生效
1285 \param palette 要设置的调色板
1286 */
1287 void DGuiApplicationHelper::setApplicationPalette(const DPalette &palette)
1288 {
1289 D_D(DGuiApplicationHelper);
1290
1291 if (qGuiApp && qGuiApp->testAttribute(Qt::AA_SetPalette)) {
1292 qWarning() << "DGuiApplicationHelper: Plase check 'QGuiApplication::setPalette', Don't use it on DTK application.";
1293 }
1294
1295 auto resolve = [](const DPalette &palette) ->bool {
1296 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1297 return palette.resolveMask();
1298 #else
1299 return palette.resolve();
1300 #endif
1301 };
1302
1303 if (d->appPalette) {
1304 if (resolve(palette)) {
1305 *d->appPalette = palette;
1306 } else {
1307 d->appPalette.reset();
1308 }
1309 } else if (resolve(palette)) {
1310 d->appPalette.reset(new DPalette(palette));
1311 } else {
1312 return;
1313 }
1314
1315 // 通知QGuiApplication更新自身的palette和font
1316 d->notifyAppThemeChanged();
1317 }
1318
1319 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
1320 /*!
1321 \brief DGuiApplicationHelper::windowPalette.
1322
1323 返回窗口所对应的调色板数据, 同 applicationPalette, 如果程序中自定义了
1324 调色板, 则直接使用 applicationPalette. 自定义调色板的三种方式如下:
1325 1. 通过 setApplicationPalette 固定调色板
1326 2. 通过 setThemeType 固定调色板的类型
1327 3. 通过 QGuiApplication::setPalette 固定调色板, 需要注意此方法不可逆.
1328 否则将基于窗口所对应的 DPlatformTheme 获取调色板( fetchPalette).
1329 \param window
1330 \return 调色板
1331 \sa windowTheme(), fetchPalette(), standardPalette(), generatePalette(), applicationPalette()
1332 \warning 使用时要同时关注 applicationPaletteChanged() , 收到此信号后可能需要重新获取窗口的调色板
1333 \warning 已废弃, 不再对外暴露控制窗口级别调色板的接口
1334 */
1335 DPalette DGuiApplicationHelper::windowPalette(QWindow *window) const
1336 {
1337 D_DC(DGuiApplicationHelper);
1338
1339 // 如果程序自定义了调色版, 则不再关心窗口对应的平台主题上的设置
1340 if (Q_UNLIKELY(d->isCustomPalette())) {
1341 return applicationPalette();
1342 }
1343
1344 DPlatformTheme *theme = windowTheme(window);
1345
1346 if (Q_UNLIKELY(!theme)) {
1347 return applicationPalette();
1348 }
1349
1350 return fetchPalette(theme);
1351 }
1352 #endif
1353
1354 /*!
1355 \brief DGuiApplicationHelper::fontManager.
1356
1357 程序中唯一的DFontManager对象, 会根据程序的fontChanged信号
1358 更新 DFontManager::baseFontPixelSize
1359 \warning 请不要尝试更改它的 baseFontPixelSize 属性
1360 */
1361 const DFontManager *DGuiApplicationHelper::fontManager() const
1362 {
1363 // 为对象初始化信号链接
1364 if (!_globalFM.exists()) {
1365 // fontManager's base font is update from qGuiApp.
1366 _globalFM->setBaseFont(qGuiApp->font());
1367 connect(this, &DGuiApplicationHelper::fontChanged, _globalFM, &DFontManager::setBaseFont);
1368 }
1369
1370 return _globalFM;
1371 }
1372
1373 /*!
1374 \brief 获取颜色的明亮度,将其转换为主题类型的枚举值.
1375
1376 转换的策略为:先将颜色转换为rgb格式,再根据 Y = 0.299R + 0.587G + 0.114B 的公式
1377 计算出颜色的亮度,亮度大于 191 时认为其为浅色,否则认为其为深色。
1378 \param color 需要转换为主题的类型的颜色
1379 \return 颜色类型的枚举值
1380 */
1381 DGuiApplicationHelper::ColorType DGuiApplicationHelper::toColorType(const QColor &color)
1382 {
1383 if (!color.isValid())
1384 return UnknownType;
1385
1386 QColor rgb_color = color.toRgb();
1387 // 获取rgb颜色的亮度(转换为YUV格式)
1388 float luminance = 0.299 * rgb_color.redF() + 0.587 * rgb_color.greenF() + 0.114 * rgb_color.blueF();
1389
1390 if (qRound(luminance * 255) > 191) {
1391 return LightType;
1392 }
1393
1394 return DarkType;
1395 }
1396
1397 /*!
1398 \brief 将调色板 \a palette 转换为主题类型的枚举.
1399 \overload
1400
1401 使用 QPalette::background 获取颜色的明亮度,将其转换为主题类型的枚举值。
1402 返回调色板的颜色类型
1403
1404 \param palette 调色板
1405 \return 颜色类型的枚举值
1406 */
1407 DGuiApplicationHelper::ColorType DGuiApplicationHelper::toColorType(const QPalette &palette)
1408 {
1409 return toColorType(palette.window().color());
1410 }
1411
1412 /*!
1413 \brief 返回程序的主题类型.
1414
1415 当themeType为UnknownType时, 将自动根据
1416 GuiApplication::palette的QPalette::background颜色计算主题
1417 类型, 否则与 paletteType 的值一致. 程序中应当使用此值作为
1418 暗色/亮色主题类型的判断.
1419
1420 \return 主题的颜色类型.
1421 \sa toColorType
1422 */
1423 DGuiApplicationHelper::ColorType DGuiApplicationHelper::themeType() const
1424 {
1425 D_DC(DGuiApplicationHelper);
1426
1427 d->initPaletteType();
1428 if (d->paletteType != UnknownType) {
1429 return d->paletteType;
1430 }
1431
1432 return toColorType(qGuiApp->palette());
1433 }
1434
1435 /*!
1436 \brief 返回当前已设置的调色板类型.
1437
1438 如果未调用过 setPaletteType, 默认为 UnknownType.
1439
1440 \return 返回当前已设置的调色板类型.
1441 \warning 与 themetype 不同,此值与程序当前的 QPalette 没有关系。
1442 \sa DGuiApplicationHelper::themeType
1443 */
1444 DGuiApplicationHelper::ColorType DGuiApplicationHelper::paletteType() const
1445 {
1446 D_DC(DGuiApplicationHelper);
1447
1448 d->initPaletteType();
1449 return d->paletteType;
1450 }
1451
1452 /*!
1453 \brief 设置DGuiApplicationHelper实例.
1454
1455 \param key 实例关键字
1456 \param singleScope 实例使用范围
1457 \return 设置是否成功
1458 \note 此处所用到DGuiApplicationHelperPrivate::waitTime默认值为3000ms,可通过
1459 \note DGuiApplicationHelper::setSingleInstanceInterval设置
1460 */
1461 bool DGuiApplicationHelper::setSingleInstance(const QString &key, DGuiApplicationHelper::SingleScope singleScope)
1462 {
1463 bool new_server = !_d_singleServer.exists();
1464
1465 if (_d_singleServer->isListening()) {
1466 _d_singleServer->close();
1467 }
1468
1469 QString socket_key = "_d_dtk_single_instance_";
1470
1471 switch (singleScope) {
1472 case GroupScope:
1473 _d_singleServer->setSocketOptions(QLocalServer::GroupAccessOption);
1474 #ifdef Q_OS_LINUX
1475 socket_key += QString("%1_").arg(getgid());
1476 #endif
1477 break;
1478 case WorldScope:
1479 _d_singleServer->setSocketOptions(QLocalServer::WorldAccessOption);
1480 break;
1481 default:
1482 _d_singleServer->setSocketOptions(QLocalServer::UserAccessOption);
1483 #ifdef Q_OS_LINUX
1484 socket_key += QString("%1_").arg(getuid());
1485 #endif
1486 break;
1487 }
1488
1489 socket_key += key;
1490 QString lockfile = socket_key;
1491 if (!lockfile.startsWith(QLatin1Char('/'))) {
1492 lockfile = QDir::cleanPath(QDir::tempPath());
1493 lockfile += QLatin1Char('/') + socket_key;
1494 }
1495 lockfile += QStringLiteral(".lock");
1496 static QScopedPointer <QLockFile> lock(new QLockFile(lockfile));
1497 // 同一个进程多次调用本接口使用最后一次设置的 key
1498 // FIX dcc 使用不同的 key 两次调用 setSingleInstance 后无法启动的问题
1499 qint64 pid = -1;
1500 QString hostname, appname;
1501 if (lock->isLocked() && lock->getLockInfo(&pid, &hostname, &appname) && pid == getpid()) {
1502 qCWarning(dgAppHelper) << "call setSingleInstance again within the same process";
1503 lock->unlock();
1504 lock.reset(new QLockFile(lockfile));
1505 }
1506
1507 if (!lock->tryLock()) {
1508 qCDebug(dgAppHelper) << "===> new client <===" << getpid();
1509 // 通知别的实例
1510 QLocalSocket socket;
1511 socket.connectToServer(socket_key);
1512
1513 // 等待到有效数据时认为server实例有效
1514 if (socket.waitForConnected(DGuiApplicationHelperPrivate::waitTime) &&
1515 socket.waitForReadyRead(DGuiApplicationHelperPrivate::waitTime)) {
1516 // 读取数据
1517 qint8 version;
1518 qint64 pid;
1519 QStringList arguments;
1520
1521 QDataStream ds(&socket);
1522 ds >> version >> pid >> arguments;
1523 qCInfo(dgAppHelper) << "Process is started: pid=" << pid << "arguments=" << arguments;
1524
1525 // 把自己的信息告诉第一个实例
1526 ds << _d_singleServerVersion << qApp->applicationPid() << qApp->arguments();
1527 socket.flush();
1528 }
1529
1530 return false;
1531 }
1532
1533 if (!_d_singleServer->listen(socket_key)) {
1534 qCWarning(dgAppHelper) << "listen failed:" << _d_singleServer->errorString();
1535 return false;
1536 } else {
1537 qCDebug(dgAppHelper) << "===> listen <===" << _d_singleServer->serverName() << getpid();
1538 }
1539
1540 if (new_server) {
1541 qCDebug(dgAppHelper) << "===> new server <===" << _d_singleServer->serverName() << getpid();
1542 QObject::connect(_d_singleServer, &QLocalServer::newConnection, qApp, [] {
1543 QLocalSocket *instance = _d_singleServer->nextPendingConnection();
1544 // 先发送数据告诉新的实例自己收到了它的请求
1545 QDataStream ds(instance);
1546 ds << _d_singleServerVersion // 协议版本
1547 << qApp->applicationPid() // 进程id
1548 << qApp->arguments(); // 启动时的参数
1549
1550 QObject::connect(instance, &QLocalSocket::readyRead, qApp, [instance] {
1551 // 读取数据
1552 QDataStream ds(instance);
1553
1554 qint8 version;
1555 qint64 pid;
1556 QStringList arguments;
1557
1558 ds >> version >> pid >> arguments;
1559 instance->close();
1560
1561 qCInfo(dgAppHelper) << "New instance: pid=" << pid << "arguments=" << arguments;
1562
1563 // 通知新进程的信息
1564 if (_globalHelper.exists() && _globalHelper->helper())
1565 Q_EMIT _globalHelper->helper()->newProcessInstance(pid, arguments);
1566 });
1567
1568 instance->flush(); //发送数据给新的实例
1569 });
1570 }
1571
1572 return true;
1573 }
1574
1575 /*!
1576 \brief 设置从QLocalServer获取消息的等待时间.
1577
1578 用于在重新创建DGuiApplicationHelper单例时,检测DGuiApplicationHelper单例是否存在且有响应
1579 \param interval 等待时间,如 \a interval 为 -1 则没有超时一直等待,默认和 QLocalSocket 一致 3000ms
1580 \note 需要在 DGuiApplicationHelper::setSingleInstance 之前调用否则无效。
1581 */
1582 void DGuiApplicationHelper::setSingleInstanceInterval(int interval)
1583 {
1584 Q_ASSERT_X(!_d_singleServer->isListening(), "DGuiApplicationHelper::setSingleInstanceInterval","Must call before setSingleInstance");
1585 DGuiApplicationHelperPrivate::waitTime = interval;
1586 }
1587
1588 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
1589 /*!
1590 \brief 设置从QLocalServer获取消息的等待时间.
1591
1592 \param interval 等待时间,typo 请使用 DGuiApplicationHelper::setSingleInstanceInterval
1593 */
1594 void DGuiApplicationHelper::setSingelInstanceInterval(int interval)
1595 {
1596 DGuiApplicationHelperPrivate::waitTime = interval;
1597 }
1598 #endif
1599
1600 /*!
1601 \brief 获取帮助手册目录
1602
1603 \return 帮助手册目录
1604 */
1605 QStringList DGuiApplicationHelper::userManualPaths(const QString &appName)
1606 {
1607 Q_ASSERT(!appName.isEmpty());
1608
1609 // 获取主目录
1610 DCORE_USE_NAMESPACE
1611 QStringList manualPaths;
1612 const auto &pathlist = DStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
1613
1614 // 获取所有已存在的帮助手册目录
1615 for (int i = 0; i < pathlist.size(); ++i) {
1616 QString strAssetPath = QStringList{pathlist[i], "deepin-manual/manual-assets"}.join(QDir::separator());
1617 for (const QString &type : QDir(strAssetPath).entryList(QDir::NoDotAndDotDot | QDir::Dirs)) {
1618 QString appDirPath = QStringList{strAssetPath, type, appName}.join(QDir::separator());
1619 if (QDir(appDirPath).exists())
1620 manualPaths.push_back(appDirPath);
1621 }
1622 }
1623
1624 return manualPaths;
1625 }
1626
1627 /*!
1628 * \brief Determine whether it's a user manual for this application.
1629 * \return
1630 */
1631 bool DGuiApplicationHelper::hasUserManual() const
1632 {
1633 return userManualPaths(qApp->applicationName()).size() > 0;
1634 }
1635
1636 bool DGuiApplicationHelper::loadTranslator(const QString &fileName, const QList<QString> &translateDirs, const QList<QLocale> &localeFallback)
1637 {
1638 DCORE_USE_NAMESPACE;
1639
1640 QList<QString> dirs = translateDirs;
1641 const QList<DPathBuf> defaultDirPrefix {
1642 qApp->applicationDirPath(),
1643 QDir::currentPath()
1644 };
1645 for (auto item : defaultDirPrefix)
1646 dirs << item.join("translations").toString();
1647
1648 QStringList missingQmfiles;
1649 for (const auto &locale : localeFallback) {
1650 QStringList translateFilenames {QString("%1_%2").arg(fileName).arg(locale.name())};
1651 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
1652 auto behavior = Qt::SkipEmptyParts;
1653 #else
1654 auto behavior = QString::SkipEmptyParts;
1655 #endif
1656 const QStringList parseLocalNameList = locale.name().split("_", behavior);
1657 if (parseLocalNameList.length() > 0)
1658 translateFilenames << QString("%1_%2").arg(fileName).arg(parseLocalNameList.at(0));
1659
1660 for (const auto &translateFilename : translateFilenames) {
1661 for (const auto &dir : dirs) {
1662 DPathBuf path(dir);
1663 QString translatePath = (path / translateFilename).toString();
1664 if (QFile::exists(translatePath + ".qm")) {
1665 qCDebug(dgAppHelper) << "load translate" << translatePath;
1666 auto translator = new QTranslator(qApp);
1667 translator->load(translatePath);
1668 qApp->installTranslator(translator);
1669 qApp->setProperty("dapp_locale", locale.name());
1670 return true;
1671 }
1672 }
1673
1674 // fix english does not need to translation.
1675 if (locale.language() != QLocale::English)
1676 missingQmfiles << translateFilename + ".qm";
1677 }
1678 }
1679
1680 if (missingQmfiles.size() > 0) {
1681 qWarning() << fileName << "can not find qm files" << missingQmfiles;
1682 }
1683 return false;
1684 }
1685
1686 bool DGuiApplicationHelper::loadTranslator(const QList<QLocale> &localeFallback)
1687 {
1688 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1689 auto qTranslationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
1690 #else
1691 auto qTranslationsPath = QLibraryInfo::path(QLibraryInfo::TranslationsPath);
1692 #endif
1693
1694 DCORE_USE_NAMESPACE
1695 //("/home/user/.local/share", "/usr/local/share", "/usr/share")
1696 auto dataDirs = DStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
1697 QList<QString> qtranslateDirs = { qTranslationsPath };
1698 for (const auto &path : dataDirs) {
1699 DPathBuf pathBuf(path);
1700 qtranslateDirs << (pathBuf / "qt" QT_STRINGIFY(QT_VERSION_MAJOR) / "translations").toString();
1701 }
1702
1703 loadTranslator("qt", qtranslateDirs, localeFallback);
1704 loadTranslator("qtbase", qtranslateDirs, localeFallback);
1705
1706 QList<QString> translateDirs;
1707 auto appName = qApp->applicationName();
1708 for (const auto &path : dataDirs) {
1709 DPathBuf pathBuf(path);
1710 translateDirs << (pathBuf / appName / "translations").toString();
1711 }
1712
1713 // ${translateDir}/${appName}_${localeName}.qm
1714 return loadTranslator(appName, translateDirs, localeFallback);
1715 }
1716
1717 DGuiApplicationHelper::SizeMode DGuiApplicationHelper::sizeMode() const
1718 {
1719 D_DC(DGuiApplicationHelper);
1720 return d->fetchSizeMode();
1721 }
1722
1723 void DGuiApplicationHelper::setSizeMode(const DGuiApplicationHelper::SizeMode mode)
1724 {
1725 D_D(DGuiApplicationHelper);
1726 const auto old = d->fetchSizeMode();
1727 d->explicitSizeMode = mode;
1728 const auto current = d->fetchSizeMode();
1729 if (old != current)
1730 Q_EMIT sizeModeChanged(current);
1731 }
1732
1733 void DGuiApplicationHelper::resetSizeMode()
1734 {
1735 D_D(DGuiApplicationHelper);
1736 const auto old = d->fetchSizeMode();
1737 d->explicitSizeMode = InvalidSizeMode;
1738 const auto current = d->fetchSizeMode();
1739 if (current != old)
1740 Q_EMIT sizeModeChanged(current);
1741 }
1742
1743 void DGuiApplicationHelper::setAttribute(DGuiApplicationHelper::Attribute attribute, bool enable)
1744 {
1745 if (attribute < Attribute::ReadOnlyLimit) {
1746 DGuiApplicationHelperPrivate::attributes.setFlag(attribute, enable);
1747 } else {
1748 qWarning() << "You are setting for the read-only option.";
1749 return;
1750 }
1751 }
1752
1753 bool DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::Attribute attribute)
1754 {
1755 switch (attribute) {
1756 case IsXWindowPlatform:
1757 return qGuiApp->platformName() == QByteArrayLiteral("xcb")
1758 || qGuiApp->platformName() == QByteArrayLiteral("dxcb");
1759 case IsDXcbPlatform:
1760 return DPlatformHandle::isDXcbPlatform();
1761 case IsTableEnvironment:
1762 return QGuiApplicationPrivate::instance()->platformIntegration()->services()->desktopEnvironment().toLower().endsWith("tablet");
1763 case IsDeepinPlatformTheme:
1764 if (!QGuiApplicationPrivate::platform_name) {
1765 return false;
1766 }
1767 return QString(typeid(*QGuiApplicationPrivate::platform_theme).name()).contains("QDeepinTheme");
1768 case IsDeepinEnvironment: {
1769 const auto &de = QGuiApplicationPrivate::instance()->platformIntegration()->services()->desktopEnvironment();
1770 return de.toLower().contains("deepin") || de == "DDE";
1771 }
1772 case IsSpecialEffectsEnvironment: {
1773 return qgetenv("DTK_DISABLED_SPECIAL_EFFECTS").toInt() != 1;
1774 }
1775 case IsWaylandPlatform: {
1776 static bool isTreeland = qApp->platformName() == QByteArrayLiteral("wayland");
1777 return isTreeland;
1778 }
1779 case HasAnimations: {
1780 static bool isDisable = qEnvironmentVariableIsSet(DTK_ANIMATIONS_ENV);
1781 if (isDisable)
1782 return false;
1783
1784 static bool shouldEnable = _d_dconfig->value(DTK_ENABLE_ANIMATIONS, false).toBool();
1785 return shouldEnable;
1786 }
1787 default:
1788 return DGuiApplicationHelperPrivate::attributes.testFlag(attribute);
1789 }
1790 }
1791
1792 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
1793 /*!
1794 \brief DGuiApplicationHelper::setThemeType.
1795 \deprecated 同 setPaletteType, 已废弃,请不要再使用。
1796 \param themeType 主题类型.
1797 */
1798 void DGuiApplicationHelper::setThemeType(DGuiApplicationHelper::ColorType themeType)
1799 {
1800 setPaletteType(themeType);
1801 }
1802 #endif
1803
1804 /*!
1805 \brief 设置程序所应用的调色板类型.
1806
1807 将固定程序的调色板类型, 此行为可能导致 applicationPalette 变化, 前提是未使用
1808 setApplicationPalette 固定过程序的调色板, 此方法不影响程序的调色板跟随
1809 活动色改变, 可用于控制程序使用亮色还是暗色调色板.
1810 \note 主动设置调色板颜色类型的操作会导致程序不再使用 DPlatformTheme 中调色板相关的数据, 也
1811 包括窗口级别的 windowTheme 所对应的 DPlatformTheme, 届时设置 DPlatformTheme
1812 的 themeName 和所有与 palette 相关的属性都不再生效.
1813 \param paletteType 主题类型的枚举值
1814 */
1815 void DGuiApplicationHelper::setPaletteType(DGuiApplicationHelper::ColorType paletteType)
1816 {
1817 if (!qApp) {
1818 qWarning() << "Can't call `DGuiApplicationHelper::setPaletteType` before QCoreApplication constructed.";
1819 return;
1820 }
1821
1822 D_D(DGuiApplicationHelper);
1823
1824 d->initPaletteType();
1825 d->setPaletteType(paletteType, true);
1826 _d_dconfig->setValue(APP_THEME_TYPE, paletteType);
1827 }
1828
1829 /*!
1830 * \brief Open manual for this application.
1831 */
1832 void DGuiApplicationHelper::handleHelpAction()
1833 {
1834 if (!hasUserManual()) {
1835 return;
1836 }
1837 #ifdef Q_OS_LINUX
1838 QString appid = qApp->applicationName();
1839
1840 // new interface use applicationName as id
1841 QDBusInterface manual("com.deepin.Manual.Open",
1842 "/com/deepin/Manual/Open",
1843 "com.deepin.Manual.Open");
1844 QDBusPendingCall call = manual.asyncCall("ShowManual", appid);
1845 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
1846 QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [appid](QDBusPendingCallWatcher *pWatcher) {
1847 QDBusPendingReply<void> reply = *pWatcher;
1848 if (reply.isError()) {
1849 // fallback to old interface
1850 qWarning() << reply.error() << "fallback to dman appid";
1851 QProcess::startDetached("dman", QStringList() << appid);
1852 }
1853
1854 pWatcher->deleteLater();
1855 });
1856
1857 #else
1858 qWarning() << "not support dman now";
1859 #endif
1860 }
1861
1862 void DGuiApplicationHelper::openUrl(const QString &url)
1863 {
1864 #ifdef Q_OS_UNIX
1865 // workaround for pkexec apps
1866 bool ok = false;
1867 const int pkexecUid = qEnvironmentVariableIntValue("PKEXEC_UID", &ok);
1868
1869 if (ok)
1870 {
1871 EnvReplaceGuard _env_guard(pkexecUid);
1872 Q_UNUSED(_env_guard);
1873
1874 QDesktopServices::openUrl(url);
1875 }
1876 else
1877 #endif
1878 {
1879 QDesktopServices::openUrl(url);
1880 }
1881 }
1882 DGUI_END_NAMESPACE
1883
1884 #include "moc_dguiapplicationhelper.cpp"