Search result highlights are now properly repositioned on resize
[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
96 protected:
97   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
98   virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
99   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
100
101 protected slots:
102   void rowsInserted(const QModelIndex &, int, int);
103   void rowsAboutToBeRemoved(const QModelIndex &, int, int);
104
105 private slots:
106   void firstHandlePositionChanged(qreal xpos);
107   void secondHandlePositionChanged(qreal xpos);
108   void showWebPreviewEvent();
109   void deleteWebPreviewEvent();
110
111 private:
112   void setHandleXLimits();
113   void updateSelection(const QPointF &pos);
114   QString selectionToString() const;
115
116   QString _idString;
117   QAbstractItemModel *_model;
118   QList<ChatLine *> _lines;
119   bool _singleBufferScene;
120
121   // calls to QChatScene::sceneRect() are very expensive. As we manage the scenerect ourselves
122   // we store the size in a member variable.
123   QRectF _sceneRect;
124   int _firstLineRow; // the first row to display (aka: not a daychange msg)
125   void updateSceneRect(qreal width);
126   inline void updateSceneRect() { updateSceneRect(_sceneRect.width()); }
127   void updateSceneRect(const QRectF &rect);
128   qreal _viewportHeight;
129
130   ColumnHandleItem *_firstColHandle, *_secondColHandle;
131   qreal _firstColHandlePos, _secondColHandlePos;
132   CutoffMode _cutoffMode;
133
134   ChatItem *_selectingItem;
135   int _selectionStartCol, _selectionMinCol;
136   int _selectionStart;
137   int _selectionEnd;
138   int _firstSelectionRow;
139   bool _isSelecting;
140
141   struct WebPreview {
142     ChatItem *parentItem;
143     QGraphicsItem *previewItem;
144     QString url;
145     QRectF urlRect;
146     QTimer delayTimer;
147     QTimer deleteTimer;
148     WebPreview() : parentItem(0), previewItem(0) {}
149   };
150   WebPreview webPreview;
151 };
152
153 bool ChatScene::containsBuffer(const BufferId &id) const {
154   MessageFilter *filter = qobject_cast<MessageFilter*>(model());
155   if(filter)
156     return filter->containsBuffer(id);
157   else
158     return false;
159 }
160
161 #endif