Source

pynoto / src / Theme / Theme.cpp

#include <QDebug>
#include <QApplication>
#include <QPalette>
#include <QDir>
#include <QStringBuilder>
#include "Theme.h"

namespace Theme {

Theme::Theme(const QString& name, bool temp):
    ITheme(),
    _name(name)
{
    QSettings set;
    QDir dir(set.fileName());
    dir.cdUp();
    dir.cd("themes");
    _themePath = dir.filePath(name+".conf"+(temp ? "~" : ""));
    _theme = new QSettings(_themePath, QSettings::IniFormat);
}

Theme::~Theme()
{
    delete _theme;
}

QTextCharFormat Theme::format(const QString& lexer, const QString& role, const QColor& defColor, bool isbold)
{
    QTextCharFormat fmt;
    fmt.setForeground(color(lexer, role, defColor));
    fmt.setBackground(background(lexer, role));
    fmt.setFontWeight(bold(lexer, role) ? QFont::Bold: QFont::Normal);
    fmt.setFontItalic(italic(lexer, role));
    fmt.setFontUnderline(underline(lexer, role));
    return fmt;
}

QTextCharFormat Theme::defFormat(const QString& lexer, const QString& role, const QString& defRole, bool isbold)
{
    QString fromLexer = lexer;
    QString fromRole = defRole;
    QStringList defs = defRole.split("/");
    if (defs.length() > 1){
        fromLexer = defs[0];
        fromRole = defs[1];
    }
    QTextCharFormat def = format(fromLexer, fromRole, Qt::black);
    QTextCharFormat fmt;
    fmt.setForeground(color(lexer, role, def.foreground().color()));
    fmt.setBackground(background(lexer, role, def.background().color()));
    fmt.setFontWeight(bold(lexer, role, isbold) ? QFont::Bold: QFont::Normal);
    fmt.setFontItalic(italic(lexer, role, def.fontItalic()));
    fmt.setFontUnderline(underline(lexer, role, def.fontUnderline()));
    return fmt;
}

QColor Theme::color(const QString& lexer, const QString& role, const QColor& def)
{
    return QColor(_theme->value(lexer % "/" % role % "/color", colorName(def)).toString());
}

QColor Theme::background(const QString& lexer, const QString& role, const QColor& def)
{
    return QColor(_theme->value(lexer % "/" % role % "/background", colorName(def)).toString());
}

QString Theme::colorName(const QColor& color)
{
    if (color.alpha() == 0)
        return "transparent";
    return color.name();
}

void Theme::setColor(const QString& lexer, const QString& role, const QColor& color)
{
    _theme->setValue(lexer % "/" % role % "/color", colorName(color));
}

void Theme::setBackground(const QString& lexer, const QString& role, const QColor& color)
{
    QString name = colorName(color);
    if (name == "transparent")
        _theme->remove(lexer % "/" % role % "/background");
    else
        _theme->setValue(lexer % "/" % role % "/background", name);
}

bool Theme::italic(const QString& lexer, const QString& role, bool def)
{
    return _theme->value(lexer % "/" % role % "/italic", def).toBool();
}

bool Theme::bold(const QString& lexer, const QString& role, bool def)
{
    return _theme->value(lexer % "/" % role % "/bold", def).toBool();
}

bool Theme::underline(const QString& lexer, const QString& role, bool def)
{
    return _theme->value(lexer % "/" % role % "/underline", def).toBool();
}

void Theme::setItalic(const QString& lexer, const QString& role, bool on)
{
    if (!on)
        _theme->remove(lexer % "/" % role % "/italic");
    else
        _theme->setValue(lexer % "/" % role % "/italic", on);
}

void Theme::setBold(const QString& lexer, const QString& role, bool on)
{
    if (!on)
        _theme->remove(lexer % "/" % role % "/bold");
    else
        _theme->setValue(lexer % "/" % role % "/bold", on);
}

void Theme::setUnderline(const QString& lexer, const QString& role, bool on)
{
    if (!on)
        _theme->remove(lexer % "/" % role % "/underline");
    else
        _theme->setValue(lexer % "/" % role % "/underline", on);
}

void Theme::save()
{
    _theme->sync();
}

}