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>
26 #include <QTextCharFormat>
27 #include <QTextLayout>
34 Q_DECLARE_TR_FUNCTIONS(UiStyle)
37 UiStyle(const QString &settingsKey);
40 typedef QList<QPair<quint16, quint32> > FormatList;
42 //! This enumerates the possible formats a text element may have. */
43 /** These formats are ordered on increasing importance, in cases where a given property is specified
44 * by multiple active formats.
45 * \NOTE: Do not change/add values here without also adapting the relevant
46 * methods in this class (in particular mergedFormat())!
47 * Also, we _do_ rely on certain properties of these values in styleString() and friends!
53 // Message Formats (mutually exclusive!)
54 PlainMsg = 0x00000001,
55 NoticeMsg = 0x00000002,
56 ServerMsg = 0x00000003,
57 ErrorMsg = 0x00000004,
62 RenameMsg = 0x00000009,
64 ActionMsg = 0x0000000b,
65 HighlightMsg = 0x0000000f,
67 // Note: mergedFormat() assumes that 0x10 - 0x80 are *only* used within the message contents,
68 // e.g. not together with any of 0x0100-0x2000!
69 // If we happen to find a use case for that, we can see if/how to implement that though.
74 Underline = 0x00000040,
77 // Individual parts of a message
78 Timestamp = 0x00000100,
81 Hostmask = 0x00000800,
82 ChannelName = 0x00001000,
83 ModeFlags = 0x00002000,
85 // URL is special, we want that to take precedence over the rest...
88 // mIRC Colors - we assume those to be present only in plain contents
100 FgCol11 = 0x0b400000,
101 FgCol12 = 0x0c400000,
102 FgCol13 = 0x0d400000,
103 FgCol14 = 0x0e400000,
104 FgCol15 = 0x0f400000,
106 BgCol00 = 0x00800000,
107 BgCol01 = 0x10800000,
108 BgCol02 = 0x20800000,
109 BgCol03 = 0x30800000,
110 BgCol04 = 0x40800000,
111 BgCol05 = 0x50800000,
112 BgCol06 = 0x60800000,
113 BgCol07 = 0x70800000,
114 BgCol08 = 0x80800000,
115 BgCol09 = 0x90800000,
116 BgCol10 = 0xa0800000,
117 BgCol11 = 0xb0800000,
118 BgCol12 = 0xc0800000,
119 BgCol13 = 0xd0800000,
120 BgCol14 = 0xe0800000,
121 BgCol15 = 0xf0800000,
123 // Colors used for sender auto coloring
124 // (starting at 01 because 00 is the default Sender format)
125 SenderCol01 = 0x01000200,
126 SenderCol02 = 0x02000200,
127 SenderCol03 = 0x03000200,
128 SenderCol04 = 0x04000200,
129 SenderCol05 = 0x05000200,
130 SenderCol06 = 0x06000200,
131 SenderCol07 = 0x07000200,
132 SenderCol08 = 0x08000200,
133 SenderCol09 = 0x09000200,
134 SenderCol10 = 0x0a000200,
135 SenderCol11 = 0x0b000200,
136 SenderCol12 = 0x0c000200,
137 SenderCol13 = 0x0d000200,
138 SenderCol14 = 0x0e000200,
139 SenderCol15 = 0x0f000200,
140 SenderCol16 = 0x10000200,
141 SenderCol17 = 0x11000200,
142 SenderCol18 = 0x12000200,
143 SenderCol19 = 0x13000200,
144 SenderCol20 = 0x14000200,
145 SenderCol21 = 0x15000200,
147 SenderColSelf = 0xff000200
150 struct StyledString {
152 FormatList formatList; // starting pos, ftypes
157 StyledString styleString(const QString &);
158 QString mircToInternal(const QString &) const;
160 void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
161 void setSenderAutoColor(bool state);
162 QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
163 QTextCharFormat mergedFormat(quint32 formatType);
164 QFontMetricsF *fontMetrics(quint32 formatType);
166 FormatType formatType(const QString &code) const;
167 QString formatCode(FormatType) const;
169 inline QFont defaultFont() const { return _defaultFont; }
171 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
174 bool _senderAutoColor;
177 QTextCharFormat _defaultPlainFormat;
178 QHash<FormatType, QTextCharFormat> _defaultFormats;
179 QHash<FormatType, QTextCharFormat> _customFormats;
180 QHash<quint32, QTextCharFormat> _cachedFormats;
181 QHash<quint32, QFontMetricsF *> _cachedFontMetrics;
182 QHash<QString, FormatType> _formatCodes;
184 QString _settingsKey;
187 class UiStyle::StyledMessage : public Message {
189 explicit StyledMessage(const Message &message);
191 //! Styling is only needed for calls to plainContents() and contentsFormatList()
192 // StyledMessage can't style lazily by itself, as it doesn't know the used style
193 bool inline needsStyling() const { return _contents.plainText.isNull(); }
194 void style(UiStyle *style) const;
197 QString decoratedTimestamp() const;
198 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
199 QString decoratedSender() const;
200 inline const QString &plainContents() const { return _contents.plainText; }
202 inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
203 FormatType senderFormat() const;
204 inline const FormatList &contentsFormatList() const { return _contents.formatList; }
207 mutable StyledString _contents;
210 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
211 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
213 Q_DECLARE_METATYPE(UiStyle::FormatList)