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>
35 class UiStyle : public QObject{
39 UiStyle(QObject *parent = 0);
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 ActionMsg = 0x00000003,
66 ServerMsg = 0x0000000b,
68 ErrorMsg = 0x0000000d,
69 DayChangeMsg = 0x0000000e,
74 Underline = 0x00000040,
77 // Individual parts of a message
78 Timestamp = 0x00000100,
80 Contents = 0x00000400,
82 Hostmask = 0x00001000,
83 ChannelName = 0x00002000,
84 ModeFlags = 0x00004000,
86 // URL is special, we want that to take precedence over the rest...
89 // mIRC Colors - we assume those to be present only in plain contents
90 // foreground: 0x0.400000
91 // background: 0x.0800000
96 Highlight = 0x00000002,
97 Selected = 0x00000004 // must be last!
100 struct StyledString {
102 FormatList formatList; // starting pos, ftypes
107 static FormatType formatType(Message::Type msgType);
108 static StyledString styleString(const QString &string, quint32 baseFormat = None);
109 static QString mircToInternal(const QString &);
110 static inline QString timestampFormatString() { return _timestampFormatString; }
112 QTextCharFormat format(quint32 formatType, quint32 messageLabel = 0);
113 QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
115 inline QFont defaultFont() const { return _defaultFont; }
117 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel = 0);
126 void loadStyleSheet();
127 QString loadStyleSheet(const QString &name, bool shouldExist = false);
129 QTextCharFormat cachedFormat(quint64 key) const;
130 QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const;
131 void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
132 void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
133 void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
135 static FormatType formatType(const QString &code);
136 static QString formatCode(FormatType);
137 static void setTimestampFormatString(const QString &format);
141 QHash<quint64, QTextCharFormat> _formatCache;
142 QHash<quint64, QFontMetricsF *> _metricsCache;
143 static QHash<QString, FormatType> _formatCodes;
144 static QString _timestampFormatString;
147 class UiStyle::StyledMessage : public Message {
149 explicit StyledMessage(const Message &message);
151 QString decoratedTimestamp() const;
152 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
153 QString decoratedSender() const;
154 const QString &plainContents() const;
156 inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
157 FormatType senderFormat() const;
158 const FormatList &contentsFormatList() const;
160 quint8 senderHash() const;
166 mutable StyledString _contents;
167 mutable quint8 _senderHash;
170 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
171 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
173 Q_DECLARE_METATYPE(UiStyle::FormatList)