1 /***************************************************************************
2 * Copyright (C) 2005-2010 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
24 #include <QAbstractItemModel>
26 #include <QGraphicsItem>
27 #include <QGraphicsScene>
31 #include "chatlinemodel.h"
32 #include "messagefilter.h"
38 class ColumnHandleItem;
42 class QGraphicsSceneMouseEvent;
44 class ChatScene : public QGraphicsScene {
54 ChatLineType = QGraphicsItem::UserType + 1,
56 TimestampChatItemType,
73 ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, ChatView *parent);
76 inline QAbstractItemModel *model() const { return _model; }
77 inline MessageFilter *filter() const { return qobject_cast<MessageFilter*>(_model); }
78 inline QString idString() const { return _idString; }
80 int rowByScenePos(qreal y) const;
81 inline int rowByScenePos(const QPointF &pos) const { return rowByScenePos(pos.y()); }
82 ChatLineModel::ColumnType columnByScenePos(qreal x) const ;
83 inline ChatLineModel::ColumnType columnByScenePos(const QPointF &pos) const { return columnByScenePos(pos.x()); }
85 ChatView *chatView() const;
86 ChatItem *chatItemAt(const QPointF &pos) const;
87 inline ChatLine *chatLine(int row) const { return (row < _lines.count()) ? _lines.value(row) : 0; }
88 inline ChatLine *chatLine(const QModelIndex &index) const { return _lines.value(index.row()); }
90 //! Find the ChatLine belonging to a MsgId
91 /** Searches for the ChatLine belonging to a MsgId. If there are more than one ChatLine with the same msgId,
92 * the first one is returned.
93 * Note that this method performs a binary search, hence it has as complexity of O(log n).
94 * If matchExact is false, and we don't have an exact match for the given msgId, we return the visible line right
95 * above the requested one.
96 * \param msgId The message ID to look for
97 * \param matchExact Whether we find only exact matches
98 * \param ignoreDayChange Whether we ignore day change messages
99 * \return The ChatLine corresponding to the given MsgId
101 ChatLine *chatLine(MsgId msgId, bool matchExact = true, bool ignoreDayChange = true) const;
103 inline ChatLine *lastLine() const { return _lines.count() ? _lines.last() : 0; }
105 inline MarkerLineItem *markerLine() const { return _markerLine; }
107 inline bool isSingleBufferScene() const { return _singleBufferId.isValid(); }
108 inline BufferId singleBufferId() const { return _singleBufferId; }
109 bool containsBuffer(const BufferId &id) const;
111 ColumnHandleItem *firstColumnHandle() const;
112 ColumnHandleItem *secondColumnHandle() const;
114 inline CutoffMode senderCutoffMode() const { return _cutoffMode; }
115 inline void setSenderCutoffMode(CutoffMode mode) { _cutoffMode = mode; }
117 QString selection() const;
118 bool hasSelection() const;
119 bool hasGlobalSelection() const;
120 bool isPosOverSelection(const QPointF &) const;
121 bool isGloballySelecting() const;
122 void initiateDrag(QWidget *source);
124 bool isScrollingAllowed() const;
127 void updateForViewport(qreal width, qreal height);
128 void setWidth(qreal width);
129 void layout(int start, int end, qreal width);
131 void setMarkerLineVisible(bool visible = true);
132 void setMarkerLine(MsgId msgId = MsgId());
133 void jumpToMarkerLine(bool requestBacklog);
135 // these are used by the chatitems to notify the scene and manage selections
136 void setSelectingItem(ChatItem *item);
137 ChatItem *selectingItem() const { return _selectingItem; }
138 void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
139 void clearGlobalSelection();
140 void clearSelection();
141 void selectionToClipboard(QClipboard::Mode = QClipboard::Clipboard);
142 void stringToClipboard(const QString &str, QClipboard::Mode = QClipboard::Clipboard);
144 void requestBacklog();
147 void loadWebPreview(ChatItem *parentItem, const QUrl &url, const QRectF &urlRect);
148 void clearWebPreview(ChatItem *parentItem = 0);
152 void lastLineChanged(QGraphicsItem *item, qreal offset);
153 void layoutChanged(); // indicates changes to the scenerect due to resizing of the contentsitems
154 void mouseMoveWhileSelecting(const QPointF &scenePos);
157 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *contextMenuEvent);
158 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
159 virtual void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
160 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
161 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *mouseEvent);
162 virtual void handleClick(Qt::MouseButton button, const QPointF &scenePos);
165 void rowsInserted(const QModelIndex &, int, int);
166 void rowsAboutToBeRemoved(const QModelIndex &, int, int);
167 void dataChanged(const QModelIndex &, const QModelIndex &);
170 void firstHandlePositionChanged(qreal xpos);
171 void secondHandlePositionChanged(qreal xpos);
173 void webPreviewNextStep();
175 void showWebPreviewChanged();
182 void setHandleXLimits();
183 void updateSelection(const QPointF &pos);
187 QAbstractItemModel *_model;
188 QList<ChatLine *> _lines;
189 BufferId _singleBufferId;
191 // calls to QChatScene::sceneRect() are very expensive. As we manage the scenerect ourselves
192 // we store the size in a member variable.
194 int _firstLineRow; // the first row to display (aka: not a daychange msg)
195 void updateSceneRect(qreal width);
196 inline void updateSceneRect() { updateSceneRect(_sceneRect.width()); }
197 void updateSceneRect(const QRectF &rect);
198 qreal _viewportHeight;
200 MarkerLineItem *_markerLine;
201 bool _markerLineVisible, _markerLineValid, _markerLineJumpPending;
203 ColumnHandleItem *_firstColHandle, *_secondColHandle;
204 qreal _firstColHandlePos, _secondColHandlePos;
205 CutoffMode _cutoffMode;
207 ChatItem *_selectingItem;
208 int _selectionStartCol, _selectionMinCol;
211 int _firstSelectionRow;
215 ClickMode _clickMode;
218 bool _leftButtonPressed;
220 bool _showWebPreview;
231 ChatItem *parentItem;
232 QGraphicsItem *previewItem;
235 PreviewState previewState;
237 WebPreview() : parentItem(0), previewItem(0), previewState(NoPreview) {}
239 WebPreview webPreview;
240 #endif // HAVE_WEBKIT