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