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