Moving branches/0.3 to trunk
[quassel.git] / src / uisupport / uistyle.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 #ifndef SPUTDEV
25 # include "old-uistyle.h"
26 #else
27
28 #include <QDataStream>
29 #include <QTextCharFormat>
30 #include <QTextLayout>
31 #include <QUrl>
32
33 #include "message.h"
34 #include "settings.h"
35
36 class UiStyle {
37   Q_DECLARE_TR_FUNCTIONS (UiStyle);
38
39   public:
40     UiStyle(const QString &settingsKey);
41     virtual ~UiStyle();
42
43     typedef QList<QPair<int, quint32> > FormatList;
44
45     //! This enumerates the possible formats a text element may have. */
46     /** These formats are ordered on increasing importance, in cases where a given property is specified
47      *  by multiple active formats.
48      *  \NOTE: Do not change/add values here without also adapting the relevant
49      *         methods in this class (in particular mergedFormat())!
50      *         Also, we _do_ rely on certain properties of these values in styleString() and friends!
51      */
52     enum FormatType {
53       None            = 0x00000000,
54       Invalid         = 0x11111111,
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       // Standard Formats
68       Bold            = 0x00000010,
69       Italic          = 0x00000020,
70       Underline       = 0x00000040,
71       Reverse         = 0x00000080,
72       // Individual parts of a message
73       Timestamp       = 0x00000100,
74       Sender          = 0x00000200,
75       Nick            = 0x00000400,
76       Hostmask        = 0x00000800,
77       ChannelName     = 0x00001000,
78       ModeFlags       = 0x00002000,
79       // URL is special, we want that to take precedence over the rest...
80       Url             = 0x00100000,
81       // Colors
82       FgCol00         = 0x00400000,
83       FgCol01         = 0x01400000,
84       FgCol02         = 0x02400000,
85       FgCol03         = 0x03400000,
86       FgCol04         = 0x04400000,
87       FgCol05         = 0x05400000,
88       FgCol06         = 0x06400000,
89       FgCol07         = 0x07400000,
90       FgCol08         = 0x08400000,
91       FgCol09         = 0x09400000,
92       FgCol10         = 0x0a400000,
93       FgCol11         = 0x0b400000,
94       FgCol12         = 0x0c400000,
95       FgCol13         = 0x0d400000,
96       FgCol14         = 0x0e400000,
97       FgCol15         = 0x0f400000,
98
99       BgCol00         = 0x00800000,
100       BgCol01         = 0x10800000,
101       BgCol02         = 0x20800000,
102       BgCol03         = 0x30800000,
103       BgCol04         = 0x40800000,
104       BgCol05         = 0x50800000,
105       BgCol06         = 0x60800000,
106       BgCol07         = 0x70800000,
107       BgCol08         = 0x80800000,
108       BgCol09         = 0x90800000,
109       BgCol10         = 0xa0800000,
110       BgCol11         = 0xb0800000,
111       BgCol12         = 0xc0800000,
112       BgCol13         = 0xd0800000,
113       BgCol14         = 0xe0800000,
114       BgCol15         = 0xf0800000
115
116     };
117
118     struct UrlInfo {
119       int start, end;
120       QUrl url;
121     };
122
123     struct StyledString {
124       QString plainText;
125       FormatList formatList;  // starting pos, ftypes
126     };
127
128     struct StyledMessage {
129       StyledString timestamp;
130       StyledString sender;
131       StyledString contents;
132     };
133
134     StyledString styleString(const QString &);
135     StyledMessage styleMessage(const Message &);
136
137     void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
138     QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
139     QTextCharFormat mergedFormat(quint32 formatType);
140
141     FormatType formatType(const QString &code) const;
142     QString formatCode(FormatType) const;
143
144   protected:
145
146
147   private:
148     QString mircToInternal(const QString &);
149
150     QTextCharFormat _defaultPlainFormat;
151     QHash<FormatType, QTextCharFormat> _defaultFormats;
152     QHash<FormatType, QTextCharFormat> _customFormats;
153     QHash<quint32, QTextCharFormat> _cachedFormats;
154     QHash<QString, FormatType> _formatCodes;
155
156     QString _settingsKey;
157 };
158
159 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
160 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
161
162 Q_DECLARE_METATYPE(UiStyle::FormatList);
163
164 #endif // SPUTDEV
165 #endif