-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdsplitedbar.cpp
1392 lines (1179 loc) · 40.1 KB
/
dsplitedbar.cpp
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
/*
* Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dsplitedbar.h"
#include "dsplitedwindow.h"
#include "daddonapplication.h"
#include "private/dapplication_p.h"
#include <QDebug>
#include <QMenu>
#include <QHBoxLayout>
#include <QApplication>
#include <QMouseEvent>
#include <QProcess>
#include <QTimer>
#include <DWindowManagerHelper>
LDA_BEGIN_NAMESPACE
const int DefaultTitlebarHeight = 50;
const int DefaultIconHeight = 32;
const int DefaultIconWidth = 32;
DSplitedBarPrivate::DSplitedBarPrivate(DSplitedBar *qq): DObjectPrivate(qq)
{
}
void DSplitedBarPrivate::init()
{
D_Q(DSplitedBar);
mainLayout = new QHBoxLayout;
leftArea = new QWidget;
leftLayout = new QHBoxLayout(leftArea);
rightArea = new QWidget;
rightLayout = new QHBoxLayout;
centerArea = new DLabel(q);
centerLayout = new QHBoxLayout(centerArea);
iconLabel = new DIconButton(q);
buttonArea = new QWidget;
minButton = new DWindowMinButton;
maxButton = new DWindowMaxButton;
closeButton = new DWindowCloseButton;
optionButton = new DWindowOptionButton;
quitFullButton = new DImageButton;
separatorTop = new DHorizontalLine(q);
separator = new DHorizontalLine(q);
titleLabel = centerArea;
titleLabel->setElideMode(Qt::ElideMiddle);
minButton->installEventFilter(q);
maxButton->installEventFilter(q);
closeButton->installEventFilter(q);
optionButton->installEventFilter(q);
quitFullButton->installEventFilter(q);
optionButton->setObjectName("DSplitedBarDWindowOptionButton");
optionButton->setIconSize(QSize(DefaultTitlebarHeight, DefaultTitlebarHeight));
minButton->setObjectName("DSplitedBarDWindowMinButton");
minButton->setIconSize(QSize(DefaultTitlebarHeight, DefaultTitlebarHeight));
maxButton->setObjectName("DSplitedBarDWindowMaxButton");
maxButton->setIconSize(QSize(DefaultTitlebarHeight, DefaultTitlebarHeight));
closeButton->setObjectName("DSplitedBarDWindowCloseButton");
closeButton->setIconSize(QSize(DefaultTitlebarHeight, DefaultTitlebarHeight));
quitFullButton->setObjectName("DSplitedBarDWindowQuitFullscreenButton");
quitFullButton->hide();
iconLabel->setIconSize(QSize(DefaultIconWidth, DefaultIconHeight));
iconLabel->setWindowFlags(Qt::WindowTransparentForInput);
iconLabel->setFocusPolicy(Qt::NoFocus);
iconLabel->setFlat(true);
// 默认无图标,所以隐藏
iconLabel->hide();
leftArea->setWindowFlag(Qt::WindowTransparentForInput);
leftArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
leftLayout->setContentsMargins(0, 0, 0, 0);
leftArea->setFixedWidth(q->left_margin);
centerLayout->setContentsMargins(0, 0, 0, 0);
centerArea->setText(qApp->applicationDisplayName());
centerArea->setWindowFlags(Qt::WindowTransparentForInput);
centerArea->setFrameShape(QFrame::NoFrame);
centerArea->setAutoFillBackground(false);
centerArea->setBackgroundRole(QPalette::NoRole);
centerArea->setAlignment(Qt::AlignCenter);
centerArea->setWordWrap(true);
centerArea->setMaximumWidth(q->left_margin);
buttonArea->setWindowFlag(Qt::WindowTransparentForInput);
buttonArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
QHBoxLayout *buttonLayout = new QHBoxLayout(buttonArea);
buttonLayout->setContentsMargins(0, 0, 0, 0);
buttonLayout->setSpacing(0);
buttonLayout->addWidget(optionButton);
buttonLayout->addWidget(minButton);
buttonLayout->addWidget(maxButton);
buttonLayout->addWidget(closeButton);
buttonLayout->addWidget(quitFullButton);
rightArea->setWindowFlag(Qt::WindowTransparentForInput);
rightArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
rightLayout->setContentsMargins(0, 0, 0, 0);
auto rightAreaLayout = new QHBoxLayout(rightArea);
rightAreaLayout->setContentsMargins(0, 0, 0, 0);
rightAreaLayout->setMargin(0);
rightAreaLayout->setSpacing(0);
rightAreaLayout->addLayout(rightLayout);
rightAreaLayout->addWidget(buttonArea);
separatorTop->setFixedHeight(1);
separatorTop->hide();
separatorTop->setWindowFlags(Qt::WindowTransparentForInput);
separator->setFixedHeight(1);
separator->hide();
separator->setWindowFlags(Qt::WindowTransparentForInput);
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(leftArea, 0, Qt::AlignLeft);
mainLayout->addWidget(rightArea, 0, Qt::AlignRight);
titleLabel->setMargin(5);
q->setLayout(mainLayout);
q->setFixedHeight(DefaultTitlebarHeight);
q->setMinimumHeight(DefaultTitlebarHeight);
q->connect(quitFullButton, &DImageButton::clicked, q, [ = ]() {
bool isFullscreen = targetWindow()->windowState().testFlag(Qt::WindowFullScreen);
if (isFullscreen) {
targetWindow()->showNormal();
} else {
targetWindow()->showFullScreen();
}
});
q->connect(optionButton, &DWindowOptionButton::clicked, q, &DSplitedBar::optionClicked);
q->connect(DWindowManagerHelper::instance(), SIGNAL(windowMotifWMHintsChanged(quint32)),
q, SLOT(_q_onTopWindowMotifHintsChanged(quint32)));
q->setFrameShape(QFrame::NoFrame);
q->setBackgroundRole(QPalette::Base);
q->setAutoFillBackground(true);
q->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
// 确保点击标题栏后输入框能失去焦点
// 另外,让标题栏接收焦点,还是为了避免一个focus控件隐藏时,会把焦点转移给标题栏上的按钮控件
q->setFocusPolicy(Qt::StrongFocus);
#ifndef QT_NO_MENU
q->setIconMenu(this->automateIconMenu());
QObject::connect(iconLabel, &DIconButton::clicked, q, &DSplitedBar::showIconMenu);
#endif
}
QWidget *DSplitedBarPrivate::targetWindow()
{
D_Q(DSplitedBar);
return q->topLevelWidget()->window();
}
bool DSplitedBarPrivate::isVisableOnFullscreen()
{
D_Q(DSplitedBar);
return !q->property("_restore_height").isValid();
}
void DSplitedBarPrivate::hideOnFullscreen()
{
D_Q(DSplitedBar);
if (q->height() > 0) {
q->setProperty("_restore_height", q->height());
}
q->setFixedHeight(0);
}
void DSplitedBarPrivate::showOnFullscreen()
{
D_Q(DSplitedBar);
if (q->property("_restore_height").isValid()) {
q->setFixedHeight(q->property("_restore_height").toInt());
q->setProperty("_restore_height", QVariant());
}
}
void DSplitedBarPrivate::updateFullscreen()
{
D_Q(DSplitedBar);
if (!autoHideOnFullscreen) {
return;
}
bool isFullscreen = targetWindow()->windowState().testFlag(Qt::WindowFullScreen);
auto mainWindow = qobject_cast<DSplitedWindow *>(targetWindow());
if (!isFullscreen) {
quitFullButton->hide();
showOnFullscreen();
} else {
// must set to empty
quitFullButton->show();
q->setParent(mainWindow);
q->show();
hideOnFullscreen();
}
}
void DSplitedBarPrivate::updateButtonsState(Qt::WindowFlags type)
{
D_Q(DSplitedBar);
bool useDXcb = DPlatformWindowHandle::isEnabledDXcb(targetWindow());
bool isFullscreen = targetWindow()->windowState().testFlag(Qt::WindowFullScreen);
// bool forceShow = !useDXcb;
bool forceShow = false;
#ifndef Q_OS_LINUX
forceShow = false;
#endif
bool showTitle = (type.testFlag(Qt::WindowTitleHint) || forceShow) && !embedMode;
if (titleLabel) {
titleLabel->setVisible(showTitle);
}
// Never show in embed/fullscreen
bool forceHide = (!useDXcb) || embedMode || isFullscreen;
bool showMin = (type.testFlag(Qt::WindowMinimizeButtonHint) || forceShow) && !forceHide;
minButton->setVisible(showMin);
bool allowResize = true;
if (q->window() && q->window()->windowHandle()) {
auto functions_hints = DWindowManagerHelper::getMotifFunctions(q->window()->windowHandle());
allowResize = functions_hints.testFlag(DWindowManagerHelper::FUNC_RESIZE);
}
bool showMax = (type.testFlag(Qt::WindowMaximizeButtonHint) || forceShow) && !forceHide && allowResize;
// qDebug() << "max:"
// << "allowResize" << allowResize
// << "useDXcb" << useDXcb
// << "forceHide" << forceHide
// << "type.testFlag(Qt::WindowMaximizeButtonHint)" << type.testFlag(Qt::WindowMaximizeButtonHint);
maxButton->setVisible(showMax);
bool showClose = (type.testFlag(Qt::WindowCloseButtonHint) || forceShow) && !forceHide;
closeButton->setVisible(showClose);
}
void DSplitedBarPrivate::updateButtonsFunc()
{
// TASK-18145 (bug-17474) do not setMotifFunctions on wayland
if (!targetWindowHandle || !qgetenv("WAYLAND_DISPLAY").isEmpty()) {
return;
}
// 根据 disableFlags 更新窗口标志,而标题栏上具体按钮的开启/禁用状态只根据
// 窗口标志改变后做更新,且窗口标志改变后也会同步更新 disableFlags 的值,
// 也就是说,实际上窗口按钮的状态只受窗口标志影响,而 setDisableFlags
// 只是单纯的记录新的 disableFlags 的值,由于调用 setDisableFlags
// 是应用中的主动行为,所以同时也会根据这个值更新窗口标志。
DWindowManagerHelper::setMotifFunctions(
targetWindowHandle,
DWindowManagerHelper::FUNC_MAXIMIZE,
!disableFlags.testFlag(Qt::WindowMaximizeButtonHint));
DWindowManagerHelper::setMotifFunctions(
targetWindowHandle,
DWindowManagerHelper::FUNC_MINIMIZE,
!disableFlags.testFlag(Qt::WindowMinimizeButtonHint));
DWindowManagerHelper::setMotifFunctions(
targetWindowHandle,
DWindowManagerHelper::FUNC_CLOSE,
!disableFlags.testFlag(Qt::WindowCloseButtonHint));
}
void DSplitedBarPrivate::updateCenterArea()
{
D_QC(DSplitedBar);
if (centerArea->isHidden()) {
return;
}
int padding = qMax(leftArea->width(), rightArea->width());
QRect rect(0, 0, q->width() - 2 * padding, q->height());
rect.moveCenter(q->rect().center());
centerArea->setGeometry(rect);
}
void DSplitedBarPrivate::handleParentWindowStateChange()
{
maxButton->setMaximized(targetWindow()->windowState() == Qt::WindowMaximized);
updateFullscreen();
updateButtonsState(targetWindow()->windowFlags());
}
//!
//! \brief DSplitedBarPrivate::handleParentWindowIdChange
//! Them WindowStateChnage Event will miss some state changed message,
//! So use windowHandle::windowStateChanged instead
void DSplitedBarPrivate::handleParentWindowIdChange()
{
if (!targetWindowHandle) {
targetWindowHandle = targetWindow()->windowHandle();
updateButtonsFunc();
} else if (targetWindow()->windowHandle() != targetWindowHandle) {
// Parent change???, show never here
qWarning() << "targetWindowHandle change" << targetWindowHandle << targetWindow()->windowHandle();
}
}
void DSplitedBarPrivate::_q_toggleWindowState()
{
QWidget *parentWindow = targetWindow();
if (!parentWindow || disableFlags.testFlag(Qt::WindowMaximizeButtonHint)) {
return;
}
if (parentWindow->isMaximized()) {
parentWindow->showNormal();
} else if (!parentWindow->isFullScreen()
&& (maxButton->isVisible())) {
parentWindow->showMaximized();
}
}
void DSplitedBarPrivate::_q_showMinimized()
{
targetWindow()->showMinimized();
}
#if QT_VERSION < QT_VERSION_CHECK(5, 7, 0)
static Qt::WindowFlags &setWindowFlag(Qt::WindowFlags &flags, Qt::WindowType type, bool on)
{
return on ? (flags |= type) : (flags &= ~int(type));
}
#endif
void DSplitedBarPrivate::_q_onTopWindowMotifHintsChanged(quint32 winId)
{
D_QC(DSplitedBar);
if (!DPlatformWindowHandle::isEnabledDXcb(targetWindow())) {
q->disconnect(DWindowManagerHelper::instance(), SIGNAL(windowMotifWMHintsChanged(quint32)),
q, SLOT(_q_onTopWindowMotifHintsChanged(quint32)));
return;
}
if (winId != q->window()->internalWinId()) {
return;
}
DWindowManagerHelper::MotifDecorations decorations_hints = DWindowManagerHelper::getMotifDecorations(q->window()->windowHandle());
DWindowManagerHelper::MotifFunctions functions_hints = DWindowManagerHelper::getMotifFunctions(q->window()->windowHandle());
if (titleLabel) {
titleLabel->setVisible(decorations_hints.testFlag(DWindowManagerHelper::DECOR_TITLE));
}
updateButtonsState(targetWindow()->windowFlags());
minButton->setEnabled(functions_hints.testFlag(DWindowManagerHelper::FUNC_MINIMIZE));
maxButton->setEnabled(functions_hints.testFlag(DWindowManagerHelper::FUNC_MAXIMIZE)
&& functions_hints.testFlag(DWindowManagerHelper::FUNC_RESIZE));
closeButton->setEnabled(functions_hints.testFlag(DWindowManagerHelper::FUNC_CLOSE));
// sync button state
#if QT_VERSION >= QT_VERSION_CHECK(5, 7, 0)
disableFlags.setFlag(Qt::WindowMinimizeButtonHint, !minButton->isEnabled());
disableFlags.setFlag(Qt::WindowMaximizeButtonHint, !maxButton->isEnabled());
disableFlags.setFlag(Qt::WindowCloseButtonHint, !closeButton->isEnabled());
#else
setWindowFlag(disableFlags, Qt::WindowMinimizeButtonHint, !minButton->isEnabled());
setWindowFlag(disableFlags, Qt::WindowMaximizeButtonHint, !maxButton->isEnabled());
setWindowFlag(disableFlags, Qt::WindowCloseButtonHint, !closeButton->isEnabled());
#endif
}
#ifndef QT_NO_MENU
void DSplitedBarPrivate::_q_addDefaultMenuItems()
{
D_Q(DSplitedBar);
if (!menu) {
q->setMenu(new QMenu(q));
}
// add switch theme sub menu
if (!switchThemeMenu) {
bool disableDtkSwitchThemeMenu = qEnvironmentVariableIsSet("KLU_DISABLE_MENU_THEME");
if (!disableDtkSwitchThemeMenu) {
switchThemeMenu = new QMenu(qApp->translate("TitleBarMenu", "Theme"), menu);
lightThemeAction = switchThemeMenu->addAction(qApp->translate("TitleBarMenu", "Light Theme"));
darkThemeAction = switchThemeMenu->addAction(qApp->translate("TitleBarMenu", "Dark Theme"));
autoThemeAction = switchThemeMenu->addAction(qApp->translate("TitleBarMenu", "System Theme"));
autoThemeAction->setCheckable(true);
lightThemeAction->setCheckable(true);
darkThemeAction->setCheckable(true);
QActionGroup *group = new QActionGroup(switchThemeMenu);
group->addAction(autoThemeAction);
group->addAction(lightThemeAction);
group->addAction(darkThemeAction);
QObject::connect(group, SIGNAL(triggered(QAction*)),
q, SLOT(_q_switchThemeActionTriggered(QAction*)));
menu->addMenu(switchThemeMenu);
themeSeparator = menu->addSeparator();
switchThemeMenu->menuAction()->setVisible(canSwitchTheme);
themeSeparator->setVisible(canSwitchTheme);
}
}
// add help menu item.
if (!helpAction && DApplicationPrivate::isUserManualExists()) {
helpAction = new QAction(qApp->translate("TitleBarMenu", "Help"), menu);
QObject::connect(helpAction, SIGNAL(triggered(bool)), q, SLOT(_q_helpActionTriggered()));
menu->addAction(helpAction);
}
// add about menu item.
if (!aboutAction) {
aboutAction = new QAction(qApp->translate("TitleBarMenu", "About"), menu);
QObject::connect(aboutAction, SIGNAL(triggered(bool)), q, SLOT(_q_aboutActionTriggered()));
menu->addAction(aboutAction);
}
// add quit menu item.
if (!quitAction) {
quitAction = new QAction(qApp->translate("TitleBarMenu", "Exit"), menu);
QObject::connect(quitAction, SIGNAL(triggered(bool)), q, SLOT(_q_quitActionTriggered()));
menu->addAction(quitAction);
}
}
void DSplitedBarPrivate::_q_helpActionTriggered()
{
DAddonApplication *dapp = qobject_cast<DAddonApplication *>(qApp);
if (dapp) {
dapp->handleHelpAction();
}
}
void DSplitedBarPrivate::_q_aboutActionTriggered()
{
DAddonApplication *dapp = qobject_cast<DAddonApplication *>(qApp);
if (dapp) {
dapp->handleAboutAction();
}
}
void DSplitedBarPrivate::_q_quitActionTriggered()
{
DAddonApplication *dapp = qobject_cast<DAddonApplication *>(qApp);
if (dapp) {
dapp->handleQuitAction();
}
}
void DSplitedBarPrivate::_q_switchThemeActionTriggered(QAction *action)
{
DGuiApplicationHelper::ColorType type = DGuiApplicationHelper::UnknownType;
if (action == lightThemeAction) {
type = DGuiApplicationHelper::LightType;
} else if (action == darkThemeAction) {
type = DGuiApplicationHelper::DarkType;
}
DGuiApplicationHelper::instance()->setPaletteType(type);
}
void DSplitedBarPrivate::setIconVisible(bool visible)
{
if (iconLabel->isVisible() == visible)
return;
if (visible) {
leftLayout->insertSpacing(0, 10);
leftLayout->insertWidget(1, iconLabel, 0, Qt::AlignLeading | Qt::AlignVCenter);
iconLabel->show();
} else {
iconLabel->hide();
// 从布局中移除图标相关的东西
delete leftLayout->takeAt(0);
delete leftLayout->takeAt(1);
}
}
void DSplitedBarPrivate::updateTabOrder()
{
D_Q(DSplitedBar);
QList<QWidget *> orderWidget;
QList<QHBoxLayout *> orderLayout;
orderLayout << leftLayout << centerLayout << rightLayout;
//查找 leftLayout、centerLayout、rightLayout 三个区域中有 TabFocus 属性的 widget
for (QHBoxLayout * lyt : orderLayout) {
if (!lyt) {
continue;
}
for (int i = 0; i < lyt->count(); ++i) {
QWidget *wdg = lyt->itemAt(i)->widget();
if (wdg && (wdg->focusPolicy() & Qt::FocusPolicy::TabFocus)) {
orderWidget.append(wdg);
}
}
}
if (orderWidget.isEmpty()) {
return;
}
//对筛选出来的 widget 重新设置 taborder
QWidget::setTabOrder(q, orderWidget.first());
for (int i = 0; i < orderWidget.count() - 1; ++i) {
QWidget::setTabOrder(orderWidget.at(i), orderWidget.at(i + 1));
}
}
#endif
/*!
* \~english \class DSplitedBar
* \brief The DSplitedBar class is an universal title bar on the top of windows.
* \param parent is the parent widget to be attached on.
*
* Usually you don't need to construct a DSplitedBar instance by your self, you
* can get an DSplitedBar instance by DMainWindow::titlebar .
*/
/*!
* \~chinese \class DSplitedBar
* \brief DSplitedBar是Dtk程序通用的标题栏组件,用于实现标题栏的高度定制化。
* \param 父组件,一般为标题栏所在的窗口
*
* 一般情况下,请使用Dtk::Widget::DMainWindow::titlebar()来获取已经自动初始化的标题栏,
* 不要自己来创建这个标题栏。
*/
/*!
* \~english @brief DSplitedBar::DSplitedBar create an default widget with icon/title/and buttons
* @param parent
*/
/*!
* \~chinese @brief 创建一个DSplitedBar对象,包含默认的窗口按钮。
*/
DSplitedBar::DSplitedBar(QWidget *parent) :
QFrame(parent),
DObject(*new DSplitedBarPrivate(this))
{
if (DApplication::buildDtkVersion() < DTK_VERSION_CHECK(2, 0, 6, 1)) {
setBackgroundTransparent(true);
}
D_D(DSplitedBar);
d->init();
// 默认只在普通窗口中显示窗口菜单按钮
if (parent && parent->window()->windowType() != Qt::Window) {
d->optionButton->hide();
}
this->setIcon(qApp->windowIcon());
}
#ifndef QT_NO_MENU
/*!
* \~english @brief DSplitedBar::menu holds the QMenu object attached to this title bar.
* @return the QMenu object it holds, returns null if there's no one set.
*/
/*!
* \~chinese @brief 获取和标题栏关联的应用查询菜单。
* @return 如该标题栏没有设置菜单,这里会返回空,但是如该使用 Dtk::Widget::DApplication ,
* 那么这里一般会自动创建一个程序菜单。
*/
QMenu *DSplitedBar::menu() const
{
D_DC(DSplitedBar);
return d->menu;
}
QMenu *DSplitedBar::leftMenu() const
{
D_DC(DSplitedBar);
return d->m_iconmenu;
}
/*!
* \~english @brief DSplitedBar::setMenu attaches a QMenu object to the title bar.
* @param menu is the target menu.
*/
/*!
* \~chinese @brief 设置自定义的程序菜单
* @param 需要被设置的菜单
*/
void DSplitedBar::setMenu(QMenu *menu)
{
D_D(DSplitedBar);
d->menu = menu;
if (d->menu) {
disconnect(this, &DSplitedBar::optionClicked, nullptr, nullptr);
connect(this, &DSplitedBar::optionClicked, this, &DSplitedBar::showMenu);
}
}
void DSplitedBar::setLeftMenu(QMenu *menu)
{
D_D(DSplitedBar);
d->m_iconmenu = menu;
if (d->m_iconmenu) {
disconnect(this, &DSplitedBar::optionClicked, nullptr, nullptr);
connect(this, &DSplitedBar::optionClicked, this, &DSplitedBar::showMenu);
}
}
#endif
/*!
* \~english @brief DSplitedBar::customWidget
* @return the customized widget used in this title bar.
*
* One can set customized widget to show some extra widgets on the title bar.
* \see Dtk::Widget::DSplitedBar::setCustomWidget()
*/
/*!
* \~chinese @brief 标题栏绑定的自定义控件
* @return 自定义控件
*
* 可以通过自定义控件来在标题栏上显示复杂的组合控件
*
* \see Dtk::Widget::DSplitedBar::setCustomWidget()
*/
QWidget *DSplitedBar::customWidget() const
{
D_DC(DSplitedBar);
return d->customWidget;
}
#ifndef QT_NO_MENU
/*!
* \~english \brief DSplitedBar::showMenu pop the menu of application on titlebar.
*/
/*!
* \~chineses \brief 弹出应用程序菜单
*/
void DSplitedBar::setCustomTitleAlign(Qt::Alignment a)
{
D_D(DSplitedBar);
d->titleLabel->setAlignment(a);
}
void DSplitedBar::showMenu()
{
D_D(DSplitedBar);
if (d->menu) {
// 更新主题选中的项
if (d->switchThemeMenu) {
QAction *action;
switch (DGuiApplicationHelper::instance()->paletteType()) {
case DGuiApplicationHelper::LightType:
action = d->lightThemeAction;
break;
case DGuiApplicationHelper::DarkType:
action = d->darkThemeAction;
break;
default:
action = d->autoThemeAction;
break;
}
action->setChecked(true);
}
d->menu->exec(d->optionButton->mapToGlobal(d->optionButton->rect().bottomLeft()));
d->optionButton->update(); // FIX: bug-25253 sometimes optionButton not udpate after menu exec(but why?)
}
}
#endif
void DSplitedBar::showEvent(QShowEvent *event)
{
//fix the width issue and process menu
D_D(DSplitedBar);
d->separatorTop->setFixedWidth(width());
d->separatorTop->move(0, 0);
d->separator->setFixedWidth(width());
d->separator->move(0, height() - d->separator->height());
#ifndef QT_NO_MENU
d->_q_addDefaultMenuItems();
#endif
QWidget::showEvent(event);
if (DPlatformWindowHandle::isEnabledDXcb(window())) {
d->_q_onTopWindowMotifHintsChanged(
static_cast<quint32>(window()->internalWinId()));
}
d->updateCenterArea();
}
void DSplitedBar::mousePressEvent(QMouseEvent *event)
{
D_D(DSplitedBar);
d->mousePressed = (event->button() == Qt::LeftButton);
if (event->button() == Qt::RightButton) {
DWindowManagerHelper::popupSystemWindowMenu(window()->windowHandle());
return;
}
#ifdef DTK_TITLE_DRAG_WINDOW
Q_EMIT mousePosPressed(event->buttons(), event->globalPos());
#endif
Q_EMIT mousePressed(event->buttons());
}
void DSplitedBar::mouseReleaseEvent(QMouseEvent *event)
{
D_D(DSplitedBar);
if (event->button() == Qt::LeftButton) {
d->mousePressed = false;
}
}
bool DSplitedBar::eventFilter(QObject *obj, QEvent *event)
{
D_D(DSplitedBar);
if (event->type() == QEvent::MouseButtonPress &&
static_cast<QMouseEvent *>(event)->button() == Qt::RightButton &&
(obj ==d->minButton || obj == d->maxButton ||
obj == d->closeButton || obj == d->optionButton ||
obj == d->quitFullButton))
{
event->accept(); // button on titlebar should not show kwin menu
return true;
}
if (obj == d->targetWindow()) {
switch (event->type()) {
case QEvent::ShowToParent:
d->handleParentWindowIdChange();
d->updateButtonsState(d->targetWindow()->windowFlags());
break;
case QEvent::Resize:
if (d->autoHideOnFullscreen) {
setFixedWidth(d->targetWindow()->width());
}
break;
case QEvent::HoverMove: {
auto mouseEvent = reinterpret_cast<QMouseEvent *>(event);
bool isFullscreen = d->targetWindow()->windowState().testFlag(Qt::WindowFullScreen);
if (isFullscreen && d->autoHideOnFullscreen) {
if (mouseEvent->pos().y() > height() && d->isVisableOnFullscreen()) {
d->hideOnFullscreen();
}
if (mouseEvent->pos().y() < 2) {
d->showOnFullscreen();
}
}
break;
}
case QEvent::WindowStateChange: {
d->handleParentWindowStateChange();
break;
}
default:
break;
}
}
return QWidget::eventFilter(obj, event);
}
bool DSplitedBar::event(QEvent *e)
{
if (e->type() == QEvent::LayoutRequest) {
D_D(DSplitedBar);
d->updateCenterArea();
}
return QFrame::event(e);
}
void DSplitedBar::resizeEvent(QResizeEvent *event)
{
//override QWidget::resizeEvent to fix button and separator pos.
D_D(DSplitedBar);
d->separatorTop->setFixedWidth(event->size().width());
d->separator->setFixedWidth(event->size().width());
d->updateCenterArea();
if (d->blurWidget) {
d->blurWidget->resize(event->size().width() - left_margin, event->size().height());
if (isFirstMarginSet == true) {
d->blurWidget->move(left_margin, 0);
}
}
return QWidget::resizeEvent(event);
}
/*!
* \~english @brief DSplitedBar::setCustomWidget is an overloaded function.
* @param w is the widget to be used as the customize widget shown in the title
* bar.
* @param fixCenterPos indicates whether it should automatically move the
* customize widget to the horizontal center of the title bar or not.
*/
/*!
* \~chinese @brief 设置标题栏上的自定义控件
* @param w 需要显示的控件。
* @param fixCenterPos 是否需要自动修正控件位置,用于保持控件居中显示。
*/
void DSplitedBar::setCustomWidget(QWidget *w, bool fixCenterPos)
{
D_D(DSplitedBar);
if (w == d->customWidget) {
return;
}
if (d->customWidget) {
d->mainLayout->removeWidget(d->customWidget);
d->customWidget->hide();
d->customWidget->deleteLater();
}
d->customWidget = w;
if (w) {
w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
} else {
d->centerArea->show();
d->titleLabel = d->centerArea;
return;
}
if (fixCenterPos) {
for (int i = 0; i < d->centerLayout->count(); ++i) {
delete d->centerLayout->itemAt(i);
}
addWidget(w, Qt::Alignment());
d->centerArea->show();
d->titleLabel = d->centerArea;
} else {
d->mainLayout->insertWidget(1, w);
d->titleLabel = nullptr;
d->centerArea->hide();
}
}
void DSplitedBar::addWidget(QWidget *w, Qt::Alignment alignment)
{
D_D(DSplitedBar);
if (alignment & Qt::AlignLeft) {
d->leftLayout->addWidget(w, 0, alignment & ~Qt::AlignLeft);
} else if (alignment & Qt::AlignRight) {
d->rightLayout->addWidget(w, 0, alignment & ~Qt::AlignRight);
} else {
d->centerLayout->addWidget(w, 0, alignment);
d->centerArea->clear();
d->titleLabel = nullptr;
}
updateGeometry();
d->updateTabOrder();
}
void DSplitedBar::removeWidget(QWidget *w)
{
D_D(DSplitedBar);
d->leftLayout->removeWidget(w);
d->centerLayout->removeWidget(w);
d->rightLayout->removeWidget(w);
if (d->centerLayout->isEmpty()) {
d->titleLabel = d->centerArea;
d->titleLabel->setText(d->targetWindowHandle->title());
}
updateGeometry();
d->updateTabOrder();
}
/*!
* \~english @brief DSplitedBar::setFixedHeight change the height of the title bar to
* another value.
* @param h is the target height.
*/
/*!
* \~chinese @brief 设置标题栏的高度,默认高度为 50。
* @param h 需要设置的高度
*/
void DSplitedBar::setFixedHeight(int h)
{
QWidget::setFixedHeight(h);
}
/*!
* \~english @brief DSplitedBar::setBackgroundTransparent set the title background transparent
* @param transparent is the targeting value.
*/
/*!
* \~chinese @brief 设置标题栏背景是否透明,当为透明时标题栏直接叠加在下层控件上。
* @param transparent 是否透明
*/
void DSplitedBar::setBackgroundTransparent(bool transparent)
{
setAutoFillBackground(!transparent);
if (transparent)
setBackgroundRole(QPalette::NoRole);
else
setBackgroundRole(QPalette::Base);
}
/*!
* \~english @brief DSplitedBar::setSeparatorVisible sets the bottom separator of the title
* bar and the window contents to be visible or not.
* @param visible is the targeting value.
*/
/*!
* \~chinese @brief 设置菜单下面的分隔线是否可见,默认是可见的。
* @param 是否可见
*/
void DSplitedBar::setSeparatorVisible(bool visible)
{
D_D(DSplitedBar);
if (visible) {
d->separator->show();
d->separator->raise();
} else {
d->separator->hide();
}
}
/*!
* \~english @brief DSplitedBar::setTitle sets the title to be shown on the title bar.
* @param title is the text to be used as the window title.
*/
/*!
* \~chinese @brief 设置标题栏文本。
* @param 待设置内容
*/
void DSplitedBar::setTitle(const QString &title)
{
D_D(DSplitedBar);
if (d->titleLabel && !d->embedMode) {
d->titleLabel->setText(title);
} else if (parentWidget()) {
parentWidget()->setWindowTitle(title);
}
}
/*!
* \~english @brief DSplitedBar::setIcon sets the icon to be shown on the title bar.
* @param icon is to be used as the window icon.
*/
/*!