098800cd5a3d85272dac6d2753b0c507606bab60
[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 {
36   Q_DECLARE_TR_FUNCTIONS(UiStyle)
37
38 public:
39   UiStyle(const QString &settingsKey);
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     None            = 0x00000000,
53     Invalid         = 0x11111111,
54
55     // Message Formats (mutually exclusive!)
56     PlainMsg        = 0x00000001,
57     NoticeMsg       = 0x00000002,
58     ServerMsg       = 0x00000003,
59     ErrorMsg        = 0x00000004,
60     JoinMsg         = 0x00000005,
61     PartMsg         = 0x00000006,
62     QuitMsg         = 0x00000007,
63     KickMsg         = 0x00000008,
64     RenameMsg       = 0x00000009,
65     ModeMsg         = 0x0000000a,
66     ActionMsg       = 0x0000000b,
67
68     // Standard Formats
69     Bold            = 0x00000010,
70     Italic          = 0x00000020,
71     Underline       = 0x00000040,
72     Reverse         = 0x00000080,
73
74     // Individual parts of a message
75     Timestamp       = 0x00000100,
76     Sender          = 0x00000200,
77     Nick            = 0x00000400,
78     Hostmask        = 0x00000800,
79     ChannelName     = 0x00001000,
80     ModeFlags       = 0x00002000,
81
82     // URL is special, we want that to take precedence over the rest...
83     Url             = 0x00080000,
84
85     // mIRC Colors - we assume those to be present only in plain contents
86     FgCol00         = 0x00400000,
87     FgCol01         = 0x01400000,
88     FgCol02         = 0x02400000,
89     FgCol03         = 0x03400000,
90     FgCol04         = 0x04400000,
91     FgCol05         = 0x05400000,
92     FgCol06         = 0x06400000,
93     FgCol07         = 0x07400000,
94     FgCol08         = 0x08400000,
95     FgCol09         = 0x09400000,
96     FgCol10         = 0x0a400000,
97     FgCol11         = 0x0b400000,
98     FgCol12         = 0x0c400000,
99     FgCol13         = 0x0d400000,
100     FgCol14         = 0x0e400000,
101     FgCol15         = 0x0f400000,
102
103     BgCol00         = 0x00800000,
104     BgCol01         = 0x10800000,
105     BgCol02         = 0x20800000,
106     BgCol03         = 0x30800000,
107     BgCol04         = 0x40800000,
108     BgCol05         = 0x50800000,
109     BgCol06         = 0x60800000,
110     BgCol07         = 0x70800000,
111     BgCol08         = 0x80800000,
112     BgCol09         = 0x90800000,
113     BgCol10         = 0xa0800000,
114     BgCol11         = 0xb0800000,
115     BgCol12         = 0xc0800000,
116     BgCol13         = 0xd0800000,
117     BgCol14         = 0xe0800000,
118     BgCol15         = 0xf0800000,
119
120     // Colors used for sender auto coloring
121     // (starting at 01 because 00 is the default Sender format)
122     SenderCol01     = 0x01000200,
123     SenderCol02     = 0x02000200,
124     SenderCol03     = 0x03000200,
125     SenderCol04     = 0x04000200,
126     SenderCol05     = 0x05000200,
127     SenderCol06     = 0x06000200,
128     SenderCol07     = 0x07000200,
129     SenderCol08     = 0x08000200,
130     SenderCol09     = 0x09000200,
131     SenderCol10     = 0x0a000200,
132     SenderCol11     = 0x0b000200,
133     SenderCol12     = 0x0c000200,
134     SenderCol13     = 0x0d000200,
135     SenderCol14     = 0x0e000200,
136     SenderCol15     = 0x0f000200,
137     SenderCol16     = 0x10000200,
138     SenderCol17     = 0x11000200,
139     SenderCol18     = 0x12000200,
140     SenderCol19     = 0x13000200,
141     SenderCol20     = 0x14000200,
142     SenderCol21     = 0x15000200,
143
144     SenderColSelf   = 0xff000200
145   };
146
147   enum MessageLabel {
148     Highlight = 0x00000001
149   };
150
151   struct StyledString {
152     QString plainText;
153     FormatList formatList;  // starting pos, ftypes
154   };
155
156   class StyledMessage;
157   class QssParser;
158
159   StyledString styleString(const QString &);
160   QString mircToInternal(const QString &) const;
161
162   void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/); // FIXME go away
163   void setSenderAutoColor(bool state); // FIXME go away
164   QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;  // FIXME go away
165
166   QTextCharFormat cachedFormat(quint64 key) const;
167   QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const;
168   void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
169
170   QTextCharFormat mergedFormat(quint32 formatType, quint32 messageLabel = 0);
171   QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
172
173   FormatType formatType(const QString &code) const;
174   QString formatCode(FormatType) const;
175
176   inline QFont defaultFont() const { return _defaultFont; }
177
178   QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
179
180 protected:
181   void loadStyleSheet();
182
183   //! Determines the format set to be used for the given hostmask
184   //int formatSetIndex(const QString &hostmask) const;
185   //int formatSetIndexForSelf() const;
186
187   void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
188
189   bool _senderAutoColor;
190
191 private:
192   QFont _defaultFont;
193   QTextCharFormat _defaultPlainFormat;
194   QHash<FormatType, QTextCharFormat> _defaultFormats;
195   QHash<FormatType, QTextCharFormat> _customFormats;
196   QHash<quint64, QTextCharFormat> _formatCache;
197   QHash<quint64, QFontMetricsF *> _metricsCache;
198   QHash<QString, FormatType> _formatCodes;
199
200   QString _settingsKey;
201 };
202
203 class UiStyle::StyledMessage : public Message {
204 public:
205   explicit StyledMessage(const Message &message);
206
207   //! Styling is only needed for calls to plainContents() and contentsFormatList()
208   // StyledMessage can't style lazily by itself, as it doesn't know the used style
209   bool inline needsStyling() const { return _contents.plainText.isNull(); }
210   void style(UiStyle *style) const;
211
212
213   QString decoratedTimestamp() const;
214   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
215   QString decoratedSender() const;
216   inline const QString &plainContents() const { return _contents.plainText; }
217
218   inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
219   FormatType senderFormat() const;
220   inline const FormatList &contentsFormatList() const { return _contents.formatList; }
221
222 private:
223   mutable StyledString _contents;
224 };
225
226 class UiStyle::QssParser {
227 public:
228   enum Column {
229     Any,
230     Timestamp,
231     Sender,
232     Contents
233   };
234
235   struct ChatLineFormat {
236     QVector<QTextCharFormat> senderColors;
237     QVector<QTextCharFormat> mircColors;
238     QHash<FormatType, QTextCharFormat> formats;
239
240   };
241
242   QssParser();
243
244   void loadStyleSheet(const QString &sheet);
245
246   inline QPalette palette() const { return _palette; }
247   ChatLineFormat basicFormat() const;
248   QHash<FormatType, ChatLineFormat> specialFormats() const;
249
250 protected:
251   typedef QList<qreal> ColorTuple;
252
253   void parseChatLineData(const QString &decl, const QString &contents);
254   void parsePaletteData(const QString &decl, const QString &contents);
255
256   QTextCharFormat parseFormat(const QString &qss);
257   bool parsePalette(QPalette &, const QString &qss);
258
259   // Parse basic data types
260   QBrush parseBrushValue(const QString &str);
261   QColor parseColorValue(const QString &str);
262   QFont parseFontValue(const QString &str);
263
264   // Parse subelements
265   ColorTuple parseColorTuple(const QString &str);
266   QGradientStops parseGradientStops(const QString &str);
267
268   QHash<QString, QPalette::ColorRole> _paletteColorRoles;
269
270 private:
271   QPalette _palette;
272   ChatLineFormat _basicFormat;
273   QHash<FormatType, ChatLineFormat> _specialFormats;
274 };
275
276 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
277 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
278
279 Q_DECLARE_METATYPE(UiStyle::FormatList)
280
281 #endif