-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathbreezehelper.cpp
1925 lines (1634 loc) · 66.8 KB
/
breezehelper.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-License-Identifier: GPL-2.0-or-later
*/
#include "breezehelper.h"
#include "breeze.h"
#include "breezepropertynames.h"
#include <KColorScheme>
#include <KColorUtils>
#include <KIconLoader>
#include <KWindowSystem>
#include <qobject.h>
#if __has_include(<KX11Extras>)
#include <KX11Extras>
#endif
#include <QApplication>
#include <QDockWidget>
#include <QFileInfo>
#include <QMainWindow>
#include <QMdiArea>
#include <QMenuBar>
#include <QPainter>
#include <QStyleOption>
#include <QWindow>
#include <QDialog>
#include <algorithm>
namespace Breeze
{
//* contrast for arrow and treeline rendering
static const qreal arrowShade = 0.15;
static const qreal highlightBackgroundAlpha = 0.33;
static const auto radioCheckSunkenDarkeningFactor = 110;
PaletteChangedEventFilter::PaletteChangedEventFilter(Helper *helper)
: QObject(helper)
, _helper(helper)
{
}
bool PaletteChangedEventFilter::eventFilter(QObject *watched, QEvent *event)
{
if (event->type() != QEvent::ApplicationPaletteChange || watched != qApp) {
return QObject::eventFilter(watched, event);
}
if (!qApp->property("KDE_COLOR_SCHEME_PATH").isValid()) {
return QObject::eventFilter(watched, event);
}
const auto path = qApp->property("KDE_COLOR_SCHEME_PATH").toString();
if (!path.isEmpty()) {
KConfig config(path, KConfig::SimpleConfig);
KConfigGroup group(config.group(QStringLiteral("WM")));
const QPalette palette(QApplication::palette());
_helper->_activeTitleBarColor = group.readEntry("activeBackground", palette.color(QPalette::Active, QPalette::Highlight));
_helper->_activeTitleBarTextColor = group.readEntry("activeForeground", palette.color(QPalette::Active, QPalette::HighlightedText));
_helper->_inactiveTitleBarColor = group.readEntry("inactiveBackground", palette.color(QPalette::Disabled, QPalette::Highlight));
_helper->_inactiveTitleBarTextColor = group.readEntry("inactiveForeground", palette.color(QPalette::Disabled, QPalette::HighlightedText));
}
return QObject::eventFilter(watched, event);
}
//____________________________________________________________________
Helper::Helper(KSharedConfig::Ptr config)
: QObject()
, _config(std::move(config))
, _kwinConfig(KSharedConfig::openConfig("kwinrc"))
, _decorationConfig(new InternalSettings())
, _eventFilter(new PaletteChangedEventFilter(this))
{
}
//____________________________________________________________________
KSharedConfig::Ptr Helper::config() const
{
return _config;
}
//____________________________________________________________________
QSharedPointer<InternalSettings> Helper::decorationConfig() const
{
return _decorationConfig;
}
//____________________________________________________________________
void Helper::loadConfig()
{
_viewFocusBrush = KStatefulBrush(KColorScheme::View, KColorScheme::FocusColor);
_viewHoverBrush = KStatefulBrush(KColorScheme::View, KColorScheme::HoverColor);
_buttonFocusBrush = KStatefulBrush(KColorScheme::Button, KColorScheme::FocusColor);
_buttonHoverBrush = KStatefulBrush(KColorScheme::Button, KColorScheme::HoverColor);
_viewNegativeTextBrush = KStatefulBrush(KColorScheme::View, KColorScheme::NegativeText);
_viewNeutralTextBrush = KStatefulBrush(KColorScheme::View, KColorScheme::NeutralText);
const QPalette palette(QApplication::palette());
_config->reparseConfiguration();
_kwinConfig->reparseConfiguration();
_cachedAutoValid = false;
_decorationConfig->load();
KConfigGroup globalGroup(_config->group(QStringLiteral("WM")));
_activeTitleBarColor = globalGroup.readEntry("activeBackground", palette.color(QPalette::Active, QPalette::Highlight));
_activeTitleBarTextColor = globalGroup.readEntry("activeForeground", palette.color(QPalette::Active, QPalette::HighlightedText));
_inactiveTitleBarColor = globalGroup.readEntry("inactiveBackground", palette.color(QPalette::Disabled, QPalette::Highlight));
_inactiveTitleBarTextColor = globalGroup.readEntry("inactiveForeground", palette.color(QPalette::Disabled, QPalette::HighlightedText));
if (const QString colorSchemePath = qApp->property("KDE_COLOR_SCHEME_PATH").toString(); !colorSchemePath.isEmpty()) {
KConfig config(colorSchemePath, KConfig::SimpleConfig);
KConfigGroup appGroup(config.group(QStringLiteral("WM")));
_activeTitleBarColor = appGroup.readEntry("activeBackground", _activeTitleBarColor);
_activeTitleBarTextColor = appGroup.readEntry("activeForeground", _activeTitleBarTextColor);
_inactiveTitleBarColor = appGroup.readEntry("inactiveBackground", _inactiveTitleBarColor);
_inactiveTitleBarTextColor = appGroup.readEntry("inactiveForeground", _inactiveTitleBarTextColor);
}
}
void Helper::installEventFilter(QApplication *app) const
{
if (app) {
app->installEventFilter(_eventFilter);
}
}
void Helper::removeEventFilter(QApplication *app) const
{
if (app) {
app->removeEventFilter(_eventFilter);
}
}
QColor transparentize(const QColor &color, qreal amount)
{
auto clone = color;
clone.setAlphaF(amount);
return clone;
}
//____________________________________________________________________
QColor Helper::frameOutlineColor(const QPalette &palette, bool mouseOver, bool hasFocus, qreal opacity, AnimationMode mode) const
{
QColor outline(KColorUtils::mix(palette.color(QPalette::Window), palette.color(QPalette::WindowText), Metrics::Bias_Default));
// focus takes precedence over hover
if (mode == AnimationFocus) {
const QColor focus(focusColor(palette));
const QColor hover(hoverColor(palette));
if (mouseOver) {
outline = KColorUtils::mix(hover, focus, opacity);
} else {
outline = KColorUtils::mix(outline, focus, opacity);
}
} else if (hasFocus) {
outline = focusColor(palette);
} else if (mode == AnimationHover) {
const QColor hover(hoverColor(palette));
outline = KColorUtils::mix(outline, hover, opacity);
} else if (mouseOver) {
outline = hoverColor(palette);
}
return outline;
}
//____________________________________________________________________
QColor Helper::focusOutlineColor(const QPalette &palette) const
{
return KColorUtils::mix(focusColor(palette), palette.color(QPalette::WindowText), 0.15);
}
//____________________________________________________________________
QColor Helper::hoverOutlineColor(const QPalette &palette) const
{
return KColorUtils::mix(hoverColor(palette), palette.color(QPalette::WindowText), 0.15);
}
//____________________________________________________________________
QColor Helper::buttonFocusOutlineColor(const QPalette &palette) const
{
return KColorUtils::mix(buttonFocusColor(palette), palette.color(QPalette::ButtonText), 0.15);
}
//____________________________________________________________________
QColor Helper::buttonHoverOutlineColor(const QPalette &palette) const
{
return KColorUtils::mix(buttonHoverColor(palette), palette.color(QPalette::ButtonText), 0.15);
}
//____________________________________________________________________
QColor Helper::sidePanelOutlineColor(const QPalette &palette, bool hasFocus, qreal opacity, AnimationMode mode) const
{
QColor outline(palette.color(QPalette::Inactive, QPalette::Highlight));
const QColor &focus = palette.color(QPalette::Active, QPalette::Highlight);
if (mode == AnimationFocus) {
outline = KColorUtils::mix(outline, focus, opacity);
} else if (hasFocus) {
outline = focus;
}
return outline;
}
//____________________________________________________________________
QColor Helper::frameBackgroundColor(const QPalette &palette, QPalette::ColorGroup group) const
{
return KColorUtils::mix(palette.color(group, QPalette::Window), palette.color(group, QPalette::Base), 0.3);
}
//____________________________________________________________________
QColor Helper::arrowColor(const QPalette &palette, QPalette::ColorGroup group, QPalette::ColorRole role) const
{
switch (role) {
case QPalette::Text:
return KColorUtils::mix(palette.color(group, QPalette::Text), palette.color(group, QPalette::Base), arrowShade);
case QPalette::WindowText:
return KColorUtils::mix(palette.color(group, QPalette::WindowText), palette.color(group, QPalette::Window), arrowShade);
case QPalette::ButtonText:
return KColorUtils::mix(palette.color(group, QPalette::ButtonText), palette.color(group, QPalette::Button), arrowShade);
default:
return palette.color(group, role);
}
}
//____________________________________________________________________
QColor Helper::arrowColor(const QPalette &palette, bool mouseOver, bool hasFocus, qreal opacity, AnimationMode mode) const
{
QColor outline(arrowColor(palette, QPalette::WindowText));
if (mode == AnimationHover) {
const QColor focus(focusColor(palette));
const QColor hover(hoverColor(palette));
if (hasFocus) {
outline = KColorUtils::mix(focus, hover, opacity);
} else {
outline = KColorUtils::mix(outline, hover, opacity);
}
} else if (mouseOver) {
outline = hoverColor(palette);
} else if (mode == AnimationFocus) {
const QColor focus(focusColor(palette));
outline = KColorUtils::mix(outline, focus, opacity);
} else if (hasFocus) {
outline = focusColor(palette);
}
return outline;
}
//____________________________________________________________________
QColor Helper::sliderOutlineColor(const QPalette &palette, bool mouseOver, bool hasFocus, qreal opacity, AnimationMode mode) const
{
QColor outline(KColorUtils::mix(palette.color(QPalette::Button), palette.color(QPalette::ButtonText), Metrics::Bias_Default));
// hover takes precedence over focus
if (mode == AnimationHover) {
const QColor hover(hoverColor(palette));
const QColor focus(focusColor(palette));
if (hasFocus) {
outline = KColorUtils::mix(focus, hover, opacity);
} else {
outline = KColorUtils::mix(outline, hover, opacity);
}
} else if (mouseOver) {
outline = hoverColor(palette);
} else if (mode == AnimationFocus) {
const QColor focus(focusColor(palette));
outline = KColorUtils::mix(outline, focus, opacity);
} else if (hasFocus) {
outline = focusColor(palette);
}
return outline;
}
//____________________________________________________________________
QColor Helper::scrollBarHandleColor(const QPalette &palette, bool mouseOver, bool hasFocus, qreal opacity, AnimationMode mode) const
{
QColor color(alphaColor(palette.color(QPalette::WindowText), 0.5));
// hover takes precedence over focus
if (mode == AnimationHover) {
const QColor hover(hoverColor(palette));
const QColor focus(focusColor(palette));
if (hasFocus) {
color = KColorUtils::mix(focus, hover, opacity);
} else {
color = KColorUtils::mix(color, hover, opacity);
}
} else if (mouseOver) {
color = hoverColor(palette);
} else if (mode == AnimationFocus) {
const QColor focus(focusColor(palette));
color = KColorUtils::mix(color, focus, opacity);
} else if (hasFocus) {
color = focusColor(palette);
}
return color;
}
//______________________________________________________________________________
QColor Helper::checkBoxIndicatorColor(const QPalette &palette, bool mouseOver, bool active, qreal opacity, AnimationMode mode) const
{
QColor color(KColorUtils::mix(palette.color(QPalette::Window), palette.color(QPalette::WindowText), 0.6));
if (mode == AnimationHover) {
const QColor focus(focusColor(palette));
const QColor hover(hoverColor(palette));
if (active) {
color = KColorUtils::mix(focus, hover, opacity);
} else {
color = KColorUtils::mix(color, hover, opacity);
}
} else if (mouseOver) {
color = hoverColor(palette);
} else if (active) {
color = focusColor(palette);
}
return color;
}
//______________________________________________________________________________
QColor Helper::separatorColor(const QPalette &palette) const
{
return KColorUtils::mix(palette.color(QPalette::Window), palette.color(QPalette::WindowText), Metrics::Bias_Default);
}
//______________________________________________________________________________
QPalette Helper::disabledPalette(const QPalette &source, qreal ratio) const
{
QPalette copy(source);
const QList<QPalette::ColorRole> roles =
{QPalette::Window, QPalette::Highlight, QPalette::WindowText, QPalette::ButtonText, QPalette::Text, QPalette::Button};
for (const QPalette::ColorRole &role : roles) {
copy.setColor(role, KColorUtils::mix(source.color(QPalette::Active, role), source.color(QPalette::Disabled, role), 1.0 - ratio));
}
return copy;
}
//____________________________________________________________________
QColor Helper::alphaColor(QColor color, qreal alpha) const
{
if (alpha >= 0 && alpha < 1.0) {
color.setAlphaF(alpha * color.alphaF());
}
return color;
}
//______________________________________________________________________________
void Helper::renderDebugFrame(QPainter *painter, const QRectF &rect) const
{
painter->save();
painter->setRenderHints(QPainter::Antialiasing);
painter->setBrush(Qt::NoBrush);
painter->setPen(Qt::red);
painter->drawRect(strokedRect(rect));
painter->restore();
}
//______________________________________________________________________________
void Helper::renderFocusRect(QPainter *painter, const QRectF &rect, const QColor &color, const QColor &outline, Sides sides) const
{
if (!color.isValid()) {
return;
}
painter->save();
painter->setRenderHints(QPainter::Antialiasing);
painter->setBrush(color);
if (!(outline.isValid() && sides)) {
painter->setPen(Qt::NoPen);
painter->drawRect(rect);
} else {
painter->setClipRect(rect);
QRectF copy(strokedRect(rect));
const qreal radius(frameRadius(PenWidth::Frame));
if (!(sides & SideTop)) {
copy.adjust(0, -radius, 0, 0);
}
if (!(sides & SideBottom)) {
copy.adjust(0, 0, 0, radius);
}
if (!(sides & SideLeft)) {
copy.adjust(-radius, 0, 0, 0);
}
if (!(sides & SideRight)) {
copy.adjust(0, 0, radius, 0);
}
painter->setPen(outline);
// painter->setBrush( Qt::NoBrush );
painter->drawRoundedRect(copy, radius, radius);
}
painter->restore();
}
//______________________________________________________________________________
void Helper::renderFocusLine(QPainter *painter, const QRectF &rect, const QColor &color) const
{
if (!color.isValid()) {
return;
}
painter->save();
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setBrush(Qt::NoBrush);
painter->setPen(color);
painter->translate(0, 2);
painter->drawLine(rect.bottomLeft(), rect.bottomRight());
painter->restore();
}
//______________________________________________________________________________
void Helper::renderFrameWithSides(QPainter *painter, const QRectF &rect, const QColor &color, Qt::Edges edges, const QColor &outline) const
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
QRectF frameRect(rect);
// set brush
painter->setBrush(color);
painter->setPen(Qt::NoPen);
// render
painter->drawRect(frameRect);
// set brush again
painter->setBrush(Qt::NoBrush);
painter->setPen(outline);
// manually apply the effects of StrokedRect here but only to the edges with a frame
if (edges & Qt::LeftEdge) {
frameRect.adjust(0.5, 0.0, 0.0, 0.0);
}
if (edges & Qt::RightEdge) {
frameRect.adjust(0.0, 0, -0.5, 0.0);
}
if (edges & Qt::TopEdge) {
frameRect.adjust(0.0, 0.5, 0.0, 0.0);
}
if (edges & Qt::BottomEdge) {
frameRect.adjust(0.0, 0.0, 0.0, -0.5);
}
// draw lines
if (edges & Qt::LeftEdge) {
painter->drawLine(QLineF(frameRect.topLeft(), frameRect.bottomLeft()));
}
if (edges & Qt::RightEdge) {
painter->drawLine(QLineF(frameRect.topRight(), frameRect.bottomRight()));
}
if (edges & Qt::TopEdge) {
painter->drawLine(QLineF(frameRect.topLeft(), frameRect.topRight()));
}
if (edges & Qt::BottomEdge) {
painter->drawLine(QLineF(frameRect.bottomLeft(), frameRect.bottomRight()));
}
painter->restore();
}
//______________________________________________________________________________
void Helper::renderFrame(QPainter *painter, const QRectF &rect, const QColor &color, const QColor &outline) const
{
painter->setRenderHint(QPainter::Antialiasing);
QRectF frameRect(rect);
qreal radius(frameRadius(PenWidth::NoPen));
// set pen
if (outline.isValid()) {
painter->setPen(outline);
frameRect = strokedRect(frameRect);
radius = frameRadiusForNewPenWidth(radius, PenWidth::Frame);
} else {
painter->setPen(Qt::NoPen);
}
// set brush
if (color.isValid()) {
painter->setBrush(color);
} else {
painter->setBrush(Qt::NoBrush);
}
// render
painter->drawRoundedRect(frameRect, radius, radius);
}
//______________________________________________________________________________
void Helper::renderSidePanelFrame(QPainter *painter, const QRectF &rect, const QColor &outline, Side side) const
{
// check color
if (!outline.isValid()) {
return;
}
// adjust rect
QRectF frameRect(strokedRect(rect));
// setup painter
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(outline);
// render
switch (side) {
default:
case SideLeft:
painter->drawLine(frameRect.topRight(), frameRect.bottomRight());
break;
case SideTop:
painter->drawLine(frameRect.topLeft(), frameRect.topRight());
break;
case SideRight:
painter->drawLine(frameRect.topLeft(), frameRect.bottomLeft());
break;
case SideBottom:
painter->drawLine(frameRect.bottomLeft(), frameRect.bottomRight());
break;
case AllSides: {
const qreal radius(frameRadius(PenWidth::Frame));
painter->drawRoundedRect(frameRect, radius, radius);
break;
}
}
}
//______________________________________________________________________________
void Helper::renderMenuFrame(QPainter *painter, const QRectF &rect, const QColor &color, const QColor &outline, bool roundCorners, Qt::Edges seamlessEdges)
const
{
painter->save();
// set brush
if (color.isValid()) {
painter->setBrush(color);
} else {
painter->setBrush(Qt::NoBrush);
}
// We simulate being able to independently adjust corner radii by
// setting a clip region and then extending the rectangle beyond it.
if (seamlessEdges != Qt::Edges()) {
painter->setClipRect(rect);
}
if (roundCorners) {
painter->setRenderHint(QPainter::Antialiasing);
QRectF frameRect(rect);
qreal radius(Metrics::Frame_FrameRadius);
frameRect.adjust( //
seamlessEdges.testFlag(Qt::LeftEdge) ? -radius : 0,
seamlessEdges.testFlag(Qt::TopEdge) ? -radius : 0,
seamlessEdges.testFlag(Qt::RightEdge) ? radius : 0,
seamlessEdges.testFlag(Qt::BottomEdge) ? radius : 0);
// set pen
if (outline.isValid()) {
painter->setPen(outline);
frameRect = strokedRect(frameRect);
radius = frameRadiusForNewPenWidth(radius, PenWidth::Frame);
} else {
painter->setPen(Qt::NoPen);
}
// render
painter->drawRoundedRect(frameRect, radius, radius);
} else {
painter->setRenderHint(QPainter::Antialiasing, false);
QRectF frameRect(rect);
frameRect.adjust( //
seamlessEdges.testFlag(Qt::LeftEdge) ? 1 : 0,
seamlessEdges.testFlag(Qt::TopEdge) ? 1 : 0,
seamlessEdges.testFlag(Qt::RightEdge) ? -1 : 0,
seamlessEdges.testFlag(Qt::BottomEdge) ? -1 : 0);
if (outline.isValid()) {
painter->setPen(outline);
frameRect.adjust(0, 0, -1, -1);
} else {
painter->setPen(Qt::NoPen);
}
painter->drawRect(frameRect);
}
painter->restore();
}
QRegion Helper::menuFrameRegion(const QMenu *widget)
{
if (!widget) {
return {};
}
const bool hasAlpha(hasAlphaChannel(widget));
const auto seamlessEdges = menuSeamlessEdges(widget);
const auto roundCorners = hasAlpha;
if (roundCorners) {
QRectF frameRect(widget->rect());
qreal radius(Metrics::Frame_FrameRadius);
frameRect.adjust( //
seamlessEdges.testFlag(Qt::LeftEdge) ? -radius : 0,
seamlessEdges.testFlag(Qt::TopEdge) ? -radius : 0,
seamlessEdges.testFlag(Qt::RightEdge) ? radius : 0,
seamlessEdges.testFlag(Qt::BottomEdge) ? radius : 0);
// outline is always valid/drawn
frameRect = strokedRect(frameRect);
radius = frameRadiusForNewPenWidth(radius, PenWidth::Frame);
QPainterPath path;
path.addRoundedRect(frameRect, radius, radius);
return QRegion(path.toFillPolygon().toPolygon()).intersected(widget->rect());
}
return QRegion(widget->rect());
}
//______________________________________________________________________________
void Helper::renderButtonFrame(QPainter *painter,
const QRectF &rect,
const QPalette &palette,
const QHash<QByteArray, bool> &stateProperties,
qreal bgAnimation,
qreal penAnimation) const
{
bool enabled = stateProperties.value("enabled", true);
bool visualFocus = stateProperties.value("visualFocus");
bool hovered = stateProperties.value("hovered");
bool down = stateProperties.value("down");
bool checked = stateProperties.value("checked");
bool flat = stateProperties.value("flat");
bool defaultButton = stateProperties.value("defaultButton");
bool hasNeutralHighlight = stateProperties.value("hasNeutralHighlight");
bool isActiveWindow = stateProperties.value("isActiveWindow");
// don't render background if flat and not hovered, down, checked, or given visual focus
if (flat && !(hovered || down || checked || visualFocus) && bgAnimation == AnimationData::OpacityInvalid && penAnimation == AnimationData::OpacityInvalid) {
return;
}
QRectF shadowedRect = this->shadowedRect(rect);
QRectF frameRect = strokedRect(shadowedRect);
qreal radius = frameRadius(PenWidth::Frame);
// setting color group to work around KColorScheme feature
const QColor &highlightColor = palette.color(!enabled ? QPalette::Disabled : QPalette::Active, QPalette::Highlight);
QBrush bgBrush;
QBrush penBrush;
// Colors
if (flat) {
if (down && enabled) {
bgBrush = alphaColor(highlightColor, highlightBackgroundAlpha);
} else if (checked) {
bgBrush = hasNeutralHighlight ? alphaColor(neutralText(palette), highlightBackgroundAlpha) : alphaColor(palette.buttonText().color(), 0.125);
penBrush =
hasNeutralHighlight ? neutralText(palette) : KColorUtils::mix(palette.button().color(), palette.buttonText().color(), Metrics::Bias_Default);
} else if (isActiveWindow && defaultButton) {
bgBrush = alphaColor(highlightColor, 0.125);
penBrush = KColorUtils::mix(highlightColor, KColorUtils::mix(palette.button().color(), palette.buttonText().color(), Metrics::Bias_Default), 0.5);
} else {
bgBrush = alphaColor(highlightColor, 0);
penBrush = hasNeutralHighlight ? neutralText(palette) : bgBrush;
}
} else {
if (down && enabled) {
bgBrush = KColorUtils::mix(palette.button().color(), highlightColor, 0.333);
} else if (checked) {
bgBrush = hasNeutralHighlight ? KColorUtils::mix(palette.button().color(), neutralText(palette), 0.333)
: KColorUtils::mix(palette.button().color(), palette.buttonText().color(), 0.125);
penBrush =
hasNeutralHighlight ? neutralText(palette) : KColorUtils::mix(palette.button().color(), palette.buttonText().color(), Metrics::Bias_Default);
} else if (isActiveWindow && defaultButton) {
bgBrush = KColorUtils::mix(palette.button().color(), highlightColor, 0.2);
penBrush = KColorUtils::mix(highlightColor, KColorUtils::mix(palette.button().color(), palette.buttonText().color(), Metrics::Bias_Default), 0.5);
} else {
bgBrush = palette.button().color();
penBrush =
hasNeutralHighlight ? neutralText(palette) : KColorUtils::mix(palette.button().color(), palette.buttonText().color(), Metrics::Bias_Default);
}
}
if ((hovered || visualFocus || down) && enabled) {
penBrush = highlightColor;
}
// Animations
if (bgAnimation != AnimationData::OpacityInvalid && enabled) {
QColor color1 = bgBrush.color();
QColor color2 = flat ? alphaColor(highlightColor, highlightBackgroundAlpha) : KColorUtils::mix(palette.button().color(), highlightColor, 0.333);
bgBrush = KColorUtils::mix(color1, color2, bgAnimation);
}
if (penAnimation != AnimationData::OpacityInvalid && enabled) {
QColor color1 = penBrush.color();
QColor color2 = highlightColor;
penBrush = KColorUtils::mix(color1, color2, penAnimation);
}
// Shadow
if (isActiveWindow && !(flat || down || checked) && enabled) {
renderRoundedRectShadow(painter, shadowedRect, shadowColor(palette));
}
// Render button
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setBrush(bgBrush);
painter->setPen(QPen(penBrush, PenWidth::Frame));
painter->drawRoundedRect(frameRect, radius, radius);
}
//______________________________________________________________________________
void Helper::renderToolBoxFrame(QPainter *painter, const QRectF &rect, int tabWidth, const QColor &outline) const
{
if (!outline.isValid()) {
return;
}
// round radius
const qreal radius(frameRadius(PenWidth::Frame));
const QSizeF cornerSize(2 * radius, 2 * radius);
// if rect - tabwidth is even, need to increase tabWidth by 1 unit
// for anti aliasing
if (!((rect.toRect().width() - tabWidth) % 2)) {
++tabWidth;
}
// adjust rect for antialiasing
QRectF baseRect(strokedRect(rect));
// create path
QPainterPath path;
path.moveTo(0, baseRect.height() - 1);
path.lineTo((baseRect.width() - tabWidth) / 2 - radius, baseRect.height() - 1);
path.arcTo(QRectF(QPointF((baseRect.width() - tabWidth) / 2 - 2 * radius, baseRect.height() - 1 - 2 * radius), cornerSize), 270, 90);
path.lineTo((baseRect.width() - tabWidth) / 2, radius);
path.arcTo(QRectF(QPointF((baseRect.width() - tabWidth) / 2, 0), cornerSize), 180, -90);
path.lineTo((baseRect.width() + tabWidth) / 2 - 1 - radius, 0);
path.arcTo(QRectF(QPointF((baseRect.width() + tabWidth) / 2 - 1 - 2 * radius, 0), cornerSize), 90, -90);
path.lineTo((baseRect.width() + tabWidth) / 2 - 1, baseRect.height() - 1 - radius);
path.arcTo(QRectF(QPointF((baseRect.width() + tabWidth) / 2 - 1, baseRect.height() - 1 - 2 * radius), cornerSize), 180, 90);
path.lineTo(baseRect.width() - 1, baseRect.height() - 1);
// render
painter->setRenderHints(QPainter::Antialiasing);
painter->setBrush(Qt::NoBrush);
painter->setPen(outline);
painter->translate(baseRect.topLeft());
painter->drawPath(path);
}
//______________________________________________________________________________
void Helper::renderTabWidgetFrame(QPainter *painter, const QRectF &rect, const QColor &color, const QColor &outline, Corners corners) const
{
painter->setRenderHint(QPainter::Antialiasing);
QRectF frameRect(rect.adjusted(1, 1, -1, -1));
qreal radius(frameRadius(PenWidth::NoPen));
// set pen
if (outline.isValid()) {
painter->setPen(outline);
frameRect = strokedRect(frameRect);
radius = frameRadiusForNewPenWidth(radius, PenWidth::Frame);
} else {
painter->setPen(Qt::NoPen);
}
// set brush
if (color.isValid()) {
painter->setBrush(color);
} else {
painter->setBrush(Qt::NoBrush);
}
// render
QPainterPath path(roundedPath(frameRect, corners, radius));
painter->drawPath(path);
}
//______________________________________________________________________________
void Helper::renderSelection(QPainter *painter, const QRectF &rect, const QColor &color) const
{
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
painter->setBrush(color);
painter->drawRect(rect);
}
//______________________________________________________________________________
void Helper::renderSeparator(QPainter *painter, const QRectF &rect, const QColor &color, bool vertical) const
{
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setBrush(Qt::NoBrush);
painter->setPen(color);
if (vertical) {
painter->translate(rect.width() / 2, 0);
painter->drawLine(rect.topLeft(), rect.bottomLeft());
} else {
painter->translate(0, rect.height() / 2);
painter->drawLine(rect.topLeft(), rect.topRight());
}
}
//______________________________________________________________________________
void Helper::renderCheckBoxBackground(QPainter *painter,
const QRectF &rect,
const QPalette &palette,
CheckBoxState state,
bool neutalHighlight,
bool sunken,
qreal animation) const
{
// setup painter
painter->setRenderHint(QPainter::Antialiasing, true);
// copy rect
QRectF frameRect(rect);
frameRect.adjust(2, 2, -2, -2);
frameRect = strokedRect(frameRect);
auto transparent = neutalHighlight ? neutralText(palette) : palette.highlight().color();
transparent.setAlphaF(highlightBackgroundAlpha);
QBrush penBrush;
if (neutalHighlight) {
penBrush = neutralText(palette);
} else if (state == CheckOn || state == CheckPartial) {
penBrush = palette.highlight().color();
} else {
penBrush = separatorColor(palette);
}
painter->setPen(QPen(penBrush, PenWidth::Frame));
const auto radius = Metrics::CheckBox_Radius;
switch (state) {
case CheckOff:
painter->setBrush(palette.button().color().darker(sunken ? radioCheckSunkenDarkeningFactor : 100));
painter->drawRoundedRect(frameRect, radius, radius);
break;
case CheckPartial:
case CheckOn:
painter->setBrush(transparent.darker(sunken ? radioCheckSunkenDarkeningFactor : 100));
painter->drawRoundedRect(frameRect, radius, radius);
break;
case CheckAnimated:
painter->setBrush(palette.button().color().darker(sunken ? radioCheckSunkenDarkeningFactor : 100));
painter->drawRoundedRect(frameRect, radius, radius);
painter->setBrush(transparent);
painter->setOpacity(animation);
painter->drawRoundedRect(frameRect, radius, radius);
break;
}
}
//______________________________________________________________________________
void Helper::renderCheckBox(QPainter *painter,
const QRectF &rect,
const QPalette &palette,
bool mouseOver,
CheckBoxState state,
CheckBoxState target,
bool neutalHighlight,
bool sunken,
qreal animation,
qreal hoverAnimation) const
{
Q_UNUSED(sunken)
// setup painter
painter->setRenderHint(QPainter::Antialiasing, true);
// copy rect and radius
QRectF frameRect(rect);
frameRect.adjust(2, 2, -2, -2);
if (mouseOver) {
painter->save();
if (hoverAnimation != AnimationData::OpacityInvalid) {
painter->setOpacity(hoverAnimation);
}
painter->setPen(QPen(neutalHighlight ? neutralText(palette).lighter() : focusColor(palette), PenWidth::Frame));
painter->setBrush(Qt::NoBrush);
painter->drawRoundedRect(frameRect.adjusted(0.5, 0.5, -0.5, -0.5), Metrics::CheckBox_Radius, Metrics::CheckBox_Radius);
painter->restore();
}
// check
auto leftPoint = frameRect.center();
leftPoint.setX(frameRect.left() + 4);
auto bottomPoint = frameRect.center();
bottomPoint.setX(bottomPoint.x() - 1);
bottomPoint.setY(frameRect.bottom() - 5);
auto rightPoint = frameRect.center();
rightPoint.setX(rightPoint.x() + 4.5);
rightPoint.setY(frameRect.top() + 5.5);
QPainterPath path;
path.moveTo(leftPoint);
path.lineTo(bottomPoint);
path.lineTo(rightPoint);
// dots
auto centerDot = QRectF(frameRect.center(), QSize(2, 2));
centerDot.adjust(-1, -1, -1, -1);
auto leftDot = centerDot.adjusted(-4, 0, -4, 0);
auto rightDot = centerDot.adjusted(4, 0, 4, 0);
painter->setPen(Qt::transparent);
painter->setBrush(Qt::transparent);
auto checkPen = QPen(palette.text(), PenWidth::Frame * 2);
checkPen.setJoinStyle(Qt::MiterJoin);
switch (state) {
case CheckOff:
break;
case CheckOn:
painter->setPen(checkPen);
painter->drawPath(path);
break;
case CheckPartial:
painter->setBrush(palette.text());
painter->drawRect(leftDot);
painter->drawRect(centerDot);
painter->drawRect(rightDot);
break;
case CheckAnimated:
checkPen.setDashPattern({path.length() * animation, path.length()});
switch (target) {
case CheckOff:
break;
case CheckOn:
painter->setPen(checkPen);
painter->drawPath(path);
break;
case CheckPartial:
if (animation >= 3.0 / 3.0) {
painter->drawRect(rightDot);
}