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