common: Port remote nicks to NickHighlightMatcher
[quassel.git] / src / qtui / qtuistyle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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("SenderPrefixMode", this, SLOT(updateSenderPrefixDisplay()));
36     updateSenderPrefixDisplay();
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::updateSenderPrefixDisplay()
60 {
61     ChatViewSettings s;
62     setSenderPrefixDisplay(s.SenderPrefixDisplay());
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     QFont::Style style = font.style();
219     int weight = font.weight();
220
221     return QString("font: %1 %2 %3pt \"%4\"")
222         .arg(style == QFont::StyleItalic  ? "italic"  :
223              style == QFont::StyleOblique ? "oblique" :
224                                             "normal")
225         .arg(100 * qBound(1, (weight * 8 + 50) / 100, 9))
226         .arg(font.pointSize())
227         .arg(font.family());
228 }
229
230
231 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
232 {
233     return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
234 }
235
236
237 QString QtUiStyle::senderPaletteQss(UiSettings &settings) const
238 {
239     QString result;
240     result += "Palette {\n";
241
242     // Generate entries for sender-color-self
243     result += QString("    sender-color-self: %1;\n")
244               .arg(color("SenderSelf", settings, defaultSenderColorSelf));
245
246     // Generate entries for sender-color-HASH
247     for (int i = 0; i < defaultSenderColors.count(); i++) {
248         QString dez = QString::number(i);
249         if (dez.length() == 1) dez.prepend('0');
250         result += QString("    sender-color-0%1: %2;\n")
251                 .arg(QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i]));
252     }
253     result += "}\n\n";
254     return result;
255 }
256
257
258 QString QtUiStyle::senderQss(int i, const QString &messageType, bool includeNick) const
259 {
260     QString dez = QString::number(i);
261     if (dez.length() == 1) dez.prepend('0');
262
263     if (includeNick) {
264         // Include the nickname in the color rules
265         return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n"
266                        "ChatLine::nick#%1[sender=\"0%2\"]   { foreground: palette(sender-color-0%2); }\n")
267                 .arg(messageType, QString::number(i, 16));
268     } else {
269         return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n")
270                 .arg(messageType, QString::number(i, 16));
271     }
272 }
273
274
275 QString QtUiStyle::nickQss(int i) const
276 {
277     QString dez = QString::number(i);
278     if (dez.length() == 1) dez.prepend('0');
279
280     return QString("ChatLine::nick[sender=\"0%1\"]   { foreground: palette(sender-color-0%1); }\n")
281             .arg(QString::number(i, 16));
282 }
283
284
285 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
286 {
287     return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));
288 }