Make the newly arrived topicbutton display background colors and font styles.
[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
21 #include "uistyle.h"
22 #include "uistylesettings.h"
23
24 UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
25   // Default format
26   QTextCharFormat def;
27   def.setForeground(QBrush("#000000"));
28   //def.setFont(QFont("Courier", 10));
29   def.font().setStyleHint(QFont::TypeWriter);
30   _defaultFormats = QVector<QTextCharFormat>(NumFormatTypes, def);
31   _customFormats = QVector<QTextCharFormat>(NumFormatTypes, QTextFormat().toCharFormat());
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     //FIXME fix the havoc caused by ColorSettingsPage
82     setFormat((FormatType)(FgCol00 + i), fgf, Settings::Custom);
83     setFormat((FormatType)(BgCol00 + i), bgf, Settings::Custom);
84   }
85
86   // Set a few more standard formats
87   QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
88   setFormat(Bold, bold, Settings::Default);
89
90   QTextCharFormat italic; italic.setFontItalic(true);
91   setFormat(Italic, italic, Settings::Default);
92
93   QTextCharFormat underline; underline.setFontUnderline(true);
94   setFormat(Underline, underline, Settings::Default);
95
96   // All other formats should be defined in derived classes.
97 }
98
99 UiStyle::~ UiStyle() {
100   
101 }
102
103 void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
104   if(mode == Settings::Default) {
105     _defaultFormats[ftype] = fmt;
106   } else {
107     UiStyleSettings s(_settingsKey);
108     if(fmt != _defaultFormats[ftype]) {
109       _customFormats[ftype] = fmt;
110       s.setCustomFormat(ftype, fmt);
111     } else {
112       _customFormats[ftype] = QTextFormat().toCharFormat();
113       s.removeCustomFormat(ftype);
114     }
115   }
116 }
117
118 QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
119   if(mode == Settings::Custom && _customFormats[ftype].isValid()) return _customFormats[ftype];
120   else return _defaultFormats[ftype];
121 }
122
123 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
124   if(_formatCodes.contains(code)) return _formatCodes.value(code);
125   return Invalid;
126 }
127
128 QString UiStyle::formatCode(FormatType ftype) const {
129   return _formatCodes.key(ftype);
130 }
131
132 UiStyle::StyledText UiStyle::styleString(QString s) {
133   StyledText result;
134   QList<FormatType> fmtList;
135   fmtList.append(None);
136   QTextLayout::FormatRange curFmtRng;
137   curFmtRng.format = format(None);
138   curFmtRng.start = 0;
139   result.formats.append(curFmtRng);
140   int pos = 0; int length;
141   int fgCol = -1, bgCol = -1;  // marks current mIRC color
142   for(;;) {
143     pos = s.indexOf('%', pos);
144     if(pos < 0) break;
145     if(s[pos+1] == '%') { // escaped %, just remove one and continue
146       s.remove(pos, 1);
147       pos++;
148       continue;
149     } else if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
150       if(s[pos+3] == '-') { // color off
151         if(fgCol >= 0) {
152           fmtList.removeAll((FormatType)(FgCol00 + fgCol));
153           fgCol = -1;
154         }
155         if(bgCol >= 0) {
156           fmtList.removeAll((FormatType)(BgCol00 + bgCol));
157           bgCol = -1;
158         }
159         curFmtRng.format = mergedFormat(fmtList);
160         length = 4;
161       } else {
162         int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
163         //TODO: use 99 as transparent color (re mirc color "standard")
164         color &= 0x0f;
165         int *colptr; FormatType coltype;
166         if(s[pos+3] == 'f') { // foreground
167           colptr = &fgCol; coltype = FgCol00;
168         } else {              // background
169           Q_ASSERT(s[pos+3] == 'b');
170           colptr = &bgCol; coltype = BgCol00;
171         }
172         if(*colptr >= 0) {
173           // color already set, remove format code and add new one
174           Q_ASSERT(fmtList.contains((FormatType)(coltype + *colptr)));
175           fmtList.removeAll((FormatType)(coltype + *colptr));
176           fmtList.append((FormatType)(coltype + color));
177           curFmtRng.format = mergedFormat(fmtList);
178         } else {
179           fmtList.append((FormatType)(coltype + color));
180           curFmtRng.format.merge(format(fmtList.last()));
181         }
182         *colptr = color;
183         length = 6;
184       }
185     } else if(s[pos+1] == 'O') { // reset formatting
186       fmtList.clear(); fmtList.append(None);
187       curFmtRng.format = format(None);
188       fgCol = bgCol = -1;
189       length = 2;
190     } else if(s[pos+1] == 'R') { // reverse
191       // TODO: implement reverse formatting
192
193       length = 2;
194     } else { // all others are toggles
195       QString code = QString("%") + s[pos+1];
196       if(s[pos+1] == 'D') code += s[pos+2];
197       FormatType ftype = formatType(code);
198       if(ftype == Invalid) {
199         qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
200         continue;
201       }
202       //Q_ASSERT(ftype != Invalid);
203       length = code.length();
204       if(!fmtList.contains(ftype)) {
205         // toggle it on
206         fmtList.append(ftype);
207         curFmtRng.format.merge(format(ftype));
208       } else {
209         // toggle it off
210         fmtList.removeAll(ftype);
211         curFmtRng.format = mergedFormat(fmtList);
212       }
213     }
214     s.remove(pos, length); // remove format code from string
215     // now see if something changed and else insert the format
216     if(curFmtRng.format == result.formats.last().format) continue;  // no change, so we just ignore
217     curFmtRng.start = pos;
218     if(pos == result.formats.last().start) {
219       // same starting point -> we just overwrite the old format
220       result.formats.last() = curFmtRng;
221     } else {
222       // fix length of last format
223       result.formats.last().length = pos - result.formats.last().start;
224       result.formats.append(curFmtRng);
225     }
226   }
227   result.formats.last().length = s.length() - result.formats.last().start;
228   if(result.formats.last().length == 0) result.formats.removeLast();
229   result.text = s;
230   return result;
231 }
232
233 QTextCharFormat UiStyle::mergedFormat(QList<FormatType> formatList) {
234   QTextCharFormat fmt;
235   foreach(FormatType ftype, formatList) {
236     fmt.merge(format(ftype));
237   }
238   return fmt;
239 }
240
241