strawberry-audio-player-win.../src/core/stylesheetloader.cpp

165 lines
5.4 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2018-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#include "config.h"
2021-06-20 19:04:08 +02:00
#include <chrono>
2020-02-08 03:40:30 +01:00
#include <QtGlobal>
#include <QObject>
#include <QWidget>
2021-07-12 08:28:52 +02:00
#include <QHash>
#include <QTimer>
#include <QIODevice>
2020-02-08 03:40:30 +01:00
#include <QTextStream>
2018-02-27 18:06:05 +01:00
#include <QFile>
#include <QString>
#include <QColor>
#include <QPalette>
2020-02-08 03:40:30 +01:00
#include <QEvent>
#include <QtDebug>
#include "core/logging.h"
#include "stylesheetloader.h"
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
using namespace std::chrono_literals;
2021-07-11 07:40:57 +02:00
StyleSheetLoader::StyleSheetLoader(QObject *parent)
: QObject(parent),
timer_reset_counter_(new QTimer(this)) {
2018-02-27 18:06:05 +01:00
timer_reset_counter_->setSingleShot(true);
2021-06-20 19:04:08 +02:00
timer_reset_counter_->setInterval(1s);
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
QObject::connect(timer_reset_counter_, &QTimer::timeout, this, &StyleSheetLoader::ResetCounters);
2018-02-27 18:06:05 +01:00
}
void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
2018-02-27 18:06:05 +01:00
// Load the file
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
qLog(Error) << "Could not open stylesheet file" << filename << "for reading:" << file.errorString();
2018-02-27 18:06:05 +01:00
return;
}
2019-01-10 20:07:17 +01:00
QTextStream stream(&file);
QString stylesheet;
2019-01-10 20:07:17 +01:00
forever {
QString line = stream.readLine();
stylesheet.append(line);
2019-01-10 20:07:17 +01:00
if (stream.atEnd()) break;
}
file.close();
2018-02-27 18:06:05 +01:00
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_;
2018-02-27 18:06:05 +01:00
// Replace %palette-role with actual colours
QPalette p(widget->palette());
QColor alt = p.color(QPalette::AlternateBase);
alt.setAlpha(50);
stylesheet.replace("%palette-alternate-base", QString("rgba(%1,%2,%3,%4%)")
2018-02-27 18:06:05 +01:00
.arg(alt.red())
.arg(alt.green())
.arg(alt.blue())
.arg(alt.alpha()));
ReplaceColor(&stylesheet, "Window", p, QPalette::Window);
ReplaceColor(&stylesheet, "Background", p, QPalette::Window);
ReplaceColor(&stylesheet, "WindowText", p, QPalette::WindowText);
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);
2018-02-27 18:06:05 +01:00
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
stylesheet.replace("macos", "*");
2018-02-27 18:06:05 +01:00
#endif
if (stylesheet != styledata.stylesheet_current_) {
widget->setStyleSheet(stylesheet);
styledata.stylesheet_current_ = widget->styleSheet();
styledata_[widget] = styledata;
}
2018-02-27 18:06:05 +01:00
}
2021-06-22 13:41:38 +02:00
void StyleSheetLoader::ReplaceColor(QString *css, const QString &name, const QPalette &palette, QPalette::ColorRole role) {
2018-02-27 18:06:05 +01:00
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 || !styledata_.contains(widget)) return false;
StyleSheetData styledata = styledata_[widget];
++styledata.count_;
styledata_[widget] = styledata;
timer_reset_counter_->start();
if (styledata.count_ < 5) {
UpdateStyleSheet(widget, styledata);
}
2018-02-27 18:06:05 +01:00
return false;
}
void StyleSheetLoader::ResetCounters() {
2021-07-12 08:28:52 +02:00
for (QHash<QWidget*, StyleSheetData>::iterator it = styledata_.begin(); it != styledata_.end(); ++it) {
it.value().count_ = 0;
}
}