Implement UI and serialization logic for sender modes
[quassel.git] / src / qtui / qtuistyle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "chatviewsettings.h"
22 #include "qtuistyle.h"
23
24 #include <QFile>
25 #include <QFileInfo>
26 #include <QTextStream>
27
28 QtUiStyle::QtUiStyle(QObject *parent) : UiStyle(parent)
29 {
30     ChatViewSettings s;
31     s.notify("UseCustomTimestampFormat", this, SLOT(updateUseCustomTimestampFormat()));
32     updateUseCustomTimestampFormat();
33     s.notify("TimestampFormat", this, SLOT(updateTimestampFormatString()));
34     updateTimestampFormatString();
35     s.notify("ShowSenderPrefixes", this, SLOT(updateShowSenderPrefixes()));
36     updateShowSenderPrefixes();
37     s.notify("ShowSenderBrackets", this, SLOT(updateShowSenderBrackets()));
38     updateShowSenderBrackets();
39
40     // If no style sheet exists, generate it on first run.
41     initializeSettingsQss();
42 }
43
44
45 QtUiStyle::~QtUiStyle() {}
46
47 void QtUiStyle::updateUseCustomTimestampFormat()
48 {
49     ChatViewSettings s;
50     setUseCustomTimestampFormat(s.useCustomTimestampFormat());
51 }
52
53 void QtUiStyle::updateTimestampFormatString()
54 {
55     ChatViewSettings s;
56     setTimestampFormatString(s.timestampFormatString());
57 }
58
59 void QtUiStyle::updateShowSenderPrefixes()
60 {
61     ChatViewSettings s;
62     enableSenderPrefixes(s.showSenderPrefixes());
63 }
64
65 void QtUiStyle::updateShowSenderBrackets()
66 {
67     ChatViewSettings s;
68     enableSenderBrackets(s.showSenderBrackets());
69 }
70
71
72 void QtUiStyle::initializeSettingsQss()
73 {
74     QFileInfo settingsQss(Quassel::configDirPath() + "settings.qss");
75     // Only initialize if it doesn't already exist
76     if (settingsQss.exists())
77         return;
78
79     // Generate and load the new stylesheet
80     generateSettingsQss();
81     reload();
82 }
83
84 void QtUiStyle::generateSettingsQss() const
85 {
86     QFile settingsQss(Quassel::configDirPath() + "settings.qss");
87
88     if (!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
89         qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
90         return;
91     }
92     QTextStream out(&settingsQss);
93
94     out << "// Style settings made in Quassel's configuration dialog\n"
95         << "// This file is automatically generated, do not edit\n";
96
97     // ChatView
98     ///////////
99     QtUiStyleSettings fs("Fonts");
100     if (fs.value("UseCustomChatViewFont").toBool())
101         out << "\n// ChatView Font\n"
102             << "ChatLine { " << fontDescription(fs.value("ChatView").value<QFont>()) << "; }\n";
103
104     QtUiStyleSettings s("Colors");
105     if (s.value("UseChatViewColors").toBool()) {
106         out << "\n// Custom ChatView Colors\n"
107
108         // markerline is special in that it always used to use a gradient, so we keep this behavior even with the new implementation
109         << "Palette { marker-line: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 " << color("MarkerLine", s) << ", stop: 0.1 transparent); }\n"
110         << "ChatView { background: " << color("ChatViewBackground", s) << "; }\n\n"
111         << "ChatLine[label=\"highlight\"] {\n"
112         << "  foreground: " << color("Highlight", s) << ";\n"
113         << "  background: " << color("HighlightBackground", s) << ";\n"
114         << "}\n\n"
115         << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
116
117         << msgTypeQss("plain", "ChannelMsg", s)
118         << msgTypeQss("notice", "ServerMsg", s)
119         << msgTypeQss("action", "ActionMsg", s)
120         << msgTypeQss("nick", "CommandMsg", s)
121         << msgTypeQss("mode", "CommandMsg", s)
122         << msgTypeQss("join", "CommandMsg", s)
123         << msgTypeQss("part", "CommandMsg", s)
124         << msgTypeQss("quit", "CommandMsg", s)
125         << msgTypeQss("kick", "CommandMsg", s)
126         << msgTypeQss("kill", "CommandMsg", s)
127         << msgTypeQss("server", "ServerMsg", s)
128         << msgTypeQss("info", "ServerMsg", s)
129         << msgTypeQss("error", "ErrorMsg", s)
130         << msgTypeQss("daychange", "ServerMsg", s)
131         << msgTypeQss("topic", "CommandMsg", s)
132         << msgTypeQss("netsplit-join", "CommandMsg", s)
133         << msgTypeQss("netsplit-quit", "CommandMsg", s)
134         << msgTypeQss("invite", "CommandMsg", s)
135         << "\n";
136     }
137
138     if (s.value("UseSenderColors", true).toBool()) {
139         out << "\n// Sender Colors\n";
140         // Generate a color palette for easy reuse elsewhere
141         // NOTE: A color palette is not a complete replacement for specifying the colors below, as
142         // specifying the colors one-by-one instead of with QtUi::style()->brush(...) makes it easy
143         // to toggle the specific coloring of sender/nick at the cost of regenerating this file.
144         // See UiStyle::ColorRole
145         out << senderPaletteQss(s);
146
147         out << "ChatLine::sender#plain[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
148
149         // Matches qssparser.cpp for UiStyle::PlainMsg
150         for (int i = 0; i < defaultSenderColors.count(); i++)
151             out << senderQss(i, "plain");
152
153         // Only color the nicks in CTCP ACTIONs if sender colors are enabled
154         if (s.value("UseSenderActionColors", true).toBool()) {
155             // For action messages, color the 'sender' column -and- the nick itself
156             out << "\n// Sender Nickname Colors for action messages\n"
157                 << "ChatLine::sender#action[sender=\"self\"] { foreground: palette(sender-color-self); }\n"
158                 << "ChatLine::nick#action[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
159
160             // Matches qssparser.cpp for UiStyle::ActionMsg
161             for (int i = 0; i < defaultSenderColors.count(); i++)
162                 out << senderQss(i, "action", true);
163         }
164
165         // Only color the nicks in CTCP ACTIONs if sender colors are enabled
166         if (s.value("UseNickGeneralColors", true).toBool()) {
167             // For action messages, color the 'sender' column -and- the nick itself
168             out << "\n// Nickname colors for all messages\n"
169                 << "ChatLine::nick[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
170
171             // Matches qssparser.cpp for any style of message (UiStyle::...)
172             for (int i = 0; i < defaultSenderColors.count(); i++)
173                 out << nickQss(i);
174         }
175
176     }
177
178     // ItemViews
179     ////////////
180
181     UiStyleSettings uiFonts("Fonts");
182     if (uiFonts.value("UseCustomItemViewFont").toBool()) {
183         QString fontDesc = fontDescription(uiFonts.value("ItemView").value<QFont>());
184         out << "\n// ItemView Font\n"
185             << "ChatListItem { " << fontDesc << "; }\n"
186             << "NickListItem { " << fontDesc << "; }\n\n";
187     }
188
189     UiStyleSettings uiColors("Colors");
190     if (uiColors.value("UseBufferViewColors").toBool()) {
191         out << "\n// BufferView Colors\n"
192             << "ChatListItem { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
193             << chatListItemQss("inactive", "InactiveBuffer", uiColors)
194             << chatListItemQss("channel-event", "ActiveBuffer", uiColors)
195             << chatListItemQss("unread-message", "UnreadBuffer", uiColors)
196             << chatListItemQss("highlighted", "HighlightedBuffer", uiColors);
197     }
198
199     if (uiColors.value("UseNickViewColors").toBool()) {
200         out << "\n// NickView Colors\n"
201             << "NickListItem[type=\"category\"] { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
202             << "NickListItem[type=\"user\"] { foreground: " << color("OnlineNick", uiColors) << "; }\n"
203             << "NickListItem[type=\"user\", state=\"away\"] { foreground: " << color("AwayNick", uiColors) << "; }\n";
204     }
205
206     settingsQss.close();
207 }
208
209
210 QString QtUiStyle::color(const QString &key, UiSettings &settings, const QColor &defaultColor) const
211 {
212     return settings.value(key, defaultColor).value<QColor>().name();
213 }
214
215
216 QString QtUiStyle::fontDescription(const QFont &font) const
217 {
218     QString desc = "font: ";
219     if (font.italic())
220         desc += "italic ";
221     if (font.bold())
222         desc += "bold ";
223     if (!font.italic() && !font.bold())
224         desc += "normal ";
225     desc += QString("%1pt \"%2\"").arg(font.pointSize()).arg(font.family());
226     return desc;
227 }
228
229
230 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
231 {
232     return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
233 }
234
235
236 QString QtUiStyle::senderPaletteQss(UiSettings &settings) const
237 {
238     QString result;
239     result += "Palette {\n";
240
241     // Generate entries for sender-color-self
242     result += QString("    sender-color-self: %1;\n")
243               .arg(color("SenderSelf", settings, defaultSenderColorSelf));
244
245     // Generate entries for sender-color-HASH
246     for (int i = 0; i < defaultSenderColors.count(); i++) {
247         QString dez = QString::number(i);
248         if (dez.length() == 1) dez.prepend('0');
249         result += QString("    sender-color-0%1: %2;\n")
250                 .arg(QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i]));
251     }
252     result += "}\n\n";
253     return result;
254 }
255
256
257 QString QtUiStyle::senderQss(int i, const QString &messageType, bool includeNick) const
258 {
259     QString dez = QString::number(i);
260     if (dez.length() == 1) dez.prepend('0');
261
262     if (includeNick) {
263         // Include the nickname in the color rules
264         return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n"
265                        "ChatLine::nick#%1[sender=\"0%2\"]   { foreground: palette(sender-color-0%2); }\n")
266                 .arg(messageType, QString::number(i, 16));
267     } else {
268         return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n")
269                 .arg(messageType, QString::number(i, 16));
270     }
271 }
272
273
274 QString QtUiStyle::nickQss(int i) const
275 {
276     QString dez = QString::number(i);
277     if (dez.length() == 1) dez.prepend('0');
278
279     return QString("ChatLine::nick[sender=\"0%1\"]   { foreground: palette(sender-color-0%1); }\n")
280             .arg(QString::number(i, 16));
281 }
282
283
284 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
285 {
286     return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));
287 }