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