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