581c420eeccb7177bd123c53867e6afc1b4107e3
[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 "message.h"
33 #include "settings.h"
34
35 class UiStyle : public QObject{
36   Q_OBJECT
37
38 public:
39   UiStyle(QObject *parent = 0);
40   virtual ~UiStyle();
41
42   typedef QList<QPair<quint16, quint32> > FormatList;
43
44   //! This enumerates the possible formats a text element may have. */
45   /** These formats are ordered on increasing importance, in cases where a given property is specified
46    *  by multiple active formats.
47    *  \NOTE: Do not change/add values here without also adapting the relevant
48    *         methods in this class (in particular mergedFormat())!
49    *         Also, we _do_ rely on certain properties of these values in styleString() and friends!
50    */
51   enum FormatType {
52     Base            = 0x00000000,
53     Invalid         = 0xffffffff,
54
55     // Message Formats (mutually exclusive!)
56     PlainMsg        = 0x00000001,
57     NoticeMsg       = 0x00000002,
58     ActionMsg       = 0x00000003,
59     NickMsg         = 0x00000004,
60     ModeMsg         = 0x00000005,
61     JoinMsg         = 0x00000006,
62     PartMsg         = 0x00000007,
63     QuitMsg         = 0x00000008,
64     KickMsg         = 0x00000009,
65     KillMsg         = 0x0000000a,
66     ServerMsg       = 0x0000000b,
67     InfoMsg         = 0x0000000c,
68     ErrorMsg        = 0x0000000d,
69     DayChangeMsg    = 0x0000000e,
70
71     // Standard Formats
72     Bold            = 0x00000100,
73     Italic          = 0x00000200,
74     Underline       = 0x00000400,
75     Reverse         = 0x00000800,
76
77     // Individual parts of a message
78     Timestamp       = 0x00001000,
79     Sender          = 0x00002000,
80     Contents        = 0x00004000,
81     Nick            = 0x00008000,
82     Hostmask        = 0x00010000,
83     ChannelName     = 0x00020000,
84     ModeFlags       = 0x00040000,
85
86     // URL is special, we want that to take precedence over the rest...
87     Url             = 0x00080000
88
89     // mIRC Colors - we assume those to be present only in plain contents
90     // foreground: 0x0.400000
91     // background: 0x.0800000
92   };
93
94   enum MessageLabel {
95     OwnMsg          = 0x00000001,
96     Highlight       = 0x00000002,
97     Selected        = 0x00000004  // must be last!
98   };
99
100   enum ColorRole {
101     MarkerLine,
102     ActiveNick,
103     InactiveNick,
104     Channel,
105     InactiveChannel,
106     ActiveChannel,
107     UnreadChannel,
108     HighlightedChannel,
109     Query,
110     InactiveQuery,
111     ActiveQuery,
112     UnreadQuery,
113     HighlightedQuery,
114     NumRoles  // must be last!
115   };
116
117   struct StyledString {
118     QString plainText;
119     FormatList formatList;  // starting pos, ftypes
120   };
121
122   class StyledMessage;
123
124   static FormatType formatType(Message::Type msgType);
125   static StyledString styleString(const QString &string, quint32 baseFormat = Base);
126   static QString mircToInternal(const QString &);
127   static inline QString timestampFormatString() { return _timestampFormatString; }
128
129   QTextCharFormat format(quint32 formatType, quint32 messageLabel = 0);
130   QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
131
132   inline QFont defaultFont() const { return _defaultFont; }
133
134   inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int) role); }
135   inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int) role] = brush; }
136
137   QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel = 0);
138
139 public slots:
140   void reload();
141
142 signals:
143   void changed();
144
145 protected:
146   void loadStyleSheet();
147   QString loadStyleSheet(const QString &name, bool shouldExist = false);
148
149   QTextCharFormat cachedFormat(quint64 key) const;
150   QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const;
151   void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
152   void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
153   void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel);
154
155   static FormatType formatType(const QString &code);
156   static QString formatCode(FormatType);
157   static void setTimestampFormatString(const QString &format);
158
159 private:
160   QFont _defaultFont;
161   QVector<QBrush> _uiStylePalette;
162   QBrush _markerLineBrush;
163   QHash<quint64, QTextCharFormat> _formatCache;
164   QHash<quint64, QFontMetricsF *> _metricsCache;
165   static QHash<QString, FormatType> _formatCodes;
166   static QString _timestampFormatString;
167 };
168
169 class UiStyle::StyledMessage : public Message {
170 public:
171   explicit StyledMessage(const Message &message);
172
173   QString decoratedTimestamp() const;
174   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
175   QString decoratedSender() const;
176   const QString &plainContents() const;
177
178   const FormatList &contentsFormatList() const;
179
180   quint8 senderHash() const;
181
182 protected:
183   void style() const;
184
185 private:
186   mutable StyledString _contents;
187   mutable quint8 _senderHash;
188 };
189
190 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
191 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
192
193 Q_DECLARE_METATYPE(UiStyle::FormatList)
194
195 #endif