1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "chatviewsettings.h"
22 #include "qtuistyle.h"
26 #include <QTextStream>
28 QtUiStyle::QtUiStyle(QObject *parent) : UiStyle(parent)
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();
40 // If no style sheet exists, generate it on first run.
41 initializeSettingsQss();
45 QtUiStyle::~QtUiStyle() {}
47 void QtUiStyle::updateUseCustomTimestampFormat()
50 setUseCustomTimestampFormat(s.useCustomTimestampFormat());
53 void QtUiStyle::updateTimestampFormatString()
56 setTimestampFormatString(s.timestampFormatString());
59 void QtUiStyle::updateSenderPrefixDisplay()
62 setSenderPrefixDisplay(s.SenderPrefixDisplay());
65 void QtUiStyle::updateShowSenderBrackets()
68 enableSenderBrackets(s.showSenderBrackets());
72 void QtUiStyle::initializeSettingsQss()
74 QFileInfo settingsQss(Quassel::configDirPath() + "settings.qss");
75 // Only initialize if it doesn't already exist
76 if (settingsQss.exists())
79 // Generate and load the new stylesheet
80 generateSettingsQss();
84 void QtUiStyle::generateSettingsQss() const
86 QFile settingsQss(Quassel::configDirPath() + "settings.qss");
88 if (!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
89 qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
92 QTextStream out(&settingsQss);
94 out << "// Style settings made in Quassel's configuration dialog\n"
95 << "// This file is automatically generated, do not edit\n";
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";
104 QtUiStyleSettings s("Colors");
105 if (s.value("UseChatViewColors").toBool()) {
106 out << "\n// Custom ChatView Colors\n"
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"
115 << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
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)
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);
147 out << "ChatLine::sender#plain[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
149 // Matches qssparser.cpp for UiStyle::PlainMsg
150 for (int i = 0; i < defaultSenderColors.count(); i++)
151 out << senderQss(i, "plain");
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";
160 // Matches qssparser.cpp for UiStyle::ActionMsg
161 for (int i = 0; i < defaultSenderColors.count(); i++)
162 out << senderQss(i, "action", true);
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";
171 // Matches qssparser.cpp for any style of message (UiStyle::...)
172 for (int i = 0; i < defaultSenderColors.count(); i++)
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";
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);
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";
210 QString QtUiStyle::color(const QString &key, UiSettings &settings, const QColor &defaultColor) const
212 return settings.value(key, defaultColor).value<QColor>().name();
216 QString QtUiStyle::fontDescription(const QFont &font) const
218 QFont::Style style = font.style();
219 int weight = font.weight();
221 return QString("font: %1 %2 %3pt \"%4\"")
222 .arg(style == QFont::StyleItalic ? "italic" :
223 style == QFont::StyleOblique ? "oblique" :
225 .arg(100 * qBound(1, (weight * 8 + 50) / 100, 9))
226 .arg(font.pointSize())
231 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
233 return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
237 QString QtUiStyle::senderPaletteQss(UiSettings &settings) const
240 result += "Palette {\n";
242 // Generate entries for sender-color-self
243 result += QString(" sender-color-self: %1;\n")
244 .arg(color("SenderSelf", settings, defaultSenderColorSelf));
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]));
258 QString QtUiStyle::senderQss(int i, const QString &messageType, bool includeNick) const
260 QString dez = QString::number(i);
261 if (dez.length() == 1) dez.prepend('0');
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));
269 return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n")
270 .arg(messageType, QString::number(i, 16));
275 QString QtUiStyle::nickQss(int i) const
277 QString dez = QString::number(i);
278 if (dez.length() == 1) dez.prepend('0');
280 return QString("ChatLine::nick[sender=\"0%1\"] { foreground: palette(sender-color-0%1); }\n")
281 .arg(QString::number(i, 16));
285 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
287 return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));