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 ***************************************************************************/
20 #include <QApplication>
23 #include "uistylesettings.h"
25 UiStyle::UiStyle(const QString &settingsKey) : _settingsKey(settingsKey) {
27 _defaultPlainFormat.setForeground(QBrush("#000000"));
28 _defaultPlainFormat.setFont(QFont("Monospace", QApplication::font().pointSize()));
29 _defaultPlainFormat.font().setFixedPitch(true);
30 _defaultPlainFormat.font().setStyleHint(QFont::TypeWriter);
31 setFormat(None, _defaultPlainFormat, Settings::Default);
33 // Load saved custom formats
34 UiStyleSettings s(_settingsKey);
35 foreach(FormatType type, s.availableFormats()) {
36 _customFormats[type] = s.customFormat(type);
39 // Initialize color codes according to mIRC "standard"
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";
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;
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;
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;
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);
83 // Set a few more standard formats
84 QTextCharFormat bold; bold.setFontWeight(QFont::Bold);
85 setFormat(Bold, bold, Settings::Default);
87 QTextCharFormat italic; italic.setFontItalic(true);
88 setFormat(Italic, italic, Settings::Default);
90 QTextCharFormat underline; underline.setFontUnderline(true);
91 setFormat(Underline, underline, Settings::Default);
93 // All other formats should be defined in derived classes.
96 UiStyle::~ UiStyle() {
100 void UiStyle::setFormat(FormatType ftype, QTextCharFormat fmt, Settings::Mode mode) {
101 if(mode == Settings::Default) {
102 _defaultFormats[ftype] = fmt;
104 UiStyleSettings s(_settingsKey);
105 if(fmt != _defaultFormats[ftype]) {
106 _customFormats[ftype] = fmt;
107 s.setCustomFormat(ftype, fmt);
109 _customFormats[ftype] = QTextFormat().toCharFormat();
110 s.removeCustomFormat(ftype);
115 QTextCharFormat UiStyle::format(FormatType ftype, Settings::Mode mode) const {
116 if(mode == Settings::Custom && _customFormats.contains(ftype)) return _customFormats.value(ftype);
117 else return _defaultFormats.value(ftype, QTextCharFormat());
120 // NOTE: This function is intimately tied to the values in FormatType. Don't change this
121 // until you _really_ know what you do!
122 QTextCharFormat UiStyle::mergedFormat(quint32 ftype) {
123 if(_cachedFormats.contains(ftype)) return _cachedFormats[ftype];
124 if(ftype == Invalid) return QTextCharFormat();
125 // Now we construct the merged format, starting with the default
126 QTextCharFormat fmt = format(None);
127 // First: general message format
128 fmt.merge(format((FormatType)(ftype & 0x0f)));
129 // now more specific ones
130 for(quint32 mask = 0x0010; mask <= 0x2000; mask <<= 1) {
131 if(ftype & mask) fmt.merge(format((FormatType)mask));
134 if(ftype & 0x00400000) fmt.merge(format((FormatType)(ftype & 0x0f400000))); // foreground
135 if(ftype & 0x00800000) fmt.merge(format((FormatType)(ftype & 0xf0800000))); // background
137 if(ftype & Url) fmt.merge(format(Url));
141 UiStyle::FormatType UiStyle::formatType(const QString & code) const {
142 if(_formatCodes.contains(code)) return _formatCodes.value(code);
146 QString UiStyle::formatCode(FormatType ftype) const {
147 return _formatCodes.key(ftype);
150 // This method expects a well-formatted string, there is no error checking!
151 // Since we create those ourselves, we should be pretty safe that nobody does something crappy here.
152 UiStyle::StyledString UiStyle::styleString(const QString &s_) {
155 result.formats.append(qMakePair(0, (quint32)None));
156 quint32 curfmt = (quint32)None;
157 int pos = 0; int length = 0;
159 pos = s.indexOf('%', pos);
161 if(s[pos+1] == '%') { // escaped %, we just remove one and continue
166 if(s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
167 if(s[pos+3] == '-') { // color off
168 curfmt &= 0x003fffff;
171 int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue();
172 //TODO: use 99 as transparent color (re mirc color "standard")
175 curfmt |= (color << 24) | 0x00400000;
177 curfmt |= (color << 28) | 0x00800000;
180 } else if(s[pos+1] == 'O') { // reset formatting
181 curfmt &= 0x0000000f; // we keep message type-specific formatting
183 } else if(s[pos+1] == 'R') { // reverse
184 // TODO: implement reverse formatting
187 } else { // all others are toggles
188 QString code = QString("%") + s[pos+1];
189 if(s[pos+1] == 'D') code += s[pos+2];
190 FormatType ftype = formatType(code);
191 if(ftype == Invalid) {
192 qWarning(qPrintable(QString("Invalid format code in string: %1").arg(s)));
196 length = code.length();
198 s.remove(pos, length);
199 if(pos == result.formats.last().first)
200 result.formats.last().second = curfmt;
202 result.formats.append(qMakePair(pos, curfmt));