2014-08-11 16:00:08 +08:00
|
|
|
/*
|
2016-11-28 21:33:38 +08:00
|
|
|
Copyright © 2014-2015 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
2014-08-11 16:00:08 +08:00
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
2014-08-11 16:00:08 +08:00
|
|
|
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.
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
2014-08-11 16:00:08 +08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-06-06 09:40:08 +08:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2014-08-11 16:00:08 +08:00
|
|
|
|
2015-06-06 09:40:08 +08:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
2014-08-11 16:00:08 +08:00
|
|
|
*/
|
|
|
|
|
2014-08-11 00:14:50 +08:00
|
|
|
#include "style.h"
|
2016-12-19 10:26:26 +08:00
|
|
|
#include "src/persistence/settings.h"
|
|
|
|
#include "src/widget/gui.h"
|
2014-11-16 04:30:20 +08:00
|
|
|
|
2014-08-11 00:14:50 +08:00
|
|
|
#include <QDebug>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QFile>
|
|
|
|
#include <QFontInfo>
|
2014-10-01 15:58:14 +08:00
|
|
|
#include <QMap>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QPainter>
|
2014-10-01 15:58:14 +08:00
|
|
|
#include <QRegularExpression>
|
2014-10-01 17:12:02 +08:00
|
|
|
#include <QStyle>
|
2015-02-12 20:18:04 +08:00
|
|
|
#include <QSvgRenderer>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <QWidget>
|
2014-10-01 05:45:33 +08:00
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @enum Style::Font
|
|
|
|
*
|
|
|
|
* @var ExtraBig
|
|
|
|
* @brief [SystemDefault + 2]px, bold
|
|
|
|
*
|
|
|
|
* @var Big
|
|
|
|
* @brief [SystemDefault]px
|
|
|
|
*
|
|
|
|
* @var BigBold
|
|
|
|
* @brief [SystemDefault]px, bold
|
|
|
|
*
|
|
|
|
* @var Medium
|
|
|
|
* @brief [SystemDefault - 1]px
|
|
|
|
*
|
|
|
|
* @var MediumBold
|
|
|
|
* @brief [SystemDefault - 1]px, bold
|
|
|
|
*
|
|
|
|
* @var Small
|
|
|
|
* @brief [SystemDefault - 2]px
|
|
|
|
*
|
|
|
|
* @var SmallLight
|
|
|
|
* @brief [SystemDefault - 2]px, light
|
|
|
|
*/
|
2016-07-27 06:20:39 +08:00
|
|
|
|
2014-10-01 17:12:02 +08:00
|
|
|
// helper functions
|
|
|
|
QFont appFont(int pixelSize, int weight)
|
|
|
|
{
|
2014-10-01 22:17:09 +08:00
|
|
|
QFont font;
|
2014-10-01 05:45:33 +08:00
|
|
|
font.setPixelSize(pixelSize);
|
|
|
|
font.setWeight(weight);
|
|
|
|
return font;
|
|
|
|
}
|
2014-08-11 00:14:50 +08:00
|
|
|
|
2014-10-01 22:17:09 +08:00
|
|
|
QString qssifyFont(QFont font)
|
2014-10-01 17:12:02 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return QString("%1 %2px \"%3\"").arg(font.weight() * 8).arg(font.pixelSize()).arg(font.family());
|
2014-10-01 17:12:02 +08:00
|
|
|
}
|
|
|
|
|
2014-11-16 04:30:20 +08:00
|
|
|
// colors as defined in
|
|
|
|
// https://github.com/ItsDuke/Tox-UI/blob/master/UI%20GUIDELINES.md
|
|
|
|
static QColor palette[] = {
|
2017-02-26 19:52:45 +08:00
|
|
|
QColor("#6bc260"), QColor("#cebf44"), QColor("#c84e4e"), QColor("#000000"), QColor("#1c1c1c"),
|
|
|
|
QColor("#414141"), QColor("#414141").lighter(120), QColor("#d1d1d1"), QColor("#ffffff"),
|
2014-11-16 04:30:20 +08:00
|
|
|
QColor("#ff7700"),
|
|
|
|
|
|
|
|
// Theme colors
|
2017-02-26 19:52:45 +08:00
|
|
|
QColor("#1c1c1c"), QColor("#2a2a2a"), QColor("#414141"), QColor("#4e4e4e"),
|
2014-11-16 04:30:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static QMap<QString, QString> dict;
|
|
|
|
|
2015-10-09 06:27:08 +08:00
|
|
|
QStringList Style::getThemeColorNames()
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
return {QObject::tr("Default"), QObject::tr("Blue"), QObject::tr("Olive"), QObject::tr("Red"),
|
|
|
|
QObject::tr("Violet")};
|
2015-10-09 06:27:08 +08:00
|
|
|
}
|
2014-11-16 04:30:20 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
QList<QColor> Style::themeColorColors = {QColor(), QColor("#004aa4"), QColor("#97ba00"),
|
|
|
|
QColor("#c23716"), QColor("#4617b5")};
|
2014-11-16 04:30:20 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
QString Style::getStylesheet(const QString& filename, const QFont& baseFont)
|
2014-08-11 00:14:50 +08:00
|
|
|
{
|
2015-06-20 19:50:20 +08:00
|
|
|
QFile file(filename);
|
2017-02-26 19:52:45 +08:00
|
|
|
if (!file.open(QFile::ReadOnly | QFile::Text)) {
|
2015-06-20 19:50:20 +08:00
|
|
|
qWarning() << "Stylesheet " << filename << " not found";
|
2015-06-26 20:15:27 +08:00
|
|
|
return QString();
|
|
|
|
}
|
2014-08-11 00:14:50 +08:00
|
|
|
|
2016-07-10 19:43:50 +08:00
|
|
|
return resolve(file.readAll(), baseFont);
|
2014-08-11 00:14:50 +08:00
|
|
|
}
|
2014-10-01 02:10:42 +08:00
|
|
|
|
|
|
|
QColor Style::getColor(Style::ColorPalette entry)
|
|
|
|
{
|
|
|
|
return palette[entry];
|
|
|
|
}
|
|
|
|
|
|
|
|
QFont Style::getFont(Style::Font font)
|
|
|
|
{
|
2014-10-03 22:31:42 +08:00
|
|
|
// fonts as defined in
|
|
|
|
// https://github.com/ItsDuke/Tox-UI/blob/master/UI%20GUIDELINES.md
|
2014-10-06 22:39:07 +08:00
|
|
|
|
|
|
|
static int defSize = QFontInfo(QFont()).pixelSize();
|
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
static QFont fonts[] = {
|
2017-02-26 19:52:45 +08:00
|
|
|
appFont(defSize + 3, QFont::Bold), // extra big
|
|
|
|
appFont(defSize + 1, QFont::Normal), // big
|
|
|
|
appFont(defSize + 1, QFont::Bold), // big bold
|
|
|
|
appFont(defSize, QFont::Normal), // medium
|
|
|
|
appFont(defSize, QFont::Bold), // medium bold
|
|
|
|
appFont(defSize - 1, QFont::Normal), // small
|
|
|
|
appFont(defSize - 1, QFont::Light), // small light
|
2014-10-01 02:10:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return fonts[font];
|
|
|
|
}
|
2014-10-01 15:58:14 +08:00
|
|
|
|
2016-07-10 19:43:50 +08:00
|
|
|
QString Style::resolve(QString qss, const QFont& baseFont)
|
2014-10-01 15:58:14 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (dict.isEmpty()) {
|
|
|
|
dict = {// colors
|
|
|
|
{"@green", Style::getColor(Style::Green).name()},
|
|
|
|
{"@yellow", Style::getColor(Style::Yellow).name()},
|
|
|
|
{"@red", Style::getColor(Style::Red).name()},
|
|
|
|
{"@black", Style::getColor(Style::Black).name()},
|
|
|
|
{"@darkGrey", Style::getColor(Style::DarkGrey).name()},
|
|
|
|
{"@mediumGrey", Style::getColor(Style::MediumGrey).name()},
|
|
|
|
{"@mediumGreyLight", Style::getColor(Style::MediumGreyLight).name()},
|
|
|
|
{"@lightGrey", Style::getColor(Style::LightGrey).name()},
|
|
|
|
{"@white", Style::getColor(Style::White).name()},
|
|
|
|
{"@orange", Style::getColor(Style::Orange).name()},
|
|
|
|
{"@themeDark", Style::getColor(Style::ThemeDark).name()},
|
|
|
|
{"@themeMediumDark", Style::getColor(Style::ThemeMediumDark).name()},
|
|
|
|
{"@themeMedium", Style::getColor(Style::ThemeMedium).name()},
|
|
|
|
{"@themeLight", Style::getColor(Style::ThemeLight).name()},
|
|
|
|
|
|
|
|
// fonts
|
|
|
|
{"@baseFont",
|
|
|
|
QString::fromUtf8("'%1' %2px").arg(baseFont.family()).arg(QFontInfo(baseFont).pixelSize())},
|
|
|
|
{"@extraBig", qssifyFont(Style::getFont(Style::ExtraBig))},
|
|
|
|
{"@big", qssifyFont(Style::getFont(Style::Big))},
|
|
|
|
{"@bigBold", qssifyFont(Style::getFont(Style::BigBold))},
|
|
|
|
{"@medium", qssifyFont(Style::getFont(Style::Medium))},
|
|
|
|
{"@mediumBold", qssifyFont(Style::getFont(Style::MediumBold))},
|
|
|
|
{"@small", qssifyFont(Style::getFont(Style::Small))},
|
|
|
|
{"@smallLight", qssifyFont(Style::getFont(Style::SmallLight))}};
|
2014-11-16 04:30:20 +08:00
|
|
|
}
|
2014-10-01 15:58:14 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
for (const QString& key : dict.keys()) {
|
2014-10-01 15:58:14 +08:00
|
|
|
qss.replace(QRegularExpression(QString("%1\\b").arg(key)), dict[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qss;
|
|
|
|
}
|
2014-10-01 17:12:02 +08:00
|
|
|
|
2017-01-06 19:02:54 +08:00
|
|
|
void Style::repolish(QWidget* w)
|
2014-10-01 17:12:02 +08:00
|
|
|
{
|
|
|
|
w->style()->unpolish(w);
|
|
|
|
w->style()->polish(w);
|
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
for (QObject* o : w->children()) {
|
2014-10-01 17:12:02 +08:00
|
|
|
QWidget* c = qobject_cast<QWidget*>(o);
|
2017-02-26 19:52:45 +08:00
|
|
|
if (c) {
|
2014-10-01 17:12:02 +08:00
|
|
|
c->style()->unpolish(c);
|
|
|
|
c->style()->polish(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-16 04:30:20 +08:00
|
|
|
|
|
|
|
void Style::setThemeColor(int color)
|
|
|
|
{
|
|
|
|
if (color < 0 || color >= themeColorColors.size())
|
|
|
|
setThemeColor(QColor());
|
|
|
|
else
|
|
|
|
setThemeColor(themeColorColors[color]);
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Set theme color.
|
|
|
|
* @param color Color to set.
|
|
|
|
*
|
|
|
|
* Pass an invalid QColor to reset to defaults.
|
|
|
|
*/
|
2017-02-26 19:52:45 +08:00
|
|
|
void Style::setThemeColor(const QColor& color)
|
2014-11-16 04:30:20 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (!color.isValid()) {
|
2014-11-16 04:30:20 +08:00
|
|
|
// Reset to default
|
|
|
|
palette[ThemeDark] = QColor("#1c1c1c");
|
|
|
|
palette[ThemeMediumDark] = QColor("#2a2a2a");
|
|
|
|
palette[ThemeMedium] = QColor("#414141");
|
|
|
|
palette[ThemeLight] = QColor("#4e4e4e");
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
2014-11-16 04:30:20 +08:00
|
|
|
palette[ThemeDark] = color.darker(155);
|
|
|
|
palette[ThemeMediumDark] = color.darker(135);
|
|
|
|
palette[ThemeMedium] = color.darker(120);
|
|
|
|
palette[ThemeLight] = color.lighter(110);
|
|
|
|
}
|
|
|
|
|
|
|
|
dict["@themeDark"] = getColor(ThemeDark).name();
|
|
|
|
dict["@themeMediumDark"] = getColor(ThemeMediumDark).name();
|
|
|
|
dict["@themeMedium"] = getColor(ThemeMedium).name();
|
|
|
|
dict["@themeLight"] = getColor(ThemeLight).name();
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:20:39 +08:00
|
|
|
/**
|
2016-08-01 16:21:23 +08:00
|
|
|
* @brief Reloads some CCS
|
|
|
|
*/
|
2014-11-16 04:30:20 +08:00
|
|
|
void Style::applyTheme()
|
|
|
|
{
|
2015-02-07 02:01:31 +08:00
|
|
|
GUI::reloadTheme();
|
2014-11-16 04:30:20 +08:00
|
|
|
}
|
2015-02-12 20:18:04 +08:00
|
|
|
|
|
|
|
QPixmap Style::scaleSvgImage(const QString& path, uint32_t width, uint32_t height)
|
|
|
|
{
|
|
|
|
QSvgRenderer render(path);
|
|
|
|
QPixmap pixmap(width, height);
|
|
|
|
pixmap.fill(QColor(0, 0, 0, 0));
|
|
|
|
QPainter painter(&pixmap);
|
|
|
|
render.render(&painter, pixmap.rect());
|
|
|
|
return pixmap;
|
|
|
|
}
|