Adapt the stylesheets and the stylesheet generator to the new markerline
[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";
50
51   // ChatView
52   ///////////
53   QtUiStyleSettings fs("Fonts");
54   if(fs.value("UseCustomChatViewFont").toBool())
55     out << "\n// ChatView Font\n"
56         << "ChatLine { " << fontDescription(fs.value("ChatView").value<QFont>()) << "; }\n";
57
58   QtUiStyleSettings s("Colors");
59   if(s.value("UseChatViewColors").toBool()) {
60     out << "\n// Custom ChatView Colors\n"
61
62         // markerline is special in that it always used to use a gradient, so we keep this behavior even with the new implementation
63         << "Palette { marker-line: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 " << color("MarkerLine", s) << ", stop: 0.1 transparent); }\n"
64         << "ChatView { background: " << color("ChatViewBackground", s) << "; }\n\n"
65         << "ChatLine[label=\"highlight\"] {\n"
66         << "  foreground: " << color("Highlight",s) << ";\n"
67         << "  background: " << color("HighlightBackground", s) << ";\n"
68         << "}\n\n"
69         << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
70
71         << msgTypeQss("plain", "ChannelMsg", s)
72         << msgTypeQss("notice", "ServerMsg", s)
73         << msgTypeQss("action", "ActionMsg", s)
74         << msgTypeQss("nick", "CommandMsg", s)
75         << msgTypeQss("mode", "CommandMsg", s)
76         << msgTypeQss("join", "CommandMsg", s)
77         << msgTypeQss("part", "CommandMsg", s)
78         << msgTypeQss("quit", "CommandMsg", s)
79         << msgTypeQss("kick", "CommandMsg", s)
80         << msgTypeQss("kill", "CommandMsg", s)
81         << msgTypeQss("server", "ServerMsg", s)
82         << msgTypeQss("info", "ServerMsg", s)
83         << msgTypeQss("error", "ErrorMsg", s)
84         << msgTypeQss("daychange", "ServerMsg", s)
85         << msgTypeQss("topic", "CommandMsg", s)
86         << msgTypeQss("netsplit-join", "CommandMsg", s)
87         << msgTypeQss("netsplit-quit", "CommandMsg", s)
88         << "\n";
89   }
90
91   if(s.value("UseSenderColors").toBool()) {
92     out << "\n// Sender Colors\n"
93         << "ChatLine::sender#plain[sender=\"self\"] { foreground: " << color("SenderSelf", s) << "; }\n\n";
94
95     for(int i = 0; i < 16; i++)
96       out << senderQss(i, s);
97   }
98
99   // ItemViews
100   ////////////
101
102   UiStyleSettings uiFonts("Fonts");
103   if(uiFonts.value("UseCustomItemViewFont").toBool()) {
104     QString fontDesc = fontDescription(uiFonts.value("ItemView").value<QFont>());
105     out << "\n// ItemView Font\n"
106         << "ChatListItem { " << fontDesc << "; }\n"
107         << "NickListItem { " << fontDesc << "; }\n\n";
108   }
109
110   UiStyleSettings uiColors("Colors");
111   if(uiColors.value("UseBufferViewColors").toBool()) {
112     out << "\n// BufferView Colors\n"
113         << "ChatListItem { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
114         << chatListItemQss("inactive", "InactiveBuffer", uiColors)
115         << chatListItemQss("channel-event", "ActiveBuffer", uiColors)
116         << chatListItemQss("unread-message", "UnreadBuffer", uiColors)
117         << chatListItemQss("highlighted", "HighlightedBuffer", uiColors);
118   }
119
120   if(uiColors.value("UseNickViewColors").toBool()) {
121     out << "\n// NickView Colors\n"
122         << "NickListItem[type=\"category\"] { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
123         << "NickListItem[type=\"user\"] { foreground: " << color("OnlineNick", uiColors) << "; }\n"
124         << "NickListItem[type=\"user\", state=\"away\"] { foreground: " << color("AwayNick", uiColors) << "; }\n";
125   }
126
127   settingsQss.close();
128 }
129
130 QString QtUiStyle::color(const QString &key, UiSettings &settings) const {
131   return settings.value(key).value<QColor>().name();
132 }
133
134 QString QtUiStyle::fontDescription(const QFont &font) const {
135   QString desc = "font: ";
136   if(font.italic())
137     desc += "italic ";
138   if(font.bold())
139     desc += "bold ";
140   if(!font.italic() && !font.bold())
141     desc += "normal ";
142   desc += QString("%1pt \"%2\"").arg(font.pointSize()).arg(font.family());
143   return desc;
144 }
145
146 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const {
147   return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
148 }
149
150 QString QtUiStyle::senderQss(int i, UiSettings &settings) const {
151   QString dez = QString::number(i);
152   if(dez.length() == 1) dez.prepend('0');
153
154   return QString("ChatLine::sender#plain[sender=\"0%1\"] { foreground: %2; }\n").arg(QString::number(i, 16), color("Sender"+dez, settings));
155 }
156
157 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const {
158   return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));
159 }