1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include "uistylesettings.h"
24 UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
27 def.setForeground(QBrush("#000000"));
28 def.setFont(QFont("Courier", 10));
29 _defaultFormats = QVector<QTextCharFormat>(NumFormatTypes, def);
30 _customFormats = QVector<QTextCharFormat>(NumFormatTypes, QTextFormat().toCharFormat());
32 // Load saved custom formats
33 UiStyleSettings s(_settingsKey);
34 foreach(FormatType type, s.availableFormats()) {
35 _customFormats[type] = s.customFormat(type);
38 // Initialize color codes according to mIRC "standard"
40 //colors << "white" << "black" << "navy" << "green" << "red" << "maroon" << "purple" << "orange";
41 //colors << "yellow" << "lime" << "teal" << "aqua" << "royalblue" << "fuchsia" << "grey" << "silver";
42 colors << "#ffffff" << "#000000" << "#000080" << "#008000" << "#ff0000" << "#800000" << "#800080" << "#ffa500";
43 colors << "#ffff00" << "#00ff00" << "#008080" << "#00ffff" << "#4169E1" << "#ff00ff" << "#808080" << "#c0c0c0";
45 // Now initialize the mapping between FormatCodes and FormatTypes...
46 _formatCodes["%O"] = None;
47 _formatCodes["%B"] = Bold;
48 _formatCodes["%S"] = Italic;
49 _formatCodes["%U"] = Underline;
50 _formatCodes["%R"] = Reverse;
52 _formatCodes["%D0"] = PlainMsg;
53 _formatCodes["%Dn"] = NoticeMsg;
54 _formatCodes["%Ds"] = ServerMsg;
55 _formatCodes["%De"] = ErrorMsg;
56 _formatCodes["%Dj"] = JoinMsg;
57 _formatCodes["%Dp"] = PartMsg;
58 _formatCodes["%Dq"] = QuitMsg;
59 _formatCodes["%Dk"] = KickMsg;
60 _formatCodes["%Dr"] = RenameMsg;
61 _formatCodes["%Dm"] = ModeMsg;
62 _formatCodes["%Da"] = ActionMsg;
64 _formatCodes["%DT"] = Timestamp;
65 _formatCodes["%DS"] = Sender;
66 _formatCodes["%DN"] = Nick;
67 _formatCodes["%DH"] = Hostmask;
68 _formatCodes["%DC"] = ChannelName;
69 _formatCodes["%DM"] = ModeFlags;
70 _formatCodes["%DU"] = Url;
73 for(int i = 0; i < 16; i++) {
74 QString idx = QString("%1").arg(i, (int)2, (int)10, (QChar)'0');
75 _formatCodes[QString("%Dcf%1").arg(idx)] = (FormatType)(FgCol00 + i);
76 _formatCodes[QString("%Dcb%1").arg(idx)] = (FormatType)(BgCol00 + i);
77 QTextCharFormat fgf, bgf;
78 fgf.setForeground(QBrush(QColor(colors[i]))); setFormat((FormatType)(FgCol00 + i), fgf, Settings::Default);
79 bgf.setBackground(QBrush(QColor(colors[i]))); setFormat((FormatType)(BgCol00 + i), bgf, Settings::Default);
82 // Set a few more standard formats
83 QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
84 setFormat(Bold, bold, Settings::Default);
86 QTextCharFormat italic; italic.setFontItalic(true);
87 setFormat(Italic, italic, Settings::Default);
89 QTextCharFormat underline; underline.setFontUnderline(true);
90 setFormat(Underline, underline, Settings::Default);
92 // All other formats should be defined in derived classes.
95 UiStyle::~ UiStyle() {
99 void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
100 if(mode == Settings::Default) {
101 _defaultFormats[ftype] = fmt;
103 UiStyleSettings s(_settingsKey);
104 if(fmt != _defaultFormats[ftype]) {
105 _customFormats[ftype] = fmt;
106 s.setCustomFormat(ftype, fmt);
108 _customFormats[ftype] = QTextFormat().toCharFormat();
109 s.removeCustomFormat(ftype);
114 QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
115 if(mode == Settings::Custom && _customFormats[ftype].isValid()) return _customFormats[ftype];
116 else return _defaultFormats[ftype];
119 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
120 if(_formatCodes.contains(code)) return _formatCodes.value(code);
124 QString UiStyle::formatCode(FormatType ftype) const {
125 return _formatCodes.key(ftype);
128 UiStyle::StyledText UiStyle::styleString(QString s) {
130 QList<FormatType> fmtList;
131 fmtList.append(None);
132 QTextLayout::FormatRange curFmtRng;
133 curFmtRng.format = format(None);
135 result.formats.append(curFmtRng);
136 int pos = 0; int length;
137 int fgCol = -1, bgCol = -1; // marks current mIRC color
139 pos = s.indexOf('%', pos);
141 if(s[pos+1] == '%') { // escaped %, just remove one and continue
145 } else if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
146 if(s[pos+3] == '-') { // color off
148 fmtList.removeAll((FormatType)(FgCol00 + fgCol));
152 fmtList.removeAll((FormatType)(BgCol00 + bgCol));
155 curFmtRng.format = mergedFormat(fmtList);
158 int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
159 //TODO: use 99 as transparent color (re mirc color "standard")
161 int *colptr; FormatType coltype;
162 if(s[pos+3] == 'f') { // foreground
163 colptr = &fgCol; coltype = FgCol00;
164 } else { // background
165 Q_ASSERT(s[pos+3] == 'b');
166 colptr = &bgCol; coltype = BgCol00;
169 // color already set, remove format code and add new one
170 Q_ASSERT(fmtList.contains((FormatType)(coltype + *colptr)));
171 fmtList.removeAll((FormatType)(coltype + *colptr));
172 fmtList.append((FormatType)(coltype + color));
173 curFmtRng.format = mergedFormat(fmtList);
175 fmtList.append((FormatType)(coltype + color));
176 curFmtRng.format.merge(format(fmtList.last()));
181 } else if(s[pos+1] == 'O') { // reset formatting
182 fmtList.clear(); fmtList.append(None);
183 curFmtRng.format = format(None);
186 } else if(s[pos+1] == 'R') { // reverse
187 // TODO: implement reverse formatting
190 } else { // all others are toggles
191 QString code = QString("%") + s[pos+1];
192 if(s[pos+1] == 'D') code += s[pos+2];
193 FormatType ftype = formatType(code);
194 if(ftype == Invalid) {
195 qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
198 //Q_ASSERT(ftype != Invalid);
199 length = code.length();
200 if(!fmtList.contains(ftype)) {
202 fmtList.append(ftype);
203 curFmtRng.format.merge(format(ftype));
206 fmtList.removeAll(ftype);
207 curFmtRng.format = mergedFormat(fmtList);
210 s.remove(pos, length); // remove format code from string
211 // now see if something changed and else insert the format
212 if(curFmtRng.format == result.formats.last().format) continue; // no change, so we just ignore
213 curFmtRng.start = pos;
214 if(pos == result.formats.last().start) {
215 // same starting point -> we just overwrite the old format
216 result.formats.last() = curFmtRng;
218 // fix length of last format
219 result.formats.last().length = pos - result.formats.last().start;
220 result.formats.append(curFmtRng);
223 result.formats.last().length = s.length() - result.formats.last().start;
224 if(result.formats.last().length == 0) result.formats.removeLast();
229 QTextCharFormat UiStyle::mergedFormat(QList<FormatType> formatList) {
231 foreach(FormatType ftype, formatList) {
232 fmt.merge(format(ftype));