Query buffers are now automatically renamed on nickchanges.
[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   }
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[ftype].isValid()) return _customFormats[ftype];
117   else return _defaultFormats[ftype];
118 }
119
120 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
121   if(_formatCodes.contains(code)) return _formatCodes.value(code);
122   return Invalid;
123 }
124
125 QString UiStyle::formatCode(FormatType ftype) const {
126   return _formatCodes.key(ftype);
127 }
128
129 UiStyle::StyledText UiStyle::styleString(QString s) {
130   StyledText result;
131   QList<FormatType> fmtList;
132   fmtList.append(None);
133   QTextLayout::FormatRange curFmtRng;
134   curFmtRng.format = format(None);
135   curFmtRng.start = 0;
136   result.formats.append(curFmtRng);
137   int pos = 0; int length;
138   int fgCol = -1, bgCol = -1;  // marks current mIRC color
139   for(;;) {
140     pos = s.indexOf('%', pos);
141     if(pos < 0) break;
142     if(s[pos+1] == '%') { // escaped %, just remove one and continue
143       s.remove(pos, 1);
144       pos++;
145       continue;
146     } else if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
147       if(s[pos+3] == '-') { // color off
148         if(fgCol >= 0) {
149           fmtList.removeAll((FormatType)(FgCol00 + fgCol));
150           fgCol = -1;
151         }
152         if(bgCol >= 0) {
153           fmtList.removeAll((FormatType)(BgCol00 + bgCol));
154           bgCol = -1;
155         }
156         curFmtRng.format = mergedFormat(fmtList);
157         length = 4;
158       } else {
159         int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
160         //TODO: use 99 as transparent color (re mirc color "standard")
161         color &= 0x0f;
162         int *colptr; FormatType coltype;
163         if(s[pos+3] == 'f') { // foreground
164           colptr = &fgCol; coltype = FgCol00;
165         } else {              // background
166           Q_ASSERT(s[pos+3] == 'b');
167           colptr = &bgCol; coltype = BgCol00;
168         }
169         if(*colptr >= 0) {
170           // color already set, remove format code and add new one
171           Q_ASSERT(fmtList.contains((FormatType)(coltype + *colptr)));
172           fmtList.removeAll((FormatType)(coltype + *colptr));
173           fmtList.append((FormatType)(coltype + color));
174           curFmtRng.format = mergedFormat(fmtList);
175         } else {
176           fmtList.append((FormatType)(coltype + color));
177           curFmtRng.format.merge(format(fmtList.last()));
178         }
179         *colptr = color;
180         length = 6;
181       }
182     } else if(s[pos+1] == 'O') { // reset formatting
183       fmtList.clear(); fmtList.append(None);
184       curFmtRng.format = format(None);
185       fgCol = bgCol = -1;
186       length = 2;
187     } else if(s[pos+1] == 'R') { // reverse
188       // TODO: implement reverse formatting
189
190       length = 2;
191     } else { // all others are toggles
192       QString code = QString("%") + s[pos+1];
193       if(s[pos+1] == 'D') code += s[pos+2];
194       FormatType ftype = formatType(code);
195       if(ftype == Invalid) {
196         qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
197         continue;
198       }
199       //Q_ASSERT(ftype != Invalid);
200       length = code.length();
201       if(!fmtList.contains(ftype)) {
202         // toggle it on
203         fmtList.append(ftype);
204         curFmtRng.format.merge(format(ftype));
205       } else {
206         // toggle it off
207         fmtList.removeAll(ftype);
208         curFmtRng.format = mergedFormat(fmtList);
209       }
210     }
211     s.remove(pos, length); // remove format code from string
212     // now see if something changed and else insert the format
213     if(curFmtRng.format == result.formats.last().format) continue;  // no change, so we just ignore
214     curFmtRng.start = pos;
215     if(pos == result.formats.last().start) {
216       // same starting point -> we just overwrite the old format
217       result.formats.last() = curFmtRng;
218     } else {
219       // fix length of last format
220       result.formats.last().length = pos - result.formats.last().start;
221       result.formats.append(curFmtRng);
222     }
223   }
224   result.formats.last().length = s.length() - result.formats.last().start;
225   if(result.formats.last().length == 0) result.formats.removeLast();
226   result.text = s;
227   return result;
228 }
229
230 QTextCharFormat UiStyle::mergedFormat(QList<FormatType> formatList) {
231   QTextCharFormat fmt;
232   foreach(FormatType ftype, formatList) {
233     fmt.merge(format(ftype));
234   }
235   return fmt;
236 }
237
238