Add buffer-specific actions to ChatView's context menu
[quassel.git] / src / qtui / chatscene.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 QString idString() const { return _idString; }
76
77   int rowByScenePos(qreal y) const;
78   inline int rowByScenePos(const QPointF &pos) const { return rowByScenePos(pos.y()); }
79   ChatLineModel::ColumnType columnByScenePos(qreal x) const ;
80   inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) const { return columnByScenePos(pos.x()); }
81
82   ChatView *chatView() const;
83   ChatItem *chatItemAt(const QPointF &pos) const;
84   inline ChatLine *chatLine(int row) { return (row < _lines.count()) ? _lines[row] : 0; }
85
86   inline bool isSingleBufferScene() const { return _singleBufferId.isValid(); }
87   inline BufferId singleBufferId() const { return _singleBufferId; }
88   bool containsBuffer(const BufferId &id) const;
89
90   ColumnHandleItem *firstColumnHandle() const;
91   ColumnHandleItem *secondColumnHandle() const;
92
93   inline CutoffMode senderCutoffMode() const { return _cutoffMode; }
94   inline void setSenderCutoffMode(CutoffMode mode) { _cutoffMode = mode; }
95
96   QString selection() const;
97   bool hasSelection() const;
98   bool hasGlobalSelection() const;
99   bool isPosOverSelection(const QPointF &) const;
100   bool isGloballySelecting() const;
101   void initiateDrag(QWidget *source);
102
103   bool isScrollingAllowed() const;
104
105   virtual bool event(QEvent *e);
106
107  public slots:
108   void updateForViewport(qreal width, qreal height);
109   void setWidth(qreal width);
110
111   // these are used by the chatitems to notify the scene and manage selections
112   void setSelectingItem(ChatItem *item);
113   ChatItem *selectingItem() const { return _selectingItem; }
114   void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
115   void clearGlobalSelection();
116   void clearSelection();
117   void selectionToClipboard(QClipboard::Mode = QClipboard::Clipboard);
118   void stringToClipboard(const QString &str, QClipboard::Mode = QClipboard::Clipboard);
119
120   void requestBacklog();
121
122   void loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect);
123   void clearWebPreview(ChatItem *parentItem = 0);
124
125 signals:
126   void lastLineChanged(QGraphicsItem *item, qreal offset);
127   void layoutChanged(); // indicates changes to the scenerect due to resizing of the contentsitems
128   void mouseMoveWhileSelecting(const QPointF &scenePos);
129
130 protected:
131   virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent);
132   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
133   virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
134   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
135   virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
136   virtual void handleClick(Qt::MouseButton button, const QPointF &scenePos);
137
138 protected slots:
139   void rowsInserted(const QModelIndex &, int, int);
140   void rowsAboutToBeRemoved(const QModelIndex &, int, int);
141
142 private slots:
143   void firstHandlePositionChanged(qreal xpos);
144   void secondHandlePositionChanged(qreal xpos);
145   void showWebPreviewEvent();
146   void deleteWebPreviewEvent();
147   void showWebPreviewChanged();
148
149   void clickTimeout();
150
151 private:
152   void setHandleXLimits();
153   void updateSelection(const QPointF &pos);
154
155   ChatView *_chatView;
156   QString _idString;
157   QAbstractItemModel *_model;
158   QList<ChatLine *> _lines;
159   BufferId _singleBufferId;
160
161   // calls to QChatScene::sceneRect() are very expensive. As we manage the scenerect ourselves
162   // we store the size in a member variable.
163   QRectF _sceneRect;
164   int _firstLineRow; // the first row to display (aka: not a daychange msg)
165   void updateSceneRect(qreal width);
166   inline void updateSceneRect() { updateSceneRect(_sceneRect.width()); }
167   void updateSceneRect(const QRectF &rect);
168   qreal _viewportHeight;
169
170   ColumnHandleItem *_firstColHandle, *_secondColHandle;
171   qreal _firstColHandlePos, _secondColHandlePos;
172   CutoffMode _cutoffMode;
173
174   ChatItem *_selectingItem;
175   int _selectionStartCol, _selectionMinCol;
176   int _selectionStart;
177   int _selectionEnd;
178   int _firstSelectionRow;
179   bool _isSelecting;
180
181   QTimer _clickTimer;
182   ClickMode _clickMode;
183   QPointF _clickPos;
184   bool _clickHandled;
185   bool _leftButtonPressed;
186
187   bool _showWebPreview;
188
189   struct WebPreview {
190     ChatItem *parentItem;
191     QGraphicsItem *previewItem;
192     QString url;
193     QRectF urlRect;
194     QTimer delayTimer;
195     QTimer deleteTimer;
196     WebPreview() : parentItem(0), previewItem(0) {}
197   };
198   WebPreview webPreview;
199 };
200
201 #endif