Add and use ItemViewSettings
[quassel.git] / src / uisupport / uistyle.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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   Q_OBJECT
39
40 public:
41   UiStyle(QObject *parent = 0);
42   virtual ~UiStyle();
43
44   typedef QList<QPair<quint16, quint32> > FormatList;
45
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!
52    */
53   enum FormatType {
54     Base            = 0x00000000,
55     Invalid         = 0xffffffff,
56
57     // Message Formats (mutually exclusive!)
58     PlainMsg        = 0x00000001,
59     NoticeMsg       = 0x00000002,
60     ActionMsg       = 0x00000003,
61     NickMsg         = 0x00000004,
62     ModeMsg         = 0x00000005,
63     JoinMsg         = 0x00000006,
64     PartMsg         = 0x00000007,
65     QuitMsg         = 0x00000008,
66     KickMsg         = 0x00000009,
67     KillMsg         = 0x0000000a,
68     ServerMsg       = 0x0000000b,
69     InfoMsg         = 0x0000000c,
70     ErrorMsg        = 0x0000000d,
71     DayChangeMsg    = 0x0000000e,
72
73     // Standard Formats
74     Bold            = 0x00000100,
75     Italic          = 0x00000200,
76     Underline       = 0x00000400,
77     Reverse         = 0x00000800,
78
79     // Individual parts of a message
80     Timestamp       = 0x00001000,
81     Sender          = 0x00002000,
82     Contents        = 0x00004000,
83     Nick            = 0x00008000,
84     Hostmask        = 0x00010000,
85     ChannelName     = 0x00020000,
86     ModeFlags       = 0x00040000,
87
88     // URL is special, we want that to take precedence over the rest...
89     Url             = 0x00080000
90
91     // mIRC Colors - we assume those to be present only in plain contents
92     // foreground: 0x0.400000
93     // background: 0x.0800000
94   };
95
96   enum MessageLabel {
97     OwnMsg          = 0x00000001,
98     Highlight       = 0x00000002,
99     Selected        = 0x00000004  // must be last!
100   };
101
102   enum ItemFormatType {
103     BufferViewItem    = 0x00000001,
104     NickViewItem      = 0x00000002,
105
106     NetworkItem       = 0x00000010,
107     ChannelBufferItem = 0x00000020,
108     QueryBufferItem   = 0x00000040,
109     IrcUserItem       = 0x00000080,
110     UserCategoryItem  = 0x00000100,
111
112     InactiveBuffer    = 0x00001000,
113     ActiveBuffer      = 0x00002000,
114     UnreadBuffer      = 0x00004000,
115     HighlightedBuffer = 0x00008000,
116     UserAway          = 0x00010000
117   };
118
119   enum ColorRole {
120     MarkerLine,
121     NumRoles  // must be last!
122   };
123
124   struct StyledString {
125     QString plainText;
126     FormatList formatList;  // starting pos, ftypes
127   };
128
129   class StyledMessage;
130
131   static FormatType formatType(Message::Type msgType);
132   static StyledString styleString(const QString &string, quint32 baseFormat = Base);
133   static QString mircToInternal(const QString &);
134   static inline QString timestampFormatString() { return _timestampFormatString; }
135
136   QTextCharFormat format(quint32 formatType, quint32 messageLabel);
137   QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel);
138
139   QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel);
140
141   inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int) role); }
142   inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int) role] = brush; }
143
144   QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
145   QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
146
147 public slots:
148   void reload();
149
150 signals:
151   void changed();
152
153 protected:
154   void loadStyleSheet();
155   QString loadStyleSheet(const QString &name, bool shouldExist = false);
156
157   QTextCharFormat format(quint64 key) const;
158   QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const;
159   void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel);
160   void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
161   void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
162
163   static FormatType formatType(const QString &code);
164   static QString formatCode(FormatType);
165   static void setTimestampFormatString(const QString &format);
166
167   QVariant itemData(int role, const QTextCharFormat &format) const;
168
169 private slots:
170   void showItemViewIconsChanged();
171
172 private:
173   QVector<QBrush> _uiStylePalette;
174   QBrush _markerLineBrush;
175   QHash<quint64, QTextCharFormat> _formats;
176   QHash<quint64, QTextCharFormat> _formatCache;
177   QHash<quint64, QFontMetricsF *> _metricsCache;
178   QHash<quint32, QTextCharFormat> _listItemFormats;
179   static QHash<QString, FormatType> _formatCodes;
180   static QString _timestampFormatString;
181
182   QPixmap _channelJoinedIcon;
183   QPixmap _channelPartedIcon;
184   QPixmap _userOfflineIcon;
185   QPixmap _userOnlineIcon;
186   QPixmap _userAwayIcon;
187   QPixmap _categoryOpIcon;
188   QPixmap _categoryVoiceIcon;
189   int _opIconLimit;
190   int _voiceIconLimit;
191   bool _showNickViewIcons;
192   bool _showBufferViewIcons;
193 };
194
195 class UiStyle::StyledMessage : public Message {
196 public:
197   explicit StyledMessage(const Message &message);
198
199   QString decoratedTimestamp() const;
200   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
201   QString decoratedSender() const;
202   const QString &plainContents() const;
203
204   const FormatList &contentsFormatList() const;
205
206   quint8 senderHash() const;
207
208 protected:
209   void style() const;
210
211 private:
212   mutable StyledString _contents;
213   mutable quint8 _senderHash;
214 };
215
216 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
217 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
218
219 Q_DECLARE_METATYPE(UiStyle::FormatList)
220
221 #endif