-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathbreezestyle.cpp
8517 lines (7184 loc) · 316 KB
/
breezestyle.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
/* SPDX-FileCopyrightText: 2014 Hugo Pereira Da Costa <hugo.pereira@free.fr>
* SPDX-FileCopyrightText: 2016 The Qt Company Ltd.
* SPDX-FileCopyrightText: 2021 Noah Davis <noahadvs@gmail.com>
* SPDX-FileCopyrightText: 2023 ivan tkachenko <me@ratijas.tk>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "breezestyle.h"
#include "breezeanimations.h"
#include "breezeblurhelper.h"
#include "breezeframeshadow.h"
#include "breezemdiwindowshadow.h"
#include "breezemetrics.h"
#include "breezemnemonics.h"
#include "breezepropertynames.h"
#include "breezeshadowhelper.h"
#include "breezesplitterproxy.h"
#include "breezestyleconfigdata.h"
#include "breezetoolsareamanager.h"
#include "breezewidgetexplorer.h"
#include "breezewindowmanager.h"
#include <KColorUtils>
#include <KIconLoader>
#include <kguiaddons_version.h>
#include <QApplication>
#include <QBitmap>
#include <QCheckBox>
#include <QComboBox>
#include <QDial>
#include <QDialog>
#include <QDialogButtonBox>
#include <QFormLayout>
#include <QGraphicsItem>
#include <QGraphicsProxyWidget>
#include <QGraphicsView>
#include <QGroupBox>
#include <QItemDelegate>
#include <QLineEdit>
#include <QMainWindow>
#include <QMdiArea>
#include <QMenu>
#include <QMenuBar>
#include <QMetaEnum>
#include <QPainter>
#include <QPushButton>
#include <QRadioButton>
#include <QScrollBar>
#include <QSplitterHandle>
#include <QStackedLayout>
#include <QTextEdit>
#include <QToolBar>
#include <QToolBox>
#include <QToolButton>
#include <QTreeView>
#include <QWidgetAction>
#if HAVE_QTDBUS
#include <QDBusConnection>
#endif
#if BREEZE_HAVE_QTQUICK
#include <KCoreAddons>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#include <Kirigami/Platform/TabletModeWatcher>
using TabletModeWatcher = Kirigami::Platform::TabletModeWatcher;
#else
#if __has_include(<Kirigami/TabletModeWatcher>)
// the namespaced include is new in KF 5.91
#include <Kirigami/TabletModeWatcher>
#else
#include <TabletModeWatcher>
#endif
using TabletModeWatcher = Kirigami::TabletModeWatcher;
#endif
#include <QQuickWindow>
#endif
#include "breeze_logging.h"
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
constexpr auto HighlightColor = QPalette::Accent;
#else
constexpr auto HighlightColor = QPalette::Highlight;
#endif
namespace BreezePrivate
{
class PainterStateSaver
{
public:
PainterStateSaver(QPainter *painter, bool bSaveRestore = true)
: m_painter(painter)
, m_bSaveRestore(bSaveRestore)
{
if (m_bSaveRestore) {
m_painter->save();
}
}
~PainterStateSaver()
{
restore();
}
void restore()
{
if (m_bSaveRestore) {
m_bSaveRestore = false;
m_painter->restore();
}
}
private:
QPainter *const m_painter;
bool m_bSaveRestore;
};
// needed to keep track of tabbars when being dragged
class TabBarData : public QObject
{
public:
//* constructor
explicit TabBarData()
: QObject()
{
}
//* assign target tabBar
void lock(const QWidget *widget)
{
_tabBar = widget;
}
//* true if tabbar is locked
bool isLocked(const QWidget *widget) const
{
return _tabBar && _tabBar.data() == widget;
}
//* release
void release()
{
_tabBar.clear();
}
private:
//* pointer to target tabBar
Breeze::WeakPointer<const QWidget> _tabBar;
};
//* needed to have spacing added to items in combobox
class ComboBoxItemDelegate : public QItemDelegate
{
public:
//* constructor
explicit ComboBoxItemDelegate(QAbstractItemView *parent)
: QItemDelegate(parent)
, _proxy(parent->itemDelegate())
, _itemMargin(Breeze::Metrics::ItemView_ItemMarginWidth)
{
}
//* paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
painter->setRenderHints(QPainter::Antialiasing);
// if the app sets an item delegate that isn't the default, use its drawing...
if (_proxy && _proxy->metaObject()->className() != QStringLiteral("QComboBoxDelegate")) {
_proxy.data()->paint(painter, option, index);
return;
}
// otherwise we draw the selected/highlighted background ourselves.
if (option.showDecorationSelected && (option.state & QStyle::State_Selected)) {
using namespace Breeze;
auto c = option.palette.brush((option.state & QStyle::State_Enabled) ? QPalette::Normal : QPalette::Disabled, HighlightColor).color();
painter->setPen(c);
c.setAlphaF(c.alphaF() * 0.3);
painter->setBrush(c);
auto radius = Metrics::Frame_FrameRadius - (0.5 * PenWidth::Frame);
painter->drawRoundedRect(QRectF(option.rect).adjusted(0.5, 0.5, -0.5, -0.5), radius, radius);
}
// and ask the base class to do everything else for us besides the selected/highlighted part which we just did
auto opt = option;
opt.showDecorationSelected = false;
opt.state &= ~QStyle::State_Selected;
QItemDelegate::paint(painter, opt, index);
}
//* size hint for index
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
// get size from either proxy or parent class
auto size(_proxy ? _proxy.data()->sizeHint(option, index) : QItemDelegate::sizeHint(option, index));
// adjust and return
if (size.isValid()) {
size.rheight() += _itemMargin * 2;
}
return size;
}
private:
//* proxy
Breeze::WeakPointer<QAbstractItemDelegate> _proxy;
//* margin
int _itemMargin;
};
//_______________________________________________________________
bool isProgressBarHorizontal(const QStyleOptionProgressBar *option)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return option && (option->state & QStyle::State_Horizontal);
#else
return option && ((option->state & QStyle::State_Horizontal) || option->orientation == Qt::Horizontal);
#endif
}
enum class ToolButtonMenuArrowStyle {
None,
InlineLarge,
InlineSmall,
SubControl,
};
ToolButtonMenuArrowStyle toolButtonMenuArrowStyle(const QStyleOption *option)
{
const auto toolButtonOption = qstyleoption_cast<const QStyleOptionToolButton *>(option);
if (!toolButtonOption) {
return ToolButtonMenuArrowStyle::None;
}
const bool hasPopupMenu(toolButtonOption->features & QStyleOptionToolButton::HasMenu
&& toolButtonOption->features & QStyleOptionToolButton::MenuButtonPopup);
const bool hasInlineIndicator(toolButtonOption->features & QStyleOptionToolButton::HasMenu && !hasPopupMenu);
const bool hasDelayedMenu(hasInlineIndicator && toolButtonOption->features & QStyleOptionToolButton::PopupDelay);
const bool hasIcon = !toolButtonOption->icon.isNull() || (toolButtonOption->features & QStyleOptionToolButton::Arrow);
const bool iconOnly = toolButtonOption->toolButtonStyle == Qt::ToolButtonIconOnly || (toolButtonOption->text.isEmpty() && hasIcon);
if (hasPopupMenu) {
return ToolButtonMenuArrowStyle::SubControl;
}
if (hasDelayedMenu) {
return ToolButtonMenuArrowStyle::InlineSmall;
}
if (hasInlineIndicator && !iconOnly) {
return ToolButtonMenuArrowStyle::InlineLarge;
}
return ToolButtonMenuArrowStyle::None;
}
}
namespace Breeze
{
//______________________________________________________________
Style::Style()
: _helper(std::make_shared<Helper>(StyleConfigData::self()->sharedConfig()))
, _shadowHelper(std::make_unique<ShadowHelper>(_helper))
, _animations(std::make_unique<Animations>())
, _mnemonics(std::make_unique<Mnemonics>())
, _blurHelper(std::make_unique<BlurHelper>(_helper))
, _windowManager(std::make_unique<WindowManager>())
, _frameShadowFactory(std::make_unique<FrameShadowFactory>())
, _mdiWindowShadowFactory(std::make_unique<MdiWindowShadowFactory>())
, _splitterFactory(std::make_unique<SplitterFactory>())
, _toolsAreaManager(std::make_unique<ToolsAreaManager>())
, _widgetExplorer(std::make_unique<WidgetExplorer>())
, _tabBarData(std::make_unique<BreezePrivate::TabBarData>())
#if BREEZE_HAVE_KSTYLE
, SH_ArgbDndWindow(newStyleHint(QStringLiteral("SH_ArgbDndWindow")))
, CE_CapacityBar(newControlElement(QStringLiteral("CE_CapacityBar")))
#endif
{
#if HAVE_QTDBUS
// use DBus connection to update on breeze configuration change
auto dbus = QDBusConnection::sessionBus();
dbus.connect(QString(),
QStringLiteral("/BreezeStyle"),
QStringLiteral("org.kde.Breeze.Style"),
QStringLiteral("reparseConfiguration"),
this,
SLOT(configurationChanged()));
dbus.connect(QString(),
QStringLiteral("/BreezeDecoration"),
QStringLiteral("org.kde.Breeze.Style"),
QStringLiteral("reparseConfiguration"),
this,
SLOT(configurationChanged()));
dbus.connect(QString(),
QStringLiteral("/KGlobalSettings"),
QStringLiteral("org.kde.KGlobalSettings"),
QStringLiteral("notifyChange"),
this,
SLOT(configurationChanged()));
dbus.connect(QString(), QStringLiteral("/KWin"), QStringLiteral("org.kde.KWin"), QStringLiteral("reloadConfig"), this, SLOT(configurationChanged()));
#endif
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
qApp->installEventFilter(this);
#else
connect(qApp, &QApplication::paletteChanged, this, &Style::configurationChanged);
#endif
// call the slot directly; this initial call will set up things that also
// need to be reset when the system palette changes
loadConfiguration();
}
//______________________________________________________________
Style::~Style()
{
}
//______________________________________________________________
void Style::polish(QWidget *widget)
{
if (!widget) {
return;
}
// register widget to animations
_animations->registerWidget(widget);
_windowManager->registerWidget(widget);
_frameShadowFactory->registerWidget(widget, _helper);
_mdiWindowShadowFactory->registerWidget(widget);
_shadowHelper->registerWidget(widget);
_splitterFactory->registerWidget(widget);
_toolsAreaManager->registerWidget(widget);
// enable mouse over effects for all necessary widgets
if (qobject_cast<QAbstractItemView *>(widget) || qobject_cast<QAbstractSpinBox *>(widget) || qobject_cast<QCheckBox *>(widget)
|| qobject_cast<QComboBox *>(widget) || qobject_cast<QDial *>(widget) || qobject_cast<QLineEdit *>(widget) || qobject_cast<QPushButton *>(widget)
|| qobject_cast<QRadioButton *>(widget) || qobject_cast<QScrollBar *>(widget) || qobject_cast<QSlider *>(widget)
|| qobject_cast<QSplitterHandle *>(widget) || qobject_cast<QTabBar *>(widget) || qobject_cast<QTextEdit *>(widget)
|| qobject_cast<QToolButton *>(widget) || widget->inherits("KTextEditor::View")) {
widget->setAttribute(Qt::WA_Hover);
}
// enforce translucency for drag and drop window
if (widget->testAttribute(Qt::WA_X11NetWmWindowTypeDND) && _helper->compositingActive()) {
widget->setAttribute(Qt::WA_TranslucentBackground);
widget->clearMask();
}
// scrollarea polishing is somewhat complex. It is moved to a dedicated method
polishScrollArea(qobject_cast<QAbstractScrollArea *>(widget));
if (auto itemView = qobject_cast<QAbstractItemView *>(widget)) {
// enable mouse over effects in the viewport of the itemview
itemView->viewport()->setAttribute(Qt::WA_Hover);
} else if (auto groupBox = qobject_cast<QGroupBox *>(widget)) {
// checkable group boxes
if (groupBox->isCheckable()) {
groupBox->setAttribute(Qt::WA_Hover);
}
} else if (qobject_cast<QAbstractButton *>(widget) && qobject_cast<QDockWidget *>(widget->parent())) {
widget->setAttribute(Qt::WA_Hover);
} else if (qobject_cast<QAbstractButton *>(widget) && qobject_cast<QToolBox *>(widget->parent())) {
widget->setAttribute(Qt::WA_Hover);
#if KGUIADDONS_VERSION < QT_VERSION_CHECK(6, 4, 0)
} else if (qobject_cast<QFrame *>(widget) && widget->parent() && widget->parent()->inherits("KTitleWidget")) {
// Using available KGuiAddons version as reference, assuming KF6 modules all same version
// With KWidgetsAddons >= 6.4 the child QFrame is gone and all children default to sutoFillBackground == false.
widget->setAutoFillBackground(false);
#endif
}
if (qobject_cast<QScrollBar *>(widget)) {
// remove opaque painting for scrollbars
widget->setAttribute(Qt::WA_OpaquePaintEvent, false);
} else if (widget->parent() && widget->parent()->inherits("QComboBoxListView")) {
widget->setAutoFillBackground(false);
} else if (widget->inherits("KTextEditor::View")) {
addEventFilter(widget);
} else if (auto toolButton = qobject_cast<QToolButton *>(widget)) {
if (toolButton->autoRaise()) {
// for flat toolbuttons, adjust foreground and background role accordingly
widget->setBackgroundRole(QPalette::NoRole);
widget->setForegroundRole(QPalette::WindowText);
}
if (widget->parentWidget() && widget->parentWidget()->parentWidget() && widget->parentWidget()->parentWidget()->inherits("Gwenview::SideBarGroup")) {
widget->setProperty(PropertyNames::toolButtonAlignment, Qt::AlignLeft);
}
} else if (qobject_cast<QDockWidget *>(widget)) {
// add event filter on dock widgets
// and alter palette
widget->setAutoFillBackground(false);
widget->setContentsMargins({});
addEventFilter(widget);
} else if (qobject_cast<QMdiSubWindow *>(widget)) {
widget->setAutoFillBackground(false);
addEventFilter(widget);
} else if (qobject_cast<QToolBox *>(widget)) {
widget->setBackgroundRole(QPalette::NoRole);
widget->setAutoFillBackground(false);
} else if (widget->parentWidget() && widget->parentWidget()->parentWidget()
&& qobject_cast<QToolBox *>(widget->parentWidget()->parentWidget()->parentWidget())) {
widget->setBackgroundRole(QPalette::NoRole);
widget->setAutoFillBackground(false);
widget->parentWidget()->setAutoFillBackground(false);
} else if (qobject_cast<QMenu *>(widget)) {
setTranslucentBackground(widget);
if (_helper->hasAlphaChannel(widget) && StyleConfigData::menuOpacity() < 100) {
_blurHelper->registerWidget(widget->window());
}
} else if (qobject_cast<QCommandLinkButton *>(widget)) {
addEventFilter(widget);
} else if (auto comboBox = qobject_cast<QComboBox *>(widget)) {
if (!hasParent(widget, "QWebView")) {
auto itemView(comboBox->view());
if (itemView && itemView->itemDelegate() && itemView->itemDelegate()->inherits("QComboBoxDelegate")) {
itemView->setItemDelegate(new BreezePrivate::ComboBoxItemDelegate(itemView));
}
}
} else if (widget->inherits("QComboBoxPrivateContainer")) {
addEventFilter(widget);
setTranslucentBackground(widget);
} else if (widget->inherits("QTipLabel")) {
setTranslucentBackground(widget);
} else if (widget->inherits("KMultiTabBar")) {
enum class Position {
Left,
Right,
Top,
Bottom,
};
const Position position = static_cast<Position>(widget->property("position").toInt());
const auto splitterWidth = Metrics::Splitter_SplitterWidth;
int left = 0, right = 0;
if ((position == Position::Left && widget->layoutDirection() == Qt::LeftToRight)
|| (position == Position::Right && widget->layoutDirection() == Qt::RightToLeft)) {
right += splitterWidth;
} else if ((position == Position::Right && widget->layoutDirection() == Qt::LeftToRight)
|| (position == Position::Left && widget->layoutDirection() == Qt::RightToLeft)) {
left += splitterWidth;
}
widget->setContentsMargins(left, splitterWidth, right, splitterWidth);
} else if (qobject_cast<QMainWindow *>(widget)) {
widget->setAttribute(Qt::WA_StyledBackground);
} else if (qobject_cast<QDialogButtonBox *>(widget)) {
addEventFilter(widget);
} else if (qobject_cast<QDialog *>(widget)) {
widget->setAttribute(Qt::WA_StyledBackground);
} else if (auto pushButton = qobject_cast<QPushButton *>(widget)) {
QDialog *dialog = nullptr;
auto p = pushButton->parentWidget();
while (p && !p->isWindow()) {
p = p->parentWidget();
if (auto d = qobject_cast<QDialog *>(p)) {
dialog = d;
}
}
// Internally, QPushButton::autoDefault can be explicitly on,
// explicitly off, or automatic (enabled if in a QDialog).
// If autoDefault is explicitly on and not in a dialog,
// or on/automatic in a dialog and has a QDialogButtonBox parent,
// explicitly enable autoDefault, else explicitly disable autoDefault.
bool autoDefaultNoDialog = pushButton->autoDefault() && !dialog;
bool autoDefaultInDialog = pushButton->autoDefault() && dialog;
auto dialogButtonBox = qobject_cast<QDialogButtonBox *>(pushButton->parent());
pushButton->setAutoDefault(autoDefaultNoDialog || (autoDefaultInDialog && dialogButtonBox));
}
if (_toolsAreaManager->hasHeaderColors()) {
// style TitleWidget and Search KPageView to look the same as KDE System Settings
if (widget->objectName() == QLatin1String("KPageView::TitleWidget")) {
widget->setAutoFillBackground(true);
widget->setPalette(_toolsAreaManager->palette());
addEventFilter(widget);
} else if (widget->objectName() == QLatin1String("KPageView::Search")) {
widget->setBackgroundRole(QPalette::Window);
widget->setPalette(_toolsAreaManager->palette());
addEventFilter(widget);
}
}
// base class polishing
ParentStyleClass::polish(widget);
}
//______________________________________________________________
void Style::polish(QApplication *application)
{
_toolsAreaManager->registerApplication(application);
_helper->installEventFilter(application);
}
void Style::unpolish(QApplication *application)
{
_helper->removeEventFilter(application);
}
//______________________________________________________________
void Style::polishScrollArea(QAbstractScrollArea *scrollArea)
{
// check argument
if (!scrollArea) {
return;
}
// enable mouse over effect in sunken scrollareas that support focus
if (scrollArea->frameShadow() == QFrame::Sunken && scrollArea->focusPolicy() & Qt::StrongFocus) {
scrollArea->setAttribute(Qt::WA_Hover);
}
if (scrollArea->viewport() && scrollArea->inherits("KItemListContainer") && scrollArea->frameShape() == QFrame::NoFrame) {
scrollArea->viewport()->setBackgroundRole(QPalette::Window);
scrollArea->viewport()->setForegroundRole(QPalette::WindowText);
}
// add event filter, to make sure proper background is rendered behind scrollbars
addEventFilter(scrollArea);
// force side panels as flat, on option
if (scrollArea->inherits("KDEPrivate::KPageListView") || scrollArea->inherits("KDEPrivate::KPageTreeView")) {
scrollArea->setProperty(PropertyNames::sidePanelView, true);
}
// for all side view panels, unbold font (design choice)
if (scrollArea->property(PropertyNames::sidePanelView).toBool()) {
// upbold list font
auto font(scrollArea->font());
font.setBold(false);
scrollArea->setFont(font);
}
// disable autofill background for flat (== NoFrame) scrollareas, with QPalette::Window as a background
// this fixes flat scrollareas placed in a tinted widget, such as groupboxes, tabwidgets or framed dock-widgets
if (!(scrollArea->frameShape() == QFrame::NoFrame || scrollArea->backgroundRole() == QPalette::Window)) {
return;
}
// get viewport and check background role
auto viewport(scrollArea->viewport());
if (!(viewport && viewport->backgroundRole() == QPalette::Window)) {
return;
}
// change viewport autoFill background.
// do the same for all children if the background role is QPalette::Window
viewport->setAutoFillBackground(false);
const QList<QWidget *> children(viewport->findChildren<QWidget *>());
for (QWidget *child : children) {
if (child->parent() == viewport && child->backgroundRole() == QPalette::Window) {
child->setAutoFillBackground(false);
}
}
/*
QTreeView animates expanding/collapsing branches. It paints them into a
temp pixmap whose background is unconditionally filled with the palette's
*base* color which is usually different from the window's color
cf. QTreeViewPrivate::renderTreeToPixmapForAnimation()
*/
if (auto treeView = qobject_cast<QTreeView *>(scrollArea)) {
if (treeView->isAnimated()) {
QPalette pal(treeView->palette());
pal.setColor(QPalette::Active, QPalette::Base, treeView->palette().color(treeView->backgroundRole()));
treeView->setPalette(pal);
}
}
}
//_______________________________________________________________
void Style::unpolish(QWidget *widget)
{
// register widget to animations
_animations->unregisterWidget(widget);
_frameShadowFactory->unregisterWidget(widget);
_mdiWindowShadowFactory->unregisterWidget(widget);
_shadowHelper->unregisterWidget(widget);
_windowManager->unregisterWidget(widget);
_splitterFactory->unregisterWidget(widget);
_blurHelper->unregisterWidget(widget);
_toolsAreaManager->unregisterWidget(widget);
// remove event filter
if (qobject_cast<QAbstractScrollArea *>(widget) || qobject_cast<QDockWidget *>(widget) || qobject_cast<QMdiSubWindow *>(widget)
|| widget->inherits("QComboBoxPrivateContainer")) {
widget->removeEventFilter(this);
}
ParentStyleClass::unpolish(widget);
}
/// Find direct parent layout as the parentWidget()->layout() might not be
/// the direct parent layout but just a parent layout.
QLayout *findParentLayout(const QWidget *widget)
{
if (!widget->parentWidget()) {
return nullptr;
}
auto layout = widget->parentWidget()->layout();
if (!layout) {
return nullptr;
}
if (layout->indexOf(const_cast<QWidget *>(widget)) > -1) {
return layout;
}
QList<QObject *> children = layout->children();
while (!children.isEmpty()) {
layout = qobject_cast<QLayout *>(children.takeFirst());
if (!layout) {
continue;
}
if (layout->indexOf(const_cast<QWidget *>(widget)) > -1) {
return layout;
}
children += layout->children();
}
return nullptr;
}
//______________________________________________________________
int Style::pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
{
// handle special cases
switch (metric) {
case PM_MenuHMargin:
case PM_MenuVMargin:
return Metrics::MenuItem_HighlightGap;
// small icon size
case PM_SmallIconSize: {
auto iconSize = ParentStyleClass::pixelMetric(metric, option, widget);
if (!isTabletMode()) {
return iconSize;
}
// in tablet mode, we try to figure out the next size and use it
// see bug 455513
auto metaEnum = QMetaEnum::fromType<KIconLoader::StdSizes>();
for (int i = 0; i + 1 < metaEnum.keyCount(); ++i) {
if (iconSize == metaEnum.value(i)) {
return metaEnum.value(i + 1);
}
}
// size is either too large or unknown, just increase it by 50%
return iconSize * 3 / 2;
}
// frame width
case PM_DefaultFrameWidth: {
const auto isControl = isQtQuickControl(option, widget);
if (!widget && !isControl) {
return 0;
}
if (qobject_cast<const QMenu *>(widget)) {
return Metrics::Menu_FrameWidth;
}
if (qobject_cast<const QLineEdit *>(widget)) {
return Metrics::LineEdit_FrameWidth;
} else if (isControl) {
const QString &elementType = option->styleObject->property("elementType").toString();
if (elementType == QLatin1String("edit") || elementType == QLatin1String("spinbox")) {
return Metrics::LineEdit_FrameWidth;
} else if (elementType == QLatin1String("combobox")) {
return Metrics::ComboBox_FrameWidth;
}
return Metrics::Frame_FrameWidth;
}
const auto forceFrame = widget->property(PropertyNames::forceFrame);
if (forceFrame.isValid() && !forceFrame.toBool()) {
return 0;
}
if ((forceFrame.isValid() && forceFrame.toBool()) || widget->property(PropertyNames::bordersSides).isValid()) {
return Metrics::Frame_FrameWidth;
}
if (qobject_cast<const QAbstractScrollArea *>(widget)) {
auto layout = findParentLayout(widget);
if (!layout) {
if (widget->parentWidget() && widget->parentWidget()->layout()) {
layout = widget->parentWidget()->layout();
}
}
if (layout) {
if (layout->inherits("QDockWidgetLayout") || layout->inherits("QMainWindowLayout") || qobject_cast<const QStackedLayout *>(layout)) {
return 0;
}
if (auto grid = qobject_cast<const QGridLayout *>(layout)) {
if (grid->horizontalSpacing() > 0 || grid->verticalSpacing() > 0) {
return Metrics::Frame_FrameWidth;
}
}
// Add frame when scroll area is in a layout with more than an item and the
// layout has some spacing.
if (layout->spacing() > 0 && layout->count() > 1) {
return Metrics::Frame_FrameWidth;
}
}
}
if (qobject_cast<const QTabWidget *>(widget)) {
return Metrics::Frame_FrameWidth;
}
// fallback
return 0;
}
case PM_ComboBoxFrameWidth: {
const auto comboBoxOption(qstyleoption_cast<const QStyleOptionComboBox *>(option));
return comboBoxOption && comboBoxOption->editable ? Metrics::LineEdit_FrameWidth : Metrics::ComboBox_FrameWidth;
}
case PM_SpinBoxFrameWidth:
return Metrics::SpinBox_FrameWidth;
case PM_ToolBarFrameWidth:
return Metrics::ToolBar_FrameWidth;
case PM_ToolTipLabelFrameWidth:
return Metrics::ToolTip_FrameWidth;
case PM_FocusFrameVMargin:
case PM_FocusFrameHMargin:
return 2;
// layout
case PM_LayoutLeftMargin:
case PM_LayoutTopMargin:
case PM_LayoutRightMargin:
case PM_LayoutBottomMargin: {
/*
* use either Child margin or TopLevel margin,
* depending on widget type
*/
if ((option && (option->state & QStyle::State_Window)) || (widget && widget->isWindow())) {
return Metrics::Layout_TopLevelMarginWidth;
} else if (widget && widget->inherits("KPageView")) {
return 0;
} else {
return Metrics::Layout_ChildMarginWidth;
}
}
case PM_LayoutHorizontalSpacing:
return Metrics::Layout_DefaultSpacing;
case PM_LayoutVerticalSpacing:
return Metrics::Layout_DefaultSpacing;
// buttons
case PM_ButtonMargin: {
// needs special case for kcalc buttons, to prevent the application to set too small margins
if (widget && widget->inherits("KCalcButton")) {
return Metrics::Button_MarginWidth + 4;
} else {
return Metrics::Button_MarginWidth;
}
}
case PM_ButtonDefaultIndicator:
return 0;
case PM_ButtonShiftHorizontal:
return 0;
case PM_ButtonShiftVertical:
return 0;
// menubars
case PM_MenuBarPanelWidth:
return 0;
case PM_MenuBarHMargin:
return 0;
case PM_MenuBarVMargin:
return 0;
case PM_MenuBarItemSpacing:
return 0;
case PM_MenuDesktopFrameWidth:
return 0;
// menu buttons
case PM_MenuButtonIndicator:
return Metrics::MenuButton_IndicatorWidth;
// toolbars
case PM_ToolBarHandleExtent:
return Metrics::ToolBar_HandleExtent;
case PM_ToolBarSeparatorExtent:
return Metrics::ToolBar_SeparatorWidth;
case PM_ToolBarExtensionExtent:
return pixelMetric(PM_SmallIconSize, option, widget) + 2 * Metrics::ToolButton_MarginWidth;
case PM_ToolBarItemMargin:
return Metrics::ToolBar_ItemMargin;
case PM_ToolBarItemSpacing:
return Metrics::ToolBar_ItemSpacing;
// tabbars
case PM_TabBarTabShiftVertical:
return 0;
case PM_TabBarTabShiftHorizontal:
return 0;
case PM_TabBarTabOverlap:
return Metrics::TabBar_TabOverlap;
case PM_TabBarBaseOverlap:
return Metrics::TabBar_BaseOverlap;
case PM_TabBarTabHSpace:
return 2 * Metrics::TabBar_TabMarginWidth;
case PM_TabBarTabVSpace:
return 2 * Metrics::TabBar_TabMarginHeight;
case PM_TabCloseIndicatorWidth:
case PM_TabCloseIndicatorHeight:
return pixelMetric(PM_SmallIconSize, option, widget);
// scrollbars
case PM_ScrollBarExtent:
return Metrics::ScrollBar_Extend;
case PM_ScrollBarSliderMin:
return Metrics::ScrollBar_MinSliderHeight;
// title bar
case PM_TitleBarHeight:
return 2 * Metrics::TitleBar_MarginWidth + pixelMetric(PM_SmallIconSize, option, widget);
// sliders
case PM_SliderThickness:
return Metrics::Slider_ControlThickness;
case PM_SliderControlThickness:
return Metrics::Slider_ControlThickness;
case PM_SliderLength:
return Metrics::Slider_ControlThickness;
// checkboxes and radio buttons
case PM_IndicatorWidth:
case PM_IndicatorHeight:
case PM_ExclusiveIndicatorWidth:
case PM_ExclusiveIndicatorHeight:
return Metrics::CheckBox_Size;
case PM_CheckBoxLabelSpacing:
case PM_RadioButtonLabelSpacing:
return Metrics::CheckBox_ItemSpacing;
// list headers
case PM_HeaderMarkSize:
return Metrics::Header_ArrowSize;
case PM_HeaderMargin:
return Metrics::Header_MarginWidth;
// dock widget
// return 0 here, since frame is handled directly in polish
case PM_DockWidgetFrameWidth:
return 0;
case PM_DockWidgetTitleMargin:
return Metrics::Frame_FrameWidth;
case PM_DockWidgetTitleBarButtonMargin:
return Metrics::ToolButton_MarginWidth;
case PM_SplitterWidth:
return Metrics::Splitter_SplitterWidth;
case PM_DockWidgetSeparatorExtent:
return Metrics::Splitter_SplitterWidth;
// fallback
default:
return ParentStyleClass::pixelMetric(metric, option, widget);
}
}
//______________________________________________________________
int Style::styleHint(StyleHint hint, const QStyleOption *option, const QWidget *widget, QStyleHintReturn *returnData) const
{
switch (hint) {
case SH_RubberBand_Mask: {
if (auto mask = qstyleoption_cast<QStyleHintReturnMask *>(returnData)) {
mask->region = option->rect;
/*
* need to check on widget before removing inner region
* in order to still preserve rubberband in MainWindow and QGraphicsView
* in QMainWindow because it looks better
* in QGraphicsView because the painting fails completely otherwise
*/
if (widget
&& (qobject_cast<const QAbstractItemView *>(widget->parent()) || qobject_cast<const QGraphicsView *>(widget->parent())
|| qobject_cast<const QMainWindow *>(widget->parent()))) {
return true;
}
// also check if widget's parent is some itemView viewport
if (widget && widget->parent() && qobject_cast<const QAbstractItemView *>(widget->parent()->parent())
&& static_cast<const QAbstractItemView *>(widget->parent()->parent())->viewport() == widget->parent()) {
return true;
}
// mask out center
mask->region -= insideMargin(option->rect, 1);
return true;
}
return false;
}
case SH_ComboBox_ListMouseTracking:
return true;
case SH_MenuBar_MouseTracking:
return true;
case SH_Menu_Scrollable:
return true;
case SH_Menu_MouseTracking:
return true;
case SH_Menu_SubMenuPopupDelay:
return 150;
case SH_Menu_SloppySubMenus:
return true;
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
case SH_Widget_Animate:
return StyleConfigData::animationsEnabled();
#endif
case SH_Menu_SupportsSections:
return true;
case SH_Widget_Animation_Duration:
return StyleConfigData::animationsEnabled() ? StyleConfigData::animationsDuration() : 0;
case SH_DialogButtonBox_ButtonsHaveIcons:
return true;
case SH_GroupBox_TextLabelVerticalAlignment:
return Qt::AlignVCenter;
case SH_TabBar_Alignment:
return StyleConfigData::tabBarDrawCenteredTabs() ? Qt::AlignCenter : Qt::AlignLeft;
case SH_ToolBox_SelectedPageTitleBold:
return false;
case SH_ScrollBar_MiddleClickAbsolutePosition:
return true;
case SH_ScrollView_FrameOnlyAroundContents:
return false;
case SH_FormLayoutFormAlignment:
return Qt::AlignLeft | Qt::AlignTop;
case SH_FormLayoutLabelAlignment:
return Qt::AlignRight;
case SH_FormLayoutFieldGrowthPolicy:
return QFormLayout::ExpandingFieldsGrow;
case SH_FormLayoutWrapPolicy:
return QFormLayout::DontWrapRows;
case SH_MessageBox_TextInteractionFlags:
return Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse;
case SH_ProgressDialog_CenterCancelButton:
return false;
case SH_MessageBox_CenterButtons:
return false;
case SH_FocusFrame_AboveWidget:
return true;
case SH_FocusFrame_Mask:
return false;