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