Use handleClick() rather than mouseReleaseEvent() for handling URL clicks
[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 <QGraphicsItem>
26 #include <QGraphicsScene>
27 #include <QSet>
28 #include <QTimer>
29
30 #include "chatlinemodel.h"
31 #include "messagefilter.h"
32
33 class AbstractUiMsg;
34 class ChatItem;
35 class ChatLine;
36 class ChatView;
37 class ColumnHandleItem;
38 class WebPreviewItem;
39
40 class QGraphicsSceneMouseEvent;
41
42 class ChatScene : public QGraphicsScene {
43   Q_OBJECT
44
45 public:
46   enum CutoffMode {
47     CutoffLeft,
48     CutoffRight
49   };
50
51   enum ItemType {
52     ChatLineType = QGraphicsItem::UserType + 1,
53     ChatItemType,
54     TimestampChatItemType,
55     SenderChatItemType,
56     ContentsChatItemType,
57     SearchHighlightType,
58     WebPreviewType,
59     ColumnHandleType
60   };
61
62   enum ClickMode {
63     NoClick,
64     SingleClick,
65     DoubleClick,
66     TripleClick,
67     DragStartClick
68   };
69
70   ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent);
71   virtual ~ChatScene();
72
73   inline QAbstractItemModel *model() const { return _model; }
74   inline QString idString() const { return _idString; }
75
76   int rowByScenePos(qreal y);
77   inline int rowByScenePos(const QPointF &pos) { return rowByScenePos(pos.y()); }
78   ChatLineModel::ColumnType columnByScenePos(qreal x);
79   inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) { return columnByScenePos(pos.x()); }
80
81   ChatView *chatView() const;
82   ChatItem *chatItemAt(const QPointF &pos) const;
83
84   inline bool isSingleBufferScene() const { return _singleBufferScene; }
85   bool containsBuffer(const BufferId &id) const;
86   inline ChatLine *chatLine(int row) { return (row < _lines.count()) ? _lines[row] : 0; }
87
88   ColumnHandleItem *firstColumnHandle() const;
89   ColumnHandleItem *secondColumnHandle() const;
90
91   inline CutoffMode senderCutoffMode() const { return _cutoffMode; }
92   inline void setSenderCutoffMode(CutoffMode mode) { _cutoffMode = mode; }
93
94   QString selection() const;
95   inline bool hasGlobalSelection() const { return _selectionStart >= 0; }
96   inline bool isGloballySelecting() const { return _isSelecting; }
97   bool isPosOverSelection(const QPointF &) const;
98   void initiateDrag(QWidget *source);
99
100   bool isScrollingAllowed() const;
101
102   virtual bool event(QEvent *e);
103
104  public slots:
105   void updateForViewport(qreal width, qreal height);
106   void setWidth(qreal width);
107
108   // these are used by the chatitems to notify the scene and manage selections
109   void setSelectingItem(ChatItem *item);
110   ChatItem *selectingItem() const { return _selectingItem; }
111   void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
112   void clearGlobalSelection();
113   void clearSelection();
114
115   void putToClipboard(const QString &);
116
117   void requestBacklog();
118
119   void loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect);
120   void clearWebPreview(ChatItem *parentItem = 0);
121
122 signals:
123   void lastLineChanged(QGraphicsItem *item, qreal offset);
124   void layoutChanged(); // indicates changes to the scenerect due to resizing of the contentsitems
125   void mouseMoveWhileSelecting(const QPointF &scenePos);
126
127 protected:
128   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
129   virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
130   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
131   virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
132   virtual void handleClick(Qt::MouseButton button, const QPointF &scenePos);
133
134 protected slots:
135   void rowsInserted(const QModelIndex &, int, int);
136   void rowsAboutToBeRemoved(const QModelIndex &, int, int);
137
138 private slots:
139   void firstHandlePositionChanged(qreal xpos);
140   void secondHandlePositionChanged(qreal xpos);
141   void showWebPreviewEvent();
142   void deleteWebPreviewEvent();
143   void showWebPreviewChanged();
144
145   void clickTimeout();
146
147 private:
148   void setHandleXLimits();
149   void updateSelection(const QPointF &pos);
150
151   ChatView *_chatView;
152   QString _idString;
153   QAbstractItemModel *_model;
154   QList<ChatLine *> _lines;
155   bool _singleBufferScene;
156
157   // calls to QChatScene::sceneRect() are very expensive. As we manage the scenerect ourselves
158   // we store the size in a member variable.
159   QRectF _sceneRect;
160   int _firstLineRow; // the first row to display (aka: not a daychange msg)
161   void updateSceneRect(qreal width);
162   inline void updateSceneRect() { updateSceneRect(_sceneRect.width()); }
163   void updateSceneRect(const QRectF &rect);
164   qreal _viewportHeight;
165
166   ColumnHandleItem *_firstColHandle, *_secondColHandle;
167   qreal _firstColHandlePos, _secondColHandlePos;
168   CutoffMode _cutoffMode;
169
170   ChatItem *_selectingItem;
171   int _selectionStartCol, _selectionMinCol;
172   int _selectionStart;
173   int _selectionEnd;
174   int _firstSelectionRow;
175   bool _isSelecting;
176
177   bool _showWebPreview;
178
179   QTimer _clickTimer;
180   ClickMode _clickMode;
181   QPointF _clickPos;
182   bool _clickHandled;
183   bool _leftButtonPressed;
184
185   struct WebPreview {
186     ChatItem *parentItem;
187     QGraphicsItem *previewItem;
188     QString url;
189     QRectF urlRect;
190     QTimer delayTimer;
191     QTimer deleteTimer;
192     WebPreview() : parentItem(0), previewItem(0) {}
193   };
194   WebPreview webPreview;
195 };
196
197 #endif