Fix includes
[quassel.git] / src / qtui / chatscene.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2012 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 CHATSCENE_H_
22 #define CHATSCENE_H_
23
24 #include <QAbstractItemModel>
25 #include <QClipboard>
26 #include <QGraphicsItem>
27 #include <QGraphicsScene>
28 #include <QSet>
29 #include <QTimer>
30 #include <QUrl>
31
32 #include "chatlinemodel.h"
33 #include "messagefilter.h"
34
35 class AbstractUiMsg;
36 class ChatItem;
37 class ChatLine;
38 class ChatView;
39 class ColumnHandleItem;
40 class MarkerLineItem;
41 class WebPreviewItem;
42
43 class QGraphicsSceneMouseEvent;
44
45 class ChatScene : public QGraphicsScene {
46   Q_OBJECT
47
48 public:
49   enum CutoffMode {
50     CutoffLeft,
51     CutoffRight
52   };
53
54   enum ItemType {
55     ChatLineType = QGraphicsItem::UserType + 1,
56     ChatItemType,
57     TimestampChatItemType,
58     SenderChatItemType,
59     ContentsChatItemType,
60     SearchHighlightType,
61     WebPreviewType,
62     ColumnHandleType,
63     MarkerLineType
64   };
65
66   enum ClickMode {
67     NoClick,
68     DragStartClick,
69     SingleClick,
70     DoubleClick,
71     TripleClick
72   };
73
74   ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent);
75   virtual ~ChatScene();
76
77   inline QAbstractItemModel *model() const { return _model; }
78   inline MessageFilter *filter() const { return qobject_cast<MessageFilter*>(_model); }
79   inline QString idString() const { return _idString; }
80
81   int rowByScenePos(qreal y) const;
82   inline int rowByScenePos(const QPointF &pos) const { return rowByScenePos(pos.y()); }
83   ChatLineModel::ColumnType columnByScenePos(qreal x) const ;
84   inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) const { return columnByScenePos(pos.x()); }
85
86   ChatView *chatView() const;
87   ChatItem *chatItemAt(const QPointF &pos) const;
88   inline ChatLine *chatLine(int row) const { return (row < _lines.count()) ? _lines.value(row) : 0; }
89   inline ChatLine *chatLine(const QModelIndex &index) const { return _lines.value(index.row()); }
90
91   //! Find the ChatLine belonging to a MsgId
92   /** Searches for the ChatLine belonging to a MsgId. If there are more than one ChatLine with the same msgId,
93    *  the first one is returned.
94    *  Note that this method performs a binary search, hence it has as complexity of O(log n).
95    *  If matchExact is false, and we don't have an exact match for the given msgId, we return the visible line right
96    *  above the requested one.
97    *  \param msgId      The message ID to look for
98    *  \param matchExact Whether we find only exact matches
99    *  \param ignoreDayChange Whether we ignore day change messages
100    *  \return The ChatLine corresponding to the given MsgId
101    */
102   ChatLine *chatLine(MsgId msgId, bool matchExact = true, bool ignoreDayChange = true) const;
103
104   inline ChatLine *lastLine() const { return _lines.count() ? _lines.last() : 0; }
105
106   inline MarkerLineItem *markerLine() const { return _markerLine; }
107
108   inline bool isSingleBufferScene() const { return _singleBufferId.isValid(); }
109   inline BufferId singleBufferId() const { return _singleBufferId; }
110   bool containsBuffer(const BufferId &id) const;
111
112   ColumnHandleItem *firstColumnHandle() const;
113   ColumnHandleItem *secondColumnHandle() const;
114
115   inline CutoffMode senderCutoffMode() const { return _cutoffMode; }
116   inline void setSenderCutoffMode(CutoffMode mode) { _cutoffMode = mode; }
117
118   QString selection() const;
119   bool hasSelection() const;
120   bool hasGlobalSelection() const;
121   bool isPosOverSelection(const QPointF &) const;
122   bool isGloballySelecting() const;
123   void initiateDrag(QWidget *source);
124
125   bool isScrollingAllowed() const;
126
127  public slots:
128   void updateForViewport(qreal width, qreal height);
129   void setWidth(qreal width);
130   void layout(int start, int end, qreal width);
131
132   void setMarkerLineVisible(bool visible = true);
133   void setMarkerLine(MsgId msgId = MsgId());
134   void jumpToMarkerLine(bool requestBacklog);
135
136   // these are used by the chatitems to notify the scene and manage selections
137   void setSelectingItem(ChatItem *item);
138   ChatItem *selectingItem() const { return _selectingItem; }
139   void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
140   void clearGlobalSelection();
141   void clearSelection();
142   void selectionToClipboard(QClipboard::Mode = QClipboard::Clipboard);
143   void stringToClipboard(const QString &str, QClipboard::Mode = QClipboard::Clipboard);
144
145   void requestBacklog();
146
147 #ifdef HAVE_WEBKIT
148   void loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect);
149   void clearWebPreview(ChatItem *parentItem = 0);
150 #endif
151
152 signals:
153   void lastLineChanged(QGraphicsItem *item, qreal offset);
154   void layoutChanged(); // indicates changes to the scenerect due to resizing of the contentsitems
155   void mouseMoveWhileSelecting(const QPointF &scenePos);
156
157 protected:
158   virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent);
159   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
160   virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
161   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
162   virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
163   virtual void handleClick(Qt::MouseButton button, const QPointF &scenePos);
164
165 protected slots:
166   void rowsInserted(const QModelIndex &, int, int);
167   void rowsAboutToBeRemoved(const QModelIndex &, int, int);
168   void dataChanged(const QModelIndex &, const QModelIndex &);
169
170 private slots:
171   void firstHandlePositionChanged(qreal xpos);
172   void secondHandlePositionChanged(qreal xpos);
173 #ifdef HAVE_WEBKIT
174   void webPreviewNextStep();
175 #endif
176   void showWebPreviewChanged();
177
178   void rowsRemoved();
179
180   void clickTimeout();
181
182 private:
183   void setHandleXLimits();
184   void updateSelection(const QPointF &pos);
185
186   ChatView *_chatView;
187   QString _idString;
188   QAbstractItemModel *_model;
189   QList<ChatLine *> _lines;
190   BufferId _singleBufferId;
191
192   // calls to QChatScene::sceneRect() are very expensive. As we manage the scenerect ourselves
193   // we store the size in a member variable.
194   QRectF _sceneRect;
195   int _firstLineRow; // the first row to display (aka: not a daychange msg)
196   void updateSceneRect(qreal width);
197   inline void updateSceneRect() { updateSceneRect(_sceneRect.width()); }
198   void updateSceneRect(const QRectF &rect);
199   qreal _viewportHeight;
200
201   MarkerLineItem *_markerLine;
202   bool _markerLineVisible, _markerLineValid, _markerLineJumpPending;
203
204   ColumnHandleItem *_firstColHandle, *_secondColHandle;
205   qreal _firstColHandlePos, _secondColHandlePos;
206   CutoffMode _cutoffMode;
207
208   ChatItem *_selectingItem;
209   int _selectionStartCol, _selectionMinCol;
210   int _selectionStart;
211   int _selectionEnd;
212   int _firstSelectionRow;
213   bool _isSelecting;
214
215   QTimer _clickTimer;
216   ClickMode _clickMode;
217   QPointF _clickPos;
218   bool _clickHandled;
219   bool _leftButtonPressed;
220
221   bool _showWebPreview;
222
223 #ifdef HAVE_WEBKIT
224   struct WebPreview {
225     enum PreviewState {
226       NoPreview,
227       NewPreview,
228       DelayPreview,
229       ShowPreview,
230       HidePreview
231     };
232     ChatItem *parentItem;
233     QGraphicsItem *previewItem;
234     QUrl url;
235     QRectF urlRect;
236     PreviewState previewState;
237     QTimer timer;
238     WebPreview() : parentItem(0), previewItem(0), previewState(NoPreview) {}
239   };
240   WebPreview webPreview;
241 #endif // HAVE_WEBKIT
242 };
243
244 #endif