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