1 /***************************************************************************
2 * Copyright (C) 2005-2016 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"
25 #include <QTextStream>
27 QtUiStyle::QtUiStyle(QObject *parent) : UiStyle(parent)
30 s.notify("TimestampFormat", this, SLOT(updateTimestampFormatString()));
31 updateTimestampFormatString();
32 s.notify("ShowSenderBrackets", this, SLOT(updateShowSenderBrackets()));
33 updateShowSenderBrackets();
35 // If no style sheet exists, generate it on first run.
36 initializeSettingsQss();
40 QtUiStyle::~QtUiStyle() {}
42 void QtUiStyle::updateTimestampFormatString()
45 setTimestampFormatString(s.timestampFormatString());
48 void QtUiStyle::updateShowSenderBrackets()
51 enableSenderBrackets(s.showSenderBrackets());
55 void QtUiStyle::initializeSettingsQss()
57 QFileInfo settingsQss(Quassel::configDirPath() + "settings.qss");
58 // Only initialize if it doesn't already exist
59 if (settingsQss.exists())
62 // Generate and load the new stylesheet
63 generateSettingsQss();
67 void QtUiStyle::generateSettingsQss() const
69 QFile settingsQss(Quassel::configDirPath() + "settings.qss");
71 if (!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
72 qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
75 QTextStream out(&settingsQss);
77 out << "// Style settings made in Quassel's configuration dialog\n"
78 << "// This file is automatically generated, do not edit\n";
82 QtUiStyleSettings fs("Fonts");
83 if (fs.value("UseCustomChatViewFont").toBool())
84 out << "\n// ChatView Font\n"
85 << "ChatLine { " << fontDescription(fs.value("ChatView").value<QFont>()) << "; }\n";
87 QtUiStyleSettings s("Colors");
88 if (s.value("UseChatViewColors").toBool()) {
89 out << "\n// Custom ChatView Colors\n"
91 // markerline is special in that it always used to use a gradient, so we keep this behavior even with the new implementation
92 << "Palette { marker-line: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 " << color("MarkerLine", s) << ", stop: 0.1 transparent); }\n"
93 << "ChatView { background: " << color("ChatViewBackground", s) << "; }\n\n"
94 << "ChatLine[label=\"highlight\"] {\n"
95 << " foreground: " << color("Highlight", s) << ";\n"
96 << " background: " << color("HighlightBackground", s) << ";\n"
98 << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
100 << msgTypeQss("plain", "ChannelMsg", s)
101 << msgTypeQss("notice", "ServerMsg", s)
102 << msgTypeQss("action", "ActionMsg", s)
103 << msgTypeQss("nick", "CommandMsg", s)
104 << msgTypeQss("mode", "CommandMsg", s)
105 << msgTypeQss("join", "CommandMsg", s)
106 << msgTypeQss("part", "CommandMsg", s)
107 << msgTypeQss("quit", "CommandMsg", s)
108 << msgTypeQss("kick", "CommandMsg", s)
109 << msgTypeQss("kill", "CommandMsg", s)
110 << msgTypeQss("server", "ServerMsg", s)
111 << msgTypeQss("info", "ServerMsg", s)
112 << msgTypeQss("error", "ErrorMsg", s)
113 << msgTypeQss("daychange", "ServerMsg", s)
114 << msgTypeQss("topic", "CommandMsg", s)
115 << msgTypeQss("netsplit-join", "CommandMsg", s)
116 << msgTypeQss("netsplit-quit", "CommandMsg", s)
117 << msgTypeQss("invite", "CommandMsg", s)
121 if (s.value("UseSenderColors", true).toBool()) {
122 out << "\n// Sender Colors\n"
123 << "ChatLine::sender#plain[sender=\"self\"] { foreground: " << color("SenderSelf", s, defaultSenderColorSelf) << "; }\n\n";
125 // Matches qssparser.cpp for UiStyle::PlainMsg
126 for (int i = 0; i < defaultSenderColors.count(); i++)
127 out << senderQss(i, s, "plain");
129 // Only color the nicks in CTCP ACTIONs if sender colors are enabled
130 if (s.value("UseSenderActionColors", true).toBool()) {
131 // For action messages, color the 'sender' column -and- the nick itself
132 out << "\n// Sender Nickname Colors for action messages\n"
133 << "ChatLine::sender#action[sender=\"self\"] { foreground: " << color("SenderSelf", s, defaultSenderColorSelf) << "; }\n"
134 << "ChatLine::nick#action[sender=\"self\"] { foreground: " << color("SenderSelf", s, defaultSenderColorSelf) << "; }\n\n";
136 // Matches qssparser.cpp for UiStyle::ActionMsg
137 for (int i = 0; i < defaultSenderColors.count(); i++)
138 out << senderQss(i, s, "action", true);
146 UiStyleSettings uiFonts("Fonts");
147 if (uiFonts.value("UseCustomItemViewFont").toBool()) {
148 QString fontDesc = fontDescription(uiFonts.value("ItemView").value<QFont>());
149 out << "\n// ItemView Font\n"
150 << "ChatListItem { " << fontDesc << "; }\n"
151 << "NickListItem { " << fontDesc << "; }\n\n";
154 UiStyleSettings uiColors("Colors");
155 if (uiColors.value("UseBufferViewColors").toBool()) {
156 out << "\n// BufferView Colors\n"
157 << "ChatListItem { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
158 << chatListItemQss("inactive", "InactiveBuffer", uiColors)
159 << chatListItemQss("channel-event", "ActiveBuffer", uiColors)
160 << chatListItemQss("unread-message", "UnreadBuffer", uiColors)
161 << chatListItemQss("highlighted", "HighlightedBuffer", uiColors);
164 if (uiColors.value("UseNickViewColors").toBool()) {
165 out << "\n// NickView Colors\n"
166 << "NickListItem[type=\"category\"] { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
167 << "NickListItem[type=\"user\"] { foreground: " << color("OnlineNick", uiColors) << "; }\n"
168 << "NickListItem[type=\"user\", state=\"away\"] { foreground: " << color("AwayNick", uiColors) << "; }\n";
175 QString QtUiStyle::color(const QString &key, UiSettings &settings, const QColor &defaultColor) const
177 return settings.value(key, defaultColor).value<QColor>().name();
181 QString QtUiStyle::fontDescription(const QFont &font) const
183 QString desc = "font: ";
188 if (!font.italic() && !font.bold())
190 desc += QString("%1pt \"%2\"").arg(font.pointSize()).arg(font.family());
195 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
197 return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
201 QString QtUiStyle::senderQss(int i, UiSettings &settings, const QString &messageType, bool includeNick) const
203 QString dez = QString::number(i);
204 if (dez.length() == 1) dez.prepend('0');
207 // Include the nickname in the color rules
208 return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: %3; }\n"
209 "ChatLine::nick#%1[sender=\"0%2\"] { foreground: %3; }\n")
210 .arg(messageType, QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i]));
212 return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: %3; }\n")
213 .arg(messageType, QString::number(i, 16), color("Sender"+dez, settings, defaultSenderColors[i]));
218 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
220 return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));