5877040cf33b2b914ba1545d6645f65f58d066d5
[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
56   QVariant data(int role) const;
57
58   // selection stuff, to be called by the scene
59   void clearSelection();
60   void setFullSelection();
61   void continueSelecting(const QPointF &pos);
62
63   QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
64
65 protected:
66   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
67   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
68   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
69
70   inline QTextLayout *layout() const;
71
72   virtual inline QVector<QTextLayout::FormatRange> additionalFormats() const { return QVector<QTextLayout::FormatRange>(); }
73   qint16 posToCursor(const QPointF &pos);
74
75   inline bool hasPrivateData() const { return (bool)_data; }
76   ChatItemPrivate *privateData() const;
77   virtual inline ChatItemPrivate *newPrivateData();
78
79   // WARNING: setGeometry and setHeight should not be used without either:
80   //  a) calling prepareGeometryChange() immediately before setColumns()
81   //  b) calling Chatline::setPos() immediately afterwards
82   inline void setGeometry(qreal width, qreal height) {
83     prepareGeometryChange();
84     _boundingRect.setWidth(width);
85     _boundingRect.setHeight(height);
86   }
87   inline void setHeight(const qreal &height) {
88     prepareGeometryChange();
89     _boundingRect.setHeight(height);
90   }
91   inline void setWidth(const qreal &width) {
92     prepareGeometryChange();
93     _boundingRect.setWidth(width);
94   }
95
96 private:
97   // internal selection stuff
98   void setSelection(int start, int length);
99
100   ChatItemPrivate *_data;
101   QRectF _boundingRect;
102
103   enum SelectionMode { NoSelection, PartialSelection, FullSelection };
104   SelectionMode _selectionMode;
105   qint16 _selectionStart, _selectionEnd;
106
107   friend class ChatLine;
108 };
109
110 struct ChatItemPrivate {
111   QTextLayout *layout;
112   ChatItemPrivate(QTextLayout *l) : layout(l) {}
113   ~ChatItemPrivate() {
114     delete layout;
115   }
116 };
117
118 // inlines of ChatItem
119 QTextLayout *ChatItem::layout() const { return privateData()->layout; }
120 ChatItemPrivate *ChatItem::newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere)); }
121
122 // ************************************************************
123 // TimestampChatItem
124 // ************************************************************
125
126 //! A ChatItem for the timestamp column
127 class TimestampChatItem : public ChatItem {
128 public:
129   TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
130   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
131 };
132
133 // ************************************************************
134 // SenderChatItem
135 // ************************************************************
136 //! A ChatItem for the sender column
137 class SenderChatItem : public ChatItem {
138 public:
139   SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
140   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
141
142 protected:
143   virtual inline ChatItemPrivate *newPrivateData() { return new ChatItemPrivate(createLayout(QTextOption::WrapAnywhere, Qt::AlignRight)); }
144 };
145
146 // ************************************************************
147 // ContentsChatItem
148 // ************************************************************
149 struct ContentsChatItemPrivate;
150
151 //! A ChatItem for the contents column
152 class ContentsChatItem : public ChatItem {
153 public:
154   ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
155
156   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
157   inline QFontMetricsF *fontMetrics() const { return _fontMetrics; }
158
159 protected:
160   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
161   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
162   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
163   virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
164   virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
165
166   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
167
168   virtual void doLayout();
169   virtual inline ChatItemPrivate *newPrivateData();
170
171 private:
172   struct Clickable;
173   class WrapColumnFinder;
174
175   inline ContentsChatItemPrivate *privateData() const;
176
177   QList<Clickable> findClickables() const;
178   void endHoverMode();
179   void showWebPreview(const Clickable &click);
180   void clearWebPreview();
181
182
183   // WARNING: setGeometry and setHeight should not be used without either:
184   //  a) calling prepareGeometryChange() immediately before setColumns()
185   //  b) calling Chatline::setPos() immediately afterwards
186   qreal setGeometryByWidth(qreal w);
187   friend class ChatLine;
188   friend struct ContentsChatItemPrivate;
189
190   QFontMetricsF *_fontMetrics;
191 };
192
193 struct ContentsChatItem::Clickable {
194   // Don't change these enums without also changing the regexps in analyze()!
195   enum Type {
196     Invalid = -1,
197     Url = 0,
198     Channel = 1,
199     Nick = 2
200   };
201
202   Type type;
203   quint16 start;
204   quint16 length;
205
206   inline Clickable() : type(Invalid) {};
207   inline Clickable(Type type_, quint16 start_, quint16 length_) : type(type_), start(start_), length(length_) {};
208   inline bool isValid() const { return type != Invalid; }
209 };
210
211 struct ContentsChatItemPrivate : ChatItemPrivate {
212   ContentsChatItem *contentsItem;
213   QList<ContentsChatItem::Clickable> clickables;
214   ContentsChatItem::Clickable currentClickable;
215   bool hasDragged;
216
217   ContentsChatItemPrivate(QTextLayout *l, const QList<ContentsChatItem::Clickable> &c, ContentsChatItem *parent) : ChatItemPrivate(l), contentsItem(parent), clickables(c), hasDragged(false) {}
218 };
219
220 //inlines regarding ContentsChatItemPrivate
221 ChatItemPrivate *ContentsChatItem::newPrivateData() { return new ContentsChatItemPrivate(createLayout(QTextOption::WrapAnywhere), findClickables(), this); }
222 ContentsChatItemPrivate *ContentsChatItem::privateData() const { return (ContentsChatItemPrivate *)ChatItem::privateData(); }
223
224 class ContentsChatItem::WrapColumnFinder {
225 public:
226   WrapColumnFinder(ChatItem *parent);
227   ~WrapColumnFinder();
228
229   qint16 nextWrapColumn();
230
231 private:
232   ChatItem *item;
233   QTextLayout *layout;
234   QTextLine line;
235   ChatLineModel::WrapList wrapList;
236   qint16 wordidx;
237   qint16 lineCount;
238   qreal choppedTrailing;
239 };
240
241 /*************************************************************************************************/
242
243 // Avoid circular include deps
244 #include "chatline.h"
245 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
246 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
247
248 #endif