@@ -294,6 +294,7 @@ set(HEADERS
|
|||||||
core/standarditemiconloader.h
|
core/standarditemiconloader.h
|
||||||
core/systemtrayicon.h
|
core/systemtrayicon.h
|
||||||
core/mimedata.h
|
core/mimedata.h
|
||||||
|
core/stylesheetloader.h
|
||||||
|
|
||||||
engine/enginebase.h
|
engine/enginebase.h
|
||||||
engine/devicefinders.h
|
engine/devicefinders.h
|
||||||
|
|||||||
@@ -758,7 +758,6 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSD *osd, co
|
|||||||
app_->appearance()->LoadUserTheme();
|
app_->appearance()->LoadUserTheme();
|
||||||
StyleSheetLoader *css_loader = new StyleSheetLoader(this);
|
StyleSheetLoader *css_loader = new StyleSheetLoader(this);
|
||||||
css_loader->SetStyleSheet(this, ":/style/strawberry.css");
|
css_loader->SetStyleSheet(this, ":/style/strawberry.css");
|
||||||
RefreshStyleSheet();
|
|
||||||
|
|
||||||
// Load playlists
|
// Load playlists
|
||||||
app_->playlist_manager()->Init(app_->collection_backend(), app_->playlist_backend(), ui_->playlist_sequence, ui_->playlist);
|
app_->playlist_manager()->Init(app_->collection_backend(), app_->playlist_backend(), ui_->playlist_sequence, ui_->playlist);
|
||||||
@@ -859,8 +858,6 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSD *osd, co
|
|||||||
}
|
}
|
||||||
if (app_->scrobbler()->IsEnabled() && !app_->scrobbler()->IsOffline()) app_->scrobbler()->Submit();
|
if (app_->scrobbler()->IsEnabled() && !app_->scrobbler()->IsOffline()) app_->scrobbler()->Submit();
|
||||||
|
|
||||||
RefreshStyleSheet();
|
|
||||||
|
|
||||||
qLog(Debug) << "Started" << QThread::currentThread();
|
qLog(Debug) << "Started" << QThread::currentThread();
|
||||||
initialised_ = true;
|
initialised_ = true;
|
||||||
|
|
||||||
@@ -964,7 +961,6 @@ void MainWindow::ReloadAllSettings() {
|
|||||||
|
|
||||||
void MainWindow::RefreshStyleSheet() {
|
void MainWindow::RefreshStyleSheet() {
|
||||||
QString contents(styleSheet());
|
QString contents(styleSheet());
|
||||||
setStyleSheet("");
|
|
||||||
setStyleSheet(contents);
|
setStyleSheet(contents);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <QTimer>
|
||||||
#include <QIODevice>
|
#include <QIODevice>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
@@ -36,22 +37,16 @@
|
|||||||
#include "core/logging.h"
|
#include "core/logging.h"
|
||||||
#include "stylesheetloader.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());
|
connect(timer_reset_counter_, SIGNAL(timeout()), this, SLOT(ResetCounters()));
|
||||||
widget->installEventFilter(this);
|
|
||||||
UpdateStyleSheet(widget);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void StyleSheetLoader::UpdateStyleSheet(QWidget *widget) {
|
void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
|
||||||
|
|
||||||
if (!widget || !widgets_.contains(widget)) return;
|
|
||||||
|
|
||||||
QString filename(widgets_[widget].first);
|
|
||||||
QString stylesheet(widgets_[widget].second);
|
|
||||||
|
|
||||||
// Load the file
|
// Load the file
|
||||||
QFile file(filename);
|
QFile file(filename);
|
||||||
@@ -60,56 +55,71 @@ void StyleSheetLoader::UpdateStyleSheet(QWidget *widget) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QTextStream stream(&file);
|
QTextStream stream(&file);
|
||||||
QString contents;
|
QString stylesheet;
|
||||||
forever {
|
forever {
|
||||||
QString line = stream.readLine();
|
QString line = stream.readLine();
|
||||||
contents.append(line);
|
stylesheet.append(line);
|
||||||
if (stream.atEnd()) break;
|
if (stream.atEnd()) break;
|
||||||
}
|
}
|
||||||
file.close();
|
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
|
// Replace %palette-role with actual colours
|
||||||
QPalette p(widget->palette());
|
QPalette p(widget->palette());
|
||||||
|
|
||||||
QColor alt = p.color(QPalette::AlternateBase);
|
QColor alt = p.color(QPalette::AlternateBase);
|
||||||
alt.setAlpha(50);
|
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.red())
|
||||||
.arg(alt.green())
|
.arg(alt.green())
|
||||||
.arg(alt.blue())
|
.arg(alt.blue())
|
||||||
.arg(alt.alpha()));
|
.arg(alt.alpha()));
|
||||||
|
|
||||||
ReplaceColor(&contents, "Window", p, QPalette::Window);
|
ReplaceColor(&stylesheet, "Window", p, QPalette::Window);
|
||||||
ReplaceColor(&contents, "Background", p, QPalette::Background);
|
ReplaceColor(&stylesheet, "Background", p, QPalette::Background);
|
||||||
ReplaceColor(&contents, "WindowText", p, QPalette::WindowText);
|
ReplaceColor(&stylesheet, "WindowText", p, QPalette::WindowText);
|
||||||
ReplaceColor(&contents, "Foreground", p, QPalette::Foreground);
|
ReplaceColor(&stylesheet, "Foreground", p, QPalette::Foreground);
|
||||||
ReplaceColor(&contents, "Base", p, QPalette::Base);
|
ReplaceColor(&stylesheet, "Base", p, QPalette::Base);
|
||||||
ReplaceColor(&contents, "AlternateBase", p, QPalette::AlternateBase);
|
ReplaceColor(&stylesheet, "AlternateBase", p, QPalette::AlternateBase);
|
||||||
ReplaceColor(&contents, "ToolTipBase", p, QPalette::ToolTipBase);
|
ReplaceColor(&stylesheet, "ToolTipBase", p, QPalette::ToolTipBase);
|
||||||
ReplaceColor(&contents, "ToolTipText", p, QPalette::ToolTipText);
|
ReplaceColor(&stylesheet, "ToolTipText", p, QPalette::ToolTipText);
|
||||||
ReplaceColor(&contents, "Text", p, QPalette::Text);
|
ReplaceColor(&stylesheet, "Text", p, QPalette::Text);
|
||||||
ReplaceColor(&contents, "Button", p, QPalette::Button);
|
ReplaceColor(&stylesheet, "Button", p, QPalette::Button);
|
||||||
ReplaceColor(&contents, "ButtonText", p, QPalette::ButtonText);
|
ReplaceColor(&stylesheet, "ButtonText", p, QPalette::ButtonText);
|
||||||
ReplaceColor(&contents, "BrightText", p, QPalette::BrightText);
|
ReplaceColor(&stylesheet, "BrightText", p, QPalette::BrightText);
|
||||||
ReplaceColor(&contents, "Light", p, QPalette::Light);
|
ReplaceColor(&stylesheet, "Light", p, QPalette::Light);
|
||||||
ReplaceColor(&contents, "Midlight", p, QPalette::Midlight);
|
ReplaceColor(&stylesheet, "Midlight", p, QPalette::Midlight);
|
||||||
ReplaceColor(&contents, "Dark", p, QPalette::Dark);
|
ReplaceColor(&stylesheet, "Dark", p, QPalette::Dark);
|
||||||
ReplaceColor(&contents, "Mid", p, QPalette::Mid);
|
ReplaceColor(&stylesheet, "Mid", p, QPalette::Mid);
|
||||||
ReplaceColor(&contents, "Shadow", p, QPalette::Shadow);
|
ReplaceColor(&stylesheet, "Shadow", p, QPalette::Shadow);
|
||||||
ReplaceColor(&contents, "Highlight", p, QPalette::Highlight);
|
ReplaceColor(&stylesheet, "Highlight", p, QPalette::Highlight);
|
||||||
ReplaceColor(&contents, "HighlightedText", p, QPalette::HighlightedText);
|
ReplaceColor(&stylesheet, "HighlightedText", p, QPalette::HighlightedText);
|
||||||
ReplaceColor(&contents, "Link", p, QPalette::Link);
|
ReplaceColor(&stylesheet, "Link", p, QPalette::Link);
|
||||||
ReplaceColor(&contents, "LinkVisited", p, QPalette::LinkVisited);
|
ReplaceColor(&stylesheet, "LinkVisited", p, QPalette::LinkVisited);
|
||||||
|
|
||||||
#ifdef Q_OS_MACOS
|
#ifdef Q_OS_MACOS
|
||||||
contents.replace("macos", "*");
|
stylesheet.replace("macos", "*");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (contents == stylesheet) return;
|
if (stylesheet != styledata.stylesheet_current_) {
|
||||||
|
widget->setStyleSheet(stylesheet);
|
||||||
widget->setStyleSheet("");
|
styledata.stylesheet_current_ = widget->styleSheet();
|
||||||
widget->setStyleSheet(contents);
|
styledata_[widget] = styledata;
|
||||||
widgets_[widget] = qMakePair(filename, contents);
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,9 +136,23 @@ bool StyleSheetLoader::eventFilter(QObject *obj, QEvent *event) {
|
|||||||
if (event->type() != QEvent::PaletteChange) return false;
|
if (event->type() != QEvent::PaletteChange) return false;
|
||||||
|
|
||||||
QWidget *widget = qobject_cast<QWidget*>(obj);
|
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;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StyleSheetLoader::ResetCounters() {
|
||||||
|
|
||||||
|
for (QMap<QWidget*, StyleSheetData>::iterator i = styledata_.begin() ; i != styledata_.end() ; ++i) {
|
||||||
|
i.value().count_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -31,9 +31,12 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
class QWidget;
|
class QWidget;
|
||||||
|
class QTimer;
|
||||||
class QEvent;
|
class QEvent;
|
||||||
|
|
||||||
class StyleSheetLoader : public QObject {
|
class StyleSheetLoader : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit StyleSheetLoader(QObject *parent = nullptr);
|
explicit StyleSheetLoader(QObject *parent = nullptr);
|
||||||
|
|
||||||
@@ -46,12 +49,24 @@ class StyleSheetLoader : public QObject {
|
|||||||
bool eventFilter(QObject *obj, QEvent *event);
|
bool eventFilter(QObject *obj, QEvent *event);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void UpdateStyleSheet(QWidget *widget);
|
struct StyleSheetData {
|
||||||
void ReplaceColor(QString *css, const QString name, const QPalette &palette, QPalette::ColorRole role) const;
|
StyleSheetData() : count_(0) {}
|
||||||
|
QString filename_;
|
||||||
|
QString stylesheet_template_;
|
||||||
|
QString stylesheet_current_;
|
||||||
|
int count_;
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMap<QWidget*, QPair<QString, QString>> widgets_;
|
void UpdateStyleSheet(QWidget *widget, StyleSheetData styledata);
|
||||||
|
void ReplaceColor(QString *css, const QString name, const QPalette &palette, QPalette::ColorRole role) const;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void ResetCounters();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QMap<QWidget*, StyleSheetData> styledata_;
|
||||||
|
QTimer *timer_reset_counter_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // STYLESHEETLOADER_H
|
#endif // STYLESHEETLOADER_H
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user