1 /***************************************************************************
2 * Copyright (C) 2005-09 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 ***************************************************************************/
24 #include <QDataStream>
25 #include <QFontMetricsF>
27 #include <QTextCharFormat>
28 #include <QTextLayout>
36 Q_DECLARE_TR_FUNCTIONS(UiStyle)
39 UiStyle(const QString &settingsKey);
42 typedef QList<QPair<quint16, quint32> > FormatList;
44 //! This enumerates the possible formats a text element may have. */
45 /** These formats are ordered on increasing importance, in cases where a given property is specified
46 * by multiple active formats.
47 * \NOTE: Do not change/add values here without also adapting the relevant
48 * methods in this class (in particular mergedFormat())!
49 * Also, we _do_ rely on certain properties of these values in styleString() and friends!
55 // Message Formats (mutually exclusive!)
56 PlainMsg = 0x00000001,
57 NoticeMsg = 0x00000002,
58 ServerMsg = 0x00000003,
59 ErrorMsg = 0x00000004,
64 RenameMsg = 0x00000009,
66 ActionMsg = 0x0000000b,
67 HighlightMsg = 0x0000000f,
69 // Note: mergedFormat() assumes that 0x10 - 0x80 are *only* used within the message contents,
70 // e.g. not together with any of 0x0100-0x2000!
71 // If we happen to find a use case for that, we can see if/how to implement that though.
76 Underline = 0x00000040,
79 // Individual parts of a message
80 Timestamp = 0x00000100,
83 Hostmask = 0x00000800,
84 ChannelName = 0x00001000,
85 ModeFlags = 0x00002000,
87 // URL is special, we want that to take precedence over the rest...
90 // mIRC Colors - we assume those to be present only in plain contents
100 FgCol09 = 0x09400000,
101 FgCol10 = 0x0a400000,
102 FgCol11 = 0x0b400000,
103 FgCol12 = 0x0c400000,
104 FgCol13 = 0x0d400000,
105 FgCol14 = 0x0e400000,
106 FgCol15 = 0x0f400000,
108 BgCol00 = 0x00800000,
109 BgCol01 = 0x10800000,
110 BgCol02 = 0x20800000,
111 BgCol03 = 0x30800000,
112 BgCol04 = 0x40800000,
113 BgCol05 = 0x50800000,
114 BgCol06 = 0x60800000,
115 BgCol07 = 0x70800000,
116 BgCol08 = 0x80800000,
117 BgCol09 = 0x90800000,
118 BgCol10 = 0xa0800000,
119 BgCol11 = 0xb0800000,
120 BgCol12 = 0xc0800000,
121 BgCol13 = 0xd0800000,
122 BgCol14 = 0xe0800000,
123 BgCol15 = 0xf0800000,
125 // Colors used for sender auto coloring
126 // (starting at 01 because 00 is the default Sender format)
127 SenderCol01 = 0x01000200,
128 SenderCol02 = 0x02000200,
129 SenderCol03 = 0x03000200,
130 SenderCol04 = 0x04000200,
131 SenderCol05 = 0x05000200,
132 SenderCol06 = 0x06000200,
133 SenderCol07 = 0x07000200,
134 SenderCol08 = 0x08000200,
135 SenderCol09 = 0x09000200,
136 SenderCol10 = 0x0a000200,
137 SenderCol11 = 0x0b000200,
138 SenderCol12 = 0x0c000200,
139 SenderCol13 = 0x0d000200,
140 SenderCol14 = 0x0e000200,
141 SenderCol15 = 0x0f000200,
142 SenderCol16 = 0x10000200,
143 SenderCol17 = 0x11000200,
144 SenderCol18 = 0x12000200,
145 SenderCol19 = 0x13000200,
146 SenderCol20 = 0x14000200,
147 SenderCol21 = 0x15000200,
149 SenderColSelf = 0xff000200
152 struct StyledString {
154 FormatList formatList; // starting pos, ftypes
160 StyledString styleString(const QString &);
161 QString mircToInternal(const QString &) const;
163 void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
164 void setSenderAutoColor(bool state);
165 QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
166 QTextCharFormat mergedFormat(quint32 formatType);
167 QFontMetricsF *fontMetrics(quint32 formatType);
169 FormatType formatType(const QString &code) const;
170 QString formatCode(FormatType) const;
172 inline QFont defaultFont() const { return _defaultFont; }
174 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
177 void loadStyleSheet();
179 bool _senderAutoColor;
183 QTextCharFormat _defaultPlainFormat;
184 QHash<FormatType, QTextCharFormat> _defaultFormats;
185 QHash<FormatType, QTextCharFormat> _customFormats;
186 QHash<quint32, QTextCharFormat> _cachedFormats;
187 QHash<quint32, QFontMetricsF *> _cachedFontMetrics;
188 QHash<QString, FormatType> _formatCodes;
190 QString _settingsKey;
193 class UiStyle::StyledMessage : public Message {
195 explicit StyledMessage(const Message &message);
197 //! Styling is only needed for calls to plainContents() and contentsFormatList()
198 // StyledMessage can't style lazily by itself, as it doesn't know the used style
199 bool inline needsStyling() const { return _contents.plainText.isNull(); }
200 void style(UiStyle *style) const;
203 QString decoratedTimestamp() const;
204 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
205 QString decoratedSender() const;
206 inline const QString &plainContents() const { return _contents.plainText; }
208 inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
209 FormatType senderFormat() const;
210 inline const FormatList &contentsFormatList() const { return _contents.formatList; }
213 mutable StyledString _contents;
216 class UiStyle::QssParser {
225 struct ChatLineFormat {
226 QVector<QTextCharFormat> senderColors;
227 QVector<QTextCharFormat> mircColors;
228 QHash<FormatType, QTextCharFormat> formats;
234 void loadStyleSheet(const QString &sheet);
236 inline QPalette palette() const { return _palette; }
237 ChatLineFormat basicFormat() const;
238 QHash<FormatType, ChatLineFormat> specialFormats() const;
241 typedef QList<qreal> ColorTuple;
243 void parseChatLineData(const QString &decl, const QString &contents);
244 void parsePaletteData(const QString &decl, const QString &contents);
246 QTextCharFormat parseFormat(const QString &qss);
247 bool parsePalette(QPalette &, const QString &qss);
249 // Parse basic data types
250 QBrush parseBrushValue(const QString &str);
251 QColor parseColorValue(const QString &str);
252 QFont parseFont(const QString &str);
255 ColorTuple parseColorTuple(const QString &str);
256 QGradientStops parseGradientStops(const QString &str);
258 QHash<QString, QPalette::ColorRole> _paletteColorRoles;
262 ChatLineFormat _basicFormat;
263 QHash<FormatType, ChatLineFormat> _specialFormats;
266 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
267 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
269 Q_DECLARE_METATYPE(UiStyle::FormatList)