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