Make the colours in the stylesheet obey the Qt colour scheme.

Fixes issue #37
This commit is contained in:
David Sansome 2010-02-27 16:36:25 +00:00
parent bdae62d502
commit cf2a8e73e2
5 changed files with 122 additions and 15 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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)

77
src/stylesheetloader.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "stylesheetloader.h"
#include <QtDebug>
#include <QFile>
#include <QEvent>
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<QWidget*>(obj);
if (!widget || !filenames_.contains(widget))
return false;
UpdateStyleSheet(widget);
return false;
}

31
src/stylesheetloader.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef STYLESHEETLOADER_H
#define STYLESHEETLOADER_H
#include <QString>
#include <QPalette>
#include <QWidget>
#include <QMap>
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<QWidget*, QString> filenames_;
};
#endif // STYLESHEETLOADER_H