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>
32 #include "bufferinfo.h"
34 #include "networkmodel.h"
37 class UiStyle : public QObject{
41 UiStyle(QObject *parent = 0);
44 typedef QList<QPair<quint16, quint32> > FormatList;
46 //! This enumerates the possible formats a text element may have. */
47 /** These formats are ordered on increasing importance, in cases where a given property is specified
48 * by multiple active formats.
49 * \NOTE: Do not change/add values here without also adapting the relevant
50 * methods in this class (in particular mergedFormat())!
51 * Also, we _do_ rely on certain properties of these values in styleString() and friends!
57 // Message Formats (mutually exclusive!)
58 PlainMsg = 0x00000001,
59 NoticeMsg = 0x00000002,
60 ActionMsg = 0x00000003,
68 ServerMsg = 0x0000000b,
70 ErrorMsg = 0x0000000d,
71 DayChangeMsg = 0x0000000e,
72 TopicMsg = 0x0000000f,
73 NetsplitJoinMsg = 0x00000010,
74 NetsplitQuitMsg = 0x00000020,
79 Underline = 0x00000400,
82 // Individual parts of a message
83 Timestamp = 0x00001000,
85 Contents = 0x00004000,
87 Hostmask = 0x00010000,
88 ChannelName = 0x00020000,
89 ModeFlags = 0x00040000,
91 // URL is special, we want that to take precedence over the rest...
94 // mIRC Colors - we assume those to be present only in plain contents
95 // foreground: 0x0.400000
96 // background: 0x.0800000
101 Highlight = 0x00000002,
102 Selected = 0x00000004 // must be last!
105 enum ItemFormatType {
106 BufferViewItem = 0x00000001,
107 NickViewItem = 0x00000002,
109 NetworkItem = 0x00000010,
110 ChannelBufferItem = 0x00000020,
111 QueryBufferItem = 0x00000040,
112 IrcUserItem = 0x00000080,
113 UserCategoryItem = 0x00000100,
115 InactiveBuffer = 0x00001000,
116 ActiveBuffer = 0x00002000,
117 UnreadBuffer = 0x00004000,
118 HighlightedBuffer = 0x00008000,
119 UserAway = 0x00010000
124 NumRoles // must be last!
127 struct StyledString {
129 FormatList formatList; // starting pos, ftypes
134 static FormatType formatType(Message::Type msgType);
135 static StyledString styleString(const QString &string, quint32 baseFormat = Base);
136 static QString mircToInternal(const QString &);
137 static inline QString timestampFormatString() { return _timestampFormatString; }
139 QTextCharFormat format(quint32 formatType, quint32 messageLabel);
140 QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel);
142 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel);
144 inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int) role); }
145 inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int) role] = brush; }
147 QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
148 QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
157 void loadStyleSheet();
158 QString loadStyleSheet(const QString &name, bool shouldExist = false);
160 QTextCharFormat format(quint64 key) const;
161 QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const;
162 void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel);
163 void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
164 void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
166 static FormatType formatType(const QString &code);
167 static QString formatCode(FormatType);
168 static void setTimestampFormatString(const QString &format);
170 QVariant itemData(int role, const QTextCharFormat &format) const;
173 void showItemViewIconsChanged();
176 QVector<QBrush> _uiStylePalette;
177 QBrush _markerLineBrush;
178 QHash<quint64, QTextCharFormat> _formats;
179 QHash<quint64, QTextCharFormat> _formatCache;
180 QHash<quint64, QFontMetricsF *> _metricsCache;
181 QHash<quint32, QTextCharFormat> _listItemFormats;
182 static QHash<QString, FormatType> _formatCodes;
183 static QString _timestampFormatString;
185 QPixmap _channelJoinedIcon;
186 QPixmap _channelPartedIcon;
187 QPixmap _userOfflineIcon;
188 QPixmap _userOnlineIcon;
189 QPixmap _userAwayIcon;
190 QPixmap _categoryOpIcon;
191 QPixmap _categoryVoiceIcon;
194 bool _showNickViewIcons;
195 bool _showBufferViewIcons;
198 class UiStyle::StyledMessage : public Message {
200 explicit StyledMessage(const Message &message);
202 QString decoratedTimestamp() const;
203 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
204 QString decoratedSender() const;
205 const QString &plainContents() const;
207 const FormatList &contentsFormatList() const;
209 quint8 senderHash() const;
215 mutable StyledString _contents;
216 mutable quint8 _senderHash;
219 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
220 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
222 Q_DECLARE_METATYPE(UiStyle::FormatList)