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