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.initAndNotify("UseCustomTimestampFormat", this, &QtUiStyle::updateUseCustomTimestampFormat);
32 s.initAndNotify("TimestampFormat", this, &QtUiStyle::updateTimestampFormatString);
33 s.initAndNotify("SenderPrefixMode", this, &QtUiStyle::updateSenderPrefixDisplay);
34 s.initAndNotify("ShowSenderBrackets", this, &QtUiStyle::updateShowSenderBrackets);
36 // If no style sheet exists, generate it on first run.
37 initializeSettingsQss();
41 void QtUiStyle::updateUseCustomTimestampFormat()
44 setUseCustomTimestampFormat(s.useCustomTimestampFormat());
47 void QtUiStyle::updateTimestampFormatString()
50 setTimestampFormatString(s.timestampFormatString());
53 void QtUiStyle::updateSenderPrefixDisplay()
56 setSenderPrefixDisplay(s.senderPrefixDisplay());
59 void QtUiStyle::updateShowSenderBrackets()
62 enableSenderBrackets(s.showSenderBrackets());
66 void QtUiStyle::initializeSettingsQss()
68 QFileInfo settingsQss(Quassel::configDirPath() + "settings.qss");
69 // Only initialize if it doesn't already exist
70 if (settingsQss.exists())
73 // Generate and load the new stylesheet
74 generateSettingsQss();
78 void QtUiStyle::generateSettingsQss() const
80 QFile settingsQss(Quassel::configDirPath() + "settings.qss");
82 if (!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
83 qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
86 QTextStream out(&settingsQss);
88 out << "// Style settings made in Quassel's configuration dialog\n"
89 << "// This file is automatically generated, do not edit\n";
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";
98 QtUiStyleSettings s("Colors");
99 if (s.value("UseChatViewColors").toBool()) {
100 out << "\n// Custom ChatView Colors\n"
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) << ", stop: 0.1 transparent); }\n"
104 << "ChatView { background: " << color("ChatViewBackground", s) << "; }\n\n"
105 << "ChatLine[label=\"highlight\"] {\n"
106 << " foreground: " << color("Highlight", s) << ";\n"
107 << " background: " << color("HighlightBackground", s) << ";\n"
109 << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
111 << msgTypeQss("plain", "ChannelMsg", s)
112 << msgTypeQss("notice", "ServerMsg", s)
113 << msgTypeQss("action", "ActionMsg", s)
114 << msgTypeQss("nick", "CommandMsg", s)
115 << msgTypeQss("mode", "CommandMsg", s)
116 << msgTypeQss("join", "CommandMsg", s)
117 << msgTypeQss("part", "CommandMsg", s)
118 << msgTypeQss("quit", "CommandMsg", s)
119 << msgTypeQss("kick", "CommandMsg", s)
120 << msgTypeQss("kill", "CommandMsg", s)
121 << msgTypeQss("server", "ServerMsg", s)
122 << msgTypeQss("info", "ServerMsg", s)
123 << msgTypeQss("error", "ErrorMsg", s)
124 << msgTypeQss("daychange", "ServerMsg", s)
125 << msgTypeQss("topic", "CommandMsg", s)
126 << msgTypeQss("netsplit-join", "CommandMsg", s)
127 << msgTypeQss("netsplit-quit", "CommandMsg", s)
128 << msgTypeQss("invite", "CommandMsg", s)
132 if (s.value("UseSenderColors", true).toBool()) {
133 out << "\n// Sender Colors\n";
134 // Generate a color palette for easy reuse elsewhere
135 // NOTE: A color palette is not a complete replacement for specifying the colors below, as
136 // specifying the colors one-by-one instead of with QtUi::style()->brush(...) makes it easy
137 // to toggle the specific coloring of sender/nick at the cost of regenerating this file.
138 // See UiStyle::ColorRole
139 out << senderPaletteQss(s);
141 out << "ChatLine::sender#plain[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
143 // Matches qssparser.cpp for UiStyle::PlainMsg
144 for (int i = 0; i < defaultSenderColors.count(); i++)
145 out << senderQss(i, "plain");
147 // Only color the nicks in CTCP ACTIONs if sender colors are enabled
148 if (s.value("UseSenderActionColors", true).toBool()) {
149 // For action messages, color the 'sender' column -and- the nick itself
150 out << "\n// Sender Nickname Colors for action messages\n"
151 << "ChatLine::sender#action[sender=\"self\"] { foreground: palette(sender-color-self); }\n"
152 << "ChatLine::nick#action[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
154 // Matches qssparser.cpp for UiStyle::ActionMsg
155 for (int i = 0; i < defaultSenderColors.count(); i++)
156 out << senderQss(i, "action", true);
159 // Only color the nicks in CTCP ACTIONs if sender colors are enabled
160 if (s.value("UseNickGeneralColors", true).toBool()) {
161 // For action messages, color the 'sender' column -and- the nick itself
162 out << "\n// Nickname colors for all messages\n"
163 << "ChatLine::nick[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
165 // Matches qssparser.cpp for any style of message (UiStyle::...)
166 for (int i = 0; i < defaultSenderColors.count(); i++)
175 UiStyleSettings uiFonts("Fonts");
176 if (uiFonts.value("UseCustomItemViewFont").toBool()) {
177 QString fontDesc = fontDescription(uiFonts.value("ItemView").value<QFont>());
178 out << "\n// ItemView Font\n"
179 << "ChatListItem { " << fontDesc << "; }\n"
180 << "NickListItem { " << fontDesc << "; }\n\n";
183 UiStyleSettings uiColors("Colors");
184 if (uiColors.value("UseBufferViewColors").toBool()) {
185 out << "\n// BufferView Colors\n"
186 << "ChatListItem { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
187 << chatListItemQss("inactive", "InactiveBuffer", uiColors)
188 << chatListItemQss("channel-event", "ActiveBuffer", uiColors)
189 << chatListItemQss("unread-message", "UnreadBuffer", uiColors)
190 << chatListItemQss("highlighted", "HighlightedBuffer", uiColors);
193 if (uiColors.value("UseNickViewColors").toBool()) {
194 out << "\n// NickView Colors\n"
195 << "NickListItem[type=\"category\"] { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
196 << "NickListItem[type=\"user\"] { foreground: " << color("OnlineNick", uiColors) << "; }\n"
197 << R"(NickListItem[type="user", state="away"] { foreground: )" << color("AwayNick", uiColors) << "; }\n";
204 QString QtUiStyle::color(const QString &key, UiSettings &settings, const QColor &defaultColor) const
206 return settings.value(key, defaultColor).value<QColor>().name();
210 QString QtUiStyle::fontDescription(const QFont &font) const
212 QFont::Style style = font.style();
213 int weight = font.weight();
215 return QString("font: %1 %2 %3pt \"%4\"")
216 .arg(style == QFont::StyleItalic ? "italic" :
217 style == QFont::StyleOblique ? "oblique" :
219 .arg(100 * qBound(1, (weight * 8 + 50) / 100, 9))
220 .arg(font.pointSize())
225 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
227 return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
231 QString QtUiStyle::senderPaletteQss(UiSettings &settings) const
234 result += "Palette {\n";
236 // Generate entries for sender-color-self
237 result += QString(" sender-color-self: %1;\n")
238 .arg(color("SenderSelf", settings, defaultSenderColorSelf));
240 // Generate entries for sender-color-HASH
241 for (int i = 0; i < defaultSenderColors.count(); i++) {
242 QString dez = QString::number(i);
243 if (dez.length() == 1) dez.prepend('0');
244 result += QString(" sender-color-0%1: %2;\n")
245 .arg(QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i]));
252 QString QtUiStyle::senderQss(int i, const QString &messageType, bool includeNick) const
254 QString dez = QString::number(i);
255 if (dez.length() == 1) dez.prepend('0');
258 // Include the nickname in the color rules
259 return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n"
260 "ChatLine::nick#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n")
261 .arg(messageType, QString::number(i, 16));
263 return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n")
264 .arg(messageType, QString::number(i, 16));
269 QString QtUiStyle::nickQss(int i) const
271 QString dez = QString::number(i);
272 if (dez.length() == 1) dez.prepend('0');
274 return QString("ChatLine::nick[sender=\"0%1\"] { foreground: palette(sender-color-0%1); }\n")
275 .arg(QString::number(i, 16));
279 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
281 return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));