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