Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
uistyle.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #ifndef UISTYLE_H_
22 #define UISTYLE_H_
23 
24 #include <QDataStream>
25 #include <QFontMetricsF>
26 #include <QHash>
27 #include <QIcon>
28 #include <QTextCharFormat>
29 #include <QTextLayout>
30 #include <QPalette>
31 #include <QVector>
32 
33 #include "bufferinfo.h"
34 #include "message.h"
35 #include "networkmodel.h"
36 #include "settings.h"
37 
38 class UiStyle : public QObject
39 {
40  Q_OBJECT
41 
42 public:
43  UiStyle(QObject *parent = 0);
44  virtual ~UiStyle();
45 
46  typedef QList<QPair<quint16, quint32> > FormatList;
47 
49 
55  enum FormatType {
56  Base = 0x00000000,
57  Invalid = 0xffffffff,
58 
59  // Message Formats (mutually exclusive!)
60  PlainMsg = 0x00000001,
61  NoticeMsg = 0x00000002,
62  ActionMsg = 0x00000003,
63  NickMsg = 0x00000004,
64  ModeMsg = 0x00000005,
65  JoinMsg = 0x00000006,
66  PartMsg = 0x00000007,
67  QuitMsg = 0x00000008,
68  KickMsg = 0x00000009,
69  KillMsg = 0x0000000a,
70  ServerMsg = 0x0000000b,
71  InfoMsg = 0x0000000c,
72  ErrorMsg = 0x0000000d,
73  DayChangeMsg = 0x0000000e,
74  TopicMsg = 0x0000000f,
75  NetsplitJoinMsg = 0x00000010,
76  NetsplitQuitMsg = 0x00000020,
77  InviteMsg = 0x00000030,
78 
79  // Standard Formats
80  Bold = 0x00000100,
81  Italic = 0x00000200,
82  Underline = 0x00000400,
83  Reverse = 0x00000800,
84 
85  // Individual parts of a message
86  Timestamp = 0x00001000,
87  Sender = 0x00002000,
88  Contents = 0x00004000,
89  Nick = 0x00008000,
90  Hostmask = 0x00010000,
91  ChannelName = 0x00020000,
92  ModeFlags = 0x00040000,
93 
94  // URL is special, we want that to take precedence over the rest...
95  Url = 0x00080000
96 
97  // mIRC Colors - we assume those to be present only in plain contents
98  // foreground: 0x0.400000
99  // background: 0x.0800000
100  };
101 
103  OwnMsg = 0x00000001,
104  Highlight = 0x00000002,
105  Selected = 0x00000004 // must be last!
106  };
107 
109  BufferViewItem = 0x00000001,
110  NickViewItem = 0x00000002,
111 
112  NetworkItem = 0x00000010,
113  ChannelBufferItem = 0x00000020,
114  QueryBufferItem = 0x00000040,
115  IrcUserItem = 0x00000080,
116  UserCategoryItem = 0x00000100,
117 
118  InactiveBuffer = 0x00001000,
119  ActiveBuffer = 0x00002000,
120  UnreadBuffer = 0x00004000,
121  HighlightedBuffer = 0x00008000,
122  UserAway = 0x00010000
123  };
124 
125  enum ColorRole {
127  NumRoles // must be last!
128  };
129 
130  struct StyledString {
131  QString plainText;
132  FormatList formatList; // starting pos, ftypes
133  };
134 
135  class StyledMessage;
136 
137  static FormatType formatType(Message::Type msgType);
138  static StyledString styleString(const QString &string, quint32 baseFormat = Base);
139  static QString mircToInternal(const QString &);
140  static inline QString timestampFormatString() { return _timestampFormatString; }
141 
142  QTextCharFormat format(quint32 formatType, quint32 messageLabel) const;
143  QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel) const;
144 
145  QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel) const;
146 
147  inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int)role); }
148  inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int)role] = brush; }
149 
150  QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
151  QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
152 
153 public slots:
154  void reload();
155 
156 signals:
157  void changed();
158 
159 protected:
160  void loadStyleSheet();
161  QString loadStyleSheet(const QString &name, bool shouldExist = false);
162 
163  QTextCharFormat format(quint64 key) const;
164  QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const;
165  void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel) const;
166  void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
167  void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
168 
169  static FormatType formatType(const QString &code);
170  static QString formatCode(FormatType);
171  static void setTimestampFormatString(const QString &format);
172 
173  QVariant itemData(int role, const QTextCharFormat &format) const;
174 
175 private slots:
176  void allowMircColorsChanged(const QVariant &);
177  void showItemViewIconsChanged(const QVariant &);
178 
179 private:
180  QVector<QBrush> _uiStylePalette;
182  QHash<quint64, QTextCharFormat> _formats;
183  mutable QHash<quint64, QTextCharFormat> _formatCache;
184  mutable QHash<quint64, QFontMetricsF *> _metricsCache;
185  QHash<quint32, QTextCharFormat> _listItemFormats;
186  static QHash<QString, FormatType> _formatCodes;
187  static QString _timestampFormatString;
188 
201 };
202 
203 
205 {
207 
208 public:
209  explicit StyledMessage(const Message &message);
210 
211  QString decoratedTimestamp() const;
212  QString plainSender() const;
213  QString decoratedSender() const;
214  const QString &plainContents() const;
215 
216  const FormatList &contentsFormatList() const;
217 
218  quint8 senderHash() const;
219 
220 protected:
221  void style() const;
222 
223 private:
225  mutable quint8 _senderHash;
226 };
227 
228 
229 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
230 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
231 
233 
234 #endif