QStyleSheetStyle: introduce class Tampered<QPalette|QFont>

... as a replacement for two QPairs and move some common
QFont/QPalette functionality into it.

Change-Id: Iaab92130dd54eaa7900ac2048014a80cbd04bfb6
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2017-02-24 00:56:55 +01:00
parent a12a3c6c8c
commit da730c90a3
2 changed files with 34 additions and 30 deletions

View File

@ -2594,7 +2594,7 @@ void QStyleSheetStyle::setPalette(QWidget *w)
if (!useStyleSheetPropagationInWidgetStyles || p.resolve() != 0) { if (!useStyleSheetPropagationInWidgetStyles || p.resolve() != 0) {
QPalette wp = w->palette(); QPalette wp = w->palette();
styleSheetCaches->customPaletteWidgets.insert(w, qMakePair(wp, p.resolve())); styleSheetCaches->customPaletteWidgets.insert(w, {wp, p.resolve()});
if (useStyleSheetPropagationInWidgetStyles) { if (useStyleSheetPropagationInWidgetStyles) {
p = p.resolve(wp); p = p.resolve(wp);
@ -2614,20 +2614,14 @@ void QStyleSheetStyle::unsetPalette(QWidget *w)
const auto it = styleSheetCaches->customPaletteWidgets.find(w); const auto it = styleSheetCaches->customPaletteWidgets.find(w);
if (it != styleSheetCaches->customPaletteWidgets.end()) { if (it != styleSheetCaches->customPaletteWidgets.end()) {
QPair<QPalette, uint> p = std::move(*it); auto customizedPalette = std::move(*it);
styleSheetCaches->customPaletteWidgets.erase(it); styleSheetCaches->customPaletteWidgets.erase(it);
QPalette original = p.first; QPalette original;
if (useStyleSheetPropagationInWidgetStyles)
if (useStyleSheetPropagationInWidgetStyles) { original = std::move(customizedPalette).reverted(w->palette());
original.resolve(original.resolve() & p.second); else
original = customizedPalette.oldWidgetValue;
QPalette wp = w->palette();
wp.resolve(wp.resolve() & ~p.second);
wp.resolve(original);
wp.resolve(wp.resolve() | original.resolve());
original = wp;
}
w->setPalette(original); w->setPalette(original);
QWidget *ew = embeddedWidget(w); QWidget *ew = embeddedWidget(w);
@ -2657,18 +2651,9 @@ void QStyleSheetStyle::unsetStyleSheetFont(QWidget *w) const
{ {
const auto it = styleSheetCaches->customFontWidgets.find(w); const auto it = styleSheetCaches->customFontWidgets.find(w);
if (it != styleSheetCaches->customFontWidgets.end()) { if (it != styleSheetCaches->customFontWidgets.end()) {
QPair<QFont, uint> f = std::move(*it); auto customizedFont = std::move(*it);
styleSheetCaches->customFontWidgets.erase(it); styleSheetCaches->customFontWidgets.erase(it);
w->setFont(std::move(customizedFont).reverted(w->font()));
QFont original = f.first;
original.resolve(original.resolve() & f.second);
QFont font = w->font();
font.resolve(font.resolve() & ~f.second);
font.resolve(original);
font.resolve(font.resolve() | original.resolve());
w->setFont(font);
} }
} }
@ -5953,7 +5938,7 @@ void QStyleSheetStyle::updateStyleSheetFont(QWidget* w) const
if (rule.font.resolve()) { if (rule.font.resolve()) {
QFont wf = w->font(); QFont wf = w->font();
styleSheetCaches->customFontWidgets.insert(w, qMakePair(wf, rule.font.resolve())); styleSheetCaches->customFontWidgets.insert(w, {wf, rule.font.resolve()});
QFont font = rule.font.resolve(wf); QFont font = rule.font.resolve(wf);
font.resolve(wf.resolve() | rule.font.resolve()); font.resolve(wf.resolve() | rule.font.resolve());

View File

@ -189,12 +189,31 @@ public:
QHash<const QObject *, QRenderRules> renderRulesCache; QHash<const QObject *, QRenderRules> renderRulesCache;
QHash<const void *, QCss::StyleSheet> styleSheetCache; // parsed style sheets QHash<const void *, QCss::StyleSheet> styleSheetCache; // parsed style sheets
QSet<const QWidget *> autoFillDisabledWidgets; QSet<const QWidget *> autoFillDisabledWidgets;
// widgets whose palettes and fonts we have tampered. stored value pair is // widgets with whose palettes and fonts we have tampered:
// QPair<old widget value, resolve mask of stylesheet value> template <typename T>
QHash<const QWidget *, QPair<QPalette, uint> > customPaletteWidgets; struct Tampered {
QHash<const QWidget *, QPair<QFont, uint> > customFontWidgets; T oldWidgetValue;
}; uint resolveMask;
// only call this function on an rvalue *this (it mangles oldWidgetValue)
T reverted(T current)
#ifdef Q_COMPILER_REF_QUALIFIERS
&&
#endif
{
oldWidgetValue.resolve(oldWidgetValue.resolve() & resolveMask);
current.resolve(current.resolve() & ~resolveMask);
current.resolve(oldWidgetValue);
current.resolve(current.resolve() | oldWidgetValue.resolve());
return current;
}
};
QHash<const QWidget *, Tampered<QPalette>> customPaletteWidgets;
QHash<const QWidget *, Tampered<QFont>> customFontWidgets;
};
template <typename T>
class QTypeInfo<QStyleSheetStyleCaches::Tampered<T>>
: QTypeInfoMerger<QStyleSheetStyleCaches::Tampered<T>, T> {};
QT_END_NAMESPACE QT_END_NAMESPACE
#endif // QT_NO_STYLE_STYLESHEET #endif // QT_NO_STYLE_STYLESHEET