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