1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
24 #include <QDataStream>
25 #include <QFontMetricsF>
28 #include <QTextCharFormat>
29 #include <QTextLayout>
33 #include "bufferinfo.h"
35 #include "networkmodel.h"
38 class UiStyle : public QObject
43 UiStyle(QObject *parent = 0);
46 typedef QList<QPair<quint16, quint32> > FormatList;
48 //! This enumerates the possible formats a text element may have. */
49 /** These formats are ordered on increasing importance, in cases where a given property is specified
50 * by multiple active formats.
51 * \NOTE: Do not change/add values here without also adapting the relevant
52 * methods in this class (in particular mergedFormat())!
53 * Also, we _do_ rely on certain properties of these values in styleString() and friends!
59 // Message Formats (mutually exclusive!)
60 PlainMsg = 0x00000001,
61 NoticeMsg = 0x00000002,
62 ActionMsg = 0x00000003,
70 ServerMsg = 0x0000000b,
72 ErrorMsg = 0x0000000d,
73 DayChangeMsg = 0x0000000e,
74 TopicMsg = 0x0000000f,
75 NetsplitJoinMsg = 0x00000010,
76 NetsplitQuitMsg = 0x00000020,
77 InviteMsg = 0x00000030,
82 Underline = 0x00000400,
85 // Individual parts of a message
86 Timestamp = 0x00001000,
88 Contents = 0x00004000,
90 Hostmask = 0x00010000,
91 ChannelName = 0x00020000,
92 ModeFlags = 0x00040000,
94 // URL is special, we want that to take precedence over the rest...
97 // mIRC Colors - we assume those to be present only in plain contents
98 // foreground: 0x0.400000
99 // background: 0x.0800000
104 Highlight = 0x00000002,
105 Selected = 0x00000004 // must be last!
108 enum ItemFormatType {
109 BufferViewItem = 0x00000001,
110 NickViewItem = 0x00000002,
112 NetworkItem = 0x00000010,
113 ChannelBufferItem = 0x00000020,
114 QueryBufferItem = 0x00000040,
115 IrcUserItem = 0x00000080,
116 UserCategoryItem = 0x00000100,
118 InactiveBuffer = 0x00001000,
119 ActiveBuffer = 0x00002000,
120 UnreadBuffer = 0x00004000,
121 HighlightedBuffer = 0x00008000,
122 UserAway = 0x00010000
127 NumRoles // must be last!
130 struct StyledString {
132 FormatList formatList; // starting pos, ftypes
137 static FormatType formatType(Message::Type msgType);
138 static StyledString styleString(const QString &string, quint32 baseFormat = Base);
139 static QString mircToInternal(const QString &);
140 static inline QString timestampFormatString() { return _timestampFormatString; }
142 QTextCharFormat format(quint32 formatType, quint32 messageLabel) const;
143 QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel) const;
145 QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel) const;
147 inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int)role); }
148 inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int)role] = brush; }
150 QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
151 QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
160 void loadStyleSheet();
161 QString loadStyleSheet(const QString &name, bool shouldExist = false);
163 QTextCharFormat format(quint64 key) const;
164 QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const;
165 void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel) const;
166 void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
167 void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
169 static FormatType formatType(const QString &code);
170 static QString formatCode(FormatType);
171 static void setTimestampFormatString(const QString &format);
173 * Updates the local setting cache of whether or not to show sender brackets
175 * @param[in] enabled If true, sender brackets are enabled, otherwise false.
177 static void enableSenderBrackets(bool enabled);
179 QVariant itemData(int role, const QTextCharFormat &format) const;
182 void allowMircColorsChanged(const QVariant &);
183 void showItemViewIconsChanged(const QVariant &);
186 QVector<QBrush> _uiStylePalette;
187 QBrush _markerLineBrush;
188 QHash<quint64, QTextCharFormat> _formats;
189 mutable QHash<quint64, QTextCharFormat> _formatCache;
190 mutable QHash<quint64, QFontMetricsF *> _metricsCache;
191 QHash<quint32, QTextCharFormat> _listItemFormats;
192 static QHash<QString, FormatType> _formatCodes;
193 static QString _timestampFormatString;
194 static bool _showSenderBrackets; /// If true, show brackets around sender names
196 QIcon _channelJoinedIcon;
197 QIcon _channelPartedIcon;
198 QIcon _userOfflineIcon;
199 QIcon _userOnlineIcon;
201 QIcon _categoryOpIcon;
202 QIcon _categoryVoiceIcon;
205 bool _showNickViewIcons;
206 bool _showBufferViewIcons;
207 bool _allowMircColors;
211 class UiStyle::StyledMessage : public Message
213 Q_DECLARE_TR_FUNCTIONS(UiStyle::StyledMessage)
216 explicit StyledMessage(const Message &message);
218 QString decoratedTimestamp() const;
219 QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else
220 QString decoratedSender() const;
221 const QString &plainContents() const;
223 const FormatList &contentsFormatList() const;
225 quint8 senderHash() const;
231 mutable StyledString _contents;
232 mutable quint8 _senderHash;
236 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
237 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
239 Q_DECLARE_METATYPE(UiStyle::FormatList)