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,
71 Underline = 0x00000040,
74 // Individual parts of a message
75 Timestamp = 0x00000100,
78 Hostmask = 0x00000800,
79 ChannelName = 0x00001000,
80 ModeFlags = 0x00002000,
82 // URL is special, we want that to take precedence over the rest...
85 // mIRC Colors - we assume those to be present only in plain contents
100 FgCol14 = 0x0e400000,
101 FgCol15 = 0x0f400000,
103 BgCol00 = 0x00800000,
104 BgCol01 = 0x10800000,
105 BgCol02 = 0x20800000,
106 BgCol03 = 0x30800000,
107 BgCol04 = 0x40800000,
108 BgCol05 = 0x50800000,
109 BgCol06 = 0x60800000,
110 BgCol07 = 0x70800000,
111 BgCol08 = 0x80800000,
112 BgCol09 = 0x90800000,
113 BgCol10 = 0xa0800000,
114 BgCol11 = 0xb0800000,
115 BgCol12 = 0xc0800000,
116 BgCol13 = 0xd0800000,
117 BgCol14 = 0xe0800000,
118 BgCol15 = 0xf0800000,
120 // Colors used for sender auto coloring
121 // (starting at 01 because 00 is the default Sender format)
122 SenderCol01 = 0x01000200,
123 SenderCol02 = 0x02000200,
124 SenderCol03 = 0x03000200,
125 SenderCol04 = 0x04000200,
126 SenderCol05 = 0x05000200,
127 SenderCol06 = 0x06000200,
128 SenderCol07 = 0x07000200,
129 SenderCol08 = 0x08000200,
130 SenderCol09 = 0x09000200,
131 SenderCol10 = 0x0a000200,
132 SenderCol11 = 0x0b000200,
133 SenderCol12 = 0x0c000200,
134 SenderCol13 = 0x0d000200,
135 SenderCol14 = 0x0e000200,
136 SenderCol15 = 0x0f000200,
137 SenderCol16 = 0x10000200,
138 SenderCol17 = 0x11000200,
139 SenderCol18 = 0x12000200,
140 SenderCol19 = 0x13000200,
141 SenderCol20 = 0x14000200,
142 SenderCol21 = 0x15000200,
144 SenderColSelf = 0xff000200
148 Highlight = 0x00000001
151 struct StyledString {
153 FormatList formatList; // starting pos, ftypes
159 StyledString styleString(const QString &);
160 QString mircToInternal(const QString &) const;
162 void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/); // FIXME go away
163 void setSenderAutoColor(bool state); // FIXME go away
164 QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const; // FIXME go away
166 QTextCharFormat cachedFormat(quint64 key) const;
167 QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const;
168 void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
170 QTextCharFormat mergedFormat(quint32 formatType, quint32 messageLabel = 0);
171 QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
173 FormatType formatType(const QString &code) const;
174 QString formatCode(FormatType) const;
176 inline QFont defaultFont() const { return _defaultFont; }
178 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
181 void loadStyleSheet();
183 //! Determines the format set to be used for the given hostmask
184 //int formatSetIndex(const QString &hostmask) const;
185 //int formatSetIndexForSelf() const;
187 void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
189 bool _senderAutoColor;
193 QTextCharFormat _defaultPlainFormat;
194 QHash<FormatType, QTextCharFormat> _defaultFormats;
195 QHash<FormatType, QTextCharFormat> _customFormats;
196 QHash<quint64, QTextCharFormat> _formatCache;
197 QHash<quint64, QFontMetricsF *> _metricsCache;
198 QHash<QString, FormatType> _formatCodes;
200 QString _settingsKey;
203 class UiStyle::StyledMessage : public Message {
205 explicit StyledMessage(const Message &message);
207 //! Styling is only needed for calls to plainContents() and contentsFormatList()
208 // StyledMessage can't style lazily by itself, as it doesn't know the used style
209 bool inline needsStyling() const { return _contents.plainText.isNull(); }
210 void style(UiStyle *style) const;
213 QString decoratedTimestamp() const;
214 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
215 QString decoratedSender() const;
216 inline const QString &plainContents() const { return _contents.plainText; }
218 inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
219 FormatType senderFormat() const;
220 inline const FormatList &contentsFormatList() const { return _contents.formatList; }
223 mutable StyledString _contents;
226 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
227 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
229 Q_DECLARE_METATYPE(UiStyle::FormatList)