1 | /*************************************************************************** |
2 | * Copyright (C) 2005-2014 by the Quassel Project * |
3 | * devel@quassel-irc.org * |
4 | * * |
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. * |
9 | * * |
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. * |
14 | * * |
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 | ***************************************************************************/ |
20 | |
21 | #ifndef UISTYLE_H_ |
22 | #define UISTYLE_H_ |
23 | |
24 | #include <QDataStream> |
25 | #include <QFontMetricsF> |
26 | #include <QHash> |
27 | #include <QTextCharFormat> |
28 | #include <QTextLayout> |
29 | #include <QPalette> |
30 | #include <QVector> |
31 | |
32 | #include "bufferinfo.h" |
33 | #include "message.h" |
34 | #include "networkmodel.h" |
35 | #include "settings.h" |
36 | |
37 | class UiStyle : public QObject |
38 | { |
39 | Q_OBJECT |
40 | |
41 | public: |
42 | UiStyle(QObject *parent = 0); |
43 | virtual ~UiStyle(); |
44 | |
45 | typedef QList<QPair<quint16, quint32> > FormatList; |
46 | |
47 | //! This enumerates the possible formats a text element may have. */ |
48 | /** These formats are ordered on increasing importance, in cases where a given property is specified |
49 | * by multiple active formats. |
50 | * \NOTE: Do not change/add values here without also adapting the relevant |
51 | * methods in this class (in particular mergedFormat())! |
52 | * Also, we _do_ rely on certain properties of these values in styleString() and friends! |
53 | */ |
54 | enum FormatType { |
55 | Base = 0x00000000, |
56 | Invalid = 0xffffffff, |
57 | |
58 | // Message Formats (mutually exclusive!) |
59 | PlainMsg = 0x00000001, |
60 | NoticeMsg = 0x00000002, |
61 | ActionMsg = 0x00000003, |
62 | NickMsg = 0x00000004, |
63 | ModeMsg = 0x00000005, |
64 | JoinMsg = 0x00000006, |
65 | PartMsg = 0x00000007, |
66 | QuitMsg = 0x00000008, |
67 | KickMsg = 0x00000009, |
68 | KillMsg = 0x0000000a, |
69 | ServerMsg = 0x0000000b, |
70 | InfoMsg = 0x0000000c, |
71 | ErrorMsg = 0x0000000d, |
72 | DayChangeMsg = 0x0000000e, |
73 | TopicMsg = 0x0000000f, |
74 | NetsplitJoinMsg = 0x00000010, |
75 | NetsplitQuitMsg = 0x00000020, |
76 | InviteMsg = 0x00000030, |
77 | |
78 | // Standard Formats |
79 | Bold = 0x00000100, |
80 | Italic = 0x00000200, |
81 | Underline = 0x00000400, |
82 | Reverse = 0x00000800, |
83 | |
84 | // Individual parts of a message |
85 | Timestamp = 0x00001000, |
86 | Sender = 0x00002000, |
87 | Contents = 0x00004000, |
88 | Nick = 0x00008000, |
89 | Hostmask = 0x00010000, |
90 | ChannelName = 0x00020000, |
91 | ModeFlags = 0x00040000, |
92 | |
93 | // URL is special, we want that to take precedence over the rest... |
94 | Url = 0x00080000 |
95 | |
96 | // mIRC Colors - we assume those to be present only in plain contents |
97 | // foreground: 0x0.400000 |
98 | // background: 0x.0800000 |
99 | }; |
100 | |
101 | enum MessageLabel { |
102 | OwnMsg = 0x00000001, |
103 | Highlight = 0x00000002, |
104 | Selected = 0x00000004 // must be last! |
105 | }; |
106 | |
107 | enum ItemFormatType { |
108 | BufferViewItem = 0x00000001, |
109 | NickViewItem = 0x00000002, |
110 | |
111 | NetworkItem = 0x00000010, |
112 | ChannelBufferItem = 0x00000020, |
113 | QueryBufferItem = 0x00000040, |
114 | IrcUserItem = 0x00000080, |
115 | UserCategoryItem = 0x00000100, |
116 | |
117 | InactiveBuffer = 0x00001000, |
118 | ActiveBuffer = 0x00002000, |
119 | UnreadBuffer = 0x00004000, |
120 | HighlightedBuffer = 0x00008000, |
121 | UserAway = 0x00010000 |
122 | }; |
123 | |
124 | enum ColorRole { |
125 | MarkerLine, |
126 | NumRoles // must be last! |
127 | }; |
128 | |
129 | struct StyledString { |
130 | QString plainText; |
131 | FormatList formatList; // starting pos, ftypes |
132 | }; |
133 | |
134 | class StyledMessage; |
135 | |
136 | static FormatType formatType(Message::Type msgType); |
137 | static StyledString styleString(const QString &string, quint32 baseFormat = Base); |
138 | static QString mircToInternal(const QString &); |
139 | static inline QString timestampFormatString() { return _timestampFormatString; } |
140 | |
141 | QTextCharFormat format(quint32 formatType, quint32 messageLabel) const; |
142 | QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel) const; |
143 | |
144 | QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel) const; |
145 | |
146 | inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int)role); } |
147 | inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int)role] = brush; } |
148 | |
149 | QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const; |
150 | QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const; |
151 | |
152 | public slots: |
153 | void reload(); |
154 | |
155 | signals: |
156 | void changed(); |
157 | |
158 | protected: |
159 | void loadStyleSheet(); |
160 | QString loadStyleSheet(const QString &name, bool shouldExist = false); |
161 | |
162 | QTextCharFormat format(quint64 key) const; |
163 | QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const; |
164 | void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel) const; |
165 | void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const; |
166 | void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const; |
167 | |
168 | static FormatType formatType(const QString &code); |
169 | static QString formatCode(FormatType); |
170 | static void setTimestampFormatString(const QString &format); |
171 | |
172 | QVariant itemData(int role, const QTextCharFormat &format) const; |
173 | |
174 | private slots: |
175 | void allowMircColorsChanged(const QVariant &); |
176 | void showItemViewIconsChanged(const QVariant &); |
177 | |
178 | private: |
179 | QVector<QBrush> _uiStylePalette; |
180 | QBrush _markerLineBrush; |
181 | QHash<quint64, QTextCharFormat> _formats; |
182 | mutable QHash<quint64, QTextCharFormat> _formatCache; |
183 | mutable QHash<quint64, QFontMetricsF *> _metricsCache; |
184 | QHash<quint32, QTextCharFormat> _listItemFormats; |
185 | static QHash<QString, FormatType> _formatCodes; |
186 | static QString _timestampFormatString; |
187 | |
188 | QPixmap _channelJoinedIcon; |
189 | QPixmap _channelPartedIcon; |
190 | QPixmap _userOfflineIcon; |
191 | QPixmap _userOnlineIcon; |
192 | QPixmap _userAwayIcon; |
193 | QPixmap _categoryOpIcon; |
194 | QPixmap _categoryVoiceIcon; |
195 | int _opIconLimit; |
196 | int _voiceIconLimit; |
197 | bool _showNickViewIcons; |
198 | bool _showBufferViewIcons; |
199 | bool _allowMircColors; |
200 | }; |
201 | |
202 | |
203 | class UiStyle::StyledMessage : public Message |
204 | { |
205 | Q_DECLARE_TR_FUNCTIONS(UiStyle::StyledMessage) |
206 | |
207 | public: |
208 | explicit StyledMessage(const Message &message); |
209 | |
210 | QString decoratedTimestamp() const; |
211 | QString plainSender() const; //!< Nickname (no decorations) for Plain and Notice, empty else |
212 | QString decoratedSender() const; |
213 | const QString &plainContents() const; |
214 | |
215 | const FormatList &contentsFormatList() const; |
216 | |
217 | quint8 senderHash() const; |
218 | |
219 | protected: |
220 | void style() const; |
221 | |
222 | private: |
223 | mutable StyledString _contents; |
224 | mutable quint8 _senderHash; |
225 | }; |
226 | |
227 | |
228 | QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList); |
229 | QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList); |
230 | |
231 | Q_DECLARE_METATYPE(UiStyle::FormatList) |
232 | |
233 | #endif |
234 | |