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