59841dc776cd004d3790ff86dfa5c5f48939d807
[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 <QTextCharFormat>
29 #include <QTextLayout>
30 #include <QUrl>
31
32 #include "message.h"
33 #include "settings.h"
34
35 class UiStyle {
36
37   public:
38     UiStyle(const QString &settingsKey);
39     virtual ~UiStyle();
40
41     //! This enumerates the possible formats a text element may have. */
42     /** These formats are ordered on increasing importance, in cases where a given property is specified
43      *  by multiple active formats.
44      *  \NOTE: Do not change/add values here without also adapting the relevant
45      *         methods in this class (in particular mergedFormat())!
46      *         Also, we _do_ rely on certain properties of these values in styleString() and friends!
47      */
48     enum FormatType {
49       None            = 0x00000000,
50       Invalid         = 0x11111111,
51       // Message Formats (mutually exclusive!)
52       PlainMsg        = 0x00000001,
53       NoticeMsg       = 0x00000002,
54       ServerMsg       = 0x00000003,
55       ErrorMsg        = 0x00000004,
56       JoinMsg         = 0x00000005,
57       PartMsg         = 0x00000006,
58       QuitMsg         = 0x00000007,
59       KickMsg         = 0x00000008,
60       RenameMsg       = 0x00000009,
61       ModeMsg         = 0x0000000a,
62       ActionMsg       = 0x0000000b,
63       // Standard Formats
64       Bold            = 0x00000010,
65       Italic          = 0x00000020,
66       Underline       = 0x00000040,
67       Reverse         = 0x00000080,
68       // Individual parts of a message
69       Timestamp       = 0x00000100,
70       Sender          = 0x00000200,
71       Nick            = 0x00000400,
72       Hostmask        = 0x00000800,
73       ChannelName     = 0x00001000,
74       ModeFlags       = 0x00002000,
75       // URL is special, we want that to take precedence over the rest...
76       Url             = 0x00100000,
77       // Colors
78       FgCol00         = 0x00400000,
79       FgCol01         = 0x01400000,
80       FgCol02         = 0x02400000,
81       FgCol03         = 0x03400000,
82       FgCol04         = 0x04400000,
83       FgCol05         = 0x05400000,
84       FgCol06         = 0x06400000,
85       FgCol07         = 0x07400000,
86       FgCol08         = 0x08400000,
87       FgCol09         = 0x09400000,
88       FgCol10         = 0x0a400000,
89       FgCol11         = 0x0b400000,
90       FgCol12         = 0x0c400000,
91       FgCol13         = 0x0d400000,
92       FgCol14         = 0x0e400000,
93       FgCol15         = 0x0f400000,
94
95       BgCol00         = 0x00800000,
96       BgCol01         = 0x10800000,
97       BgCol02         = 0x20800000,
98       BgCol03         = 0x30800000,
99       BgCol04         = 0x40800000,
100       BgCol05         = 0x50800000,
101       BgCol06         = 0x60800000,
102       BgCol07         = 0x70800000,
103       BgCol08         = 0x80800000,
104       BgCol09         = 0x90800000,
105       BgCol10         = 0xa0800000,
106       BgCol11         = 0xb0800000,
107       BgCol12         = 0xc0800000,
108       BgCol13         = 0xd0800000,
109       BgCol14         = 0xe0800000,
110       BgCol15         = 0xf0800000
111
112     };
113
114     struct UrlInfo {
115       int start, end;
116       QUrl url;
117     };
118
119     struct StyledString {
120       QString text;
121       QList<QPair<int, quint32> > formats;  // starting pos, ftypes
122     };
123
124     StyledString styleString(const QString &);
125
126     void setFormat(FormatType, QTextCharFormat, Settings::Mode mode/* = Settings::Custom*/);
127     QTextCharFormat format(FormatType, Settings::Mode mode = Settings::Custom) const;
128     QTextCharFormat mergedFormat(quint32 formatType);
129
130     FormatType formatType(const QString &code) const;
131     QString formatCode(FormatType) const;
132
133   protected:
134
135
136   private:
137     QTextCharFormat _defaultPlainFormat;
138     QHash<FormatType, QTextCharFormat> _defaultFormats;
139     QHash<FormatType, QTextCharFormat> _customFormats;
140     QHash<quint32, QTextCharFormat> _cachedFormats;
141     QHash<QString, FormatType> _formatCodes;
142
143     QString _settingsKey;
144 };
145
146 #endif // SPUTDEV
147 #endif