Make Breeze the default icon theme
[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     // If no style sheet exists, generate it on first run.
36     initializeSettingsQss();
37 }
38
39
40 QtUiStyle::~QtUiStyle() {}
41
42 void QtUiStyle::updateTimestampFormatString()
43 {
44     ChatViewSettings s;
45     setTimestampFormatString(s.timestampFormatString());
46 }
47
48 void QtUiStyle::updateShowSenderBrackets()
49 {
50     ChatViewSettings s;
51     enableSenderBrackets(s.showSenderBrackets());
52 }
53
54
55 void QtUiStyle::initializeSettingsQss()
56 {
57     QFileInfo settingsQss(Quassel::configDirPath() + "settings.qss");
58     // Only initialize if it doesn't already exist
59     if (settingsQss.exists())
60         return;
61
62     // Generate and load the new stylesheet
63     generateSettingsQss();
64     reload();
65 }
66
67 void QtUiStyle::generateSettingsQss() const
68 {
69     QFile settingsQss(Quassel::configDirPath() + "settings.qss");
70
71     if (!settingsQss.open(QFile::WriteOnly|QFile::Truncate)) {
72         qWarning() << "Could not open" << settingsQss.fileName() << "for writing!";
73         return;
74     }
75     QTextStream out(&settingsQss);
76
77     out << "// Style settings made in Quassel's configuration dialog\n"
78         << "// This file is automatically generated, do not edit\n";
79
80     // ChatView
81     ///////////
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";
86
87     QtUiStyleSettings s("Colors");
88     if (s.value("UseChatViewColors").toBool()) {
89         out << "\n// Custom ChatView Colors\n"
90
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"
97         << "}\n\n"
98         << "ChatLine::timestamp { foreground: " << color("Timestamp", s) << "; }\n\n"
99
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)
118         << "\n";
119     }
120
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";
124
125         // Matches qssparser.cpp for UiStyle::PlainMsg
126         for (int i = 0; i < defaultSenderColors.count(); i++)
127             out << senderQss(i, s, "plain");
128
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";
135
136             // Matches qssparser.cpp for UiStyle::ActionMsg
137             for (int i = 0; i < defaultSenderColors.count(); i++)
138                 out << senderQss(i, s, "action", true);
139         }
140
141     }
142
143     // ItemViews
144     ////////////
145
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";
152     }
153
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);
162     }
163
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";
169     }
170
171     settingsQss.close();
172 }
173
174
175 QString QtUiStyle::color(const QString &key, UiSettings &settings, const QColor &defaultColor) const
176 {
177     return settings.value(key, defaultColor).value<QColor>().name();
178 }
179
180
181 QString QtUiStyle::fontDescription(const QFont &font) const
182 {
183     QString desc = "font: ";
184     if (font.italic())
185         desc += "italic ";
186     if (font.bold())
187         desc += "bold ";
188     if (!font.italic() && !font.bold())
189         desc += "normal ";
190     desc += QString("%1pt \"%2\"").arg(font.pointSize()).arg(font.family());
191     return desc;
192 }
193
194
195 QString QtUiStyle::msgTypeQss(const QString &msgType, const QString &key, UiSettings &settings) const
196 {
197     return QString("ChatLine#%1 { foreground: %2; }\n").arg(msgType, color(key, settings));
198 }
199
200
201 QString QtUiStyle::senderQss(int i, UiSettings &settings, const QString &messageType, bool includeNick) const
202 {
203     QString dez = QString::number(i);
204     if (dez.length() == 1) dez.prepend('0');
205
206     if (includeNick) {
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]));
211     } else {
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]));
214     }
215 }
216
217
218 QString QtUiStyle::chatListItemQss(const QString &state, const QString &key, UiSettings &settings) const
219 {
220     return QString("ChatListItem[state=\"%1\"] { foreground: %2; }\n").arg(state, color(key, settings));
221 }