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