9b845d3f5ae2d3e78480a16cd7b203bb55d138d4
[quassel.git] / gui / style.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "style.h"
22
23 void Style::init() {
24    // Colors (mIRC standard)
25   colors["00"] = QColor("white");
26   colors["01"] = QColor("black");
27   colors["02"] = QColor("navy");
28   colors["03"] = QColor("green");
29   colors["04"] = QColor("red");
30   colors["05"] = QColor("maroon");
31   colors["06"] = QColor("purple");
32   colors["07"] = QColor("orange");
33   colors["08"] = QColor("yellow");
34   colors["09"] = QColor("lime");
35   colors["10"] = QColor("teal");
36   colors["11"] = QColor("aqua");
37   colors["12"] = QColor("royalblue");
38   colors["13"] = QColor("fuchsia");
39   colors["14"] = QColor("grey");
40   colors["15"] = QColor("silver");
41
42   QTextCharFormat def;
43   def.setForeground(QBrush("black"));
44   def.setFont(QFont("Verdana",9));
45   formats["default"] = def;
46
47   // %B - 0x02 - bold
48   QTextCharFormat bold;
49   bold.setFontWeight(QFont::Bold);
50   formats["%B"] = bold;
51
52   // %O - 0x0f - plain
53   formats["%O"] = def;
54
55   // %R - 0x12 - reverse
56   // -- - 0x16 - reverse
57   // (no format)
58
59   // %S - 0x1d - italic
60   QTextCharFormat italic;
61   italic.setFontItalic(true);
62   formats["%S"] = italic;
63
64   // %U - 0x1f - underline
65   QTextCharFormat underline;
66   underline.setFontUnderline(true);
67   formats["%U"] = underline;
68
69   // %C - 0x03 - mIRC colors
70   for(uint i = 0; i < 16; i++) {
71     QString idx = QString("%1").arg(i, (int)2, (int)10, (QChar)'0');
72     QString fg = QString("%C%1").arg(idx);
73     QString bg = QString("%C,%1").arg(idx);
74     QTextCharFormat fgf; fgf.setForeground(QBrush(colors[idx])); formats[fg] = fgf;
75     QTextCharFormat bgf; bgf.setBackground(QBrush(colors[idx])); formats[bg] = bgf;
76   }
77
78   // Internal formats - %D<char>
79   // %D0 - plain msg
80   QTextCharFormat plainMsg;
81   plainMsg.setForeground(QBrush("black"));
82   formats["%D0"] = plainMsg;
83   // %Dn - notice
84   QTextCharFormat notice;
85   notice.setForeground(QBrush("navy"));
86   formats["%Dn"] = notice;
87   // %Ds - server msg
88   QTextCharFormat server;
89   server.setForeground(QBrush("navy"));
90   formats["%Ds"] = server;
91   // %De - error msg
92   QTextCharFormat error;
93   error.setForeground(QBrush("red"));
94   formats["%De"] = error;
95   // %Dj - join
96   QTextCharFormat join;
97   join.setForeground(QBrush("green"));
98   formats["%Dj"] = join;
99   // %Dp - part
100   QTextCharFormat part;
101   part.setForeground(QBrush("indianred"));
102   formats["%Dp"] = part;
103   // %Dq - quit
104   QTextCharFormat quit;
105   quit.setForeground(QBrush("indianred"));
106   formats["%Dq"] = quit;
107   // %Dk - kick
108   QTextCharFormat kick;
109   kick.setForeground(QBrush("indianred"));
110   formats["%Dk"] = kick;
111   // %Dr - nick rename
112   QTextCharFormat nren;
113   nren.setForeground(QBrush("magenta"));
114   formats["%Dr"] = nren;
115   // %Dm - mode change
116   QTextCharFormat mode;
117   mode.setForeground(QBrush("steelblue"));
118   formats["%Dm"] = mode;
119
120   // %DT - timestamp
121   QTextCharFormat ts;
122   ts.setForeground(QBrush("grey"));
123   formats["%DT"] = ts;
124   // %DS - sender
125   QTextCharFormat sender;
126   sender.setAnchor(true);
127   sender.setForeground(QBrush("navy"));
128   formats["%DS"] = sender;
129   // %DN - nickname
130   QTextCharFormat nick;
131   nick.setAnchor(true);
132   nick.setFontWeight(QFont::Bold);
133   formats["%DN"] = nick;
134   // %DH - hostmask
135   QTextCharFormat hostmask;
136   hostmask.setFontItalic(true);
137   formats["%DH"] = hostmask;
138   // %DC - channame
139   QTextCharFormat channel;
140   channel.setAnchor(true);
141   channel.setFontWeight(QFont::Bold);
142   formats["%DC"] = channel;
143   // %DM - modeflags
144   QTextCharFormat flags;
145   flags.setFontWeight(QFont::Bold);
146   formats["%DM"] = flags;
147   // %DU - clickable URL
148   QTextCharFormat url;
149   url.setFontUnderline(true);
150   url.setAnchor(true);
151   formats["%DU"] = url;
152 }
153
154 QString Style::mircToInternal(QString mirc) {
155   mirc.replace('%', "%%");      // escape % just to be sure
156   mirc.replace('\x02', "%B");
157   mirc.replace('\x03', "%C");
158   mirc.replace('\x0f', "%O");
159   mirc.replace('\x12', "%R");
160   mirc.replace('\x16', "%R");
161   mirc.replace('\x1d', "%S");
162   mirc.replace('\x1f', "%U");
163   return mirc;
164 }
165
166 /** Returns a string stripped of format codes, and a list of FormatRange objects
167  *  describing the formats of the string.
168  * \param s string in internal format (% style format codes)
169  */ 
170 Style::FormattedString Style::internalToFormatted(QString s) {
171   QHash<QString, int> toggles;
172   QString p;
173   FormattedString sf;
174   QTextLayout::FormatRange rng;
175   rng.format = formats["default"]; rng.start = 0; rng.length = -1; sf.formats.append(rng);
176   toggles["default"] = sf.formats.count() - 1;
177   int i, j;
178   for(i = 0, j = 0; i < s.length(); i++) {
179     if(s[i] != '%') { p += s[i]; j++; continue; }
180     i++;
181     if(s[i] == '%') { p += '%'; j++; continue; }
182     else if(s[i] == 'C') {
183       if(!s[i+1].isDigit() && s[i+1] != ',') {
184         if(toggles.contains("bg")) {
185           sf.formats[toggles["bg"]].length = j - sf.formats[toggles["bg"]].start;
186           toggles.remove("bg");
187         }
188       }
189       if(s[i+1].isDigit() || s[i+1] != ',') {
190         if(toggles.contains("fg")) {
191           sf.formats[toggles["fg"]].length = j - sf.formats[toggles["fg"]].start;
192           toggles.remove("fg");
193         }
194         if(s[i+1].isDigit()) {
195           QString n(s[++i]);
196           if(s[i+1].isDigit()) n += s[++i];
197           int num = n.toInt() & 0xf;
198           n = QString("%C%1").arg(num, (int)2, (int)10, (QChar)'0');
199           //qDebug() << n << formats[n].foreground();
200           QTextLayout::FormatRange range; 
201           range.format = formats[n]; range.start = j; range.length = -1; sf.formats.append(range);
202           toggles["fg"] = sf.formats.count() - 1;
203         }
204       }
205       if(s[i+1] == ',') {
206         if(toggles.contains("bg")) {
207           sf.formats[toggles["bg"]].length = j - sf.formats[toggles["bg"]].start;
208           toggles.remove("bg");
209         }
210         i++;
211         if(s[i+1].isDigit()) {
212           QString n(s[++i]);
213           if(s[i+1].isDigit()) n += s[++i];
214           int num = n.toInt() & 0xf;
215           n = QString("%C,%1").arg(num, (int)2, (int)10, (QChar)'0');
216           QTextLayout::FormatRange range;
217           range.format = formats[n]; range.start = j; range.length = -1;
218           sf.formats.append(range);
219           toggles["bg"] = sf.formats.count() - 1;
220         }
221       }
222     } else if(s[i] == 'O') {
223       foreach(QString key, toggles.keys()) {
224         if(key == "default") continue;
225         sf.formats[toggles[key]].length = j - sf.formats[toggles[key]].start;
226         toggles.remove(key);
227       }
228
229     } else if(s[i] == 'R') {
230       // TODO implement reverse formatting
231
232     } else {
233       // all others are toggles
234       QString key = "%"; key += s[i];
235       if(s[i] == 'D') key += s[i+1];
236       if(formats.contains(key)) {
237         if(s[i] == 'D') i++;
238         if(toggles.contains(key)) {
239           sf.formats[toggles[key]].length = j - sf.formats[toggles[key]].start;
240           if(key == "%DU") {
241             // URL handling
242             // FIXME check for and handle format codes within URLs
243             QString u = s.mid(i - sf.formats[toggles[key]].length - 2, sf.formats[toggles[key]].length);
244             UrlInfo url;
245             url.start = sf.formats[toggles[key]].start;
246             url.end   = j;
247             url.url = QUrl(u);
248             sf.urls.append(url);
249           }
250           toggles.remove(key);
251         } else {
252           QTextLayout::FormatRange range;
253           range.format = formats[key]; range.start = j; range.length = -1;
254           sf.formats.append(range);
255           toggles[key] = sf.formats.count() -1;
256         }
257       } else {
258         // unknown format
259         p += '%'; p += s[i]; j+=2;
260       }
261     }
262   }
263   foreach(int idx, toggles.values()) {
264     sf.formats[idx].length = j - sf.formats[idx].start;
265   }
266   sf.text = p;
267   return sf;
268 }
269
270 QHash<QString, QTextCharFormat> Style::formats;
271 QHash<QString, QColor> Style::colors;
272