3a87e87fbeddd13e6903936e16a82a7ea9b0f60c
[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, qreal width, QObject *parent)
40   : QGraphicsScene(0, 0, width, 0, parent),
41     _idString(idString),
42     _model(model),
43     _singleBufferScene(false),
44     _selectingItem(0),
45     _selectionStart(-1),
46     _isSelecting(false),
47     _lastBacklogSize(0)
48 {
49   MessageFilter *filter = qobject_cast<MessageFilter*>(model);
50   if(filter) {
51     _singleBufferScene = filter->isSingleBufferFilter();
52   }
53
54   QtUiSettings s;
55   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
56   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
57
58   firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
59                                defaultFirstColHandlePos).toInt();
60   secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
61                                 defaultSecondColHandlePos).toInt();
62
63   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
64   addItem(firstColHandle);
65   firstColHandle->setXPos(firstColHandlePos);
66   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
67   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), firstColHandle, SLOT(sceneRectChanged(const QRectF &)));
68
69   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
70   addItem(secondColHandle);
71   secondColHandle->setXPos(secondColHandlePos);
72   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
73   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), secondColHandle, SLOT(sceneRectChanged(const QRectF &)));
74
75   setHandleXLimits();
76
77   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
78           this, SLOT(rowsInserted(const QModelIndex &, int, int)));
79   connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
80           this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
81
82   if(model->rowCount() > 0)
83     rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
84 }
85
86 ChatScene::~ChatScene() {
87 }
88
89 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
90   Q_UNUSED(index);
91   qreal h = 0;
92   qreal y = sceneRect().y();
93   qreal width = sceneRect().width();
94   bool atTop = true;
95   bool atBottom = false;
96   bool hasWidth = (width != 0);
97
98   if(start > 0) {
99     y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
100     atTop = false;
101   }
102   if(start == _lines.count())
103     atBottom = true;
104
105   // right now this method doesn't properly handle messages that are inserted at an arbitrary point
106   Q_ASSERT(atBottom || atTop);
107
108   for(int i = end; i >= start; i--) {
109     ChatLine *line = new ChatLine(i, model());
110     _lines.insert(start, line);
111     addItem(line);
112     if(hasWidth) {
113       if(atTop) {
114         h -= line->setGeometry(width);
115         line->setPos(0, y+h);
116       } else {
117         line->setPos(0, y+h);
118         h += line->setGeometry(width);
119       }
120     }
121   }
122
123   // update existing items
124   for(int i = end+1; i < _lines.count(); i++) {
125     _lines[i]->setRow(i);
126   }
127
128   // update sceneRect
129   if(atBottom) {
130     setSceneRect(sceneRect().adjusted(0, 0, 0, h));
131     emit sceneHeightChanged(h);
132   } else {
133     setSceneRect(sceneRect().adjusted(0, h, 0, 0));
134   }
135 }
136
137 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
138   Q_UNUSED(parent);
139
140   qreal h = 0; // total height of removed items;
141
142   bool atTop = (start == 0);
143   bool atBottom = (end == _lines.count() - 1);
144
145   // right now this method doesn't properly handle messages that are removed at an arbitrary point
146   Q_ASSERT(atBottom || atTop);
147
148   // remove items from scene
149   QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
150   int lineCount = start;
151   while(lineIter != _lines.end() && lineCount <= end) {
152     h += (*lineIter)->height();
153     delete *lineIter;
154     lineIter = _lines.erase(lineIter);
155     lineCount++;
156   }
157
158   // update rows of remaining chatlines
159   for(int i = start; i < _lines.count(); i++) {
160     _lines.at(i)->setRow(i);
161   }
162
163   // update sceneRect
164   if(atBottom) {
165     setSceneRect(sceneRect().adjusted(0, 0, 0, -h));
166   } else {
167     setSceneRect(sceneRect().adjusted(0, h, 0, 0));
168   }
169 }
170
171 void ChatScene::setWidth(qreal width, bool forceReposition) {
172   if(width == sceneRect().width() && !forceReposition)
173     return;
174
175   qreal oldHeight = sceneRect().height();
176   qreal y = sceneRect().y();
177   qreal linePos = y;
178
179   foreach(ChatLine *line, _lines) {
180     line->setPos(0, linePos);
181     linePos += line->setGeometry(width);
182   }
183
184   qreal height = linePos - y;
185
186   setSceneRect(QRectF(0, y, width, height));
187   setHandleXLimits();
188
189   qreal dh = height - oldHeight;
190   if(dh > 0)
191     emit sceneHeightChanged(dh);
192 }
193
194 void ChatScene::handlePositionChanged(qreal xpos) {
195   bool first = (sender() == firstColHandle);
196   qreal oldx;
197   if(first) {
198     oldx = firstColHandlePos;
199     firstColHandlePos = xpos;
200   } else {
201     oldx = secondColHandlePos;
202     secondColHandlePos = xpos;
203   }
204   QtUiSettings s;
205   s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
206   s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
207   s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
208   s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
209
210   setWidth(width(), true);  // readjust all chatlines
211   // we get ugly redraw errors if we don't update this explicitly... :(
212   // width() should be the same for both handles, so just use firstColHandle regardless
213   //update(qMin(oldx, xpos), 0, qMax(oldx, xpos) + firstColHandle->width(), height());
214 }
215
216 void ChatScene::setHandleXLimits() {
217   firstColHandle->setXLimits(0, secondColumnHandleRect().left());
218   secondColHandle->setXLimits(firstColumnHandleRect().right(), width() - minContentsWidth);
219 }
220
221 void ChatScene::setSelectingItem(ChatItem *item) {
222   if(_selectingItem) _selectingItem->clearSelection();
223   _selectingItem = item;
224 }
225
226 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
227   _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
228   _selectionStartCol = _selectionMinCol = item->column();
229   _isSelecting = true;
230   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
231   updateSelection(item->mapToScene(itemPos));
232 }
233
234 void ChatScene::updateSelection(const QPointF &pos) {
235   // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
236   // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
237   ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColumnHandleRect().right() + 1, pos.y())));
238   if(!contentItem) return;
239
240   int curRow = contentItem->row();
241   int curColumn;
242   if(pos.x() > secondColumnHandleRect().right()) curColumn = ChatLineModel::ContentsColumn;
243   else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
244   else curColumn = ChatLineModel::TimestampColumn;
245
246   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
247   if(minColumn != _selectionMinCol) {
248     _selectionMinCol = minColumn;
249     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
250       _lines[l]->setSelected(true, minColumn);
251     }
252   }
253   int newstart = qMin(curRow, _firstSelectionRow);
254   int newend = qMax(curRow, _firstSelectionRow);
255   if(newstart < _selectionStart) {
256     for(int l = newstart; l < _selectionStart; l++)
257       _lines[l]->setSelected(true, minColumn);
258   }
259   if(newstart > _selectionStart) {
260     for(int l = _selectionStart; l < newstart; l++)
261       _lines[l]->setSelected(false);
262   }
263   if(newend > _selectionEnd) {
264     for(int l = _selectionEnd+1; l <= newend; l++)
265       _lines[l]->setSelected(true, minColumn);
266   }
267   if(newend < _selectionEnd) {
268     for(int l = newend+1; l <= _selectionEnd; l++)
269       _lines[l]->setSelected(false);
270   }
271
272   _selectionStart = newstart;
273   _selectionEnd = newend;
274   _lastSelectionRow = curRow;
275
276   if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
277     if(!_selectingItem) {
278       qWarning() << "WARNING: ChatScene::updateSelection() has a null _selectingItem, this should never happen! Please report.";
279       return;
280     }
281     _lines[curRow]->setSelected(false);
282     _isSelecting = false;
283     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
284   }
285 }
286
287 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
288   if(_isSelecting && event->buttons() == Qt::LeftButton) {
289     updateSelection(event->scenePos());
290     event->accept();
291   } else {
292     QGraphicsScene::mouseMoveEvent(event);
293   }
294 }
295
296 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
297   if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
298     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
299       _lines[l]->setSelected(false);
300     }
301     _selectionStart = -1;
302     QGraphicsScene::mousePressEvent(event);  // so we can start a new local selection
303   } else {
304     QGraphicsScene::mousePressEvent(event);
305   }
306 }
307
308 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
309   if(_isSelecting && !event->buttons() & Qt::LeftButton) {
310     putToClipboard(selectionToString());
311     _isSelecting = false;
312     event->accept();
313   } else {
314     QGraphicsScene::mouseReleaseEvent(event);
315   }
316 }
317
318 void ChatScene::putToClipboard(const QString &selection) {
319   // TODO Configure clipboards
320 #   ifdef Q_WS_X11
321   QApplication::clipboard()->setText(selection, QClipboard::Selection);
322 #   endif
323 //# else
324   QApplication::clipboard()->setText(selection);
325 //# endif
326 }
327
328 //!\brief Convert current selection to human-readable string.
329 QString ChatScene::selectionToString() const {
330   //TODO Make selection format configurable!
331   if(!_isSelecting) return QString();
332   int start = qMin(_selectionStart, _selectionEnd);
333   int end = qMax(_selectionStart, _selectionEnd);
334   if(start < 0 || end >= _lines.count()) {
335     qDebug() << "Invalid selection range:" << start << end;
336     return QString();
337   }
338   QString result;
339   for(int l = start; l <= end; l++) {
340     if(_selectionMinCol == ChatLineModel::TimestampColumn)
341       result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
342     if(_selectionMinCol <= ChatLineModel::SenderColumn)
343       result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
344     result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
345   }
346   return result;
347 }
348
349 void ChatScene::requestBacklog() {
350   static const int REQUEST_COUNT = 50;
351   int backlogSize = model()->rowCount();
352   if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
353     QModelIndex msgIdx = model()->index(0, 0);
354     MsgId msgId = model()->data(msgIdx, ChatLineModel::MsgIdRole).value<MsgId>();
355     BufferId bufferId = model()->data(msgIdx, ChatLineModel::BufferIdRole).value<BufferId>();
356     _lastBacklogSize = backlogSize;
357     Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
358   }
359 }
360
361 int ChatScene::sectionByScenePos(int x) {
362   if(x < firstColHandle->x())
363     return ChatLineModel::TimestampColumn;
364   if(x < secondColHandle->x())
365     return ChatLineModel::SenderColumn;
366
367   return ChatLineModel::ContentsColumn;
368 }