This should finally fix the remaining font issues, caused by not having explicitely...
[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   QTextCharFormat def;
28   def.setForeground(QBrush("#000000"));
29   def.setFont(QFont("Monospace", QApplication::font().pointSize()));
30   def.font().setFixedPitch(true);
31   def.font().setStyleHint(QFont::TypeWriter);
32   _defaultFormats = QVector<QTextCharFormat>(NumFormatTypes, def);
33   _customFormats = QVector<QTextCharFormat>(NumFormatTypes, QTextFormat().toCharFormat());
34
35   // Load saved custom formats
36   UiStyleSettings s(_settingsKey);
37   foreach(FormatType type, s.availableFormats()) {
38     _customFormats[type] = s.customFormat(type);
39   }
40
41   // Initialize color codes according to mIRC "standard"
42   QStringList colors;
43   //colors << "white" << "black" << "navy" << "green" << "red" << "maroon" << "purple" << "orange";
44   //colors << "yellow" << "lime" << "teal" << "aqua" << "royalblue" << "fuchsia" << "grey" << "silver";
45   colors << "#ffffff" << "#000000" << "#000080" << "#008000" << "#ff0000" << "#800000" << "#800080" << "#ffa500";
46   colors << "#ffff00" << "#00ff00" << "#008080" << "#00ffff" << "#4169E1" << "#ff00ff" << "#808080" << "#c0c0c0";
47
48   // Now initialize the mapping between FormatCodes and FormatTypes...
49   _formatCodes["%O"] = None;
50   _formatCodes["%B"] = Bold;
51   _formatCodes["%S"] = Italic;
52   _formatCodes["%U"] = Underline;
53   _formatCodes["%R"] = Reverse;
54
55   _formatCodes["%D0"] = PlainMsg;
56   _formatCodes["%Dn"] = NoticeMsg;
57   _formatCodes["%Ds"] = ServerMsg;
58   _formatCodes["%De"] = ErrorMsg;
59   _formatCodes["%Dj"] = JoinMsg;
60   _formatCodes["%Dp"] = PartMsg;
61   _formatCodes["%Dq"] = QuitMsg;
62   _formatCodes["%Dk"] = KickMsg;
63   _formatCodes["%Dr"] = RenameMsg;
64   _formatCodes["%Dm"] = ModeMsg;
65   _formatCodes["%Da"] = ActionMsg;
66
67   _formatCodes["%DT"] = Timestamp;
68   _formatCodes["%DS"] = Sender;
69   _formatCodes["%DN"] = Nick;
70   _formatCodes["%DH"] = Hostmask;
71   _formatCodes["%DC"] = ChannelName;
72   _formatCodes["%DM"] = ModeFlags;
73   _formatCodes["%DU"] = Url;
74
75   // Set color formats
76   for(int i = 0; i < 16; i++) {
77     QString idx = QString("%1").arg(i, (int)2, (int)10, (QChar)'0');
78     _formatCodes[QString("%Dcf%1").arg(idx)] = (FormatType)(FgCol00 + i);
79     _formatCodes[QString("%Dcb%1").arg(idx)] = (FormatType)(BgCol00 + i);
80     QTextCharFormat fgf, bgf;
81     fgf.setForeground(QBrush(QColor(colors[i]))); setFormat((FormatType)(FgCol00 + i), fgf, Settings::Default);
82     bgf.setBackground(QBrush(QColor(colors[i]))); setFormat((FormatType)(BgCol00 + i), bgf, Settings::Default);
83     //FIXME fix the havoc caused by ColorSettingsPage
84     setFormat((FormatType)(FgCol00 + i), fgf, Settings::Custom);
85     setFormat((FormatType)(BgCol00 + i), bgf, Settings::Custom);
86   }
87
88   // Set a few more standard formats
89   QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
90   setFormat(Bold, bold, Settings::Default);
91
92   QTextCharFormat italic; italic.setFontItalic(true);
93   setFormat(Italic, italic, Settings::Default);
94
95   QTextCharFormat underline; underline.setFontUnderline(true);
96   setFormat(Underline, underline, Settings::Default);
97
98   // All other formats should be defined in derived classes.
99 }
100
101 UiStyle::~ UiStyle() {
102   
103 }
104
105 void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
106   if(mode == Settings::Default) {
107     _defaultFormats[ftype] = fmt;
108   } else {
109     UiStyleSettings s(_settingsKey);
110     if(fmt != _defaultFormats[ftype]) {
111       _customFormats[ftype] = fmt;
112       s.setCustomFormat(ftype, fmt);
113     } else {
114       _customFormats[ftype] = QTextFormat().toCharFormat();
115       s.removeCustomFormat(ftype);
116     }
117   }
118 }
119
120 QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
121   if(mode == Settings::Custom && _customFormats[ftype].isValid()) return _customFormats[ftype];
122   else return _defaultFormats[ftype];
123 }
124
125 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
126   if(_formatCodes.contains(code)) return _formatCodes.value(code);
127   return Invalid;
128 }
129
130 QString UiStyle::formatCode(FormatType ftype) const {
131   return _formatCodes.key(ftype);
132 }
133
134 UiStyle::StyledText UiStyle::styleString(const QString &_s) {
135   QString s = _s;
136   StyledText result;
137   QList<FormatType> fmtList;
138   fmtList.append(None);
139   QTextLayout::FormatRange curFmtRng;
140   curFmtRng.format = format(None);
141   curFmtRng.start = 0;
142   result.formats.append(curFmtRng);
143   int pos = 0; int length = 0;
144   int fgCol = -1, bgCol = -1;  // marks current mIRC color
145   for(;;) {
146     pos = s.indexOf('%', pos);
147     if(pos < 0) break;
148     if(s[pos+1] == '%') { // escaped %, just remove one and continue
149       s.remove(pos, 1);
150       pos++;
151       continue;
152     } else if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
153       if(s[pos+3] == '-') { // color off
154         if(fgCol >= 0) {
155           fmtList.removeAll((FormatType)(FgCol00 + fgCol));
156           fgCol = -1;
157         }
158         if(bgCol >= 0) {
159           fmtList.removeAll((FormatType)(BgCol00 + bgCol));
160           bgCol = -1;
161         }
162         curFmtRng.format = mergedFormat(fmtList);
163         length = 4;
164       } else {
165         int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
166         //TODO: use 99 as transparent color (re mirc color "standard")
167         color &= 0x0f;
168         int *colptr; FormatType coltype;
169         if(s[pos+3] == 'f') { // foreground
170           colptr = &fgCol; coltype = FgCol00;
171         } else {              // background
172           Q_ASSERT(s[pos+3] == 'b');
173           colptr = &bgCol; coltype = BgCol00;
174         }
175         if(*colptr >= 0) {
176           // color already set, remove format code and add new one
177           Q_ASSERT(fmtList.contains((FormatType)(coltype + *colptr)));
178           fmtList.removeAll((FormatType)(coltype + *colptr));
179           fmtList.append((FormatType)(coltype + color));
180           curFmtRng.format = mergedFormat(fmtList);
181         } else {
182           fmtList.append((FormatType)(coltype + color));
183           curFmtRng.format.merge(format(fmtList.last()));
184         }
185         *colptr = color;
186         length = 6;
187       }
188     } else if(s[pos+1] == 'O') { // reset formatting
189       fmtList.clear(); fmtList.append(None);
190       curFmtRng.format = format(None);
191       fgCol = bgCol = -1;
192       length = 2;
193     } else if(s[pos+1] == 'R') { // reverse
194       // TODO: implement reverse formatting
195
196       length = 2;
197     } else { // all others are toggles
198       QString code = QString("%") + s[pos+1];
199       if(s[pos+1] == 'D') code += s[pos+2];
200       FormatType ftype = formatType(code);
201       if(ftype == Invalid) {
202         qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
203         continue;
204       }
205       //Q_ASSERT(ftype != Invalid);
206       length = code.length();
207       if(!fmtList.contains(ftype)) {
208         // toggle it on
209         fmtList.append(ftype);
210         curFmtRng.format.merge(format(ftype));
211       } else {
212         // toggle it off
213         fmtList.removeAll(ftype);
214         curFmtRng.format = mergedFormat(fmtList);
215       }
216     }
217     s.remove(pos, length); // remove format code from string
218     // now see if something changed and else insert the format
219     if(curFmtRng.format == result.formats.last().format) continue;  // no change, so we just ignore
220     curFmtRng.start = pos;
221     if(pos == result.formats.last().start) {
222       // same starting point -> we just overwrite the old format
223       result.formats.last() = curFmtRng;
224     } else {
225       // fix length of last format
226       result.formats.last().length = pos - result.formats.last().start;
227       result.formats.append(curFmtRng);
228     }
229   }
230   result.formats.last().length = s.length() - result.formats.last().start;
231   if(result.formats.last().length == 0) result.formats.removeLast();
232   result.text = s;
233   return result;
234 }
235
236 QTextCharFormat UiStyle::mergedFormat(QList<FormatType> formatList) {
237   QTextCharFormat fmt;
238   foreach(FormatType ftype, formatList) {
239     fmt.merge(format(ftype));
240   }
241   return fmt;
242 }