First version of our new optimized style engine. Not functional yet, so don't bother...
[quassel.git] / src / uisupport / uistyle.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #include <QApplication>
21
22 #include "uistyle.h"
23 #include "uistylesettings.h"
24
25 UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
26   // Default format
27   _defaultPlainFormat.setForeground(QBrush("#000000"));
28   _defaultPlainFormat.setFont(QFont("Monospace", QApplication::font().pointSize()));
29   _defaultPlainFormat.font().setFixedPitch(true);
30   _defaultPlainFormat.font().setStyleHint(QFont::TypeWriter);
31   setFormat(None, _defaultPlainFormat, Settings::Default);
32   
33   // Load saved custom formats
34   UiStyleSettings s(_settingsKey);
35   foreach(FormatType type, s.availableFormats()) {
36     _customFormats[type] = s.customFormat(type);
37   }
38
39   // Initialize color codes according to mIRC "standard"
40   QStringList colors;
41   //colors << "white" << "black" << "navy" << "green" << "red" << "maroon" << "purple" << "orange";
42   //colors << "yellow" << "lime" << "teal" << "aqua" << "royalblue" << "fuchsia" << "grey" << "silver";
43   colors << "#ffffff" << "#000000" << "#000080" << "#008000" << "#ff0000" << "#800000" << "#800080" << "#ffa500";
44   colors << "#ffff00" << "#00ff00" << "#008080" << "#00ffff" << "#4169E1" << "#ff00ff" << "#808080" << "#c0c0c0";
45
46   // Now initialize the mapping between FormatCodes and FormatTypes...
47   _formatCodes["%O"] = None;
48   _formatCodes["%B"] = Bold;
49   _formatCodes["%S"] = Italic;
50   _formatCodes["%U"] = Underline;
51   _formatCodes["%R"] = Reverse;
52
53   _formatCodes["%D0"] = PlainMsg;
54   _formatCodes["%Dn"] = NoticeMsg;
55   _formatCodes["%Ds"] = ServerMsg;
56   _formatCodes["%De"] = ErrorMsg;
57   _formatCodes["%Dj"] = JoinMsg;
58   _formatCodes["%Dp"] = PartMsg;
59   _formatCodes["%Dq"] = QuitMsg;
60   _formatCodes["%Dk"] = KickMsg;
61   _formatCodes["%Dr"] = RenameMsg;
62   _formatCodes["%Dm"] = ModeMsg;
63   _formatCodes["%Da"] = ActionMsg;
64
65   _formatCodes["%DT"] = Timestamp;
66   _formatCodes["%DS"] = Sender;
67   _formatCodes["%DN"] = Nick;
68   _formatCodes["%DH"] = Hostmask;
69   _formatCodes["%DC"] = ChannelName;
70   _formatCodes["%DM"] = ModeFlags;
71   _formatCodes["%DU"] = Url;
72
73   // Set color formats
74   for(int i = 0; i < 16; i++) {
75     QString idx = QString("%1").arg(i, (int)2, (int)10, (QChar)'0');
76     _formatCodes[QString("%Dcf%1").arg(idx)] = (FormatType)(FgCol00 + i);
77     _formatCodes[QString("%Dcb%1").arg(idx)] = (FormatType)(BgCol00 + i);
78     QTextCharFormat fgf, bgf;
79     fgf.setForeground(QBrush(QColor(colors[i]))); setFormat((FormatType)(FgCol00 + i), fgf, Settings::Default);
80     bgf.setBackground(QBrush(QColor(colors[i]))); setFormat((FormatType)(BgCol00 + i), bgf, Settings::Default);
81   }
82
83   // Set a few more standard formats
84   QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
85   setFormat(Bold, bold, Settings::Default);
86
87   QTextCharFormat italic; italic.setFontItalic(true);
88   setFormat(Italic, italic, Settings::Default);
89
90   QTextCharFormat underline; underline.setFontUnderline(true);
91   setFormat(Underline, underline, Settings::Default);
92
93   // All other formats should be defined in derived classes.
94 }
95
96 UiStyle::~ UiStyle() {
97   
98 }
99
100 void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
101   if(mode == Settings::Default) {
102     _defaultFormats[ftype] = fmt;
103   } else {
104     UiStyleSettings s(_settingsKey);
105     if(fmt != _defaultFormats[ftype]) {
106       _customFormats[ftype] = fmt;
107       s.setCustomFormat(ftype, fmt);
108     } else {
109       _customFormats[ftype] = QTextFormat().toCharFormat();
110       s.removeCustomFormat(ftype);
111     }
112   }
113 }
114
115 QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
116   if(mode == Settings::Custom && _customFormats.contains(ftype)) return _customFormats.value(ftype);
117   else return _defaultFormats.value(ftype, QTextCharFormat());
118 }
119
120 // NOTE: This function is intimately tied to the values in FormatType. Don't change this
121 //       until you _really_ know what you do!
122 QTextCharFormat UiStyle::mergedFormat(quint32 ftype) {
123   if(_cachedFormats.contains(ftype)) return _cachedFormats[ftype];
124   if(ftype == Invalid) return QTextCharFormat();
125   // Now we construct the merged format, starting with the default
126   QTextCharFormat fmt = format(None);
127   // First: general message format
128   fmt.merge(format((FormatType)(ftype & 0x0f)));
129   // now more specific ones
130   for(quint32 mask = 0x0010; mask <= 0x2000; mask <<= 1) {
131     if(ftype & mask) fmt.merge(format((FormatType)mask));
132   }
133   // color codes!
134   if(ftype & 0x00400000) fmt.merge(format((FormatType)(ftype & 0x0f400000))); // foreground
135   if(ftype & 0x00800000) fmt.merge(format((FormatType)(ftype & 0xf0800000))); // background
136   // URL
137   if(ftype & Url) fmt.merge(format(Url));
138   return fmt;
139 }
140
141 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
142   if(_formatCodes.contains(code)) return _formatCodes.value(code);
143   return Invalid;
144 }
145
146 QString UiStyle::formatCode(FormatType ftype) const {
147   return _formatCodes.key(ftype);
148 }
149
150 // This method expects a well-formatted string, there is no error checking!
151 // Since we create those ourselves, we should be pretty safe that nobody does something crappy here.
152 UiStyle::StyledString UiStyle::styleString(const QString &s_) {
153   QString s = s_;
154   StyledString result;
155   result.formats.append(qMakePair(0, (quint32)None));
156   quint32 curfmt = (quint32)None;
157   int pos = 0; int length = 0;
158   for(;;) {
159     pos = s.indexOf('%', pos);
160     if(pos < 0) break;
161     if(s[pos+1] == '%') { // escaped %, we just remove one and continue
162       s.remove(pos, 1);
163       pos++;
164       continue;
165     }
166     if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
167       if(s[pos+3] == '-') {  // color off
168         curfmt &= 0x003fffff;
169         length = 4;
170       } else {
171         int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
172         //TODO: use 99 as transparent color (re mirc color "standard")
173         color &= 0x0f;
174         if(pos+3 == 'f')
175           curfmt |= (color << 24) | 0x00400000;
176         else
177           curfmt |= (color << 28) | 0x00800000;
178         length = 6;
179       }
180     } else if(s[pos+1] == 'O') { // reset formatting
181       curfmt &= 0x0000000f; // we keep message type-specific formatting
182       length = 2;
183     } else if(s[pos+1] == 'R') { // reverse
184       // TODO: implement reverse formatting
185
186       length = 2;
187     } else { // all others are toggles
188       QString code = QString("%") + s[pos+1];
189       if(s[pos+1] == 'D') code += s[pos+2];
190       FormatType ftype = formatType(code);
191       if(ftype == Invalid) {
192         qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
193         continue;
194       }
195       curfmt ^= ftype;
196       length = code.length();
197     }
198     s.remove(pos, length);
199     if(pos == result.formats.last().first)
200       result.formats.last().second = curfmt;
201     else
202       result.formats.append(qMakePair(pos, curfmt));
203   }
204   result.text = s;
205   return result;
206 }