Workaround the wordwrap issues with Qt
[quassel.git] / src / qtui / chatitem.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 <QAction>
25 #include <QGraphicsItem>
26 #include <QObject>
27
28 #include "chatlinemodel.h"
29 #include "chatscene.h"
30 #include "clickable.h"
31 #include "uistyle.h"
32 #include "qtui.h"
33
34 #include <QTextLayout>
35
36 class ChatItem : public QGraphicsItem {
37 protected:
38   ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent);
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   void initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft) const;
51   virtual inline void initLayout(QTextLayout *layout) const {
52     initLayoutHelper(layout, QTextOption::NoWrap);
53     doLayout(layout);
54   }
55   virtual void doLayout(QTextLayout *) const;
56   virtual UiStyle::FormatList formatList() const;
57
58   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
59   enum { Type = ChatScene::ChatItemType };
60   virtual inline int type() const { return Type; }
61
62   QVariant data(int role) const;
63
64   // selection stuff, to be called by the scene
65   QString selection() const;
66   void clearSelection();
67   void setFullSelection();
68   void continueSelecting(const QPointF &pos);
69   bool hasSelection() const;
70   bool isPosOverSelection(const QPointF &pos) const;
71
72   QList<QRectF> findWords(const QString &searchWord, Qt::CaseSensitivity caseSensitive);
73
74   virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
75   virtual void handleClick(const QPointF &pos, ChatScene::ClickMode);
76
77 protected:
78   enum SelectionMode {
79     NoSelection,
80     PartialSelection,
81     FullSelection
82   };
83
84   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
85   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
86   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
87
88   void paintBackground(QPainter *);
89   QVector<QTextLayout::FormatRange> selectionFormats() const;
90   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
91   void overlayFormat(UiStyle::FormatList &fmtList, int start, int end, quint32 overlayFmt) const;
92
93   inline qint16 selectionStart() const { return _selectionStart; }
94   inline void setSelectionStart(qint16 start) { _selectionStart = start; }
95   inline qint16 selectionEnd() const { return _selectionEnd; }
96   inline void setSelectionEnd(qint16 end) { _selectionEnd = end; }
97   inline SelectionMode selectionMode() const { return _selectionMode; }
98   inline void setSelectionMode(SelectionMode mode) { _selectionMode = mode; }
99   void setSelection(SelectionMode mode, qint16 selectionStart, qint16 selectionEnd);
100
101   qint16 posToCursor(const QPointF &pos) const;
102
103   // WARNING: setGeometry and setHeight should not be used without either:
104   //  a) calling prepareGeometryChange() immediately before setColumns()
105   //  b) calling Chatline::setPos() immediately afterwards
106   inline void setGeometry(qreal width, qreal height) {
107     _boundingRect.setWidth(width);
108     _boundingRect.setHeight(height);
109   }
110   inline void setHeight(const qreal &height) {
111     _boundingRect.setHeight(height);
112   }
113   inline void setWidth(const qreal &width) {
114     _boundingRect.setWidth(width);
115   }
116
117 private:
118   // internal selection stuff
119   void setSelection(int start, int length);
120
121   QRectF _boundingRect;
122
123   SelectionMode _selectionMode;
124   qint16 _selectionStart, _selectionEnd;
125
126   friend class ChatLine;
127 };
128
129 // ************************************************************
130 // TimestampChatItem
131 // ************************************************************
132
133 //! A ChatItem for the timestamp column
134 class TimestampChatItem : public ChatItem {
135 public:
136   TimestampChatItem(const qreal &width, const qreal &height, QGraphicsItem *parent) : ChatItem(width, height, QPointF(0, 0), parent) {}
137   enum { Type = ChatScene::TimestampChatItemType };
138   virtual inline int type() const { return Type; }
139   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::TimestampColumn; }
140 };
141
142 // ************************************************************
143 // SenderChatItem
144 // ************************************************************
145 //! A ChatItem for the sender column
146 class SenderChatItem : public ChatItem {
147 public:
148   SenderChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent) : ChatItem(width, height, pos, parent) {}
149   virtual inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
150
151 protected:
152   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
153   enum { Type = ChatScene::SenderChatItemType };
154   virtual inline int type() const { return Type; }
155   virtual inline void initLayout(QTextLayout *layout) const {
156     initLayoutHelper(layout, QTextOption::ManualWrap, Qt::AlignRight);
157     doLayout(layout);
158   }
159 };
160
161 // ************************************************************
162 // ContentsChatItem
163 // ************************************************************
164 struct ContentsChatItemPrivate;
165
166 //! A ChatItem for the contents column
167 class ContentsChatItem : public ChatItem {
168   Q_DECLARE_TR_FUNCTIONS(ContentsChatItem);
169
170 public:
171   ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent);
172   ~ContentsChatItem();
173
174   enum { Type = ChatScene::ContentsChatItemType };
175   virtual inline int type() const { return Type; }
176
177   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
178   QFontMetricsF *fontMetrics() const;
179
180 protected:
181   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
182   virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
183   virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
184   virtual void handleClick(const QPointF &pos, ChatScene::ClickMode clickMode);
185
186   virtual void addActionsToMenu(QMenu *menu, const QPointF &itemPos);
187   virtual void copyLinkToClipboard();
188
189   virtual QVector<QTextLayout::FormatRange> additionalFormats() const;
190
191   virtual inline void initLayout(QTextLayout *layout) const {
192     initLayoutHelper(layout, QTextOption::WrapAtWordBoundaryOrAnywhere);
193     doLayout(layout);
194   }
195   virtual void doLayout(QTextLayout *layout) const;
196   virtual UiStyle::FormatList formatList() const;
197
198 private:
199   class ActionProxy;
200   class WrapColumnFinder;
201
202   ContentsChatItemPrivate *_data;
203   ContentsChatItemPrivate *privateData() const;
204
205   Clickable clickableAt(const QPointF &pos) const;
206
207   void endHoverMode();
208   void showWebPreview(const Clickable &click);
209   void clearWebPreview();
210
211   qreal setGeometryByWidth(qreal w);
212   friend class ChatLine;
213   friend struct ContentsChatItemPrivate;
214
215   QFontMetricsF *_fontMetrics;
216
217   // we need a receiver for Action signals
218   static ActionProxy _actionProxy;
219 };
220
221 struct ContentsChatItemPrivate {
222   ContentsChatItem *contentsItem;
223   ClickableList clickables;
224   Clickable currentClickable;
225   Clickable activeClickable;
226
227   ContentsChatItemPrivate(const ClickableList &c, ContentsChatItem *parent) : contentsItem(parent), clickables(c) {}
228 };
229
230 class ContentsChatItem::WrapColumnFinder {
231 public:
232   WrapColumnFinder(const ChatItem *parent);
233   ~WrapColumnFinder();
234
235   qint16 nextWrapColumn(qreal width);
236
237 private:
238   const ChatItem *item;
239   QTextLayout layout;
240   QTextLine line;
241   ChatLineModel::WrapList wrapList;
242   qint16 wordidx;
243   qint16 lineCount;
244   qreal choppedTrailing;
245 };
246
247 //! Acts as a proxy for Action signals targetted at a ContentsChatItem
248 /** Since a ChatItem is not a QObject, hence cannot receive signals, we use a static ActionProxy
249  *  as a receiver instead. This avoids having to handle ChatItem actions (e.g. context menu entries)
250  *  outside the ChatItem.
251  */
252 class ContentsChatItem::ActionProxy : public QObject {
253   Q_OBJECT
254
255 public slots:
256   inline void copyLinkToClipboard() { item()->copyLinkToClipboard(); }
257
258 private:
259   /// Returns the ContentsChatItem that should receive the action event.
260   /** For efficiency reasons, values are not checked for validity. You gotta make sure that you set the data() member
261    *  in the Action correctly.
262    *  @return The ChatItem from which the sending Action originated
263    */
264   inline ContentsChatItem *item() const {
265     return static_cast<ContentsChatItem *>(qobject_cast<QAction *>(sender())->data().value<void *>());
266   }
267 };
268
269 /*************************************************************************************************/
270
271 // Avoid circular include deps
272 #include "chatline.h"
273 const QAbstractItemModel *ChatItem::model() const { return static_cast<ChatLine *>(parentItem())->model(); }
274 int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
275
276 #endif