-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdguiapplicationhelper.cpp
More file actions
1890 lines (1592 loc) · 62.7 KB
/
Copy pathdguiapplicationhelper.cpp
File metadata and controls
1890 lines (1592 loc) · 62.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
#include "dguiapplicationhelper.h"
#include "private/dguiapplicationhelper_p.h"
#include "dplatformhandle.h"
#include <DFontManager>
#include <DStandardPaths>
#include <DSGApplication>
#include <QHash>
#include <QColor>
#include <QPalette>
#include <QWindow>
#include <QGuiApplication>
#include <QPointer>
#include <QPlatformSurfaceEvent>
#include <QDebug>
#include <QLocalServer>
#include <QLocalSocket>
#include <QLoggingCategory>
#include <QDir>
#include <QLockFile>
#include <QDirIterator>
#include <QDesktopServices>
#include <QLibraryInfo>
#include <QTimer>
#ifdef Q_OS_UNIX
#include <QDBusError>
#include <QDBusReply>
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QProcess>
#endif
#include <QDir>
#include <QLockFile>
#include <QDirIterator>
#include <QDesktopServices>
#ifdef Q_OS_UNIX
#include <QDBusError>
#include <QDBusReply>
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QProcess>
#include <DPathBuf>
#endif
#include <private/qguiapplication_p.h>
#include <qpa/qplatformservices.h>
#include <qpa/qplatformintegration.h>
#include <qpa/qplatformtheme.h>
#ifdef Q_OS_LINUX
#include <pwd.h>
#include <unistd.h>
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(6, 9, 1))
#include <pwd.h>
#endif
#include "orgdeepindtkpreference.hpp"
#ifdef Q_OS_UNIX
class EnvReplaceGuard
{
public:
EnvReplaceGuard(const int uid);
~EnvReplaceGuard();
char *m_backupLogName;
char *m_backupHome;
bool initialized = false;
};
EnvReplaceGuard::EnvReplaceGuard(const int uid)
{
if (struct passwd *pwd = getpwuid(static_cast<__uid_t>(uid))) {
m_backupLogName = getenv("LOGNAME");
m_backupHome = getenv("HOME");
setenv("LOGNAME", pwd->pw_name, 1);
setenv("HOME", pwd->pw_dir, 1);
initialized = true;
}
}
EnvReplaceGuard::~EnvReplaceGuard()
{
if (initialized) {
setenv("LOGNAME", m_backupLogName, 1);
setenv("HOME", m_backupHome, 1);
}
}
#endif
DGUI_BEGIN_NAMESPACE
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
using HelperCreator = DGuiApplicationHelper::HelperCreator;
#else
using HelperCreator = DGuiApplicationHelper *(*)();
#endif
#ifdef QT_DEBUG
Q_LOGGING_CATEGORY(dgAppHelper, "dtk.dguihelper")
#else
Q_LOGGING_CATEGORY(dgAppHelper, "dtk.dguihelper", QtInfoMsg)
#endif
Q_GLOBAL_STATIC(QLocalServer, _d_singleServer)
static quint8 _d_singleServerVersion = 1;
Q_GLOBAL_STATIC(DFontManager, _globalFM)
#define WINDOW_THEME_KEY "_d_platform_theme"
#define DTK_ANIMATIONS_ENV "D_DTK_DISABLE_ANIMATIONS"
Q_GLOBAL_STATIC_WITH_ARGS(OrgDeepinDTKPreference, _d_dconfig, (DTK_CORE_NAMESPACE::DConfig::globalThread(), nullptr,
"org.deepin.dtk.preference", DTK_CORE_NAMESPACE::DSGApplication::id(), {}, false, nullptr))
/*!
@private
*/
class _DGuiApplicationHelper
{
public:
#define INVALID_HELPER reinterpret_cast<DGuiApplicationHelper*>(1)
inline DGuiApplicationHelper *helper()
{
// 临时存储一个无效的指针值, 用于此处条件变量的竞争
if (m_helper.testAndSetRelaxed(nullptr, INVALID_HELPER)) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
m_helper.storeRelaxed(creator());
m_helper.loadRelaxed()->initialize();
}
return m_helper.loadRelaxed();
#else
m_helper.store(creator());
m_helper.load()->initialize();
}
return m_helper.load();
#endif
}
inline void clear()
{
if (m_helper != INVALID_HELPER)
delete m_helper.fetchAndStoreRelaxed(nullptr);
}
static DGuiApplicationHelper *defaultCreator()
{
return new DGuiApplicationHelper();
}
QAtomicPointer<DGuiApplicationHelper> m_helper;
static HelperCreator creator;
};
class LoadManualServiceWorker : public QThread
{
public:
explicit LoadManualServiceWorker(QObject *parent = nullptr);
~LoadManualServiceWorker() override;
void checkManualServiceWakeUp();
protected:
void run() override;
};
LoadManualServiceWorker::LoadManualServiceWorker(QObject *parent)
: QThread(parent)
{
if (!parent)
connect(qApp, &QGuiApplication::aboutToQuit, this, std::bind(&LoadManualServiceWorker::exit, this, 0));
}
LoadManualServiceWorker::~LoadManualServiceWorker()
{
}
void LoadManualServiceWorker::run()
{
QDBusInterface("com.deepin.Manual.Search",
"/com/deepin/Manual/Search",
"com.deepin.Manual.Search");
}
void LoadManualServiceWorker::checkManualServiceWakeUp()
{
if (this->isRunning())
return;
start();
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
class Q_DECL_HIDDEN GuiApplicationEventFilter : public QObject
{
public:
explicit GuiApplicationEventFilter(DGuiApplicationHelperPrivate *transmitter,
QObject *parent = nullptr)
: QObject(parent)
, m_transmitter(transmitter)
{
}
virtual bool eventFilter(QObject *watched, QEvent *event) override
{
if (watched != qApp)
return QObject::eventFilter(watched, event);
switch(event->type()) {
case QEvent::ApplicationFontChange: {
const QFont font(qGuiApp->font());
m_transmitter->q_func()->fontChanged(font);
} break;
case QEvent::ApplicationPaletteChange: {
m_transmitter->onApplicationPaletteChanged();
break;
}
default:
break;
}
return QObject::eventFilter(watched, event);
}
private:
DGuiApplicationHelperPrivate *m_transmitter = nullptr;
};
#endif
HelperCreator _DGuiApplicationHelper::creator = _DGuiApplicationHelper::defaultCreator;
Q_GLOBAL_STATIC(_DGuiApplicationHelper, _globalHelper)
int DGuiApplicationHelperPrivate::waitTime = 3000;
DGuiApplicationHelper::Attributes DGuiApplicationHelperPrivate::attributes = DGuiApplicationHelper::UseInactiveColorGroup;
static const DGuiApplicationHelper::SizeMode InvalidSizeMode = static_cast<DGuiApplicationHelper::SizeMode>(-1);
DGuiApplicationHelperPrivate::DGuiApplicationHelperPrivate(DGuiApplicationHelper *qq)
: DObjectPrivate(qq)
, explicitSizeMode(InvalidSizeMode)
{
}
void DGuiApplicationHelperPrivate::init()
{
if (qGuiApp) {
initApplication(qGuiApp);
} else {
// 确保qAddPreRoutine只会被调用一次
static auto _ = [] {qAddPreRoutine(staticInitApplication); return true;}();
Q_UNUSED(_)
}
}
void DGuiApplicationHelperPrivate::initApplication(QGuiApplication *app)
{
D_Q(DGuiApplicationHelper);
if (!systemTheme) {
// 需要在QGuiApplication创建后再创建DPlatformTheme,否则DPlatformTheme无效.
// qGuiApp->platformFunction()会报警告,并返回nullptr.
systemTheme = new DPlatformTheme(0, q);
// 直接对应到系统级别的主题, 不再对外提供为某个单独程序设置主题的接口.
// 程序设置自身主题相关的东西皆可通过 setPaletteType 和 setApplicationPalette 实现.
appTheme = systemTheme;
}
// 跟随application销毁
qAddPostRoutine(staticCleanApplication);
// 转发程序自己变化的信号
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
app->installEventFilter(new GuiApplicationEventFilter(this, app));
#else
q->connect(app, &QGuiApplication::fontChanged, q, &DGuiApplicationHelper::fontChanged);
q->connect(app, &QGuiApplication::paletteChanged, q, [this] {
onApplicationPaletteChanged();
});
#endif
if (Q_UNLIKELY(!appTheme)) { // 此时说明appTheme可能已经被初始化为了systemtheme
if (QGuiApplicationPrivate::is_app_running) {
_q_initApplicationTheme();
} else {
// 延后初始化数据,因为在调用 clientLeader 前必须要保证QGuiApplication已经完全构造完成
q->metaObject()->invokeMethod(q, "_q_initApplicationTheme", Qt::QueuedConnection, Q_ARG(bool, true));
}
} else if (appTheme == systemTheme) {
// 此时appTheme等价于systemTheme, 可以直接信号链接
_q_initApplicationTheme();
}
systemSizeMode = static_cast<DGuiApplicationHelper::SizeMode>(systemTheme->sizeMode());
q->connect(systemTheme, SIGNAL(sizeModeChanged(int)), q, SLOT(_q_sizeModeChanged(int)));
}
void DGuiApplicationHelperPrivate::staticInitApplication()
{
if (!_globalHelper.exists())
return;
if (DGuiApplicationHelper *helper = _globalHelper->helper())
helper->d_func()->initApplication(qGuiApp);
}
void DGuiApplicationHelperPrivate::staticCleanApplication()
{
if (_globalHelper.exists())
_globalHelper->clear();
}
DPlatformTheme *DGuiApplicationHelperPrivate::initWindow(QWindow *window) const
{
DPlatformTheme *theme = new DPlatformTheme(window->winId(), q_func()->applicationTheme());
window->setProperty(WINDOW_THEME_KEY, QVariant::fromValue(theme));
theme->setParent(window); // 跟随窗口销毁
auto onWindowThemeChanged = [window, theme, this] {
// 如果程序自定义了调色板, 则没有必要再关心窗口自身平台主题的变化
// 需要注意的是, 这里的信号和事件可能会与 notifyAppThemeChanged 中的重复
// 但是不能因此而移除这里的通知, 当窗口自身所对应的平台主题发生变化时, 这里
// 的通知机制就有了用武之地.
if (Q_LIKELY(!isCustomPalette())) {
qGuiApp->postEvent(window, new QEvent(QEvent::ThemeChange));
}
};
window->connect(theme, &DPlatformTheme::themeNameChanged, window, onWindowThemeChanged);
window->connect(theme, &DPlatformTheme::activeColorChanged, window, onWindowThemeChanged);
window->connect(theme, &DPlatformTheme::paletteChanged, window, onWindowThemeChanged);
return theme;
}
void DGuiApplicationHelperPrivate::_q_initApplicationTheme(bool notifyChange)
{
if (!appTheme) {
// DPlatfromHandle::windowLeader依赖platformIntegration
Q_ASSERT(QGuiApplicationPrivate::platformIntegration());
appTheme = new DPlatformTheme(DPlatformHandle::windowLeader(), systemTheme);
}
QGuiApplication *app = qGuiApp;
auto onAppThemeChanged = [this] {
// 只有当程序未自定义调色板时才需要关心DPlatformTheme中themeName和palette的改变
if (!isCustomPalette())
notifyAppThemeChanged();
};
// 监听与程序主题相关的改变
QObject::connect(appTheme, &DPlatformTheme::themeNameChanged, app, onAppThemeChanged);
QObject::connect(appTheme, &DPlatformTheme::paletteChanged, app, onAppThemeChanged);
QTimer *timer = new QTimer(app);
timer->setInterval(100);
timer->setSingleShot(true);
QObject::connect(timer, &QTimer::timeout, timer, [this] {
if (!appPalette)
notifyAppThemeChanged();
});
QObject::connect(appTheme, &DPlatformTheme::activeColorChanged, app, [timer]{
timer->start();
});
QObject::connect(appTheme, &DPlatformTheme::darkActiveColorChanged, app, [timer] {
timer->start();
});
// appTheme在此之前可能由systemTheme所代替被使用,此时在创建appTheme
// 并初始化之后,应当发送信号通知程序主题的改变
if (notifyChange && appTheme->isValid()) {
notifyAppThemeChanged();
}
}
void DGuiApplicationHelperPrivate::notifyAppThemeChanged()
{
D_Q(DGuiApplicationHelper);
notifyAppThemeChangedByEvent();
QMetaObject::invokeMethod(q, [q] () {
// 通知主题类型发生变化, 此处可能存在误报的行为, 不过不应该对此做额外的约束
// 此信号的行为应当等价于 applicationPaletteChanged
Q_EMIT q->themeTypeChanged(q->themeType());
// 通知调色板对象的改变
Q_EMIT q->applicationPaletteChanged();
}, Qt::QueuedConnection);
}
void DGuiApplicationHelperPrivate::notifyAppThemeChangedByEvent()
{
QWindowSystemInterfacePrivate::ThemeChangeEvent event(nullptr);
// 此事件会促使QGuiApplication重新从QPlatformTheme中获取系统级别的QPalette.
// 而在deepin平台下, 系统级别的QPalette来源自 \a applicationPalette()
QGuiApplicationPrivate::processThemeChanged(&event);
}
void DGuiApplicationHelperPrivate::onApplicationPaletteChanged()
{
D_Q(DGuiApplicationHelper);
// 如果用户没有自定义颜色类型, 则应该通知程序的颜色类型发送变化
if (Q_LIKELY(!isCustomPalette())) {
Q_EMIT q->themeTypeChanged(q->toColorType(qGuiApp->palette()));
Q_EMIT q->applicationPaletteChanged();
} else {
qWarning() << "DGuiApplicationHelper: Don't use QGuiApplication::setPalette on DTK application.";
}
}
bool DGuiApplicationHelperPrivate::isCustomPalette() const
{
return appPalette || paletteType != DGuiApplicationHelper::UnknownType;
}
void DGuiApplicationHelperPrivate::setPaletteType(DGuiApplicationHelper::ColorType ct, bool emitSignal)
{
if (paletteType == ct)
return;
if (qGuiApp && qGuiApp->testAttribute(Qt::AA_SetPalette))
qWarning() << "DGuiApplicationHelper: Plase check 'QGuiApplication::setPalette',"
" Don't use it on DTK application.";
paletteType = ct;
if (!emitSignal) {
notifyAppThemeChangedByEvent();
return;
}
// 如果未固定调色板, 则paletteType的变化可能会导致调色板改变, 应当通知程序更新数据
if (!appPalette)
notifyAppThemeChanged();
D_Q(DGuiApplicationHelper);
Q_EMIT q->paletteTypeChanged(paletteType);
}
void DGuiApplicationHelperPrivate::initPaletteType() const
{
DCORE_USE_NAMESPACE
if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::DontSaveApplicationTheme))
return;
if (_d_dconfig.exists())
return;
auto applyThemeType = [this](bool emitSignal){
int ct = _d_dconfig->themeType();
if (ct > DGuiApplicationHelper::DarkType || ct < DGuiApplicationHelper::UnknownType)
ct = DGuiApplicationHelper::UnknownType;
const_cast<DGuiApplicationHelperPrivate *>(this)->setPaletteType(DGuiApplicationHelper::ColorType(ct), emitSignal);
};
applyThemeType(false);
QObject::connect(_d_dconfig.operator ()(), &OrgDeepinDTKPreference::themeTypeChanged, _d_dconfig, [applyThemeType] {
applyThemeType(true);
});
}
void DGuiApplicationHelperPrivate::_q_sizeModeChanged(int mode)
{
D_Q(DGuiApplicationHelper);
qCInfo(dgAppHelper) << "Receiving that system size mode is set to [" << static_cast<DGuiApplicationHelper::SizeMode>(mode)
<< "], and old system size mode is [" << systemSizeMode << "]";
const auto oldSizeMode = fetchSizeMode();
systemSizeMode = static_cast<DGuiApplicationHelper::SizeMode>(mode);
const auto currentSizeMode = fetchSizeMode();
if (oldSizeMode != currentSizeMode)
Q_EMIT q->sizeModeChanged(currentSizeMode);
}
DGuiApplicationHelper::SizeMode DGuiApplicationHelperPrivate::fetchSizeMode(bool *isSystemSizeMode) const
{
if (isSystemSizeMode)
*isSystemSizeMode = false;
// `setSizeMode` > `D_DTK_SIZEMODE` > `systemSizeMode`
if (explicitSizeMode != InvalidSizeMode)
return explicitSizeMode;
static const QString envSizeMode(qEnvironmentVariable("D_DTK_SIZEMODE"));
if (!envSizeMode.isEmpty()) {
bool ok = false;
const auto mode = envSizeMode.toInt(&ok);
if (ok)
return static_cast<DGuiApplicationHelper::SizeMode>(mode);
}
if (isSystemSizeMode)
*isSystemSizeMode = true;
return systemSizeMode;
}
/*!
\class Dtk::Gui::DGuiApplicationHelper
\inmodule dtkgui
\brief DGuiApplicationHelper 应用程序的 GUI ,如主题、调色板等.
*/
/*!
\enum DGuiApplicationHelper::ColorType
DGuiApplicationHelper::ColorType 定义了主题类型.
\var DGuiApplicationHelper::ColorType DGuiApplicationHelper::UnknownType
未知主题(浅色主题或深色主题)
\var DGuiApplicationHelper::ColorType DGuiApplicationHelper::LightType
浅色主题
\var DGuiApplicationHelper::ColorType DGuiApplicationHelper::DarkType
深色主题
*/
/*!
\enum DGuiApplicationHelper::Attribute
DGuiApplicationHelper::Attribute 定义了功能属性
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::UseInactiveColorGroup
如果开启,当窗口处于Inactive状态时就会使用QPalette::Inactive的颜色,否则窗口将没有任何颜色变化。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::ColorCompositing
是否采用半透明样式的调色板。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::ReadOnlyLimit
区分只读枚举。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsDeepinPlatformTheme
获取当前是否使用deepin的platformtheme插件,platformtheme插件可以为Qt程序提供特定的控件样式,默认使用chameleon主题。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsDXcbPlatform
获取当前使用的是不是dtk的xcb窗口插件,dxcb插件提供了窗口圆角和阴影功能。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsXWindowPlatform
获取当前是否运行在X11环境中。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsTableEnvironment
获取当前是否运行在deepin平板环境中,检测XDG_CURRENT_DESKTOP环境变量是不是tablet结尾。
\var DGuiApplicationHelper::Attribute DGuiApplicationHelper::IsDeepinEnvironment
获取当前是否运行在deepin桌面环境中,检测XDG_CURRENT_DESKTOP环境变量是不是deepin。
*/
DGuiApplicationHelper::DGuiApplicationHelper()
: QObject(nullptr)
, DObject(*new DGuiApplicationHelperPrivate(this))
{
}
void DGuiApplicationHelper::initialize()
{
D_D(DGuiApplicationHelper);
d->init();
}
#if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
/*!
\brief 创建 DGuiApplicationHelper 对象.
\param creator 函数指针
\note 一定要先调用此函数,再使用 DGuiApplicationHelper::instance()
*/
void DGuiApplicationHelper::registerInstanceCreator(DGuiApplicationHelper::HelperCreator creator)
{
if (creator == _DGuiApplicationHelper::creator)
return;
_DGuiApplicationHelper::creator = creator;
if (_globalHelper.exists()) {
_globalHelper->clear();
}
}
#endif
inline static int adjustColorValue(int base, qint8 increment, int max = 255)
{
return increment > 0 ? (max - base) * increment / 100.0 + base
: base * (1 + increment / 100.0);
}
/*!
\brief DGuiApplicationHelper::instance返回 DGuiApplicationHelper 对象
\return DGuiApplicationHelper对象
*/
DGuiApplicationHelper *DGuiApplicationHelper::instance()
{
return _globalHelper->helper();
}
DGuiApplicationHelper::~DGuiApplicationHelper()
{
_globalHelper->m_helper = nullptr;
}
static inline QColor adjustHSLColor(const QColor &base, qint8 hueFloat, qint8 saturationFloat,
qint8 lightnessFloat, qint8 alphaFloat = 0)
{
if (Q_LIKELY(hueFloat || saturationFloat || lightnessFloat || alphaFloat)) {
// 按HSL格式调整
int H, S, L, A;
base.getHsl(&H, &S, &L, &A);
H = H > 0 ? adjustColorValue(H, hueFloat, 359) : H;
S = adjustColorValue(S, saturationFloat);
L = adjustColorValue(L, lightnessFloat);
return QColor::fromHsl(H, S, L, A);
}
return base;
}
static inline QColor adjustRGBColor(const QColor &base, qint8 redFloat, qint8 greenFloat,
qint8 blueFloat, qint8 alphaFloat = 0)
{
if (Q_LIKELY(redFloat || greenFloat || blueFloat || alphaFloat)) {
// 按RGB格式调整
int R, G, B, A;
base.getRgb(&R, &G, &B, &A);
R = adjustColorValue(R, redFloat);
G = adjustColorValue(G, greenFloat);
B = adjustColorValue(B, blueFloat);
A = adjustColorValue(A, alphaFloat);
return QColor(R, G, B, A);
}
return base;
}
/*!
\brief 调整颜色.
\note 取值范围均为 -100 ~ 100 ,当三原色参数为-100时,颜色为黑色,参数为100时,颜色为白色.
以透明度( alphaFloat )为例,当参数为负数时基础色的 alphaFloat 值减少,现象偏向透明, 参数为正数alphaFloat 值增加,现象偏不透明
\param base 基础色
\param hueFloat 色调
\param saturationFloat 饱和度
\param lightnessFloat 亮度
\param redFloat 红色
\param greenFloat 绿色
\param blueFloat 蓝色
\param alphaFloat Alpha通道(透明度)
\return 经过调整的颜色
*/
QColor DGuiApplicationHelper::adjustColor(const QColor &base,
qint8 hueFloat, qint8 saturationFloat, qint8 lightnessFloat,
qint8 redFloat, qint8 greenFloat, qint8 blueFloat, qint8 alphaFloat)
{
if (Q_UNLIKELY(!base.isValid()))
return base;
// First do the adjust of spec of this color, Avoid precision loss caused by color spec conversion.
if (Q_UNLIKELY(base.spec() == QColor::Hsl)) {
QColor newColor = adjustHSLColor(base, hueFloat, saturationFloat, lightnessFloat, alphaFloat);
return adjustRGBColor(newColor, redFloat, greenFloat, blueFloat);
}
// For other specs, first do RGB adjust
QColor newColor = adjustRGBColor(base, redFloat, greenFloat, blueFloat, alphaFloat);
return adjustHSLColor(newColor, hueFloat, saturationFloat, lightnessFloat);
}
/*!
\brief 调整图片整体像素颜色.
\note 取值范围均为 -100 ~ 100 ,当三原色参数为-100时,颜色为黑色,参数为100时,颜色为白色.
以透明度( alphaFloat )为例,当参数为负数时基础色的 alphaFloat 值减少,现象偏向透明, 参数为正数alphaFloat 值增加,现象偏不透明
\param base 基础色
\param hueFloat 色调
\param saturationFloat 饱和度
\param lightnessFloat 亮度
\param redFloat 红色
\param greenFloat 绿色
\param blueFloat 蓝色
\param alphaFloat Alpha通道(透明度)
\return 经过调整的图片
*/
QImage DGuiApplicationHelper::adjustColor(const QImage &base, qint8 hueFloat, qint8 saturationFloat, qint8 lightnessFloat, qint8 redFloat, qint8 greenFloat, qint8 blueFloat, qint8 alphaFloat)
{
if (base.isNull())
return base;
if (!hueFloat && !saturationFloat && !lightnessFloat && !redFloat
&& !greenFloat && !blueFloat && !alphaFloat)
return base;
QImage dest = base;
for (int y = 0; y < dest.height(); ++y) {
const QRgb *rgb = reinterpret_cast<const QRgb *>(base.scanLine(y));
for (int x = 0; x < dest.width(); ++x) {
QColor base = QColor::fromRgba(rgb[x]);
if (base.alpha() == 0)
continue;
QColor color = adjustColor(base, hueFloat, saturationFloat, lightnessFloat,
redFloat, greenFloat, blueFloat, alphaFloat);
dest.setPixel(x, y, color.rgba());
}
}
return dest;
}
/*!
\brief 将两种颜色混合,合成新的颜色.
\param substrate 底层颜色
\param superstratum 上层颜色
\return 混合颜色
*/
QColor DGuiApplicationHelper::blendColor(const QColor &substrate, const QColor &superstratum)
{
QColor c2 = superstratum.toRgb();
if (c2.alpha() >= 255)
return c2;
QColor c1 = substrate.toRgb();
qreal c1_weight = 1 - c2.alphaF();
int r = c1_weight * c1.red() + c2.alphaF() * c2.red();
int g = c1_weight * c1.green() + c2.alphaF() * c2.green();
int b = c1_weight * c1.blue() + c2.alphaF() * c2.blue();
return QColor(r, g, b, c1.alpha());
}
static QColor light_qpalette[QPalette::NColorRoles] {
QColor(0, 0, 0, 0.7 * 255), //WindowText
QColor("#e5e5e5"), //Button
QColor("#e6e6e6"), //Light
QColor("#e5e5e5"), //Midlight
QColor("#e3e3e3"), //Dark
QColor("#e4e4e4"), //Mid
QColor(0, 0, 0, 0.7 * 255), //Text
Qt::black, //BrightText
QColor(0, 0, 0, 0.7 * 255), //ButtonText
Qt::white, //Base
QColor("#f8f8f8"), //Window
QColor(0, 0, 0, 0.05 * 255), //Shadow
QColor("#0081ff"), //Highlight
Qt::white, //HighlightedText
QColor("#0082fa"), //Link
QColor("#ad4579"), //LinkVisited
QColor(0, 0, 0, 0.03 * 255), //AlternateBase
Qt::white, //NoRole
QColor(255, 255, 255, 0.8 * 255), //ToolTipBase
QColor(0, 0, 0, 0.85 * 255) //ToolTipText
};
static QColor dark_qpalette[QPalette::NColorRoles] {
QColor(255, 255, 255, 0.7 * 255), //WindowText
QColor("#444444"), //Button
QColor("#484848"), //Light
QColor("#474747"), //Midlight
QColor("#414141"), //Dark
QColor("#434343"), //Mid
QColor(255, 255, 255, 0.7 * 255), //Text
Qt::white, //BrightText
QColor(255, 255, 255, 0.7 * 255), //ButtonText
QColor("#282828"), //Base
QColor("#252525"), //Window
QColor(0, 0, 0, 0.05 * 255), //Shadow
QColor("#024CCA"), //Highlight
QColor("#F1F6FF"), //HighlightedText
QColor("#0082fa"), //Link
QColor("#ad4579"), //LinkVisited
QColor(0, 0, 0, 0.05 * 255), //AlternateBase
Qt::black, //NoRole
QColor(45, 45, 45, 0.8 * 255), //ToolTipBase
QColor(255, 255, 255, 0.85 * 255) //ToolTipText
};
static QColor light_dpalette[DPalette::NColorTypes] {
QColor(), //NoType
QColor(0, 0, 0, 255 * 0.03), //ItemBackground
QColor(0, 0, 0, 0.85 * 255), //TextTitle
QColor(0, 0, 0, 0.6 * 255), //TextTips
QColor("#FF5736"), //TextWarning
Qt::black, //TextLively
QColor("#0081FF"), //LightLively
QColor("#0081FF"), //DarkLively
QColor(0, 0, 0, 0.05 * 255), //FrameBorder
QColor(85, 85, 85, 0.4 * 255), //PlaceholderText
QColor(0, 0, 0, 0.1 * 255), //FrameShadowBorder
QColor(0, 0, 0, 0.1 * 255) //ObviousBackground
};
static QColor dark_dpalette[DPalette::NColorTypes] {
QColor(), //NoType
QColor(255, 255, 255, 255 * 0.05), //ItemBackground
QColor(255, 255, 255, 0.85 * 255), //TextTitle
QColor(255, 255, 255, 0.6 * 255), //TextTips
QColor("#E43F2E"), //TextWarning
Qt::white, //TextLively
QColor("#0059d2"), //LightLively
QColor("#0059d2"), //DarkLively
QColor(255, 255, 255, 0.1 * 255), //FrameBorder
QColor(192, 198, 212, 0.4 * 255), //PlaceholderText
QColor(0, 0, 0, 0.8 * 255), //FrameShadowBorder
QColor(255, 255, 255, 0.1 * 255) //ObviousBackground
};
/*!
\brief 根据主题获取标准调色板.
\param type 主题枚举值
\return 调色板
*/
DPalette DGuiApplicationHelper::standardPalette(DGuiApplicationHelper::ColorType type)
{
static const DPalette *light_palette = nullptr, *dark_palette = nullptr;
static const DPalette *alpha_light_palette = nullptr, *alpha_dark_palette = nullptr;
const bool allowCompositingColor = DGuiApplicationHelper::testAttribute(ColorCompositing);
if (type == LightType) {
if (Q_UNLIKELY(allowCompositingColor)) {
if (Q_LIKELY(alpha_light_palette)) {
return *alpha_light_palette;
}
}
if (Q_LIKELY(light_palette)) {
return *light_palette;
}
} else if (type == DarkType) {
if (Q_UNLIKELY(allowCompositingColor)) {
if (Q_LIKELY(alpha_dark_palette)) {
return *alpha_dark_palette;
}
}
if (Q_LIKELY(dark_palette)) {
return *dark_palette;
}
} else {
return DPalette();
}
DPalette *pa;
const QColor *qcolor_list, *dcolor_list;
if (type == DarkType) {
pa = new DPalette();
if (allowCompositingColor)
alpha_dark_palette = pa;
else
dark_palette = pa;
qcolor_list = dark_qpalette;
dcolor_list = dark_dpalette;
} else {
pa = new DPalette();
if (allowCompositingColor)
alpha_light_palette = pa;
else
light_palette = pa;
qcolor_list = light_qpalette;
dcolor_list = light_dpalette;
}
for (int i = 0; i < DPalette::NColorRoles; ++i) {
QPalette::ColorRole role = static_cast<QPalette::ColorRole>(i);
QColor color = qcolor_list[i];
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
if (role == QPalette::PlaceholderText) {
color = dcolor_list[DPalette::PlaceholderText];
}
#endif
// 处理半透明色
if (allowCompositingColor) {
switch (role) {
case QPalette::Window:
color = type == LightType ? adjustColor(color, 0, 0, 0, 0, 0, 0, -20) : adjustColor(color, 0, 0, -10, 0, 0, 0, -20);
break;
case QPalette::Base:
color = adjustColor(color, 0, 0, 0, 0, 0, 0, -20);
break;
case QPalette::WindowText:
case QPalette::Text:
color = adjustColor(color, 0, 0, type == LightType ? -20 : +20, 0, 0, 0, -20);
break;
case QPalette::ButtonText:
color = type == LightType ? adjustColor(color, 0, 0, -20, 0, 0, 0, -20) : adjustColor(color, 0, 0, +20, 0, 0, 0, 0);
break;
case QPalette::Button:
case QPalette::Light:
case QPalette::Mid:
case QPalette::Midlight:
case QPalette::Dark:
color = adjustColor(color, 0, 0, -20, 0, 0, 0, -40);
break;
default:
break;
}
}
pa->setColor(DPalette::Active, role, color);
generatePaletteColor(*pa, role, type);
}
for (int i = 0; i < DPalette::NColorTypes; ++i) {
DPalette::ColorType role = static_cast<DPalette::ColorType>(i);
QColor color = dcolor_list[i];
// 处理半透明色
if (allowCompositingColor) {
switch (role) {
case DPalette::ItemBackground:
color = adjustColor(color, 0, 0, 100, 0, 0, 0, type == LightType ? -80 : -90);
break;
case DPalette::TextTitle:
color = adjustColor(color, 0, 0, -20, 0, 0, 0, -20);
break;
case DPalette::TextTips:
color = type == LightType ? adjustColor(color, 0, 0, -40, 0, 0, 0, -40) : adjustColor(color, 0, 0, +40, 0, 0, 0, -50);
break;
default:
break;
}
}
pa->setColor(DPalette::Active, role, color);
generatePaletteColor(*pa, role, type);
}
return *const_cast<const DPalette*>(pa);
}
template<typename M>
static void generatePaletteColor_helper(DPalette &base, M role, DGuiApplicationHelper::ColorType type)
{
if (type == DGuiApplicationHelper::UnknownType) {
type = DGuiApplicationHelper::toColorType(base);
}
QColor disable_mask_color, inactive_mask_color;
if (type == DGuiApplicationHelper::DarkType) {
disable_mask_color = dark_qpalette[QPalette::Window];
inactive_mask_color = dark_qpalette[QPalette::Window];
disable_mask_color.setAlphaF(0.7);
inactive_mask_color.setAlphaF(0.6);
} else {
disable_mask_color = light_qpalette[QPalette::Window];
inactive_mask_color = light_qpalette[QPalette::Window];
disable_mask_color.setAlphaF(0.6);
inactive_mask_color.setAlphaF(0.4);
}
const QColor &color = base.color(QPalette::Normal, role);
base.setColor(QPalette::Disabled, role, DGuiApplicationHelper::blendColor(color, disable_mask_color));
QPalette::ColorRole qr = static_cast<QPalette::ColorRole>(role);
if (qr == QPalette::Text) {
// disable text color olny -60% alpha
base.setColor(QPalette::Disabled, role, DGuiApplicationHelper::adjustColor(color, 0, 0, 0, 0, 0, 0, -60));
}
if (DGuiApplicationHelper::testAttribute(DGuiApplicationHelper::Attribute::UseInactiveColorGroup))
base.setColor(QPalette::Inactive, role, DGuiApplicationHelper::blendColor(color, inactive_mask_color));
else
base.setColor(QPalette::Inactive, role, color);
}
/*!
\brief 获取调色板颜色.
\param base 调色板
\param role 色码
\param type 主题枚举值
\sa QPalette::ColorRole
*/
void DGuiApplicationHelper::generatePaletteColor(DPalette &base, QPalette::ColorRole role, DGuiApplicationHelper::ColorType type)
{
if (role == QPalette::Window) {
const QBrush &window = base.brush(QPalette::Normal, role);
base.setBrush(QPalette::Disabled, role, window);
base.setBrush(QPalette::Inactive, role, window);
return;
}
generatePaletteColor_helper(base, role, type);
}
/*!