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