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!
52 // Message Formats (mutually exclusive!)
53 PlainMsg = 0x00000001,
54 NoticeMsg = 0x00000002,
55 ServerMsg = 0x00000003,
56 ErrorMsg = 0x00000004,
61 RenameMsg = 0x00000009,
63 ActionMsg = 0x0000000b,
67 Underline = 0x00000040,
69 // Individual parts of a message
70 Timestamp = 0x00000100,
73 Hostmask = 0x00000800,
74 ChannelName = 0x00001000,
75 ModeFlags = 0x00002000,
76 // URL is special, we want that to take precedence over the rest...
100 BgCol04 = 0x40800000,
101 BgCol05 = 0x50800000,
102 BgCol06 = 0x60800000,
103 BgCol07 = 0x70800000,
104 BgCol08 = 0x80800000,
105 BgCol09 = 0x90800000,
106 BgCol10 = 0xa0800000,
107 BgCol11 = 0xb0800000,
108 BgCol12 = 0xc0800000,
109 BgCol13 = 0xd0800000,
110 BgCol14 = 0xe0800000,
111 BgCol15 = 0xf0800000,
113 // Colors used for sender auto coloring
114 // (starting at 01 because 00 is the default Sender format)
115 SenderCol01 = 0x01000200,
116 SenderCol02 = 0x02000200,
117 SenderCol03 = 0x03000200,
118 SenderCol04 = 0x04000200,
119 SenderCol05 = 0x05000200,
120 SenderCol06 = 0x06000200,
121 SenderCol07 = 0x07000200,
122 SenderCol08 = 0x08000200,
123 SenderCol09 = 0x09000200,
124 SenderCol10 = 0x0a000200,
125 SenderCol11 = 0x0b000200,
126 SenderCol12 = 0x0c000200,
127 SenderCol13 = 0x0d000200,
128 SenderCol14 = 0x0e000200,
129 SenderCol15 = 0x0f000200,
130 SenderCol16 = 0x10000200,
131 SenderCol17 = 0x11000200,
132 SenderCol18 = 0x12000200,
133 SenderCol19 = 0x13000200,
134 SenderCol20 = 0x14000200,
135 SenderCol21 = 0x15000200
144 struct StyledString {
146 FormatList formatList; // starting pos, ftypes
151 StyledString styleString(const QString &);
152 QString mircToInternal(const QString &) const;
154 void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
155 void setSenderAutoColor(bool state);
156 QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
157 QTextCharFormat mergedFormat(quint32 formatType);
158 QFontMetricsF *fontMetrics(quint32 formatType);
160 FormatType formatType(const QString &code) const;
161 QString formatCode(FormatType) const;
163 inline QFont defaultFont() const { return _defaultFont; }
165 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
168 bool _senderAutoColor;
171 QTextCharFormat _defaultPlainFormat;
172 QHash<FormatType, QTextCharFormat> _defaultFormats;
173 QHash<FormatType, QTextCharFormat> _customFormats;
174 QHash<quint32, QTextCharFormat> _cachedFormats;
175 QHash<quint32, QFontMetricsF *> _cachedFontMetrics;
176 QHash<QString, FormatType> _formatCodes;
178 QString _settingsKey;
181 class UiStyle::StyledMessage : public Message {
183 explicit StyledMessage(const Message &message);
185 //! Styling is only needed for calls to plainContents() and contentsFormatList()
186 // StyledMessage can't style lazily by itself, as it doesn't know the used style
187 bool inline needsStyling() const { return _contents.plainText.isNull(); }
188 void style(UiStyle *style) const;
191 QString decoratedTimestamp() const;
192 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
193 QString decoratedSender() const;
194 inline const QString &plainContents() const { return _contents.plainText; }
196 inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
197 FormatType senderFormat() const;
198 inline const FormatList &contentsFormatList() const { return _contents.formatList; }
201 mutable StyledString _contents;
204 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
205 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
207 Q_DECLARE_METATYPE(UiStyle::FormatList)