Change shortcut for hiding the current buffer to a standard keysequence
[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     TopicMsg        = 0x0000000f,
73     NetsplitJoinMsg = 0x00000010,
74     NetsplitQuitMsg = 0x00000020,
75     InviteMsg       = 0x00000030,
76
77     // Standard Formats
78     Bold            = 0x00000100,
79     Italic          = 0x00000200,
80     Underline       = 0x00000400,
81     Reverse         = 0x00000800,
82
83     // Individual parts of a message
84     Timestamp       = 0x00001000,
85     Sender          = 0x00002000,
86     Contents        = 0x00004000,
87     Nick            = 0x00008000,
88     Hostmask        = 0x00010000,
89     ChannelName     = 0x00020000,
90     ModeFlags       = 0x00040000,
91
92     // URL is special, we want that to take precedence over the rest...
93     Url             = 0x00080000
94
95     // mIRC Colors - we assume those to be present only in plain contents
96     // foreground: 0x0.400000
97     // background: 0x.0800000
98   };
99
100   enum MessageLabel {
101     OwnMsg          = 0x00000001,
102     Highlight       = 0x00000002,
103     Selected        = 0x00000004  // must be last!
104   };
105
106   enum ItemFormatType {
107     BufferViewItem    = 0x00000001,
108     NickViewItem      = 0x00000002,
109
110     NetworkItem       = 0x00000010,
111     ChannelBufferItem = 0x00000020,
112     QueryBufferItem   = 0x00000040,
113     IrcUserItem       = 0x00000080,
114     UserCategoryItem  = 0x00000100,
115
116     InactiveBuffer    = 0x00001000,
117     ActiveBuffer      = 0x00002000,
118     UnreadBuffer      = 0x00004000,
119     HighlightedBuffer = 0x00008000,
120     UserAway          = 0x00010000
121   };
122
123   enum ColorRole {
124     MarkerLine,
125     NumRoles  // must be last!
126   };
127
128   struct StyledString {
129     QString plainText;
130     FormatList formatList;  // starting pos, ftypes
131   };
132
133   class StyledMessage;
134
135   static FormatType formatType(Message::Type msgType);
136   static StyledString styleString(const QString &string, quint32 baseFormat = Base);
137   static QString mircToInternal(const QString &);
138   static inline QString timestampFormatString() { return _timestampFormatString; }
139
140   QTextCharFormat format(quint32 formatType, quint32 messageLabel) const;
141   QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel) const;
142
143   QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel) const;
144
145   inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int) role); }
146   inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int) role] = brush; }
147
148   QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
149   QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
150
151 public slots:
152   void reload();
153
154 signals:
155   void changed();
156
157 protected:
158   void loadStyleSheet();
159   QString loadStyleSheet(const QString &name, bool shouldExist = false);
160
161   QTextCharFormat format(quint64 key) const;
162   QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const;
163   void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel) const;
164   void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
165   void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
166
167   static FormatType formatType(const QString &code);
168   static QString formatCode(FormatType);
169   static void setTimestampFormatString(const QString &format);
170
171   QVariant itemData(int role, const QTextCharFormat &format) const;
172
173 private slots:
174   void allowMircColorsChanged(const QVariant &);
175   void showItemViewIconsChanged(const QVariant &);
176
177 private:
178   QVector<QBrush> _uiStylePalette;
179   QBrush _markerLineBrush;
180   QHash<quint64, QTextCharFormat> _formats;
181   mutable QHash<quint64, QTextCharFormat> _formatCache;
182   mutable QHash<quint64, QFontMetricsF *> _metricsCache;
183   QHash<quint32, QTextCharFormat> _listItemFormats;
184   static QHash<QString, FormatType> _formatCodes;
185   static QString _timestampFormatString;
186
187   QPixmap _channelJoinedIcon;
188   QPixmap _channelPartedIcon;
189   QPixmap _userOfflineIcon;
190   QPixmap _userOnlineIcon;
191   QPixmap _userAwayIcon;
192   QPixmap _categoryOpIcon;
193   QPixmap _categoryVoiceIcon;
194   int _opIconLimit;
195   int _voiceIconLimit;
196   bool _showNickViewIcons;
197   bool _showBufferViewIcons;
198   bool _allowMircColors;
199 };
200
201 class UiStyle::StyledMessage : public Message {
202   Q_DECLARE_TR_FUNCTIONS(UiStyle::StyledMessage)
203
204 public:
205   explicit StyledMessage(const Message &message);
206
207   QString decoratedTimestamp() const;
208   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
209   QString decoratedSender() const;
210   const QString &plainContents() const;
211
212   const FormatList &contentsFormatList() const;
213
214   quint8 senderHash() const;
215
216 protected:
217   void style() const;
218
219 private:
220   mutable StyledString _contents;
221   mutable quint8 _senderHash;
222 };
223
224 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
225 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
226
227 Q_DECLARE_METATYPE(UiStyle::FormatList)
228
229 #endif