Kill old non-stylesheet code from UiStyle
[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();
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     // foreground: 0x0.400000
87     // background: 0x.0800000
88   };
89
90   enum MessageLabel {
91     OwnMsg          = 0x00000001,
92     Highlight       = 0x00000002
93   };
94
95   struct StyledString {
96     QString plainText;
97     FormatList formatList;  // starting pos, ftypes
98   };
99
100   class StyledMessage;
101
102   StyledString styleString(const QString &);
103   QString mircToInternal(const QString &) const;
104
105   QTextCharFormat format(quint32 formatType, quint32 messageLabel = 0);
106   QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel = 0);
107
108   inline QFont defaultFont() const { return _defaultFont; }
109
110   QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength);
111
112 protected:
113   void loadStyleSheet();
114
115   //! Determines the format set to be used for the given hostmask
116   //int formatSetIndex(const QString &hostmask) const;
117   //int formatSetIndexForSelf() const;
118
119   QTextCharFormat cachedFormat(quint64 key) const;
120   QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const;
121   void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
122   void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint32 messageLabel = 0);
123
124   FormatType formatType(const QString &code) const;
125   QString formatCode(FormatType) const;
126
127 private:
128   QFont _defaultFont;
129   QHash<quint64, QTextCharFormat> _formatCache;
130   QHash<quint64, QFontMetricsF *> _metricsCache;
131   QHash<QString, FormatType> _formatCodes;
132 };
133
134 class UiStyle::StyledMessage : public Message {
135 public:
136   explicit StyledMessage(const Message &message);
137
138   //! Styling is only needed for calls to plainContents() and contentsFormatList()
139   // StyledMessage can't style lazily by itself, as it doesn't know the used style
140   bool inline needsStyling() const { return _contents.plainText.isNull(); }
141   void style(UiStyle *style) const;
142
143
144   QString decoratedTimestamp() const;
145   QString plainSender() const;             //!< Nickname (no decorations) for Plain and Notice, empty else
146   QString decoratedSender() const;
147   inline const QString &plainContents() const { return _contents.plainText; }
148
149   inline FormatType timestampFormat() const { return UiStyle::Timestamp; }
150   FormatType senderFormat() const;
151   inline const FormatList &contentsFormatList() const { return _contents.formatList; }
152
153 private:
154   mutable StyledString _contents;
155 };
156
157 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
158 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
159
160 Q_DECLARE_METATYPE(UiStyle::FormatList)
161
162 #endif