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