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