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