Optionally color sender nicks in Action messages
[quassel.git] / src / qtui / qtuistyle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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 "chatviewsettings.h"
22 #include "qtuistyle.h"
23
24 #include <QFile>
25 #include <QTextStream>
26
27 QtUiStyle::QtUiStyle(QObject *parent) : UiStyle(parent)
28 {
29     ChatViewSettings s;
30     s.notify("TimestampFormat", this, SLOT(updateTimestampFormatString()));
31     updateTimestampFormatString();
32     s.notify("ShowSenderBrackets", this, SLOT(updateShowSenderBrackets()));
33     updateShowSenderBrackets();
34 }
35
36
37 QtUiStyle::~QtUiStyle() {}
38
39 void QtUiStyle::updateTimestampFormatString()
40 {
41     ChatViewSettings s;
42     setTimestampFormatString(s.timestampFormatString());
43 }
44
45 void QtUiStyle::updateShowSenderBrackets()
46 {
47     ChatViewSettings s;
48     enableSenderBrackets(s.showSenderBrackets());
49 }
50
51
52 void QtUiStyle::generateSettingsQss() const
53 {
54     QFile settingsQss(Quassel::configDirPath() + "settings.qss");
55     if (!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
56         qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
57         return;
58     }
59     QTextStream out(&settingsQss);
60
61     out << "// Style settings made in Quassel's configuration dialog\n"
62         << "// This file is automatically generated, do not edit\n";
63
64     // ChatView
65     ///////////
66     QtUiStyleSettings fs("Fonts");
67     if (fs.value("UseCustomChatViewFont").toBool())
68         out << "\n// ChatView Font\n"
69             << "ChatLine { " << fontDescription(fs.value("ChatView").value<QFont>()) << "; }\n";
70
71     QtUiStyleSettings s("Colors");
72     if (s.value("UseChatViewColors").toBool()) {
73         out << "\n// Custom ChatView Colors\n"
74
75         // markerline is special in that it always used to use a gradient, so we keep this behavior even with the new implementation
76         << "Palette { marker-line: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 " << color("MarkerLine", s) << ", stop: 0.1 transparent); }\n"
77         << "ChatView { background: " << color("ChatViewBackground", s) << "; }\n\n"
78         << "ChatLine[label=\"highlight\"] {\n"
79         << "  foreground: " << color("Highlight", s) << ";\n"
80         << "  background: " << color("HighlightBackground", s) << ";\n"
81         << "}\n\n"
82         << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
83
84         << msgTypeQss("plain", "ChannelMsg", s)
85         << msgTypeQss("notice", "ServerMsg", s)
86         << msgTypeQss("action", "ActionMsg", s)
87         << msgTypeQss("nick", "CommandMsg", s)
88         << msgTypeQss("mode", "CommandMsg", s)
89         << msgTypeQss("join", "CommandMsg", s)
90         << msgTypeQss("part", "CommandMsg", s)
91         << msgTypeQss("quit", "CommandMsg", s)
92         << msgTypeQss("kick", "CommandMsg", s)
93         << msgTypeQss("kill", "CommandMsg", s)
94         << msgTypeQss("server", "ServerMsg", s)
95         << msgTypeQss("info", "ServerMsg", s)
96         << msgTypeQss("error", "ErrorMsg", s)
97         << msgTypeQss("daychange", "ServerMsg", s)
98         << msgTypeQss("topic", "CommandMsg", s)
99         << msgTypeQss("netsplit-join", "CommandMsg", s)
100         << msgTypeQss("netsplit-quit", "CommandMsg", s)
101         << msgTypeQss("invite", "CommandMsg", s)
102         << "\n";
103     }
104
105     if (s.value("UseSenderColors").toBool()) {
106         out << "\n// Sender Colors\n"
107             << "ChatLine::sender#plain[sender=\"self\"] { foreground: " << color("SenderSelf", s) << "; }\n\n";
108
109         // Matches qssparser.cpp for UiStyle::PlainMsg
110         for (int i = 0; i < 16; i++)
111             out << senderQss(i, s, "plain");
112
113         // Only color the nicks in CTCP ACTIONs if sender colors are enabled
114         if (s.value("UseSenderActionColors").toBool()) {
115             // For action messages, color the 'sender' column -and- the nick itself
116             out << "\n// Sender Nickname Colors for action messages\n"
117                 << "ChatLine::sender#action[sender=\"self\"] { foreground: " << color("SenderSelf", s) << "; }\n"
118                 << "ChatLine::nick#action[sender=\"self\"] { foreground: " << color("SenderSelf", s) << "; }\n\n";
119
120             // Matches qssparser.cpp for UiStyle::ActionMsg
121             for (int i = 0; i < 16; i++)
122                 out << senderQss(i, s, "action", true);
123         }
124     }
125
126     // ItemViews
127     ////////////
128
129     UiStyleSettings uiFonts("Fonts");
130     if (uiFonts.value("UseCustomItemViewFont").toBool()) {
131         QString fontDesc = fontDescription(uiFonts.value("ItemView").value<QFont>());
132         out << "\n// ItemView Font\n"
133             << "ChatListItem { " << fontDesc << "; }\n"
134             << "NickListItem { " << fontDesc << "; }\n\n";
135     }
136
137     UiStyleSettings uiColors("Colors");
138     if (uiColors.value("UseBufferViewColors").toBool()) {
139         out << "\n// BufferView Colors\n"
140             << "ChatListItem { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
141             << chatListItemQss("inactive", "InactiveBuffer", uiColors)
142             << chatListItemQss("channel-event", "ActiveBuffer", uiColors)
143             << chatListItemQss("unread-message", "UnreadBuffer", uiColors)
144             << chatListItemQss("highlighted", "HighlightedBuffer", uiColors);
145     }
146
147     if (uiColors.value("UseNickViewColors").toBool()) {
148         out << "\n// NickView Colors\n"
149             << "NickListItem[type=\"category\"] { foreground: " << color("DefaultBuffer", uiColors) << "; }\n"
150             << "NickListItem[type=\"user\"] { foreground: " << color("OnlineNick", uiColors) << "; }\n"
151             << "NickListItem[type=\"user\", state=\"away\"] { foreground: " << color("AwayNick", uiColors) << "; }\n";
152     }
153
154     settingsQss.close();
155 }
156
157
158 QString QtUiStyle::color(const QString &key, UiSettings &settings) const
159 {
160     return settings.value(key).value<QColor>().name();
161 }
162
163
164 QString QtUiStyle::fontDescription(const QFont &font) const
165 {
166     QString desc = "font: ";
167     if (font.italic())
168         desc += "italic ";
169     if (font.bold())
170         desc += "bold ";
171     if (!font.italic() && !font.bold())
172         desc += "normal ";
173     desc += QString("%1pt \"%2\"").arg(font.pointSize()).arg(font.family());
174     return desc;
175 }
176
177
178 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
179 {
180     return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
181 }
182
183
184 QString QtUiStyle::senderQss(int i, UiSettings &settings, const QString &messageType, bool includeNick) const
185 {
186     QString dez = QString::number(i);
187     if (dez.length() == 1) dez.prepend('0');
188
189     if (includeNick) {
190         // Include the nickname in the color rules
191         return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: %3; }\n"
192                        "ChatLine::nick#%1[sender=\"0%2\"]   { foreground: %3; }\n")
193                 .arg(messageType, QString::number(i, 16), color("Sender"+dez, settings));
194     } else {
195         return QString("ChatLine::sender#%1[sender=\"0%2\"] { foreground: %3; }\n")
196                 .arg(messageType, QString::number(i, 16), color("Sender"+dez, settings));
197     }
198 }
199
200
201 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
202 {
203     return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));
204 }