Mercurial > hg > svapp
comparison style/AppEventFilter.cpp @ 668:dac3781826da thorn-cpp
Experimental branch based on Thorn C++ code from RG
author | Chris Cannam |
---|---|
date | Tue, 30 Apr 2019 11:36:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
667:31ea416fea3c | 668:dac3781826da |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 Vect | |
5 An experimental audio player for plural recordings of a work | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 | |
8 This file is taken from Rosegarden, a MIDI and audio sequencer and | |
9 musical notation editor. Copyright 2000-2018 the Rosegarden | |
10 development team. Thorn style developed in stylesheet form by | |
11 D. Michael McIntyre and reimplemented as a class by David Faure. | |
12 | |
13 This program is free software; you can redistribute it and/or | |
14 modify it under the terms of the GNU General Public License as | |
15 published by the Free Software Foundation; either version 2 of the | |
16 License, or (at your option) any later version. See the file | |
17 COPYING included with this distribution for more information. | |
18 */ | |
19 | |
20 #include "ThornStyle.h" | |
21 #include "AppEventFilter.h" | |
22 | |
23 #include <QApplication> | |
24 #include <QFileDialog> | |
25 #include <QAbstractItemView> | |
26 #include <QCheckBox> | |
27 #include <QLabel> | |
28 #include <QRadioButton> | |
29 #include <QToolBar> | |
30 #include <QWidget> | |
31 #include <QDialogButtonBox> | |
32 #include <QPushButton> | |
33 #include <QComboBox> | |
34 #include <QSpinBox> | |
35 | |
36 // Apply the style to widget and its children, recursively | |
37 // Even though every widget goes through the event filter, this is needed | |
38 // for the case where a whole widget hierarchy is suddenly reparented into the file dialog. | |
39 // Then we need to apply the app style again. Testcase: scrollbars in file dialog. | |
40 static void applyStyleRecursive(QWidget* widget, QStyle *style) | |
41 { | |
42 if (widget->style() != style) { | |
43 widget->setStyle(style); | |
44 } | |
45 foreach (QObject* obj, widget->children()) { | |
46 if (obj->isWidgetType()) { | |
47 QWidget *w = static_cast<QWidget *>(obj); | |
48 applyStyleRecursive(w, style); | |
49 } | |
50 } | |
51 } | |
52 | |
53 AppEventFilter::AppEventFilter() : | |
54 m_systemPalette(qApp->palette()), | |
55 m_systemStyle(qApp->style()) { | |
56 } | |
57 | |
58 bool | |
59 AppEventFilter::shouldIgnoreThornStyle(QWidget *widget) const { | |
60 return qobject_cast<QFileDialog *>(widget) | |
61 || widget->inherits("KDEPlatformFileDialog") | |
62 || widget->inherits("KDirSelectDialog"); | |
63 } | |
64 | |
65 // when we ditch Qt4, we can switch to qCDebug... | |
66 //#define DEBUG_EVENTFILTER | |
67 | |
68 bool AppEventFilter::eventFilter(QObject *watched, QEvent *event) | |
69 { | |
70 static bool s_insidePolish = false; // setStyle calls polish again, so skip doing the work twice | |
71 if (!s_insidePolish && watched->isWidgetType() && event->type() == QEvent::Polish) { | |
72 s_insidePolish = true; | |
73 // This is called after every widget is created and just before being shown | |
74 // (we use this so that it has a proper parent widget already) | |
75 QWidget *widget = static_cast<QWidget *>(watched); | |
76 if (shouldIgnoreThornStyle(widget)) { | |
77 // The palette from the mainwindow propagated to the dialog, restore it. | |
78 widget->setPalette(m_systemPalette); | |
79 #ifdef DEBUG_EVENTFILTER | |
80 qDebug() << widget << "now using app style (recursive)"; | |
81 #endif | |
82 applyStyleRecursive(widget, qApp->style()); | |
83 s_insidePolish = false; | |
84 return false; | |
85 } | |
86 QWidget *toplevel = widget->window(); | |
87 #ifdef DEBUG_EVENTFILTER | |
88 qDebug() << widget << "current widget style=" << widget->style() << "shouldignore=" << shouldIgnoreThornStyle(toplevel); | |
89 #endif | |
90 if (shouldIgnoreThornStyle(toplevel)) { | |
91 // Here we should apply qApp->style() recursively on widget and its children, in case one was reparented | |
92 #ifdef DEBUG_EVENTFILTER | |
93 qDebug() << widget << widget->objectName() << "in" << toplevel << "now using app style (recursive)"; | |
94 #endif | |
95 applyStyleRecursive(widget, qApp->style()); | |
96 } else if (widget->style() != &m_style) { | |
97 #ifdef DEBUG_EVENTFILTER | |
98 //qDebug() << " ToolTipBase=" << widget->palette().color(QPalette::ToolTipBase).name(); | |
99 #endif | |
100 // Apply style recursively because some child widgets (e.g. QHeaderView in QTreeWidget, in DeviceManagerDialog) don't seem to get here. | |
101 if (qobject_cast<QAbstractItemView *>(widget)) { | |
102 applyStyleRecursive(widget, &m_style); | |
103 } else { | |
104 widget->setStyle(&m_style); | |
105 } | |
106 #ifdef DEBUG_EVENTFILTER | |
107 qDebug() << " now using style" << widget->style(); | |
108 #endif | |
109 if (widget->windowType() != Qt::Widget) { // window, tooltip, ... | |
110 widget->setPalette(m_style.standardPalette()); | |
111 #ifdef DEBUG_EVENTFILTER | |
112 qDebug() << " after setPalette: ToolTipBase=" << widget->palette().color(QPalette::ToolTipBase).name(); | |
113 #endif | |
114 } else { | |
115 #ifdef DEBUG_EVENTFILTER | |
116 //qDebug() << " not a toplevel. ToolTipBase=" << widget->palette().color(QPalette::ToolTipBase).name(); | |
117 #endif | |
118 } | |
119 polishWidget(widget); | |
120 } | |
121 s_insidePolish = false; | |
122 } | |
123 return false; // don't eat the event | |
124 } | |
125 | |
126 void AppEventFilter::polishWidget(QWidget *widget) | |
127 { | |
128 if (QLabel *label = qobject_cast<QLabel *>(widget)) { | |
129 if (qobject_cast<QToolBar *>(widget->parentWidget())) { | |
130 /* Toolbars must be light enough for black icons, therefore black text on their | |
131 QLabels, rather than white, is more appropriate. | |
132 QToolBar QLabel { color: #000000; } */ | |
133 QPalette pal = label->palette(); | |
134 pal.setColor(label->foregroundRole(), Qt::black); | |
135 label->setPalette(pal); | |
136 //qDebug() << "made label black:" << label << label->text(); | |
137 } | |
138 if (widget->objectName() == "SPECIAL_LABEL") { | |
139 widget->setAutoFillBackground(true); | |
140 // QWidget#SPECIAL_LABEL { color: #000000; background-color: #999999; } | |
141 QPalette palette = widget->palette(); | |
142 palette.setColor(QPalette::WindowText, Qt::black); | |
143 palette.setColor(QPalette::Window, QColor(0x99, 0x99, 0x99)); | |
144 widget->setPalette(palette); | |
145 } | |
146 } else if (widget->objectName() == "Rosegarden Transport") { | |
147 // Give the non-LED parts of the dialog the groupbox "lighter black" | |
148 // background for improved contrast. | |
149 QPalette transportPalette = widget->palette(); | |
150 transportPalette.setColor(widget->backgroundRole(), QColor(0x40, 0x40, 0x40)); | |
151 widget->setPalette(transportPalette); | |
152 widget->setAutoFillBackground(true); | |
153 } else if (QCheckBox *cb = qobject_cast<QCheckBox *>(widget)) { | |
154 cb->setAttribute(Qt::WA_Hover); | |
155 } else if (QRadioButton *rb = qobject_cast<QRadioButton *>(widget)) { | |
156 rb->setAttribute(Qt::WA_Hover); | |
157 } else if (QPushButton *pb = qobject_cast<QPushButton *>(widget)) { | |
158 pb->setAttribute(Qt::WA_Hover); | |
159 if (qobject_cast<QDialogButtonBox *>(widget->parentWidget())) { | |
160 // Bug in QDialogButtonBox: if the app style sets QStyle::SH_DialogButtonBox_ButtonsHaveIcons | |
161 // a later call to setStyle() doesn't remove the button icon again. | |
162 // Fix submitted at https://codereview.qt-project.org/183788 | |
163 pb->setIcon(QIcon()); | |
164 } | |
165 } else if (QComboBox *cb = qobject_cast<QComboBox *>(widget)) { | |
166 cb->setAttribute(Qt::WA_Hover); | |
167 } else if (QAbstractSpinBox *sb = qobject_cast<QAbstractSpinBox *>(widget)) { | |
168 sb->setAttribute(Qt::WA_Hover); | |
169 } | |
170 } |