Allow reloading stylesheets at runtime
[quassel.git] / src / qtui / chatscene.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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     DragStartClick,
66     SingleClick,
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 MessageFilter *filter() const { return qobject_cast<MessageFilter*>(_model); }
76   inline QString idString() const { return _idString; }
77
78   int rowByScenePos(qreal y) const;
79   inline int rowByScenePos(const QPointF &pos) const { return rowByScenePos(pos.y()); }
80   ChatLineModel::ColumnType columnByScenePos(qreal x) const ;
81   inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) const { return columnByScenePos(pos.x()); }
82
83   ChatView *chatView() const;
84   ChatItem *chatItemAt(const QPointF &pos) const;
85   inline ChatLine *chatLine(int row) { return (row < _lines.count()) ? _lines[row] : 0; }
86
87   inline bool isSingleBufferScene() const { return _singleBufferId.isValid(); }
88   inline BufferId singleBufferId() const { return _singleBufferId; }
89   bool containsBuffer(const BufferId &id) const;
90
91   ColumnHandleItem *firstColumnHandle() const;
92   ColumnHandleItem *secondColumnHandle() const;
93
94   inline CutoffMode senderCutoffMode() const { return _cutoffMode; }
95   inline void setSenderCutoffMode(CutoffMode mode) { _cutoffMode = mode; }
96
97   QString selection() const;
98   bool hasSelection() const;
99   bool hasGlobalSelection() const;
100   bool isPosOverSelection(const QPointF &) const;
101   bool isGloballySelecting() const;
102   void initiateDrag(QWidget *source);
103
104   bool isScrollingAllowed() const;
105
106   virtual bool event(QEvent *e);
107
108  public slots:
109   void updateForViewport(qreal width, qreal height);
110   void setWidth(qreal width);
111   void layout(qreal width = -1);
112
113   // these are used by the chatitems to notify the scene and manage selections
114   void setSelectingItem(ChatItem *item);
115   ChatItem *selectingItem() const { return _selectingItem; }
116   void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
117   void clearGlobalSelection();
118   void clearSelection();
119   void selectionToClipboard(QClipboard::Mode = QClipboard::Clipboard);
120   void stringToClipboard(const QString &str, QClipboard::Mode = QClipboard::Clipboard);
121
122   void requestBacklog();
123
124 #ifdef HAVE_WEBKIT
125   void loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect);
126   void clearWebPreview(ChatItem *parentItem = 0);
127 #endif
128
129 signals:
130   void lastLineChanged(QGraphicsItem *item, qreal offset);
131   void layoutChanged(); // indicates changes to the scenerect due to resizing of the contentsitems
132   void mouseMoveWhileSelecting(const QPointF &scenePos);
133
134 protected:
135   virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent);
136   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
137   virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
138   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
139   virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
140   virtual void handleClick(Qt::MouseButton button, const QPointF &scenePos);
141
142 protected slots:
143   void rowsInserted(const QModelIndex &, int, int);
144   void rowsAboutToBeRemoved(const QModelIndex &, int, int);
145
146 private slots:
147   void firstHandlePositionChanged(qreal xpos);
148   void secondHandlePositionChanged(qreal xpos);
149 #ifdef HAVE_WEBKIT
150   void webPreviewNextStep();
151 #endif
152   void showWebPreviewChanged();
153
154   void clickTimeout();
155
156 private:
157   void setHandleXLimits();
158   void updateSelection(const QPointF &pos);
159
160   ChatView *_chatView;
161   QString _idString;
162   QAbstractItemModel *_model;
163   QList<ChatLine *> _lines;
164   BufferId _singleBufferId;
165
166   // calls to QChatScene::sceneRect() are very expensive. As we manage the scenerect ourselves
167   // we store the size in a member variable.
168   QRectF _sceneRect;
169   int _firstLineRow; // the first row to display (aka: not a daychange msg)
170   void updateSceneRect(qreal width);
171   inline void updateSceneRect() { updateSceneRect(_sceneRect.width()); }
172   void updateSceneRect(const QRectF &rect);
173   qreal _viewportHeight;
174
175   ColumnHandleItem *_firstColHandle, *_secondColHandle;
176   qreal _firstColHandlePos, _secondColHandlePos;
177   CutoffMode _cutoffMode;
178
179   ChatItem *_selectingItem;
180   int _selectionStartCol, _selectionMinCol;
181   int _selectionStart;
182   int _selectionEnd;
183   int _firstSelectionRow;
184   bool _isSelecting;
185
186   QTimer _clickTimer;
187   ClickMode _clickMode;
188   QPointF _clickPos;
189   bool _clickHandled;
190   bool _leftButtonPressed;
191
192   bool _showWebPreview;
193
194 #ifdef HAVE_WEBKIT
195   struct WebPreview {
196     enum PreviewState {
197       NoPreview,
198       NewPreview,
199       DelayPreview,
200       ShowPreview,
201       HidePreview
202     };
203     ChatItem *parentItem;
204     QGraphicsItem *previewItem;
205     QString url;
206     QRectF urlRect;
207     PreviewState previewState;
208     QTimer timer;
209     WebPreview() : parentItem(0), previewItem(0), previewState(NoPreview) {}
210   };
211   WebPreview webPreview;
212 #endif // HAVE_WEBKIT
213 };
214
215 #endif