-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvirtuality.cpp
1263 lines (1156 loc) · 47.5 KB
/
virtuality.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
/*
* Virtuality Style for Qt4 and Qt5
* Copyright 2009-2014 by Thomas Lübking <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
*
* 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 General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**================== Qt4 includes ======================*/
#include <QAbstractScrollArea>
#include <QApplication>
#include <QComboBox>
#include <QDockWidget>
#include <QElapsedTimer>
#include <QEvent>
#include <QFrame>
#include <QLabel>
#include <QListView>
#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QMouseEvent>
#include <QPainter>
#include <QPaintEvent>
#include <QPushButton>
#include <QStyleOptionTabWidgetFrame>
#include <QStylePlugin>
#include <QScrollBar>
#include <QTextBrowser>
#include <QTimer>
#include <QToolBar>
#include <QToolButton>
#include <QTreeView>
#include <QWeakPointer>
/**============= Bespin includes ==========================*/
// #include "debug.h"
#ifdef BE_WS_X11
#include "xproperty.h"
#include "fixx11h.h"
#endif
#include "FX.h"
#include "animator/hover.h"
#include "shadows.h"
#include "hacks.h"
#include "virtuality.h"
#include <QtDebug>
/**============ Internal Definitions ========================*/
#define BESPIN_MOUSE_DEBUG 0
/**=========================================================*/
/**============= extern C stuff ==========================*/
QStringList VirtualityStylePlugin::keys() const {
return QStringList() << "Virtuality" << "Sienar" << "Flynn" << "VirtualBreeze" << "VirtualArch";
}
QStyle *VirtualityStylePlugin::create(const QString &key) {
QString ikey = key.toLower();
if (ikey == "virtuality" || ikey == "sienar" || ikey == "flynn" || ikey == "virtualbreeze" ||
ikey == "virtualarch")
return new BE::Style(ikey);
return 0;
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(Virtuality, VirtualityStylePlugin)
#endif
/**=========================================================*/
using namespace BE;
AppType Style::appType;
Config Style::config;
float Style::halfStroke = 1;
QPalette *Style::originalPalette = 0;
QPalette Style::invertedPalette;
EventKiller Style::eventKiller;
#define N_PE 54
#define N_CE 50
#define N_CC 12
static void
(Style::*primitiveRoutine[N_PE])(const QStyleOption*, QPainter*, const QWidget*) const;
static void
(Style::*controlRoutine[N_CE])(const QStyleOption*, QPainter*, const QWidget*) const;
static void
(Style::*complexRoutine[N_CC])(const QStyleOptionComplex*, QPainter*, const QWidget*) const;
#define registerPE(_FUNC_, _ELEM_) primitiveRoutine[QStyle::_ELEM_] = &Style::_FUNC_
#define registerCE(_FUNC_, _ELEM_) controlRoutine[QStyle::_ELEM_] = &Style::_FUNC_
#define registerCC(_FUNC_, _ELEM_) complexRoutine[QStyle::_ELEM_] = &Style::_FUNC_
// static void registerPE(char *S0, ...)
// {
// register char *s;
// if (s=S0, s!=NULL) { register char *sa;
// va_list a;
// for (va_start(a,S0); (sa=va_arg(a,char*), sa!=NULL); )
// while (*s=*sa, *sa) ++s,++sa;
// va_end(a);
// }
// return ((int)(s-S0));
// }
void
Style::registerRoutines()
{
for (int i = 0; i < N_PE; ++i)
primitiveRoutine[i] = 0;
for (int i = 0; i < N_CE; ++i)
controlRoutine[i] = 0;
for (int i = 0; i < N_CC; ++i)
complexRoutine[i] = 0;
// buttons.cpp
registerPE(drawButtonFrame, PE_PanelButtonCommand);
registerPE(drawButtonFrame, PE_PanelButtonBevel);
registerPE(skip, PE_FrameDefaultButton);
registerCE(drawPushButton, CE_PushButton);
registerCE(drawPushButtonBevel, CE_PushButtonBevel);
registerCE(drawPushButtonLabel, CE_PushButtonLabel);
registerPE(drawCheckBox, PE_IndicatorCheckBox);
registerPE(drawRadio, PE_IndicatorRadioButton);
registerCE(drawCheckBoxItem, CE_CheckBox);
registerCE(drawRadioItem, CE_RadioButton);
registerCE(drawCheckLabel, CE_CheckBoxLabel);
registerCE(drawCheckLabel, CE_RadioButtonLabel);
// docks.cpp
registerPE(drawDockBg, PE_FrameDockWidget);
registerCE(drawDockTitle, CE_DockWidgetTitle);
registerCC(drawMDIControls, CC_MdiControls);
registerPE(drawDockHandle, PE_IndicatorDockWidgetResizeHandle);
registerPE(drawToolbarHandle, PE_IndicatorToolBarHandle);
// frames.cpp
registerCE(skip, CE_FocusFrame);
registerPE(skip, PE_PanelStatusBar);
registerPE(skip, PE_FrameStatusBarItem);
registerPE(drawFocusFrame, PE_FrameFocusRect);
registerPE(drawFrame, PE_Frame);
registerCE(drawFrame, CE_ShapedFrame);
registerCC(drawGroupBox, CC_GroupBox);
registerPE(drawGroupBoxFrame, PE_FrameGroupBox);
// input.cpp
registerPE(drawLineEditFrame, PE_FrameLineEdit);
registerPE(drawLineEdit, PE_PanelLineEdit);
registerCC(drawSpinBox, CC_SpinBox);
registerCC(drawComboBox, CC_ComboBox);
registerCE(drawComboBoxLabel, CE_ComboBoxLabel);
// menus.cpp
registerPE(skip, PE_PanelMenuBar);
registerCE(skip, CE_MenuBarEmptyArea);
registerCE(drawMenuBarItem, CE_MenuBarItem);
registerCE(drawMenuItem, CE_MenuItem);
registerCE(drawMenuScroller, CE_MenuScroller);
registerCE(drawMenuTearOff, CE_MenuTearoff);
registerCE(skip, CE_MenuEmptyArea);
registerCE(skip, CE_MenuHMargin);
registerCE(skip, CE_MenuVMargin);
// progress.cpp
registerCE(drawProgressBar, CE_ProgressBar);
registerCE(drawProgressBarGroove, CE_ProgressBarGroove);
registerCE(drawProgressBarContents, CE_ProgressBarContents);
registerCE(drawProgressBarLabel, CE_ProgressBarLabel);
// scrollareas.cpp
registerPE(drawScrollAreaCorner, PE_PanelScrollAreaCorner);
registerCC(drawScrollBar, CC_ScrollBar);
registerCE(drawScrollBarAddLine, CE_ScrollBarAddLine);
registerCE(drawScrollBarSubLine, CE_ScrollBarSubLine);
registerCE(skip, CE_ScrollBarSubPage);
registerCE(skip, CE_ScrollBarAddPage);
registerCE(drawScrollBarSlider, CE_ScrollBarSlider);
// indicators.cpp
registerPE(drawItemCheck, PE_IndicatorItemViewItemCheck);
registerPE(drawMenuCheck, PE_IndicatorMenuCheckMark);
registerPE(drawSolidArrowN, PE_IndicatorArrowUp);
registerPE(drawSolidArrowN, PE_IndicatorSpinUp);
registerPE(drawSolidArrowN, PE_IndicatorSpinPlus);
registerPE(drawSolidArrowS, PE_IndicatorArrowDown);
registerPE(drawSolidArrowS, PE_IndicatorSpinDown);
registerPE(drawSolidArrowS, PE_IndicatorSpinMinus);
registerPE(drawSolidArrowS, PE_IndicatorButtonDropDown);
registerPE(drawSolidArrowE, PE_IndicatorArrowRight);
registerPE(drawSolidArrowW, PE_IndicatorArrowLeft);
// slider.cpp
registerCC(drawSlider, CC_Slider);
registerCC(drawDial, CC_Dial);
// tabbing.cpp
registerPE(drawTabWidget, PE_FrameTabWidget);
registerPE(drawTabBar, PE_FrameTabBarBase);
registerCE(drawTab, CE_TabBarTab);
registerCE(drawTabShape, CE_TabBarTabShape);
registerCE(drawTabLabel, CE_TabBarTabLabel);
registerPE(skip, PE_IndicatorTabTear);
registerCE(drawToolboxTab, CE_ToolBoxTab);
registerCE(drawToolboxTabShape, CE_ToolBoxTabShape);
registerCE(drawToolboxTabLabel, CE_ToolBoxTabLabel);
registerPE(drawTabCloser, PE_IndicatorTabClose);
// toolbars.cpp
registerCC(drawToolButton, CC_ToolButton);
registerPE(skip, PE_PanelButtonTool);
registerPE(skip, PE_IndicatorToolBarSeparator);
registerPE(skip, PE_PanelToolBar);
registerCE(skip, CE_ToolBar);
registerCE(drawToolButtonLabel, CE_ToolButtonLabel);
registerPE(skip, PE_FrameButtonTool);
// views.cpp
registerCE(drawHeader, CE_Header);
registerCE(drawHeaderSection, CE_HeaderSection);
registerCE(drawHeaderLabel, CE_HeaderLabel);
registerPE(drawBranch, PE_IndicatorBranch);
registerCE(drawRubberBand, CE_RubberBand);
registerPE(drawHeaderArrow, PE_IndicatorHeaderArrow);
registerPE(drawItemRow, PE_PanelItemViewRow);
registerPE(drawItemItem, PE_PanelItemViewItem);
//NOTICE this is a hell lot of code in QCommonStyle, so for the moment, it's not customized
// registerCE(drawViewItem, CE_ItemViewItem);
// window.cpp
registerPE(drawWindowFrame, PE_FrameWindow);
registerPE(skip, PE_FrameMenu);
registerPE(drawWindowBg, PE_Widget);
registerPE(drawToolTip, PE_PanelTipLabel);
registerCC(drawTitleBar, CC_TitleBar);
registerCE(drawSplitterHandle, CE_Splitter);
registerCE(drawSizeGrip, CE_SizeGrip);
}
/**THE STYLE ITSELF*/
Style::Style(const QString &name) : QCommonStyle()
{
m_usingStandardPalette = (name == "sienar" || name == "flynn" || name == "virtualbreeze");
setObjectName(name);
#ifdef BE_WS_X11
if (BE::isPlatformX11()) // TODO: port XProperty for wayland
BE::XProperty::init();
#endif
FX::init();
if (m_usingStandardPalette) {
originalPalette = new QPalette(standardPalette());
polish(*originalPalette, true);
static bool isRecursion = false;
if (!isRecursion) {
isRecursion = true; // some applications (TEA editor at least) are aggressive on forcing
// palettes. Too aggressive, by creating a new style everytiem. We
// therefore detect this and escape the recursion.
// TEA looks pretty ... interesting ... anyway ;-P
QApplication::setPalette(*originalPalette);
qApp->installEventFilter(this);
QTimer::singleShot(10000, this, SLOT(removeAppEventFilter()));
isRecursion = false;
}
}
init();
registerRoutines();
}
Style::~Style()
{
Shadows::cleanUp();
// reset palette
if (!m_usingStandardPalette)
return;
QStyle *newStyle = QApplication::style();
// qobject_cast because this can be invoked from a dying proxystyle
if (!newStyle || newStyle == this || !qobject_cast<QStyle*>(newStyle))
return;
BE::Style *beStyle = qobject_cast<BE::Style*>(newStyle);
if (beStyle && beStyle->m_usingStandardPalette)
return; // can take care of itself and causes recursion
// harden a bit
qApp->removeEventFilter(this);
m_usingStandardPalette = false;
#if 0 // set default palette of new style
// setting the palette during the deconstructor is no good idea.
// at least the style kcm will try to delete the deleted style in return
// and deferring this to the end of the event queue will just cause a recursion
// on the eventloop
QPalette pal = newStyle->standardPalette();
newStyle->polish(pal);
QApplication::setPalette(pal);
#endif
}
#include "makros.h"
#undef PAL
#define PAL pal
void
Style::drawItemText(QPainter *painter, const QRect &rect, int flags, const QPalette &pal,
bool enabled, const QString& text, QPalette::ColorRole textRole, QRect *boundingRect) const
{
if (text.isEmpty())
return;
// flags |= config.mnemonic;
QPen savedPen;
bool penDirty = false;
if (textRole != QPalette::NoRole) {
penDirty = true;
savedPen = painter->pen();
painter->setPen(QPen(pal.brush(textRole), savedPen.widthF()));
}
QRect r = rect.translated( 0, config.fontOffset[painter->font().bold()] );
if (!enabled) { // let's see if we can get some blurrage here =)
if (!penDirty)
{ savedPen = painter->pen(); penDirty = true; }
const bool wasBold = painter->font().bold();
if (wasBold) {
QFont fnt(painter->font());
fnt.setBold(false);
painter->setFont(fnt);
}
QColor c = painter->pen().color();
c.setAlpha(c.alpha()/4 + 2);
painter->setPen(QPen(c, savedPen.widthF()));
QRect stroke;
painter->drawText(r, flags, text, &stroke);
const int y = stroke.y() + stroke.height()/2;
painter->drawLine(stroke.x(), y, stroke.right(), y);
if (wasBold) {
QFont fnt(painter->font());
fnt.setBold(true);
painter->setFont(fnt);
}
}
else
painter->drawText(r, flags, text, boundingRect);
if (penDirty)
painter->setPen(savedPen);
}
static struct PainterStore {
bool aliasing;
QPen pen;
QBrush brush;
QFont font;
bool clipping;
QRegion clipRegion;
int flags;
} gs_painterStore = { false, QPen(), QBrush(), QFont(), false, QRegion(), 0 };
#define X_KdeBase 0xff000000
#define SH_KCustomStyleELement 0xff000001
enum CustomElements { _CE_CapacityBar = 0 /*, ...*/, N_CustomControls };
#if 0
enum SubElements { _SE_AmarokAnalyzerSlider = 0 /*, ...*/, N_CustomSubElements };
#endif
static QStyle::ControlElement primitives[N_CustomControls];
#if 0
static QStyle::SubElement subcontrols[N_CustomSubElements];
#endif
enum ElementType { SH, CE, SE };
static QMap<QString, int> styleElements; // yes. one is enough...
// NOTICE: using QHash instead QMap is probably overhead, there won't be too many items per app
static uint counter[3] = { X_KdeBase+3 /*sic!*/, X_KdeBase, X_KdeBase };
void
Style::drawPrimitive(PrimitiveElement pe, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
Q_ASSERT(option);
Q_ASSERT(painter);
// if (pe == PE_IndicatorItemViewItemDrop)
// An indicator that is drawn to show where an item in an item view is about to
// be dropped during a drag-and-drop operation in an item view.
// qWarning("IndicatorItemViewItemDrop, %d", pe);
if (pe < N_PE && primitiveRoutine[pe])
(this->*primitiveRoutine[pe])(option, painter, widget);
else {
// qDebug() << "BESPIN, unsupported primitive:" << pe << widget;
QCommonStyle::drawPrimitive( pe, option, painter, widget );
}
if (gs_painterStore.flags) {
qDebug() << "drawPrimitive : unbalanced painter" << pe << gs_painterStore.flags;
gs_painterStore.flags = 0;
}
}
void
Style::drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
Q_ASSERT(option);
Q_ASSERT(painter);
if (element < N_CE && controlRoutine[element])
(this->*controlRoutine[element])(option, painter, widget);
else if (element > X_KdeBase) {
if (element == primitives[_CE_CapacityBar])
drawCapacityBar(option, painter, widget);
//if (pe == primitives[_PE_WhateverElse])
// ...
} else {
// qDebug() << "BESPIN, unsupported control:" << element << widget;
QCommonStyle::drawControl( element, option, painter, widget );
}
if (gs_painterStore.flags) {
qDebug() << "drawControl : unbalanced painter" << element;
gs_painterStore.flags = 0;
}
}
void
Style::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
Q_ASSERT(option);
Q_ASSERT(painter);
if (control < N_CC && complexRoutine[control])
(this->*complexRoutine[control])(option, painter, widget);
else {
// qDebug() << "BESPIN, unsupported complex control:" << control << widget;
QCommonStyle::drawComplexControl( control, option, painter, widget );
}
if (gs_painterStore.flags) {
qDebug() << "drawComplexControl : unbalanced painter" << control;
gs_painterStore.flags = 0;
}
}
int
Style::elementId(const QString &string) const
{
int id = styleElements.value(string, 0);
if (id)
return id;
if (string == "CE_CapacityBar")
primitives[_CE_CapacityBar] = (ControlElement)(id = ++counter[CE]);
#if 0
else if (string == "amarok.CC_Analyzer")
complexs[_CC_AmarokAnalyzer] = (ComplexControl)(id = ++counter[CC]);
// subcontrols (SC_) work muchg different as they're 1. flags and 2. attached to a CC
else if (string == "amarok.CC_Analyzer:SC_Slider")
{
subcontrols[_SC_AmarokAnalyzerSlider] = (SubControl)(id = (1 << scCounter[_CC_AmarokAnalyzer]));
++scCounter[_CC_AmarokAnalyzer];
}
// else if blablablaba...
#endif
if (id)
styleElements.insert(string, id);
return id;
}
/// ----------------------------------------------------------------------
void
Style::erase(const QStyleOption *option, QPainter *painter, const QWidget *widget, const QPoint *offset) const
{
const QWidget *grampa = widget;
while ( !(grampa->isWindow() ||
(grampa->autoFillBackground() && grampa->objectName() != "qt_scrollarea_viewport")))
grampa = grampa->parentWidget();
QPoint tl = widget->mapFrom(const_cast<QWidget*>(grampa), QPoint());
if (offset)
tl += *offset;
painter->save();
painter->setPen(Qt::NoPen);
const QBrush &brush = grampa->palette().brush(grampa->backgroundRole());
// we may encounter apps that have semi or *cough* fully *cough* amarok *cough*
// transparent backgrounds instead of none... ;-)
const bool needBase = brush.style() > Qt::DiagCrossPattern || brush.color().alpha() < 0xff;
if (grampa->isWindow() || needBase)
{ // means we need to paint the global bg as well
painter->setClipRect(option->rect, Qt::IntersectClip);
QStyleOption tmpOpt = *option;
// tmpOpt.rect = QRect(tl, widget->size());
tmpOpt.palette = grampa->palette();
if (tmpOpt.palette.brush(QPalette::Window).style() > 1)
painter->fillRect(tmpOpt.rect, tmpOpt.palette.brush(QPalette::Window));
painter->translate(tl);
drawWindowBg(&tmpOpt, painter, grampa);
}
if (!grampa->isWindow())
{
painter->setBrush(grampa->palette().brush(grampa->backgroundRole()));
painter->setBrushOrigin(tl);
painter->drawRect(option->rect);
}
painter->restore();
}
static bool _serverSupportsShadows = false;
static QElapsedTimer _lastCheckTime;
bool
Style::serverSupportsShadows()
{
if (!BE::isPlatformX11())
return false; // TODO: port XProperty for wayland
if (appType == KDM)
return false;
#ifdef BE_WS_X11
if (!_lastCheckTime.isValid() || _lastCheckTime.elapsed() > 1000*60*5)
{
unsigned long n = 0;
Atom *supported = BE::XProperty::get<Atom>(DefaultRootWindow(BE_X11_DISPLAY), BE::XProperty::netSupported, BE::XProperty::ATOM, &n);
for (uint i = 0; i < n; ++i)
if (supported[i] == BE::XProperty::kwinShadow)
{
_serverSupportsShadows = true;
break;
}
if (supported)
XFree(supported);
_lastCheckTime.start();
}
#endif
return _serverSupportsShadows;
}
static bool equal(const QPalette &p1, const QPalette &p2)
{
return
p1.color(QPalette::Active, QPalette::Window) == p2.color(QPalette::Active, QPalette::Window) &&
p1.color(QPalette::Active, QPalette::WindowText) == p2.color(QPalette::Active, QPalette::WindowText) &&
p1.color(QPalette::Active, QPalette::Base) == p2.color(QPalette::Active, QPalette::Base) &&
p1.color(QPalette::Active, QPalette::Text) == p2.color(QPalette::Active, QPalette::Text) &&
p1.color(QPalette::Active, QPalette::Button) == p2.color(QPalette::Active, QPalette::Button) &&
p1.color(QPalette::Active, QPalette::ButtonText) == p2.color(QPalette::Active, QPalette::ButtonText) &&
p1.color(QPalette::Active, QPalette::Highlight) == p2.color(QPalette::Active, QPalette::Highlight) &&
p1.color(QPalette::Active, QPalette::HighlightedText) == p2.color(QPalette::Active, QPalette::HighlightedText);
// p1.color(QPalette::Active, QPalette::ToolTipBase) == p2.color(QPalette::Active, QPalette::ToolTipBase) &&
// p1.color(QPalette::Active, QPalette::ToolTipText) == p2.color(QPalette::Active, QPalette::ToolTipText);
}
static const
QPalette::ColorGroup groups[3] = { QPalette::Active, QPalette::Inactive, QPalette::Disabled };
void
Style::swapPalette(QWidget *widget)
{
// looks complex? IS!
// reason nr. 1: stylesheets. they're nasty and qt operates on the app palette here
// reason nr. 2: some idiot must have spread the idea that pal.setColor(backgroundRole(), Qt::transparent)
// is a great idea instead of just using setAutoFillBackground(false), preserving all colors and just not
// using them. hey, why not call Qt to paint some nothing.... *grrrr*
QMap<QWidget*, QString> shits;
QList<QWidget*> kids = widget->findChildren<QWidget*>();
kids.prepend(widget);
QPalette pal;
QPalette::ColorGroup group;
bool hasShit = false;
for (int i = kids.count()-1; i > -1; --i) {
QWidget *kid = kids.at(i);
if (kid->testAttribute(Qt::WA_StyleSheet)) { // first get rid of shit
shits.insert(kid, kid->styleSheet());
kid->setStyleSheet(QString());
hasShit = true;
}
// now swap the palette ----------------
if (hasShit || kid->testAttribute(Qt::WA_SetPalette) || kid == widget) {
pal = kid->palette();
hasShit = false;
// if (kid->inherits("KUrlNavigatorButtonBase") || kid->inherits("BreadcrumbItemButton")) {
// // we mess up with it during painting in Hacks
// pal.setColor(QPalette::HighlightedText, pal.color(QPalette::Active, QPalette::Window));
// kid->setPalette(pal);
// continue;
// }
for (int i = 0; i < 3; ++i) {
group = groups[i];
FX::swap(pal, group, QPalette::Window, QPalette::WindowText, kid);
FX::swap(pal, group, QPalette::Button, QPalette::ButtonText, kid);
FX::swap(pal, group, QPalette::Highlight, QPalette::HighlightedText, kid);
FX::swap(pal, group, QPalette::Base, QPalette::Text, kid);
}
polish(pal, false);
kid->setPalette(pal);
}
QTextDocument *document(NULL);
if (qobject_cast<QLabel*>(kid)) {
document = kid->findChild<QTextDocument*>();
}
else if (QTextEdit *edit = qobject_cast<QTextEdit*>(kid)) {
document = edit->document();
}
if (document) {
document->setDefaultStyleSheet( QString("*{color:%1;background-color:%2;}a{color:%3;}").arg(kid->palette().color(QPalette::Active, QPalette::Text).name()).arg(kid->palette().color(QPalette::Active, QPalette::Base).name()).arg(kid->palette().color(QPalette::Active, QPalette::Link).name()) );
if (QTextBrowser *browser = qobject_cast<QTextBrowser*>(kid))
browser->reload();
}
}
// this is funny: style shits rely on QApplication::palette() (nice trick, TrottelTech... again)
// so to apply them with the proper color, we need to change the apps palette to the swapped one,...
if (!shits.isEmpty()) {
QPalette appPal = QApplication::palette();
// protect our KDE palette fix - in case
QPalette *savedPal = originalPalette;
originalPalette = 0;
// ... reapply the shits...
QMap<QWidget*, QString>::const_iterator shit = shits.constBegin();
qApp->installEventFilter(&eventKiller); // temp. change to trick next setStyleSheet calls
// nobody needs to know and some clients - looking at
// ktexteditor here - do *really* expensive stuff on app
// palette changes
while (shit != shits.constEnd()) {
QApplication::setPalette(shit.key()->palette());
if (shit.value().isEmpty()) // *sigh*
shit.key()->setStyleSheet(QString(shit.key()->metaObject()->className()) + "{color:red;}");
shit.key()->setStyleSheet(shit.value());
++shit;
}
// ... and reset the apps palette
QApplication::setPalette(appPal);
qApp->removeEventFilter(&eventKiller);
originalPalette = savedPal;
}
}
bool isUrlNaviButtonArrow = false;
static bool isLastNavigatorButton(const QWidget *w, const char *className)
{
if (QWidget *navigator = w->parentWidget())
{
QList<QPushButton*> btns = navigator->findChildren<QPushButton*>();
QList<QPushButton*>::const_iterator i = btns.constEnd();
while (i != btns.constBegin())
{
--i;
if ((*i)->inherits(className))
return (*i == w);
}
}
return false;
}
// settings the property can be expensive, so avoid for popups, combodrops etc.
// NOTICE:
// Qt::Dialog must be erased as it's in drawer et al. but takes away window as well
// Qt::FramelessWindowHint shall /not/ be ignored as the window could change it's opinion while being visible
static const
Qt::WindowFlags ignoreForDecoHints = ( Qt::Popup | Qt::SubWindow |
Qt::ToolTip | Qt::SplashScreen | Qt::Desktop | Qt::X11BypassWindowManagerHint /*| Qt::FramelessWindowHint*/ ) & (~(Qt::Dialog|Qt::Sheet));
static void detectBlurRegion(QWidget *window, const QWidget *widget, QRegion &blur)
{
const QObjectList &kids = widget->children();
QObjectList::const_iterator i;
for ( i = kids.begin(); i != kids.end(); ++i )
{
QObject *o = (*i);
if ( !o->isWidgetType() )
continue;
QWidget *w = static_cast<QWidget*>(o);
if ( w->isVisible() &&
((w->autoFillBackground() && w->palette().color(w->backgroundRole()).alpha() > 160) || (w->testAttribute(Qt::WA_OpaquePaintEvent) && !(qobject_cast<const QScrollBar*>(w) || w->inherits("QProgressBar")))) )
{
QPoint offset = w->mapTo(window, QPoint(0,0));
if (w->mask().isEmpty())
blur -= w->rect().translated(offset);
else
blur -= w->mask().translated(offset);
continue; // ne nood for deeper checks
}
else
detectBlurRegion(window, w, blur);
}
}
static QList<QWeakPointer<QWidget> > pendingBlurUpdates;
void
Style::updateBlurRegions() const
{
#ifdef BE_WS_X11 // hint blur region for the kwin plugin
if (!BE::isPlatformX11())
return; // TODO: port XProperty for wayland
for (QList<QWeakPointer<QWidget> >::const_iterator it = pendingBlurUpdates.constBegin(),
end = pendingBlurUpdates.constEnd(); it != end; ++it)
{
if (!*it)
continue;
QSharedPointer<QWidget> widget = it->toStrongRef();
if (widget && !(widget->testAttribute(Qt::WA_WState_Created) || widget->internalWinId()))
continue; // protect against pseudo widgets, see setupDecoFor()
QRegion blur = widget->mask().isEmpty() ? widget->rect() : widget->mask();
detectBlurRegion(widget.data(), widget.data(), blur);
if (blur.isEmpty())
continue;
QVector<unsigned long> data(blur.rectCount() * 4);
for ( auto i = blur.begin(); i != blur.end(); ++i )
{
if (i->width() > 0 && i->height() > 0)
data << i->x() << i->y() << i->width() << i->height();
}
BE::XProperty::set<unsigned long>(widget->winId(), BE::XProperty::blurRegion, (unsigned long*)data.constData(), BE::XProperty::LONG, data.size());
}
#endif
pendingBlurUpdates.clear();
}
static void shapeCorners( QWidget *widget, bool forceShadows )
{
#ifdef BE_WS_X11
if (forceShadows && isPlatformX11()) // kwin/beshadowed needs a little hint to shadow this one nevertheless
BE::XProperty::setAtom( widget->winId(), BE::XProperty::forceShadows );
#endif
if (widget->isWindow() && FX::compositingActive() && BE::Style::serverSupportsShadows()) {
widget->clearMask();
return;
}
const int w = widget->width();
const int h = widget->height();
QRegion mask(4, 0, w-8, h);
mask += QRegion(0, 4, w, h-8);
mask += QRegion(2, 1, w-4, h-2);
mask += QRegion(1, 2, w-2, h-4);
// only top rounded - but looks nasty
// QRegion mask(0, 0, w, h-4);
// mask += QRect(1, h-4, w-2, 2);
// mask += QRect(2, h-2, w-4, 1);
// mask += QRect(4, h-1, w-8, 1);
widget->setMask(mask);
}
void reBlur(QWidget *widget, bool round) {
#ifdef BE_WS_X11
if (!(widget->windowOpacity() < 1.0 && widget->testAttribute(Qt::WA_WState_Created) &&
widget->internalWinId() && BE::isPlatformX11())) // TODO: port XProperty for wayland
return;
if (!round) {
unsigned long zero(0);
XProperty::set<unsigned long>(widget->winId(), XProperty::blurRegion, &zero, XProperty::LONG, 1);
return;
}
const int w = widget->rect().width();
const int h = widget->rect().height();
QVector<unsigned long> data(4 * 4);
data << 4 << 0 << w - 8 << h
<< 0 << 4 << w << h - 8
<< 2 << 1 << w - 4 << h - 2
<< 1 << 2 << w - 2 << h - 4;
BE::XProperty::set<unsigned long>(widget->winId(), BE::XProperty::blurRegion, (unsigned long*)data.constData(), BE::XProperty::LONG, data.size());
#endif
}
bool
Style::eventFilter( QObject *object, QEvent *ev )
{
switch (ev->type())
{
case QEvent::HoverMove:
case QEvent::MouseMove:
case QEvent::Timer:
case QEvent::Move:
return false; // just for performance - they can occur really often
case QEvent::Paint:
// if (object->isWidgetType())
if (QWidget *window = static_cast<QWidget*>(object))
if (window->testAttribute(Qt::WA_TranslucentBackground))
if (window->testAttribute(Qt::WA_StyledBackground))
if (window->isWindow())
{
QPainter p(window);
p.setClipRegion( static_cast<QPaintEvent*>(ev)->region() );
drawWindowBg(0, &p, window);
p.end();
return false;
}
if (QTabBar *tabBar = qobject_cast<QTabBar*>(object))
{
if (tabBar->testAttribute(Qt::WA_NoSystemBackground))
return false; // shall be translucent
if (!tabBar->drawBase())
return false;
if (QTabWidget *tw = qobject_cast<QTabWidget*>(tabBar->parentWidget()))
{ // no extra tabbar here please... unless the border is StyleShitted away ;)
if (tw->styleSheet().isEmpty())
return false;
if ( !(tw->styleSheet().contains("pane", Qt::CaseInsensitive) && tw->styleSheet().contains("border", Qt::CaseInsensitive)) )
return false;
}
if ( appType == KDevelop )
{ // KDevelop does that... weird. - and of course /after/ painting the label string...
QWidget *dad = tabBar->parentWidget();
while ( dad )
{
if (dad->inherits("QMenuBar"))
return false;
dad = dad->parentWidget();
}
}
QPainter p(tabBar);
QStyleOptionTabBarBase opt;
opt.initFrom(tabBar);
opt.shape = tabBar->shape();
opt.selectedTabRect = tabBar->tabRect(tabBar->currentIndex());
opt.tabBarRect = opt.rect;
opt.documentMode = tabBar->documentMode();
// if (QWidget *window = tabBar->window())
// {
// opt.tabBarRect = window->rect();
// opt.tabBarRect.moveTopLeft(tabBar->mapFrom(window, opt.tabBarRect.topLeft()));
// }
drawTabBar(&opt, &p, NULL);
p.end();
return false;
} else if ( QTabWidget *tw = qobject_cast<QTabWidget*>( object ) ) {
// those don't paint frames and rely on the tabbar, which we ruled and rule out (looks weird with e.g. cornerwidgets...)
if (tw->documentMode()) {
QStyleOptionTabBarBase opt;
opt.initFrom(tw);
opt.documentMode = true;
QTabBar *bar = NULL;
#if QT_VERSION < 0x050000
foreach (QObject *o, tw->children()) {
if ((bar = qobject_cast<QTabBar*>(o)))
break;
}
#else
bar = tw->tabBar();
#endif
int thickness = 0;
if (bar) {
if (bar->isHidden())
return true;
opt.rect = bar->geometry();
opt.selectedTabRect = bar->tabRect(bar->currentIndex()).translated(opt.rect.topLeft());
thickness = 1;
} else {
thickness = pixelMetric(PM_TabBarBaseHeight, &opt, tw);
}
if (thickness < 1) {
return true;
}
switch (tw->tabPosition()) {
default:
case QTabWidget::North:
if (bar) {
opt.rect.setLeft(tw->rect().left());
opt.rect.setRight(tw->rect().right());
} else {
opt.rect.setBottom(opt.rect.top() + thickness);
}
opt.shape = QTabBar::RoundedNorth; break;
case QTabWidget::South:
if (bar) {
opt.rect.setLeft(tw->rect().left());
opt.rect.setRight(tw->rect().right());
} else {
opt.rect.setTop(opt.rect.bottom() - thickness);
}
opt.shape = QTabBar::RoundedSouth; break;
case QTabWidget::West:
if (bar) {
opt.rect.setTop(tw->rect().top());
opt.rect.setBottom(tw->rect().bottom());
} else {
opt.rect.setRight(opt.rect.left() + thickness);
}
opt.shape = QTabBar::RoundedWest; break;
case QTabWidget::East:
if (bar) {
opt.rect.setTop(tw->rect().top());
opt.rect.setBottom(tw->rect().bottom());
} else {
opt.rect.setLeft(opt.rect.right() - thickness);
}
opt.shape = QTabBar::RoundedEast; break;
}
if (opt.rect.isEmpty())
return true; // pointless
opt.tabBarRect = opt.rect;
QPainter p(tw);
drawTabBar(&opt, &p, NULL);
p.end();
return true; // don't let it paint weird stuff on the cornerwidgets etc.
}
return false;
} else if (QPushButton *w = qobject_cast<QPushButton*>(object)) {
bool b = false;
if ((b = w->inherits("KUrlNavigatorButtonBase")) || w->inherits("BreadcrumbItemButton"))
{
static bool isRecursion = false;
if (isRecursion)
return false;
isRecursion = true;
isUrlNaviButtonArrow = true;
object->removeEventFilter(this);
if (w->text() == "/")
w->setText("/.");
QPalette::ColorRole fg = w->parentWidget() ? w->parentWidget()->foregroundRole() : QPalette::WindowText;
if (fg != QPalette::WindowText || isLastNavigatorButton(w, b?"KUrlNavigatorButtonBase":"BreadcrumbItemButton"))
{
if (w->foregroundRole() != fg)
w->setForegroundRole(fg);
}
else if (w->foregroundRole() != QPalette::Link)
w->setForegroundRole(QPalette::Link);
QCoreApplication::sendEvent(object, ev);
object->installEventFilter(this);
isUrlNaviButtonArrow = false;
isRecursion = false;
return true;
}
return false;
}
return false;
case QEvent::Enter:
case QEvent::Leave:
{
if (qobject_cast<QPushButton*>(object))
{
bool b = false;
if ((b = object->inherits("KUrlNavigatorButtonBase")) || object->inherits("BreadcrumbItemButton"))
{
QWidget *w = static_cast<QWidget*>(object);
w->setCursor(Qt::PointingHandCursor);
QFont fnt = w->font();
if (isLastNavigatorButton(w, b?"KUrlNavigatorButtonBase":"BreadcrumbItemButton"))
{
w->setCursor(Qt::ArrowCursor);
fnt.setUnderline(false);
}
else
fnt.setUnderline(ev->type() == QEvent::Enter);
w->setFont(fnt);
return false;
}
return false;
}
return false;
}
case QEvent::Wheel:
{
if (QAbstractSlider* slider = qobject_cast<QAbstractSlider*>(object))
{
QWheelEvent *we = static_cast<QWheelEvent*>(ev);
if ((slider->value() == slider->minimum() && (we->angleDelta().y() || we->angleDelta().x()) > 0) ||
(slider->value() == slider->maximum() && (we->angleDelta().y() || we->angleDelta().x()) < 0))
Animator::Hover::Play(slider);
return false;
}
if (QListView *list = qobject_cast<QListView*>(object))
{
// if (list->verticalScrollMode() == QAbstractItemView::ScrollPerPixel) // this should be, but semms to be not
if (list->iconSize().height() > -1) // happens on e.g. config views
if (list->inherits("KCategorizedView"))
list->verticalScrollBar()->setSingleStep(list->iconSize().height()/3);