1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/***************************************************************************
* Copyright (C) 2008 by David Sansome *
* me@davidsansome.com *
* *
* This program 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 2 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 *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "gtkrcfile.h"
#include <QFile>
#include <QIODevice>
#include <QRegExp>
#include <QTextStream>
#include <QStringList>
#include <QtDebug>
QRegExp GtkRcFile::k_themeNameRe("([^/]+)/gtk-2.0/gtkrc$");
GtkRcFile::GtkRcFile(const QString& fileName)
: m_fileName(fileName)
{
}
void GtkRcFile::load()
{
QFile file(m_fileName);
file.open(QIODevice::ReadOnly);
QTextStream stream(&file);
QRegExp includeRe("include\\s*\"([^\"]*)\"");
QRegExp fontRe("gtk-font-name\\s*=\\s*\"([^\"]*)\"");
QStringList includes;
while (1)
{
QString line = stream.readLine();
if (line.isNull())
break;
if (line.startsWith("#"))
continue;
line = line.trimmed();
if (line.startsWith("include"))
{
if (includeRe.indexIn(line) == -1)
continue;
QString themePath = includeRe.cap(1);
if (themePath.startsWith("/etc"))
continue;
setTheme(themePath);
}
if (line.startsWith("gtk-font-name"))
{
if (fontRe.indexIn(line) == -1)
continue;
// Assume there will only be one font line
parseFont(fontRe.cap(1));
}
}
file.close();
}
void GtkRcFile::parseFont(QString fontString)
{
QFont font;
while (true)
{
int lastSpacePos = fontString.lastIndexOf(' ');
if (lastSpacePos == -1)
break;
QString lastWord = fontString.right(fontString.length() - lastSpacePos).trimmed();
if (lastWord.toLower() == "bold")
font.setBold(true);
else if (lastWord.toLower() == "italic")
font.setItalic(true);
else
{
bool ok;
int fontSize = lastWord.toInt(&ok);
if (!ok)
break;
font.setPointSize(fontSize);
}
fontString = fontString.left(lastSpacePos);
}
font.setFamily(fontString);
setFont(font);
}
void GtkRcFile::save()
{
QFile file(m_fileName);
file.open(QIODevice::WriteOnly);
QTextStream stream(&file);
QString fontName = m_font.family() + " " +
QString(m_font.bold() ? "Bold " : "") +
QString(m_font.italic() ? "Italic " : "") +
QString::number(m_font.pointSize());
stream << "# This file was written by KDE\n";
stream << "# You can edit it in the KDE control center, under \"GTK Styles and Fonts\"\n";
stream << "\n";
stream << "include \"" << m_themePath << "\"\n";
if (QFile::exists("/etc/gtk-2.0/gtkrc"))
stream << "include \"/etc/gtk-2.0/gtkrc\"\n";
stream << "\n";
stream << "style \"user-font\"\n";
stream << "{\n";
stream << "\tfont_name=\"" << m_font.family() << "\"\n";
stream << "}\n";
stream << "\n";
stream << "gtk-theme-name=\"" << m_themeName << "\"\n";
stream << "gtk-font-name=\"" << fontName << "\"\n";
}
void GtkRcFile::setFont(const QString& family, int pointSize, bool bold, bool italic)
{
QFont font(family, pointSize);
font.setBold(bold);
font.setItalic(italic);
setFont(font);
}
void GtkRcFile::setTheme(const QString& path)
{
if (k_themeNameRe.indexIn(path) == -1)
return;
m_themePath = path;
m_themeName = k_themeNameRe.cap(1);
}
|