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