From cf2a8e73e27ace847b16869d43c90baef99a3bb8 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 27 Feb 2010 16:36:25 +0000 Subject: [PATCH] Make the colours in the stylesheet obey the Qt colour scheme. Fixes issue #37 --- data/mainwindow.css | 10 +++--- src/mainwindow.cpp | 9 ++--- src/src.pro | 10 +++--- src/stylesheetloader.cpp | 77 ++++++++++++++++++++++++++++++++++++++++ src/stylesheetloader.h | 31 ++++++++++++++++ 5 files changed, 122 insertions(+), 15 deletions(-) create mode 100644 src/stylesheetloader.cpp create mode 100644 src/stylesheetloader.h diff --git a/data/mainwindow.css b/data/mainwindow.css index ab1f685ae..ddaaf1abc 100644 --- a/data/mainwindow.css +++ b/data/mainwindow.css @@ -1,5 +1,5 @@ #playlist { - background-color: white; + background-color: %palette-base; background-image: url(:logo.png); background-attachment: fixed; background-position: bottom right; @@ -22,8 +22,8 @@ QToolButton[popupMode="1"] { } QToolButton:hover { - border: 2px solid #fec798; - background-color: #fedbbd; + border: 2px solid %palette-highlight; + background-color: %palette-highlight-lighter; border-radius: 3px; padding: 1px; } @@ -33,8 +33,8 @@ QToolButton:hover[popupMode="1"] { } QToolButton:pressed { - border: 2px solid #fe963e; - background-color: #fedbbd; + border: 2px solid %palette-highlight-darker; + background-color: %palette-highlight-lighter; border-radius: 3px; padding: 1px; } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b3a72ef27..84d645e19 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -16,6 +16,7 @@ #include "libraryconfigdialog.h" #include "about.h" #include "addstreamdialog.h" +#include "stylesheetloader.h" #include "qxtglobalshortcut.h" @@ -263,12 +264,8 @@ MainWindow::MainWindow(QWidget *parent) multi_loading_indicator_->hide(); // Load theme - QFile stylesheet(":mainwindow.css"); - if (!stylesheet.open(QIODevice::ReadOnly)) { - qWarning("Could not open stylesheet"); - } else { - setStyleSheet(stylesheet.readAll()); - } + StyleSheetLoader* css_loader = new StyleSheetLoader(this); + css_loader->SetStyleSheet(this, ":mainwindow.css"); // Load settings settings.beginGroup(kSettingsGroup); diff --git a/src/src.pro b/src/src.pro index 5dc396016..c7bb6aee4 100644 --- a/src/src.pro +++ b/src/src.pro @@ -54,7 +54,8 @@ SOURCES += main.cpp \ about.cpp \ albumcoverfetcher.cpp \ addstreamdialog.cpp \ - savedradio.cpp + savedradio.cpp \ + stylesheetloader.cpp HEADERS += mainwindow.h \ player.h \ library.h \ @@ -109,7 +110,8 @@ HEADERS += mainwindow.h \ about.h \ albumcoverfetcher.h \ addstreamdialog.h \ - savedradio.h + savedradio.h \ + stylesheetloader.h FORMS += mainwindow.ui \ libraryconfig.ui \ fileview.ui \ @@ -178,9 +180,9 @@ win32|fedora-win32-cross:LIBS += -ltag \ -lpthreadGC2 # OSD -unix:!macx { +unix:!macx { SOURCES += osd_x11.cpp - !nolibnotify { + !nolibnotify { DEFINES += HAVE_LIBNOTIFY QMAKE_CXXFLAGS += $$system(pkg-config --cflags libnotify) LIBS += $$system(pkg-config --libs libnotify) diff --git a/src/stylesheetloader.cpp b/src/stylesheetloader.cpp new file mode 100644 index 000000000..24bfccfd1 --- /dev/null +++ b/src/stylesheetloader.cpp @@ -0,0 +1,77 @@ +#include "stylesheetloader.h" + +#include +#include +#include + +StyleSheetLoader::StyleSheetLoader(QObject* parent) + : QObject(parent) +{ +} + +void StyleSheetLoader::SetStyleSheet(QWidget* widget, const QString &filename) { + filenames_[widget] = filename; + widget->installEventFilter(this); + UpdateStyleSheet(widget); +} + +void StyleSheetLoader::UpdateStyleSheet(QWidget *widget) { + QString filename(filenames_[widget]); + + // Load the file + QFile file(filename); + if (!file.open(QIODevice::ReadOnly)) { + qWarning() << __PRETTY_FUNCTION__ << "error opening" << filename; + return; + } + QString contents(file.readAll()); + + // Replace %palette-role with actual colours + QPalette p(widget->palette()); + 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); + + widget->setStyleSheet(contents); +} + +void StyleSheetLoader::ReplaceColor(QString* css, const QString& name, + const QPalette& palette, + QPalette::ColorRole role) const { + css->replace("%palette-" + name + "-lighter", + palette.color(role).lighter().name(), Qt::CaseInsensitive); + css->replace("%palette-" + name + "-darker", + palette.color(role).darker().name(), Qt::CaseInsensitive); + css->replace("%palette-" + name, + palette.color(role).name(), Qt::CaseInsensitive); +} + +bool StyleSheetLoader::eventFilter(QObject* obj, QEvent* event) { + if (event->type() != QEvent::PaletteChange) + return false; + + QWidget* widget = qobject_cast(obj); + if (!widget || !filenames_.contains(widget)) + return false; + + UpdateStyleSheet(widget); + return false; +} diff --git a/src/stylesheetloader.h b/src/stylesheetloader.h new file mode 100644 index 000000000..15da20ce4 --- /dev/null +++ b/src/stylesheetloader.h @@ -0,0 +1,31 @@ +#ifndef STYLESHEETLOADER_H +#define STYLESHEETLOADER_H + +#include +#include +#include +#include + +class StyleSheetLoader : public QObject { + public: + StyleSheetLoader(QObject* parent = 0); + + // Sets the given stylesheet on the given widget. + // If the stylesheet contains strings like %palette-[role], these get replaced + // with actual palette colours. + // The stylesheet is reloaded when the widget's palette changes. + void SetStyleSheet(QWidget* widget, const QString& filename); + + protected: + bool eventFilter(QObject* obj, QEvent* event); + + private: + void UpdateStyleSheet(QWidget* widget); + void ReplaceColor(QString* css, const QString& name, const QPalette& palette, + QPalette::ColorRole role) const; + + private: + QMap filenames_; +}; + +#endif // STYLESHEETLOADER_H