Generate and load settings.qss
[quassel.git] / src / qtui / qtuistyle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "chatviewsettings.h"
22 #include "qtuistyle.h"
23
24 #include <QFile>
25 #include <QTextStream>
26
27 QtUiStyle::QtUiStyle(QObject *parent) : UiStyle(parent) {
28   ChatViewSettings s;
29   s.notify("TimestampFormat", this, SLOT(updateTimestampFormatString()));
30   updateTimestampFormatString();
31 }
32
33 QtUiStyle::~QtUiStyle() {}
34
35 void QtUiStyle::updateTimestampFormatString() {
36   ChatViewSettings s;
37   setTimestampFormatString(s.timestampFormatString());
38 }
39
40 void QtUiStyle::generateSettingsQss() const {
41   QFile settingsQss(Quassel::configDirPath() + "settings.qss");
42   if(!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
43     qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
44     return;
45   }
46   QTextStream out(&settingsQss);
47
48   out << "// Style settings made in Quassel's configuration dialog\n"
49       << "// This file is automatically generated, do not edit\n\n";
50
51   QtUiStyleSettings fs("Fonts");
52   QFont font = fs.value("ChatView").value<QFont>();
53   QString desc = "font: ";
54   if(font.italic())
55     desc += "italic ";
56   if(font.bold())
57     desc += "bold ";
58   if(!font.italic() && !font.bold())
59     desc += "normal ";
60   desc += QString("%1pt \"%2\"").arg(font.pointSize()).arg(font.family());
61
62   out << "// ChatView Font\n\n"
63       << "ChatLine { " << desc << "; }\n\n";
64
65   QtUiStyleSettings s("Colors");
66   if(s.value("UseChatViewColors").toBool()) {
67     out << "// Custom ChatView Colors\n\n"
68
69         << "Palette { marker-line: " << color("MarkerLine", s) << "; }\n"
70         << "ChatView { background: " << color("ChatViewBackground", s) << "; }\n\n"
71         << "ChatLine[label=\"highlight\"] {\n"
72         << "  foreground: " << color("Highlight",s) << ";\n"
73         << "  background: " << color("HighlightBackground", s) << ";\n"
74         << "}\n\n"
75         << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
76
77         << msgTypeQss("plain", "ChannelMsg", s)
78         << msgTypeQss("notice", "ServerMsg", s)
79         << msgTypeQss("action", "ActionMsg", s)
80         << msgTypeQss("nick", "CommandMsg", s)
81         << msgTypeQss("mode", "CommandMsg", s)
82         << msgTypeQss("join", "CommandMsg", s)
83         << msgTypeQss("part", "CommandMsg", s)
84         << msgTypeQss("quit", "CommandMsg", s)
85         << msgTypeQss("kick", "CommandMsg", s)
86         << msgTypeQss("kill", "CommandMsg", s)
87         << msgTypeQss("server", "ServerMsg", s)
88         << msgTypeQss("info", "ServerMsg", s)
89         << msgTypeQss("error", "ErrorMsg", s)
90         << msgTypeQss("daychange", "ServerMsg", s)
91         << "\n\n";
92   }
93
94   if(s.value("UseSenderColors").toBool()) {
95     out << "// Sender Colors\n\n"
96         << "ChatLine::sender[sender=\"self\"] { foreground: " << color("SenderSelf", s) << "; }\n\n";
97
98     for(int i = 0; i < 16; i++)
99       out << senderQss(i, s);
100   }
101
102   settingsQss.close();
103 }
104
105 QString QtUiStyle::color(const QString &key, QtUiStyleSettings &settings) const {
106   return settings.value(key).value<QColor>().name();
107 }
108
109 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, QtUiStyleSettings &settings) const {
110   return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
111 }
112
113 QString QtUiStyle::senderQss(int i, QtUiStyleSettings &settings) const {
114   QString dez = QString::number(i);
115   if(dez.length() == 1) dez.prepend('0');
116
117   return QString("ChatLine::sender[sender=\"0%1\"] { foreground: %2; }\n").arg(QString::number(i, 16), color("Sender"+dez, settings));
118 }