681996a55cec169b75d76c8f52a91ea49fb29d95
[quassel.git] / src / qtui / chatscene.cpp
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 #include <QApplication>
22 #include <QClipboard>
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPersistentModelIndex>
25
26 #include "chatitem.h"
27 #include "chatline.h"
28 #include "chatlinemodelitem.h"
29 #include "chatscene.h"
30 #include "client.h"
31 #include "clientbacklogmanager.h"
32 #include "columnhandleitem.h"
33 #include "messagefilter.h"
34 #include "qtui.h"
35 #include "qtuisettings.h"
36
37 const qreal minContentsWidth = 200;
38
39 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
40   : QGraphicsScene(parent),
41     _idString(idString),
42     _width(0),
43     _height(0),
44     _model(model),
45     _singleBufferScene(false),
46     _selectingItem(0),
47     _selectionStart(-1),
48     _isSelecting(false),
49     _fetchingBacklog(false)
50 {
51   MessageFilter *filter = qobject_cast<MessageFilter*>(model);
52   if(filter) {
53     _singleBufferScene = filter->isSingleBufferFilter();
54   }
55
56   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
57
58   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
59   connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
60   for(int i = 0; i < model->rowCount(); i++) {
61     ChatLine *line = new ChatLine(i, model);
62     _lines.append(line);
63     addItem(line);
64   }
65
66   QtUiSettings s;
67   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
68   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
69
70   firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
71                                defaultFirstColHandlePos).toInt();
72   secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
73                                 defaultSecondColHandlePos).toInt();
74
75   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
76   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
77
78   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
79   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
80
81   firstColHandle->setXPos(firstColHandlePos);
82   firstColHandle->setXLimits(0, secondColHandlePos);
83   secondColHandle->setXPos(secondColHandlePos);
84   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
85
86   emit heightChanged(height());
87 }
88
89 ChatScene::~ChatScene() {
90
91 }
92
93 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
94   Q_UNUSED(index);
95   // maybe make this more efficient by prepending stuff with negative yval
96   // dunno if that's worth not guranteeing that 0 is on the top...
97   // TODO bulk inserts, iterators
98   qreal h = 0;
99   qreal y = 0;
100   if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
101   for(int i = start; i <= end; i++) {
102     ChatLine *line = new ChatLine(i, model());
103     _lines.insert(i, line);
104     addItem(line);
105     if(_width > 0) {
106       line->setPos(0, y+h);
107       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
108     }
109   }
110   // update existing items
111   for(int i = end+1; i < _lines.count(); i++) {
112     _lines[i]->setRow(i);
113   }
114
115   // update selection
116   if(_selectionStart >= 0) {
117     int offset = end - start + 1;
118     if(_selectionStart >= start) _selectionStart += offset;
119     if(_selectionEnd >= start) _selectionEnd += offset;
120     if(_firstSelectionRow >= start) _firstSelectionRow += offset;
121     if(_lastSelectionRow >= start) _lastSelectionRow += offset;
122   }
123
124   if(h > 0) {
125     _height += h;
126     for(int i = end+1; i < _lines.count(); i++) {
127       _lines.value(i)->moveBy(0, h);
128     }
129     setSceneRect(QRectF(0, 0, _width, _height));
130     emit heightChanged(_height);
131   }
132
133   requestBacklogIfNeeded();
134 }
135
136 void ChatScene::modelReset() {
137   foreach(ChatLine *line, _lines) {
138     removeItem(line);
139     delete line;
140   }
141   _lines.clear();
142   setSceneRect(QRectF(0, 0, _width, 0));
143   emit heightChanged(0);
144 }
145
146 void ChatScene::setWidth(qreal w) {
147   _width = w;
148   _height = 0;
149   foreach(ChatLine *line, _lines) {
150     line->setPos(0, _height);
151     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
152   }
153   setSceneRect(QRectF(0, 0, w, _height));
154   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
155   emit heightChanged(_height);
156 }
157
158 void ChatScene::rectChanged(const QRectF &rect) {
159   firstColHandle->sceneRectChanged(rect);
160   secondColHandle->sceneRectChanged(rect);
161 }
162
163 void ChatScene::handlePositionChanged(qreal xpos) {
164   bool first = (sender() == firstColHandle);
165   qreal oldx;
166   if(first) {
167     oldx = firstColHandlePos;
168     firstColHandlePos = xpos;
169   } else {
170     oldx = secondColHandlePos;
171     secondColHandlePos = xpos;
172   }
173   QtUiSettings s;
174   s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
175   s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
176   s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
177   s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
178
179   setWidth(width());  // readjust all chatlines
180   // we get ugly redraw errors if we don't update this explicitly... :(
181   // width() should be the same for both handles, so just use firstColHandle regardless
182   update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
183 }
184
185 void ChatScene::setSelectingItem(ChatItem *item) {
186   if(_selectingItem) _selectingItem->clearSelection();
187   _selectingItem = item;
188 }
189
190 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
191   _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
192   _selectionStartCol = _selectionMinCol = item->column();
193   _isSelecting = true;
194   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
195   updateSelection(item->mapToScene(itemPos));
196 }
197
198 void ChatScene::updateSelection(const QPointF &pos) {
199   // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
200   // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
201   ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
202   if(!contentItem) return;
203
204   int curRow = contentItem->row();
205   int curColumn;
206   if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
207   else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
208   else curColumn = ChatLineModel::TimestampColumn;
209
210   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
211   if(minColumn != _selectionMinCol) {
212     _selectionMinCol = minColumn;
213     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
214       _lines[l]->setSelected(true, minColumn);
215     }
216   }
217   int newstart = qMin(curRow, _firstSelectionRow);
218   int newend = qMax(curRow, _firstSelectionRow);
219   if(newstart < _selectionStart) {
220     for(int l = newstart; l < _selectionStart; l++)
221       _lines[l]->setSelected(true, minColumn);
222   }
223   if(newstart > _selectionStart) {
224     for(int l = _selectionStart; l < newstart; l++)
225       _lines[l]->setSelected(false);
226   }
227   if(newend > _selectionEnd) {
228     for(int l = _selectionEnd+1; l <= newend; l++)
229       _lines[l]->setSelected(true, minColumn);
230   }
231   if(newend < _selectionEnd) {
232     for(int l = newend+1; l <= _selectionEnd; l++)
233       _lines[l]->setSelected(false);
234   }
235
236   _selectionStart = newstart;
237   _selectionEnd = newend;
238   _lastSelectionRow = curRow;
239
240   if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
241     _lines[curRow]->setSelected(false);
242     _isSelecting = false;
243     Q_ASSERT(_selectingItem); // this seems to not always be true, but I have no idea why
244                               // adding this assert to make sure the occasional segfault is caused by this
245     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
246   }
247 }
248
249 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
250   if(_isSelecting && event->buttons() == Qt::LeftButton) {
251     updateSelection(event->scenePos());
252     event->accept();
253   } else {
254     QGraphicsScene::mouseMoveEvent(event);
255   }
256 }
257
258 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
259   if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
260     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
261       _lines[l]->setSelected(false);
262     }
263     _selectionStart = -1;
264     event->accept();
265   } else {
266     QGraphicsScene::mousePressEvent(event);
267   }
268 }
269
270 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
271   if(_isSelecting && !event->buttons() & Qt::LeftButton) {
272     putToClipboard(selectionToString());
273     _isSelecting = false;
274     event->accept();
275   } else {
276     QGraphicsScene::mouseReleaseEvent(event);
277   }
278 }
279
280 void ChatScene::putToClipboard(const QString &selection) {
281   // TODO Configure clipboards
282 #   ifdef Q_WS_X11
283   QApplication::clipboard()->setText(selection, QClipboard::Selection);
284 #   endif
285 //# else
286   QApplication::clipboard()->setText(selection);
287 //# endif
288 }
289
290 //!\brief Convert current selection to human-readable string.
291 QString ChatScene::selectionToString() const {
292   //TODO Make selection format configurable!
293   if(!_isSelecting) return QString();
294   int start = qMin(_selectionStart, _selectionEnd);
295   int end = qMax(_selectionStart, _selectionEnd);
296   if(start < 0 || end >= _lines.count()) {
297     qDebug() << "Invalid selection range:" << start << end;
298     return QString();
299   }
300   QString result;
301   for(int l = start; l <= end; l++) {
302     if(_selectionMinCol == ChatLineModel::TimestampColumn)
303       result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
304     if(_selectionMinCol <= ChatLineModel::SenderColumn)
305       result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
306     result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
307   }
308   return result;
309 }
310
311 void ChatScene::setIsFetchingBacklog(bool fetch) {
312   if(!isBacklogFetchingEnabled()) return;
313
314   if(!fetch) {
315     _fetchingBacklog = false;
316   } else {
317     _fetchingBacklog = true;
318     requestBacklogIfNeeded();
319   }
320 }
321
322 void ChatScene::requestBacklogIfNeeded() {
323   const int REQUEST_COUNT = 50;
324
325   if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
326
327   MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
328   if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
329     Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
330     _lastBacklogOffset = msgId;
331     _lastBacklogSize = model()->rowCount();
332   }
333 }
334
335 int ChatScene::sectionByScenePos(int x) {
336   if(x < firstColHandlePos)
337     return ChatLineModel::TimestampColumn;
338   if(x < secondColHandlePos)
339     return ChatLineModel::SenderColumn;
340
341   return ChatLineModel::ContentsColumn;
342 }