38d43f0ddb3d7cb27d5dec2e545588fc204aedd1
[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 <QTextCharFormat>
27 #include <QTextLayout>
28 #include <QUrl>
29
30 #include "message.h"
31 #include "settings.h"
32
33 class UiStyle {
34   Q_DECLARE_TR_FUNCTIONS(UiStyle)
35
36 public:
37   UiStyle(const QString &settingsKey);
38   virtual ~UiStyle();
39
40   typedef QList<QPair<quint16, quint32> > FormatList;
41
42   //! This enumerates the possible formats a text element may have. */
43   /** These formats are ordered on increasing importance, in cases where a given property is specified
44    *  by multiple active formats.
45    *  \NOTE: Do not change/add values here without also adapting the relevant
46    *         methods in this class (in particular mergedFormat())!
47    *         Also, we _do_ rely on certain properties of these values in styleString() and friends!
48    */
49   enum FormatType {
50     None            = 0x00000000,
51     Invalid         = 0x11111111,
52
53     // Message Formats (mutually exclusive!)
54     PlainMsg        = 0x00000001,
55     NoticeMsg       = 0x00000002,
56     ServerMsg       = 0x00000003,
57     ErrorMsg        = 0x00000004,
58     JoinMsg         = 0x00000005,
59     PartMsg         = 0x00000006,
60     QuitMsg         = 0x00000007,
61     KickMsg         = 0x00000008,
62     RenameMsg       = 0x00000009,
63     ModeMsg         = 0x0000000a,
64     ActionMsg       = 0x0000000b,
65     HighlightMsg    = 0x0000000f,
66
67     // Note: mergedFormat() assumes that 0x10 - 0x80 are *only* used within the message contents,
68     //       e.g. not together with any of 0x0100-0x2000!
69     //       If we happen to find a use case for that, we can see if/how to implement that though.
70
71     // Standard Formats
72     Bold            = 0x00000010,
73     Italic          = 0x00000020,
74     Underline       = 0x00000040,
75     Reverse         = 0x00000080,
76
77     // Individual parts of a message
78     Timestamp       = 0x00000100,
79     Sender          = 0x00000200,
80     Nick            = 0x00000400,
81     Hostmask        = 0x00000800,
82     ChannelName     = 0x00001000,
83     ModeFlags       = 0x00002000,
84
85     // URL is special, we want that to take precedence over the rest...
86     Url             = 0x00100000,
87
88     // mIRC Colors - we assume those to be present only in plain contents
89     FgCol00         = 0x00400000,
90     FgCol01         = 0x01400000,
91     FgCol02         = 0x02400000,
92     FgCol03         = 0x03400000,
93     FgCol04         = 0x04400000,
94     FgCol05         = 0x05400000,
95     FgCol06         = 0x06400000,
96     FgCol07         = 0x07400000,
97     FgCol08         = 0x08400000,
98     FgCol09         = 0x09400000,
99     FgCol10         = 0x0a400000,
100     FgCol11         = 0x0b400000,
101     FgCol12         = 0x0c400000,
102     FgCol13         = 0x0d400000,
103     FgCol14         = 0x0e400000,
104     FgCol15         = 0x0f400000,
105
106     BgCol00         = 0x00800000,
107     BgCol01         = 0x10800000,
108     BgCol02         = 0x20800000,
109     BgCol03         = 0x30800000,
110     BgCol04         = 0x40800000,
111     BgCol05         = 0x50800000,
112     BgCol06         = 0x60800000,
113     BgCol07         = 0x70800000,
114     BgCol08         = 0x80800000,
115     BgCol09         = 0x90800000,
116     BgCol10         = 0xa0800000,
117     BgCol11         = 0xb0800000,
118     BgCol12         = 0xc0800000,
119     BgCol13         = 0xd0800000,
120     BgCol14         = 0xe0800000,
121     BgCol15         = 0xf0800000,
122
123     // Colors used for sender auto coloring
124     // (starting at 01 because 00 is the default Sender format)
125     SenderCol01     = 0x01000200,
126     SenderCol02     = 0x02000200,
127     SenderCol03     = 0x03000200,
128     SenderCol04     = 0x04000200,
129     SenderCol05     = 0x05000200,
130     SenderCol06     = 0x06000200,
131     SenderCol07     = 0x07000200,
132     SenderCol08     = 0x08000200,
133     SenderCol09     = 0x09000200,
134     SenderCol10     = 0x0a000200,
135     SenderCol11     = 0x0b000200,
136     SenderCol12     = 0x0c000200,
137     SenderCol13     = 0x0d000200,
138     SenderCol14     = 0x0e000200,
139     SenderCol15     = 0x0f000200,
140     SenderCol16     = 0x10000200,
141     SenderCol17     = 0x11000200,
142     SenderCol18     = 0x12000200,
143     SenderCol19     = 0x13000200,
144     SenderCol20     = 0x14000200,
145     SenderCol21     = 0x15000200,
146
147     SenderColSelf   = 0xff000200
148   };
149
150   struct StyledString {
151     QString plainText;
152     FormatList formatList;  // starting pos, ftypes
153   };
154
155   class StyledMessage;
156
157   StyledString styleString(const QString &);
158   QString mircToInternal(const QString &) const;
159
160   void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
161     void setSenderAutoColor(bool state);
162   QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
163   QTextCharFormat mergedFormat(quint32 formatType);
164   QFontMetricsF *fontMetrics(quint32 formatType);
165
166   FormatType formatType(const QString &code) const;
167   QString formatCode(FormatType) const;
168
169   inline QFont defaultFont() const { return _defaultFont; }
170
171   QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
172
173 protected:
174   bool _senderAutoColor;
175 private:
176   QFont _defaultFont;
177   QTextCharFormat _defaultPlainFormat;
178   QHash<FormatType, QTextCharFormat> _defaultFormats;
179   QHash<FormatType, QTextCharFormat> _customFormats;
180   QHash<quint32, QTextCharFormat> _cachedFormats;
181   QHash<quint32, QFontMetricsF *> _cachedFontMetrics;
182   QHash<QString, FormatType> _formatCodes;
183
184   QString _settingsKey;
185 };
186
187 class UiStyle::StyledMessage : public Message {
188 public:
189   explicit StyledMessage(const Message &message);
190
191   //! Styling is only needed for calls to plainContents() and contentsFormatList()
192   // StyledMessage can't style lazily by itself, as it doesn't know the used style
193   bool inline needsStyling() const { return _contents.plainText.isNull(); }
194   void style(UiStyle *style) const;
195
196
197   QString decoratedTimestamp() const;
198   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
199   QString decoratedSender() const;
200   inline const QString &plainContents() const { return _contents.plainText; }
201
202   inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
203   FormatType senderFormat() const;
204   inline const FormatList &contentsFormatList() const { return _contents.formatList; }
205
206 private:
207   mutable StyledString _contents;
208 };
209
210 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
211 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
212
213 Q_DECLARE_METATYPE(UiStyle::FormatList)
214
215 #endif