Fix infinite loop in stylesheetloader

Fixes #361
This commit is contained in:
Jonas Kvinge
2020-05-06 21:43:44 +02:00
parent 2c91877f83
commit 85fa86625b
4 changed files with 87 additions and 51 deletions

View File

@@ -24,6 +24,7 @@
#include <QtGlobal>
#include <QObject>
#include <QWidget>
#include <QTimer>
#include <QIODevice>
#include <QTextStream>
#include <QFile>
@@ -36,22 +37,16 @@
#include "core/logging.h"
#include "stylesheetloader.h"
StyleSheetLoader::StyleSheetLoader(QObject *parent) : QObject(parent) {}
StyleSheetLoader::StyleSheetLoader(QObject *parent) : QObject(parent), timer_reset_counter_(new QTimer(this)) {
void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
timer_reset_counter_->setSingleShot(true);
timer_reset_counter_->setInterval(1000);
widgets_[widget] = qMakePair(filename, QString());
widget->installEventFilter(this);
UpdateStyleSheet(widget);
connect(timer_reset_counter_, SIGNAL(timeout()), this, SLOT(ResetCounters()));
}
void StyleSheetLoader::UpdateStyleSheet(QWidget *widget) {
if (!widget || !widgets_.contains(widget)) return;
QString filename(widgets_[widget].first);
QString stylesheet(widgets_[widget].second);
void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
// Load the file
QFile file(filename);
@@ -60,56 +55,71 @@ void StyleSheetLoader::UpdateStyleSheet(QWidget *widget) {
return;
}
QTextStream stream(&file);
QString contents;
QString stylesheet;
forever {
QString line = stream.readLine();
contents.append(line);
stylesheet.append(line);
if (stream.atEnd()) break;
}
file.close();
StyleSheetData styledata;
styledata.filename_ = filename;
styledata.stylesheet_template_ = stylesheet;
styledata.stylesheet_current_ = widget->styleSheet();
styledata_[widget] = styledata;
widget->installEventFilter(this);
UpdateStyleSheet(widget, styledata);
}
void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, StyleSheetData styledata) {
QString stylesheet = styledata.stylesheet_template_;
// Replace %palette-role with actual colours
QPalette p(widget->palette());
QColor alt = p.color(QPalette::AlternateBase);
alt.setAlpha(50);
contents.replace("%palette-alternate-base", QString("rgba(%1,%2,%3,%4%)")
stylesheet.replace("%palette-alternate-base", QString("rgba(%1,%2,%3,%4%)")
.arg(alt.red())
.arg(alt.green())
.arg(alt.blue())
.arg(alt.alpha()));
ReplaceColor(&contents, "Window", p, QPalette::Window);
ReplaceColor(&contents, "Background", p, QPalette::Background);
ReplaceColor(&contents, "WindowText", p, QPalette::WindowText);
ReplaceColor(&contents, "Foreground", p, QPalette::Foreground);
ReplaceColor(&contents, "Base", p, QPalette::Base);
ReplaceColor(&contents, "AlternateBase", p, QPalette::AlternateBase);
ReplaceColor(&contents, "ToolTipBase", p, QPalette::ToolTipBase);
ReplaceColor(&contents, "ToolTipText", p, QPalette::ToolTipText);
ReplaceColor(&contents, "Text", p, QPalette::Text);
ReplaceColor(&contents, "Button", p, QPalette::Button);
ReplaceColor(&contents, "ButtonText", p, QPalette::ButtonText);
ReplaceColor(&contents, "BrightText", p, QPalette::BrightText);
ReplaceColor(&contents, "Light", p, QPalette::Light);
ReplaceColor(&contents, "Midlight", p, QPalette::Midlight);
ReplaceColor(&contents, "Dark", p, QPalette::Dark);
ReplaceColor(&contents, "Mid", p, QPalette::Mid);
ReplaceColor(&contents, "Shadow", p, QPalette::Shadow);
ReplaceColor(&contents, "Highlight", p, QPalette::Highlight);
ReplaceColor(&contents, "HighlightedText", p, QPalette::HighlightedText);
ReplaceColor(&contents, "Link", p, QPalette::Link);
ReplaceColor(&contents, "LinkVisited", p, QPalette::LinkVisited);
ReplaceColor(&stylesheet, "Window", p, QPalette::Window);
ReplaceColor(&stylesheet, "Background", p, QPalette::Background);
ReplaceColor(&stylesheet, "WindowText", p, QPalette::WindowText);
ReplaceColor(&stylesheet, "Foreground", p, QPalette::Foreground);
ReplaceColor(&stylesheet, "Base", p, QPalette::Base);
ReplaceColor(&stylesheet, "AlternateBase", p, QPalette::AlternateBase);
ReplaceColor(&stylesheet, "ToolTipBase", p, QPalette::ToolTipBase);
ReplaceColor(&stylesheet, "ToolTipText", p, QPalette::ToolTipText);
ReplaceColor(&stylesheet, "Text", p, QPalette::Text);
ReplaceColor(&stylesheet, "Button", p, QPalette::Button);
ReplaceColor(&stylesheet, "ButtonText", p, QPalette::ButtonText);
ReplaceColor(&stylesheet, "BrightText", p, QPalette::BrightText);
ReplaceColor(&stylesheet, "Light", p, QPalette::Light);
ReplaceColor(&stylesheet, "Midlight", p, QPalette::Midlight);
ReplaceColor(&stylesheet, "Dark", p, QPalette::Dark);
ReplaceColor(&stylesheet, "Mid", p, QPalette::Mid);
ReplaceColor(&stylesheet, "Shadow", p, QPalette::Shadow);
ReplaceColor(&stylesheet, "Highlight", p, QPalette::Highlight);
ReplaceColor(&stylesheet, "HighlightedText", p, QPalette::HighlightedText);
ReplaceColor(&stylesheet, "Link", p, QPalette::Link);
ReplaceColor(&stylesheet, "LinkVisited", p, QPalette::LinkVisited);
#ifdef Q_OS_MACOS
contents.replace("macos", "*");
stylesheet.replace("macos", "*");
#endif
if (contents == stylesheet) return;
widget->setStyleSheet("");
widget->setStyleSheet(contents);
widgets_[widget] = qMakePair(filename, contents);
if (stylesheet != styledata.stylesheet_current_) {
widget->setStyleSheet(stylesheet);
styledata.stylesheet_current_ = widget->styleSheet();
styledata_[widget] = styledata;
}
}
@@ -126,9 +136,23 @@ bool StyleSheetLoader::eventFilter(QObject *obj, QEvent *event) {
if (event->type() != QEvent::PaletteChange) return false;
QWidget *widget = qobject_cast<QWidget*>(obj);
if (!widget || !widgets_.contains(widget)) return false;
if (!widget || !styledata_.contains(widget)) return false;
UpdateStyleSheet(widget);
StyleSheetData styledata = styledata_[widget];
++styledata.count_;
styledata_[widget] = styledata;
timer_reset_counter_->start();
if (styledata.count_ < 5) {
UpdateStyleSheet(widget, styledata);
}
return false;
}
void StyleSheetLoader::ResetCounters() {
for (QMap<QWidget*, StyleSheetData>::iterator i = styledata_.begin() ; i != styledata_.end() ; ++i) {
i.value().count_ = 0;
}
}