1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/misc/style.cpp

147 lines
3.7 KiB
C++
Raw Normal View History

/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre 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.
This program 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 COPYING file for more details.
*/
2014-08-11 00:14:50 +08:00
#include "style.h"
#include "settings.h"
#include <QFile>
#include <QDebug>
2014-10-01 15:58:14 +08:00
#include <QMap>
#include <QRegularExpression>
2014-10-01 17:12:02 +08:00
#include <QWidget>
#include <QStyle>
2014-10-01 05:45:33 +08:00
2014-10-01 17:12:02 +08:00
// helper functions
QFont appFont(int pixelSize, int weight)
{
2014-10-01 05:45:33 +08:00
auto font = QFont();
font.setPixelSize(pixelSize);
font.setWeight(weight);
return font;
}
2014-08-11 00:14:50 +08:00
2014-10-01 17:12:02 +08:00
QString cssifyWeight(int weight)
{
QString weightStr = "normal";
if (weight == QFont::Bold)
weightStr = "bold";
if (weight == QFont::Light)
weightStr = "lighter";
return QString("%1").arg(weightStr);
}
QString cssifyFont(QFont font)
{
return QString("%1px %2")
.arg(font.pixelSize())
.arg(font.weight());
}
QString Style::getStylesheet(const QString &filename)
2014-08-11 00:14:50 +08:00
{
if (!Settings::getInstance().getUseNativeStyle())
{
QFile file(filename);
if (file.open(QFile::ReadOnly | QFile::Text))
2014-10-01 17:12:02 +08:00
return resolve(file.readAll());
2014-08-11 00:14:50 +08:00
else
2014-10-01 17:12:02 +08:00
qWarning() << "Style: Stylesheet " << filename << " not found";
2014-08-11 00:14:50 +08:00
}
return QString();
}
QColor Style::getColor(Style::ColorPalette entry)
{
static QColor palette[] = {
QColor("#6bc260"),
QColor("#cebf44"),
QColor("#c84e4e"),
QColor("#000000"),
QColor("#1c1c1c"),
QColor("#414141"),
2014-10-01 15:58:14 +08:00
QColor("#414141").lighter(120),
QColor("#d1d1d1"),
QColor("#ffffff"),
};
return palette[entry];
}
QFont Style::getFont(Style::Font font)
{
static QFont fonts[] = {
appFont(14, QFont::Bold),
appFont(12, QFont::Normal),
appFont(12, QFont::Bold),
appFont(11, QFont::Normal),
appFont(11, QFont::Bold),
appFont(10, QFont::Normal),
appFont(10, QFont::Light),
};
return fonts[font];
}
2014-10-01 15:58:14 +08:00
QString Style::resolve(QString qss)
{
static QMap<QString, QString> dict = {
2014-10-01 17:12:02 +08:00
// colors
2014-10-01 15:58:14 +08:00
{"@green", getColor(Green).name()},
{"@yellow", getColor(Yellow).name()},
{"@red", getColor(Red).name()},
{"@black", getColor(Black).name()},
{"@darkGrey", getColor(DarkGrey).name()},
{"@mediumGrey", getColor(MediumGrey).name()},
{"@mediumGreyLight", getColor(MediumGreyLight).name()},
{"@lightGrey", getColor(LightGrey).name()},
{"@white", getColor(White).name()},
2014-10-01 17:12:02 +08:00
// fonts
{"@extraBig", cssifyFont(getFont(ExtraBig))},
{"@big", cssifyFont(getFont(Big))},
{"@bigBold", cssifyFont(getFont(BigBold))},
{"@medium", cssifyFont(getFont(Medium))},
{"@mediumBold", cssifyFont(getFont(MediumBold))},
{"@small", cssifyFont(getFont(Small))},
{"@smallLight", cssifyFont(getFont(SmallLight))},
2014-10-01 15:58:14 +08:00
};
for (const QString& key : dict.keys())
{
qss.replace(QRegularExpression(QString("%1\\b").arg(key)), dict[key]);
}
return qss;
}
2014-10-01 17:12:02 +08:00
void Style::repolish(QWidget *w)
{
w->style()->unpolish(w);
w->style()->polish(w);
for (QObject* o : w->children())
{
QWidget* c = qobject_cast<QWidget*>(o);
if (c)
{
c->style()->unpolish(c);
c->style()->polish(c);
}
}
}