Make the colours in the stylesheet obey the Qt colour scheme.
Fixes issue #37
This commit is contained in:
parent
bdae62d502
commit
cf2a8e73e2
|
@ -1,5 +1,5 @@
|
||||||
#playlist {
|
#playlist {
|
||||||
background-color: white;
|
background-color: %palette-base;
|
||||||
background-image: url(:logo.png);
|
background-image: url(:logo.png);
|
||||||
background-attachment: fixed;
|
background-attachment: fixed;
|
||||||
background-position: bottom right;
|
background-position: bottom right;
|
||||||
|
@ -22,8 +22,8 @@ QToolButton[popupMode="1"] {
|
||||||
}
|
}
|
||||||
|
|
||||||
QToolButton:hover {
|
QToolButton:hover {
|
||||||
border: 2px solid #fec798;
|
border: 2px solid %palette-highlight;
|
||||||
background-color: #fedbbd;
|
background-color: %palette-highlight-lighter;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
padding: 1px;
|
padding: 1px;
|
||||||
}
|
}
|
||||||
|
@ -33,8 +33,8 @@ QToolButton:hover[popupMode="1"] {
|
||||||
}
|
}
|
||||||
|
|
||||||
QToolButton:pressed {
|
QToolButton:pressed {
|
||||||
border: 2px solid #fe963e;
|
border: 2px solid %palette-highlight-darker;
|
||||||
background-color: #fedbbd;
|
background-color: %palette-highlight-lighter;
|
||||||
border-radius: 3px;
|
border-radius: 3px;
|
||||||
padding: 1px;
|
padding: 1px;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include "libraryconfigdialog.h"
|
#include "libraryconfigdialog.h"
|
||||||
#include "about.h"
|
#include "about.h"
|
||||||
#include "addstreamdialog.h"
|
#include "addstreamdialog.h"
|
||||||
|
#include "stylesheetloader.h"
|
||||||
|
|
||||||
#include "qxtglobalshortcut.h"
|
#include "qxtglobalshortcut.h"
|
||||||
|
|
||||||
|
@ -263,12 +264,8 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
multi_loading_indicator_->hide();
|
multi_loading_indicator_->hide();
|
||||||
|
|
||||||
// Load theme
|
// Load theme
|
||||||
QFile stylesheet(":mainwindow.css");
|
StyleSheetLoader* css_loader = new StyleSheetLoader(this);
|
||||||
if (!stylesheet.open(QIODevice::ReadOnly)) {
|
css_loader->SetStyleSheet(this, ":mainwindow.css");
|
||||||
qWarning("Could not open stylesheet");
|
|
||||||
} else {
|
|
||||||
setStyleSheet(stylesheet.readAll());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load settings
|
// Load settings
|
||||||
settings.beginGroup(kSettingsGroup);
|
settings.beginGroup(kSettingsGroup);
|
||||||
|
|
|
@ -54,7 +54,8 @@ SOURCES += main.cpp \
|
||||||
about.cpp \
|
about.cpp \
|
||||||
albumcoverfetcher.cpp \
|
albumcoverfetcher.cpp \
|
||||||
addstreamdialog.cpp \
|
addstreamdialog.cpp \
|
||||||
savedradio.cpp
|
savedradio.cpp \
|
||||||
|
stylesheetloader.cpp
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
player.h \
|
player.h \
|
||||||
library.h \
|
library.h \
|
||||||
|
@ -109,7 +110,8 @@ HEADERS += mainwindow.h \
|
||||||
about.h \
|
about.h \
|
||||||
albumcoverfetcher.h \
|
albumcoverfetcher.h \
|
||||||
addstreamdialog.h \
|
addstreamdialog.h \
|
||||||
savedradio.h
|
savedradio.h \
|
||||||
|
stylesheetloader.h
|
||||||
FORMS += mainwindow.ui \
|
FORMS += mainwindow.ui \
|
||||||
libraryconfig.ui \
|
libraryconfig.ui \
|
||||||
fileview.ui \
|
fileview.ui \
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
Loading…
Reference in New Issue