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 "qtuistyle.h"
25 #include <QTextStream>
27 #include "chatviewsettings.h"
29 QtUiStyle::QtUiStyle(QObject* parent)
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);
38 // If no style sheet exists, generate it on first run.
39 initializeSettingsQss();
42 void QtUiStyle::updateUseCustomTimestampFormat()
45 setUseCustomTimestampFormat(s.useCustomTimestampFormat());
48 void QtUiStyle::updateTimestampFormatString()
51 setTimestampFormatString(s.timestampFormatString());
54 void QtUiStyle::updateSenderPrefixDisplay()
57 setSenderPrefixDisplay(s.senderPrefixDisplay());
60 void QtUiStyle::updateShowSenderBrackets()
63 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)
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"
110 << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
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";
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);
130 out << "ChatLine::sender#plain[sender=\"self\"] { foreground: palette(sender-color-self); }\n\n";
132 // Matches qssparser.cpp for UiStyle::PlainMsg
133 for (int i = 0; i < defaultSenderColors.count(); i++)
134 out << senderQss(i, "plain");
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";
143 // Matches qssparser.cpp for UiStyle::ActionMsg
144 for (int i = 0; i < defaultSenderColors.count(); i++)
145 out << senderQss(i, "action", true);
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";
154 // Matches qssparser.cpp for any style of message (UiStyle::...)
155 for (int i = 0; i < defaultSenderColors.count(); i++)
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";
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);
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";
189 QString QtUiStyle::color(const QString& key, UiSettings& settings, const QColor& defaultColor) const
191 return settings.value(key, defaultColor).value<QColor>().name();
194 QString QtUiStyle::fontDescription(const QFont& font) const
196 QFont::Style style = font.style();
197 int weight = font.weight();
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())
206 QString QtUiStyle::msgTypeQss(const QString& msgType, const QString& key, UiSettings& settings) const
208 return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
211 QString QtUiStyle::senderPaletteQss(UiSettings& settings) const
214 result += "Palette {\n";
216 // Generate entries for sender-color-self
217 result += QString(" sender-color-self: %1;\n").arg(color("SenderSelf", settings, defaultSenderColorSelf));
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)
224 result += QString(" sender-color-0%1: %2;\n").arg(QString::number(i, 16), color("Sender" + dez, settings, defaultSenderColors[i]));
230 QString QtUiStyle::senderQss(int i, const QString& messageType, bool includeNick) const
232 QString dez = QString::number(i);
233 if (dez.length() == 1)
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));
243 return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: palette(sender-color-0%2); }\n")
244 .arg(messageType, QString::number(i, 16));
248 QString QtUiStyle::nickQss(int i) const
250 QString dez = QString::number(i);
251 if (dez.length() == 1)
254 return QString("ChatLine::nick[sender=\"0%1\"] { foreground: palette(sender-color-0%1); }\n").arg(QString::number(i, 16));
257 QString QtUiStyle::chatListItemQss(const QString& state, const QString& key, UiSettings& settings) const
259 return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));