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