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

qss font symbols

This commit is contained in:
krepa098 2014-10-01 11:12:02 +02:00
parent 7a292b815e
commit f7f0f696d8
2 changed files with 53 additions and 5 deletions

View File

@ -21,24 +21,45 @@
#include <QDebug>
#include <QMap>
#include <QRegularExpression>
#include <QWidget>
#include <QStyle>
// helper function
QFont appFont(int pixelSize, int weight) {
// helper functions
QFont appFont(int pixelSize, int weight)
{
auto font = QFont();
font.setPixelSize(pixelSize);
font.setWeight(weight);
return font;
}
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)
{
if (!Settings::getInstance().getUseNativeStyle())
{
QFile file(filename);
if (file.open(QFile::ReadOnly | QFile::Text))
return file.readAll();
return resolve(file.readAll());
else
qWarning() << "Stylesheet " << filename << " not found";
qWarning() << "Style: Stylesheet " << filename << " not found";
}
return QString();
@ -79,6 +100,7 @@ QFont Style::getFont(Style::Font font)
QString Style::resolve(QString qss)
{
static QMap<QString, QString> dict = {
// colors
{"@green", getColor(Green).name()},
{"@yellow", getColor(Yellow).name()},
{"@red", getColor(Red).name()},
@ -88,13 +110,37 @@ QString Style::resolve(QString qss)
{"@mediumGreyLight", getColor(MediumGreyLight).name()},
{"@lightGrey", getColor(LightGrey).name()},
{"@white", getColor(White).name()},
// 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))},
};
for (const QString& key : dict.keys())
{
qDebug() << key;
qss.replace(QRegularExpression(QString("%1\\b").arg(key)), dict[key]);
}
return qss;
}
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);
}
}
}

View File

@ -21,6 +21,7 @@
#include <QFont>
class QString;
class QWidget;
class Style
{
@ -53,6 +54,7 @@ public:
static QColor getColor(ColorPalette entry);
static QFont getFont(Font font);
static QString resolve(QString qss);
static void repolish(QWidget* w);
private:
Style();