ChatMonitorSettingspage ported to 0.3.x
[quassel.git] / src / qtui / chatitem.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 CHATITEM_H_
22 #define CHATITEM_H_
23
24 #include <QAction>
25 #include <QGraphicsItem>
26 #include <QObject>
27
28 #include "chatlinemodel.h"
29 #include "chatscene.h"
30 #include "uistyle.h"
31 #include "qtui.h"
32
33 class QTextLayout;
34 struct ChatItemPrivate;
35
36 class ChatItem : public QGraphicsItem {
37 protected:
38   ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent);
39   virtual ~ChatItem();
40
41 public:
42   inline const QAbstractItemModel *model() const;
43   inline int row() const;
44   virtual ChatLineModel::ColumnType column() const = 0;
45   inline ChatScene *chatScene() const { return qobject_cast<ChatScene *>(scene()); }
46
47   inline QRectF boundingRect() const { return _boundingRect; }
48   inline qreal width() const { return _boundingRect.width(); }
49   inline qreal height() const { return _boundingRect.height(); }
50
51   QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
52   virtual void doLayout();
53   void clearLayout();
54
55   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
56   enum { Type = ChatScene::ChatItemType };
57   virtual inline int type() const { return Type; }
58
59   QVariant data(int role) const;
60
61   // selection stuff, to be called by the scene
62   QString selection() const;
63   void clearSelection();
64   void setFullSelection();
65   void continueSelecting(const QPointF &pos);
66   bool hasSelection() const;
67   bool isPosOverSelection(const QPointF &pos) const;
68
69   QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
70
71   virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
72   virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
73
74 protected:
75   enum SelectionMode {
76     NoSelection,
77     PartialSelection,
78     FullSelection
79   };
80
81   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
82   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
83   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
84
85   inline QTextLayout *layout() const;
86
87   virtual QTextLayout::FormatRange selectionFormat() const;
88   virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
89
90   inline qint16 selectionStart() const { return _selectionStart; }
91   inline void setSelectionStart(qint16 start) { _selectionStart = start; }
92   inline qint16 selectionEnd() const { return _selectionEnd; }
93   inline void setSelectionEnd(qint16 end) { _selectionEnd = end; }
94   inline SelectionMode selectionMode() const { return _selectionMode; }
95   inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; }
96   void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd);
97
98   qint16 posToCursor(const QPointF &pos) const;
99
100   inline bool hasPrivateData() const { return (bool)_data; }
101   ChatItemPrivate *privateData() const;
102   virtual inline ChatItemPrivate *newPrivateData();
103
104   // WARNING: setGeometry and setHeight should not be used without either:
105   //  a) calling prepareGeometryChange() immediately before setColumns()
106   //  b) calling Chatline::setPos() immediately afterwards
107   inline void setGeometry(qreal width, qreal height) {
108     _boundingRect.setWidth(width);
109     _boundingRect.setHeight(height);
110   }
111   inline void setHeight(const qreal &height) {
112     _boundingRect.setHeight(height);
113   }
114   inline void setWidth(const qreal &width) {
115     _boundingRect.setWidth(width);
116   }
117
118 private:
119   // internal selection stuff
120   void setSelection(int start, int length);
121
122   ChatItemPrivate *_data;
123   QRectF _boundingRect;
124
125   SelectionMode _selectionMode;
126   qint16 _selectionStart, _selectionEnd;
127
128   friend class ChatLine;
129 };
130
131 struct ChatItemPrivate {
132   QTextLayout *layout;
133   ChatItemPrivate(QTextLayout *l) : layout(l) {}
134   virtual ~ChatItemPrivate() {
135     delete layout;
136   }
137 };
138
139 // inlines of ChatItem
140 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
141 ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); }
142
143 // ************************************************************
144 // TimestampChatItem
145 // ************************************************************
146
147 //! A ChatItem for the timestamp column
148 class TimestampChatItem : public ChatItem {
149 public:
150   TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
151   enum { Type = ChatScene::TimestampChatItemType };
152   virtual inline int type() const { return Type; }
153   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
154 };
155
156 // ************************************************************
157 // SenderChatItem
158 // ************************************************************
159 //! A ChatItem for the sender column
160 class SenderChatItem : public ChatItem {
161 public:
162   SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
163   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
164
165 protected:
166   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
167   enum { Type = ChatScene::SenderChatItemType };
168   virtual inline int type() const { return Type; }
169   virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::ManualWrap, Qt::AlignRight)); }
170 };
171
172 // ************************************************************
173 // ContentsChatItem
174 // ************************************************************
175 struct ContentsChatItemPrivate;
176
177 //! A ChatItem for the contents column
178 class ContentsChatItem : public ChatItem {
179   Q_DECLARE_TR_FUNCTIONS(ContentsChatItem);
180
181 public:
182   ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
183
184   enum { Type = ChatScene::ContentsChatItemType };
185   virtual inline int type() const { return Type; }
186
187   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
188   inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
189
190 protected:
191   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
192   virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
193   virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
194   virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
195
196   virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
197   virtual void copyLinkToClipboard();
198
199   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
200
201   virtual void doLayout();
202   virtual inline ChatItemPrivate *newPrivateData();
203
204 private:
205   struct Clickable;
206   class ActionProxy;
207   class WrapColumnFinder;
208
209   inline ContentsChatItemPrivate *privateData() const;
210
211   QList<Clickable> findClickables() const;
212   Clickable clickableAt(const QPointF &pos) const;
213
214   void endHoverMode();
215   void showWebPreview(const Clickable &click);
216   void clearWebPreview();
217
218   qreal setGeometryByWidth(qreal w);
219   friend class ChatLine;
220   friend struct ContentsChatItemPrivate;
221
222   QFontMetricsF *_fontMetrics;
223
224   // we need a receiver for Action signals
225   static ActionProxy _actionProxy;
226 };
227
228 struct ContentsChatItem::Clickable {
229   // Don't change these enums without also changing the regexps in analyze()!
230   enum Type {
231     Invalid = -1,
232     Url = 0,
233     Channel = 1,
234     Nick = 2
235   };
236
237   Type type;
238   quint16 start;
239   quint16 length;
240
241   inline Clickable() : type(Invalid) {};
242   inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
243   inline bool isValid() const { return type != Invalid; }
244 };
245
246 struct ContentsChatItemPrivate : ChatItemPrivate {
247   ContentsChatItem *contentsItem;
248   QList<ContentsChatItem::Clickable> clickables;
249   ContentsChatItem::Clickable currentClickable;
250   ContentsChatItem::Clickable activeClickable;
251
252   ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c, ContentsChatItem *parent)
253   : ChatItemPrivate(l), contentsItem(parent), clickables(c) {}
254 };
255
256 //inlines regarding ContentsChatItemPrivate
257 ChatItemPrivate *ContentsChatItem::newPrivateData() {
258   return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this);
259 }
260 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
261
262 class ContentsChatItem::WrapColumnFinder {
263 public:
264   WrapColumnFinder(ChatItem *parent);
265   ~WrapColumnFinder();
266
267   qint16 nextWrapColumn();
268
269 private:
270   ChatItem *item;
271   QTextLayout *layout;
272   QTextLine line;
273   ChatLineModel::WrapList wrapList;
274   qint16 wordidx;
275   qint16 lineCount;
276   qreal choppedTrailing;
277 };
278
279 //! Acts as a proxy for Action signals targetted at a ContentsChatItem
280 /** Since a ChatItem is not a QObject, hence cannot receive signals, we use a static ActionProxy
281  *  as a receiver instead. This avoids having to handle ChatItem actions (e.g. context menu entries)
282  *  outside the ChatItem.
283  */
284 class ContentsChatItem::ActionProxy : public QObject {
285   Q_OBJECT
286
287 public slots:
288   inline void copyLinkToClipboard() { item()->copyLinkToClipboard(); }
289
290 private:
291   /// Returns the ContentsChatItem that should receive the action event.
292   /** For efficiency reasons, values are not checked for validity. You gotta make sure that you set the data() member
293    *  in the Action correctly.
294    *  @return The ChatItem from which the sending Action originated
295    */
296   inline ContentsChatItem *item() const {
297     return static_cast<ContentsChatItem *>(qobject_cast<QAction *>(sender())->data().value<void *>());
298   }
299 };
300
301 /*************************************************************************************************/
302
303 // Avoid circular include deps
304 #include "chatline.h"
305 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
306 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
307
308 #endif