1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <QApplication>
23 #include "uistylesettings.h"
26 UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
27 // register FormatList if that hasn't happened yet
28 // FIXME I don't think this actually avoids double registration... :/
29 if(QVariant::nameToType("UiStyle::FormatList") == QVariant::Invalid) {
30 qRegisterMetaType<FormatList>("UiStyle::FormatList");
31 qRegisterMetaTypeStreamOperators<FormatList>("UiStyle::FormatList");
32 Q_ASSERT(QVariant::nameToType("UiStyle::FormatList") != QVariant::Invalid);
36 _defaultPlainFormat.setForeground(QBrush("#000000"));
37 _defaultPlainFormat.setFont(QFont("Monospace", QApplication::font().pointSize()));
38 _defaultPlainFormat.font().setFixedPitch(true);
39 _defaultPlainFormat.font().setStyleHint(QFont::TypeWriter);
40 setFormat(None, _defaultPlainFormat, Settings::Default);
42 // Load saved custom formats
43 UiStyleSettings s(_settingsKey);
44 foreach(FormatType type, s.availableFormats()) {
45 _customFormats[type] = s.customFormat(type);
48 // Initialize color codes according to mIRC "standard"
50 //colors << "white" << "black" << "navy" << "green" << "red" << "maroon" << "purple" << "orange";
51 //colors << "yellow" << "lime" << "teal" << "aqua" << "royalblue" << "fuchsia" << "grey" << "silver";
52 colors << "#ffffff" << "#000000" << "#000080" << "#008000" << "#ff0000" << "#800000" << "#800080" << "#ffa500";
53 colors << "#ffff00" << "#00ff00" << "#008080" << "#00ffff" << "#4169E1" << "#ff00ff" << "#808080" << "#c0c0c0";
55 // Now initialize the mapping between FormatCodes and FormatTypes...
56 _formatCodes["%O"] = None;
57 _formatCodes["%B"] = Bold;
58 _formatCodes["%S"] = Italic;
59 _formatCodes["%U"] = Underline;
60 _formatCodes["%R"] = Reverse;
62 _formatCodes["%D0"] = PlainMsg;
63 _formatCodes["%Dn"] = NoticeMsg;
64 _formatCodes["%Ds"] = ServerMsg;
65 _formatCodes["%De"] = ErrorMsg;
66 _formatCodes["%Dj"] = JoinMsg;
67 _formatCodes["%Dp"] = PartMsg;
68 _formatCodes["%Dq"] = QuitMsg;
69 _formatCodes["%Dk"] = KickMsg;
70 _formatCodes["%Dr"] = RenameMsg;
71 _formatCodes["%Dm"] = ModeMsg;
72 _formatCodes["%Da"] = ActionMsg;
74 _formatCodes["%DT"] = Timestamp;
75 _formatCodes["%DS"] = Sender;
76 _formatCodes["%DN"] = Nick;
77 _formatCodes["%DH"] = Hostmask;
78 _formatCodes["%DC"] = ChannelName;
79 _formatCodes["%DM"] = ModeFlags;
80 _formatCodes["%DU"] = Url;
83 for(int i = 0; i < 16; i++) {
84 QString idx = QString("%1").arg(i, (int)2, (int)10, (QChar)'0');
85 _formatCodes[QString("%Dcf%1").arg(idx)] = (FormatType)(FgCol00 + i);
86 _formatCodes[QString("%Dcb%1").arg(idx)] = (FormatType)(BgCol00 + i);
87 QTextCharFormat fgf, bgf;
88 fgf.setForeground(QBrush(QColor(colors[i]))); setFormat((FormatType)(FgCol00 + i), fgf, Settings::Default);
89 bgf.setBackground(QBrush(QColor(colors[i]))); setFormat((FormatType)(BgCol00 + i), bgf, Settings::Default);
92 // Set a few more standard formats
93 QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
94 setFormat(Bold, bold, Settings::Default);
96 QTextCharFormat italic; italic.setFontItalic(true);
97 setFormat(Italic, italic, Settings::Default);
99 QTextCharFormat underline; underline.setFontUnderline(true);
100 setFormat(Underline, underline, Settings::Default);
102 // All other formats should be defined in derived classes.
105 UiStyle::~ UiStyle() {
109 void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
110 if(mode == Settings::Default) {
111 _defaultFormats[ftype] = fmt;
113 UiStyleSettings s(_settingsKey);
114 if(fmt != _defaultFormats[ftype]) {
115 _customFormats[ftype] = fmt;
116 s.setCustomFormat(ftype, fmt);
118 _customFormats[ftype] = QTextFormat().toCharFormat();
119 s.removeCustomFormat(ftype);
124 QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
125 if(mode == Settings::Custom && _customFormats.contains(ftype)) return _customFormats.value(ftype);
126 else return _defaultFormats.value(ftype, QTextCharFormat());
129 // NOTE: This function is intimately tied to the values in FormatType. Don't change this
130 // until you _really_ know what you do!
131 QTextCharFormat UiStyle::mergedFormat(quint32 ftype) {
132 if(_cachedFormats.contains(ftype)) return _cachedFormats[ftype];
133 if(ftype == Invalid) return QTextCharFormat();
134 // Now we construct the merged format, starting with the default
135 QTextCharFormat fmt = format(None);
136 // First: general message format
137 fmt.merge(format((FormatType)(ftype & 0x0f)));
138 // now more specific ones
139 for(quint32 mask = 0x0010; mask <= 0x2000; mask <<= 1) {
140 if(ftype & mask) fmt.merge(format((FormatType)mask));
143 if(ftype & 0x00400000) fmt.merge(format((FormatType)(ftype & 0x0f400000))); // foreground
144 if(ftype & 0x00800000) fmt.merge(format((FormatType)(ftype & 0xf0800000))); // background
146 if(ftype & Url) fmt.merge(format(Url));
150 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
151 if(_formatCodes.contains(code)) return _formatCodes.value(code);
155 QString UiStyle::formatCode(FormatType ftype) const {
156 return _formatCodes.key(ftype);
159 // This method expects a well-formatted string, there is no error checking!
160 // Since we create those ourselves, we should be pretty safe that nobody does something crappy here.
161 UiStyle::StyledString UiStyle::styleString(const QString &s_) {
164 result.formats.append(qMakePair(0, (quint32)None));
165 quint32 curfmt = (quint32)None;
166 int pos = 0; int length = 0;
168 pos = s.indexOf('%', pos);
170 if(s[pos+1] == '%') { // escaped %, we just remove one and continue
175 if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
176 if(s[pos+3] == '-') { // color off
177 curfmt &= 0x003fffff;
180 int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
181 //TODO: use 99 as transparent color (re mirc color "standard")
184 curfmt |= (color << 24) | 0x00400000;
186 curfmt |= (color << 28) | 0x00800000;
189 } else if(s[pos+1] == 'O') { // reset formatting
190 curfmt &= 0x0000000f; // we keep message type-specific formatting
192 } else if(s[pos+1] == 'R') { // reverse
193 // TODO: implement reverse formatting
196 } else { // all others are toggles
197 QString code = QString("%") + s[pos+1];
198 if(s[pos+1] == 'D') code += s[pos+2];
199 FormatType ftype = formatType(code);
200 if(ftype == Invalid) {
201 qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
205 length = code.length();
207 s.remove(pos, length);
208 if(pos == result.formats.last().first)
209 result.formats.last().second = curfmt;
211 result.formats.append(qMakePair(pos, curfmt));
217 QString UiStyle::mircToInternal(const QString &mirc_) {
218 QString mirc = mirc_;
219 mirc.replace('%', "%%"); // escape % just to be sure
220 mirc.replace('\x02', "%B");
221 mirc.replace('\x0f', "%O");
222 mirc.replace('\x12', "%R");
223 mirc.replace('\x16', "%R");
224 mirc.replace('\x1d', "%S");
225 mirc.replace('\x1f', "%U");
227 // Now we bring the color codes (\x03) in a sane format that can be parsed more easily later.
228 // %Dcfxx is foreground, %Dcbxx is background color, where xx is a 2 digit dec number denoting the color code.
229 // %Dc- turns color off.
230 // Note: We use the "mirc standard" as described in <http://www.mirc.co.uk/help/color.txt>.
231 // This means that we don't accept something like \x03,5 (even though others, like WeeChat, do).
234 pos = mirc.indexOf('\x03', pos);
235 if(pos < 0) break; // no more mirc color codes
237 int l = mirc.length();
239 // check for fg color
240 if(i < l && mirc[i].isDigit()) {
242 if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
243 else num.prepend('0');
244 ins = QString("%Dcf%1").arg(num);
246 if(i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) {
249 if(i < l && mirc[i].isDigit()) num.append(mirc[i++]);
250 else num.prepend('0');
251 ins += QString("%Dcb%1").arg(num);
256 mirc.replace(pos, i-pos, ins);
261 UiStyle::StyledMessage UiStyle::styleMessage(const Message &msg) {
262 QString user = userFromMask(msg.sender());
263 QString host = hostFromMask(msg.sender());
264 QString nick = nickFromMask(msg.sender());
265 QString txt = mircToInternal(msg.text());
266 QString bufferName = msg.bufferInfo().bufferName();
268 StyledMessage result;
270 result.timestamp = styleString(tr("%DT[%1]").arg(msg.timestamp().toLocalTime().toString("hh:mm:ss")));
275 s = tr("%DS<%1>").arg(nick); t = tr("%D0%1").arg(txt); break;
276 case Message::Notice:
277 s = tr("%Dn[%1]").arg(nick); t = tr("%Dn%1").arg(txt); break;
278 case Message::Server:
279 s = tr("%Ds*"); t = tr("%Ds%1").arg(txt); break;
281 s = tr("%De*"); t = tr("%De%1").arg(txt); break;
283 s = tr("%Dj-->"); t = tr("%Dj%DN%1%DN %DH(%2@%3)%DH has joined %DC%4%DC").arg(nick, user, host, bufferName); break;
285 s = tr("%Dp<--"); t = tr("%Dp%DN%1%DN %DH(%2@%3)%DH has left %DC%4%DC").arg(nick, user, host, bufferName);
286 if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
289 s = tr("%Dq<--"); t = tr("%Dq%DN%DU%1%DU%DN %DH(%2@%3)%DH has quit").arg(nick, user, host);
290 if(!txt.isEmpty()) t = QString("%1 (%2)").arg(t).arg(txt);
294 QString victim = txt.section(" ", 0, 0);
295 //if(victim == ui.ownNick->currentText()) victim = tr("you");
296 QString kickmsg = txt.section(" ", 1);
297 t = tr("%Dk%DN%1%DN has kicked %DN%2%DN from %DC%3%DC").arg(nick).arg(victim).arg(bufferName);
298 if(!kickmsg.isEmpty()) t = QString("%1 (%2)").arg(t).arg(kickmsg);
303 if(nick == msg.text()) t = tr("%DrYou are now known as %DN%1%DN").arg(txt);
304 else t = tr("%Dr%DN%1%DN is now known as %DN%2%DN").arg(nick, txt);
308 if(nick.isEmpty()) t = tr("%DmUser mode: %DM%1%DM").arg(txt);
309 else t = tr("%DmMode %DM%1%DM by %DN%2%DN").arg(txt, nick);
311 case Message::Action:
313 t = tr("%Da%DN%1%DN %2").arg(nick).arg(txt);
316 s = tr("%De%1").arg(msg.sender());
317 t = tr("%De[%1]").arg(txt);
319 result.sender = styleString(s);
320 result.text = styleString(t);
324 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList) {
325 out << formatList.count();
326 UiStyle::FormatList::const_iterator it = formatList.begin();
327 while(it != formatList.end()) {
328 out << (*it).first << (*it).second;
334 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList) {
337 for(int i = 0; i < cnt; i++) {
338 int pos; quint32 ftype;
340 formatList.append(qMakePair(pos, ftype));