Optionally use system locale for chat timestamp
[quassel.git] / src / uisupport / uistyle.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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
48     //! This enumerates the possible formats a text element may have. */
49     /** These formats are ordered on increasing importance, in cases where a given property is specified
50      *  by multiple active formats.
51      *  \NOTE: Do not change/add values here without also adapting the relevant
52      *         methods in this class (in particular mergedFormat())!
53      *         Also, we _do_ rely on certain properties of these values in styleString() and friends!
54      */
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
102     enum MessageLabel {
103         OwnMsg          = 0x00000001,
104         Highlight       = 0x00000002,
105         Selected        = 0x00000004 // must be last!
106     };
107
108     enum ItemFormatType {
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 {
126         MarkerLine,
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     /**
138      * List of default sender colors
139      *
140      * In order from 1 - 16, matching the Sender## format in the settings file.
141      * Don't change the length or values of the colors without updating the UI, too.
142      *
143      * @see ../qtui/settingspages/chatviewsettingspage.ui
144      */
145     const QList<QColor> defaultSenderColors = QList<QColor> {
146         QColor(233, 13, 127),  /// Sender00
147         QColor(142, 85, 233),  /// Sender01
148         QColor(179, 14, 14),   /// Sender02
149         QColor(23, 179, 57),   /// Sender03
150         QColor(88, 175, 179),  /// Sender04
151         QColor(157, 84, 179),  /// Sender05
152         QColor(179, 151, 117), /// Sender06
153         QColor(49, 118, 179),  /// Sender07
154         QColor(233, 13, 127),  /// Sender08
155         QColor(142, 85, 233),  /// Sender09
156         QColor(179, 14, 14),   /// Sender10
157         QColor(23, 179, 57),   /// Sender11
158         QColor(88, 175, 179),  /// Sender12
159         QColor(157, 84, 179),  /// Sender13
160         QColor(179, 151, 117), /// Sender14
161         QColor(49, 118, 179),  /// Sender15
162     };
163     // Explicitly declare QList<QColor> type for defaultSenderColors, otherwise error C2797
164     // "list initialization inside member initializer list" will occur in Windows builds with Visual
165     // Studio's compiler.
166     //
167     // See https://blogs.msdn.microsoft.com/vcblog/2014/08/19/the-future-of-non-static-data-member-initialization/
168     // Note: Qt Creator flags this as invalid unless you set Clang in
169     // Settings -> C++ -> Code Model -> Code Completion and Semantic Highlighting -> C
170     //
171     // See https://bugreports.qt.io/browse/QTCREATORBUG-1902
172
173     /**
174      * Default sender color for sent messages
175      */
176     const QColor defaultSenderColorSelf = QColor(0, 0, 0);
177
178     static FormatType formatType(Message::Type msgType);
179     static StyledString styleString(const QString &string, quint32 baseFormat = Base);
180     static QString mircToInternal(const QString &);
181
182     /**
183      * Gets if a custom timestamp format is used.
184      *
185      * @return True if custom timestamp format used, otherwise false
186      */
187     static inline bool useCustomTimestampFormat() { return _useCustomTimestampFormat; }
188
189     /**
190      * Gets the format string for chat log timestamps according to the system locale.
191      *
192      * This will return " hh:mm:ss" for system locales with 24-hour time or " h:mm:ss AP" for
193      * systems with 12-hour time.
194      *
195      * @return String representing timestamp format according to system locale, e.g. " hh:mm:ss"
196      */
197     static QString systemTimestampFormatString();
198
199     /**
200      * Gets the format string for chat log timestamps, either system locale or custom.
201      *
202      * Depending on useCustomTimestampFormat(), this will return either the system locale based
203      * time format, or the custom user-specified string.
204      *
205      * @return String representing timestamp format, e.g. "[hh:mm:ss]" or " hh:mm:ss"
206      */
207     static QString timestampFormatString();
208
209     QTextCharFormat format(quint32 formatType, quint32 messageLabel) const;
210     QFontMetricsF *fontMetrics(quint32 formatType, quint32 messageLabel) const;
211
212     QList<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel) const;
213
214     inline const QBrush &brush(ColorRole role) const { return _uiStylePalette.at((int)role); }
215     inline void setBrush(ColorRole role, const QBrush &brush) { _uiStylePalette[(int)role] = brush; }
216
217     QVariant bufferViewItemData(const QModelIndex &networkModelIndex, int role) const;
218     QVariant nickViewItemData(const QModelIndex &networkModelIndex, int role) const;
219
220 public slots:
221     void reload();
222
223 signals:
224     void changed();
225
226 protected:
227     void loadStyleSheet();
228     QString loadStyleSheet(const QString &name, bool shouldExist = false);
229
230     QTextCharFormat format(quint64 key) const;
231     QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel) const;
232     void setCachedFormat(const QTextCharFormat &format, quint32 formatType, quint32 messageLabel) const;
233     void mergeFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
234     void mergeSubElementFormat(QTextCharFormat &format, quint32 formatType, quint64 messageLabel) const;
235
236     static FormatType formatType(const QString &code);
237     static QString formatCode(FormatType);
238
239     /**
240      * Cache the system locale timestamp format string
241      *
242      * Based on whether or not AM/PM designators are used in the QLocale::system().timeFormat(),
243      * this extends the system locale timestamp format string to include seconds.
244      *
245      * @see UiStyle::systemTimestampFormatString()
246      */
247     static void updateSystemTimestampFormat();
248
249     /**
250      * Updates the local setting cache of whether or not to use the custom timestamp format
251      *
252      * @param[in] enabled  If true, custom timestamp format used, otherwise false
253      */
254     static void setUseCustomTimestampFormat(bool enabled);
255
256     /**
257      * Updates the local setting cache of the timestamp format string
258      *
259      * @param[in] format   Timestamp format string
260      */
261     static void setTimestampFormatString(const QString &format);
262
263     /**
264      * Updates the local setting cache of whether or not to show sender brackets
265      *
266      * @param[in] enabled  If true, sender brackets are enabled, otherwise false.
267      */
268     static void enableSenderBrackets(bool enabled);
269
270     QVariant itemData(int role, const QTextCharFormat &format) const;
271
272 private slots:
273     void allowMircColorsChanged(const QVariant &);
274     void showItemViewIconsChanged(const QVariant &);
275
276 private:
277     QVector<QBrush> _uiStylePalette;
278     QBrush _markerLineBrush;
279     QHash<quint64, QTextCharFormat> _formats;
280     mutable QHash<quint64, QTextCharFormat> _formatCache;
281     mutable QHash<quint64, QFontMetricsF *> _metricsCache;
282     QHash<quint32, QTextCharFormat> _listItemFormats;
283     static QHash<QString, FormatType> _formatCodes;
284     static bool _useCustomTimestampFormat;        /// If true, use the custom timestamp format
285     static QString _systemTimestampFormatString;  /// Cached copy of system locale timestamp format
286     static QString _timestampFormatString;        /// Timestamp format string
287     static bool _showSenderBrackets;              /// If true, show brackets around sender names
288
289     QIcon _channelJoinedIcon;
290     QIcon _channelPartedIcon;
291     QIcon _userOfflineIcon;
292     QIcon _userOnlineIcon;
293     QIcon _userAwayIcon;
294     QIcon _categoryOpIcon;
295     QIcon _categoryVoiceIcon;
296     int _opIconLimit;
297     int _voiceIconLimit;
298     bool _showNickViewIcons;
299     bool _showBufferViewIcons;
300     bool _allowMircColors;
301 };
302
303
304 class UiStyle::StyledMessage : public Message
305 {
306     Q_DECLARE_TR_FUNCTIONS(UiStyle::StyledMessage)
307
308 public:
309     explicit StyledMessage(const Message &message);
310
311     QString decoratedTimestamp() const;
312     QString plainSender() const;           //!< Nickname (no decorations) for Plain and Notice, empty else
313     QString decoratedSender() const;
314     const QString &plainContents() const;
315
316     const FormatList &contentsFormatList() const;
317
318     quint8 senderHash() const;
319
320 protected:
321     void style() const;
322
323 private:
324     mutable StyledString _contents;
325     mutable quint8 _senderHash;
326 };
327
328
329 QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList);
330 QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList);
331
332 Q_DECLARE_METATYPE(UiStyle::FormatList)
333
334 #endif