1 /***************************************************************************
2 * Copyright (C) 2005-08 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 ***************************************************************************/
21 #include <QApplication>
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPersistentModelIndex>
28 #include "chatlinemodelitem.h"
29 #include "chatscene.h"
31 #include "clientbacklogmanager.h"
32 #include "columnhandleitem.h"
33 #include "messagefilter.h"
35 #include "qtuisettings.h"
37 const qreal minContentsWidth = 200;
39 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, qreal width, QObject *parent)
40 : QGraphicsScene(0, 0, width, 0, parent),
43 _singleBufferScene(false),
49 MessageFilter *filter = qobject_cast<MessageFilter*>(model);
51 _singleBufferScene = filter->isSingleBufferFilter();
55 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
56 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
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();
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 &)));
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 &)));
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)));
82 if(model->rowCount() > 0)
83 rowsInserted(QModelIndex(), 0, model->rowCount() - 1);
86 ChatScene::~ChatScene() {
89 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
92 qreal y = sceneRect().y();
93 qreal width = sceneRect().width();
95 bool atBottom = false;
97 bool hasWidth = (width != 0);
100 y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
103 if(start == _lines.count())
106 for(int i = end; i >= start; i--) {
107 ChatLine *line = new ChatLine(i, model());
108 _lines.insert(start, line);
112 h -= line->setGeometry(width);
113 line->setPos(0, y+h);
115 line->setPos(0, y+h);
116 h += line->setGeometry(width);
121 // update existing items
122 for(int i = end+1; i < _lines.count(); i++) {
123 _lines[i]->setRow(i);
127 if(_selectionStart >= 0) {
128 int offset = end - start + 1;
129 if(_selectionStart >= start) _selectionStart += offset;
130 if(_selectionEnd >= start) _selectionEnd += offset;
131 if(_firstSelectionRow >= start) _firstSelectionRow += offset;
132 if(_lastSelectionRow >= start) _lastSelectionRow += offset;
135 // neither pre- or append means we have to do dirty work: move items...
136 if(!(atTop || atBottom)) {
139 int moveEnd = _lines.count() - 1;
141 if(end > _lines.count() - end) {
150 for(int i = moveStart; i <= moveEnd; i++) {
152 line->setPos(0, line->pos().y() + offset);
157 if(atTop || moveTop) {
158 setSceneRect(sceneRect().adjusted(0, h, 0, 0));
160 setSceneRect(sceneRect().adjusted(0, 0, 0, h));
161 emit sceneHeightChanged(h);
166 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
169 qreal h = 0; // total height of removed items;
171 bool atTop = (start == 0);
172 bool atBottom = (end == _lines.count() - 1);
173 bool moveTop = false;
175 // remove items from scene
176 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
177 int lineCount = start;
178 while(lineIter != _lines.end() && lineCount <= end) {
179 h += (*lineIter)->height();
181 lineIter = _lines.erase(lineIter);
185 // update rows of remaining chatlines
186 for(int i = start; i < _lines.count(); i++) {
187 _lines.at(i)->setRow(i);
191 if(_selectionStart >= 0) {
192 int offset = end - start + 1;
193 if(_selectionStart >= start)
194 _selectionStart -= offset;
195 if(_selectionEnd >= start)
196 _selectionEnd -= offset;
197 if(_firstSelectionRow >= start)
198 _firstSelectionRow -= offset;
199 if(_lastSelectionRow >= start)
200 _lastSelectionRow -= offset;
203 // neither removing at bottom or top means we have to move items...
204 if(!(atTop || atBottom)) {
207 int moveEnd = _lines.count() - 1;
209 if(start > _lines.count() - end) {
218 for(int i = moveStart; i <= moveEnd; i++) {
220 line->setPos(0, line->pos().y() + offset);
225 if(atTop || moveTop) {
226 setSceneRect(sceneRect().adjusted(0, h, 0, 0));
228 setSceneRect(sceneRect().adjusted(0, 0, 0, -h));
233 void ChatScene::setWidth(qreal width, bool forceReposition) {
234 if(width == sceneRect().width() && !forceReposition)
237 qreal oldHeight = sceneRect().height();
238 qreal y = sceneRect().y();
241 foreach(ChatLine *line, _lines) {
242 line->setPos(0, linePos);
243 linePos += line->setGeometry(width);
246 qreal height = linePos - y;
248 setSceneRect(QRectF(0, y, width, height));
251 qreal dh = height - oldHeight;
253 emit sceneHeightChanged(dh);
256 void ChatScene::handlePositionChanged(qreal xpos) {
257 bool first = (sender() == firstColHandle);
260 oldx = firstColHandlePos;
261 firstColHandlePos = xpos;
263 oldx = secondColHandlePos;
264 secondColHandlePos = xpos;
267 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
268 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
269 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
270 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
272 setWidth(width(), true); // readjust all chatlines
273 // we get ugly redraw errors if we don't update this explicitly... :(
274 // width() should be the same for both handles, so just use firstColHandle regardless
275 //update(qMin(oldx, xpos), 0, qMax(oldx, xpos) + firstColHandle->width(), height());
278 void ChatScene::setHandleXLimits() {
279 firstColHandle->setXLimits(0, secondColumnHandleRect().left());
280 secondColHandle->setXLimits(firstColumnHandleRect().right(), width() - minContentsWidth);
283 void ChatScene::setSelectingItem(ChatItem *item) {
284 if(_selectingItem) _selectingItem->clearSelection();
285 _selectingItem = item;
288 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
289 _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
290 _selectionStartCol = _selectionMinCol = item->column();
292 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
293 updateSelection(item->mapToScene(itemPos));
296 void ChatScene::updateSelection(const QPointF &pos) {
297 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
298 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
299 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColumnHandleRect().right() + 1, pos.y())));
300 if(!contentItem) return;
302 int curRow = contentItem->row();
304 if(pos.x() > secondColumnHandleRect().right()) curColumn = ChatLineModel::ContentsColumn;
305 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
306 else curColumn = ChatLineModel::TimestampColumn;
308 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
309 if(minColumn != _selectionMinCol) {
310 _selectionMinCol = minColumn;
311 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
312 _lines[l]->setSelected(true, minColumn);
315 int newstart = qMin(curRow, _firstSelectionRow);
316 int newend = qMax(curRow, _firstSelectionRow);
317 if(newstart < _selectionStart) {
318 for(int l = newstart; l < _selectionStart; l++)
319 _lines[l]->setSelected(true, minColumn);
321 if(newstart > _selectionStart) {
322 for(int l = _selectionStart; l < newstart; l++)
323 _lines[l]->setSelected(false);
325 if(newend > _selectionEnd) {
326 for(int l = _selectionEnd+1; l <= newend; l++)
327 _lines[l]->setSelected(true, minColumn);
329 if(newend < _selectionEnd) {
330 for(int l = newend+1; l <= _selectionEnd; l++)
331 _lines[l]->setSelected(false);
334 _selectionStart = newstart;
335 _selectionEnd = newend;
336 _lastSelectionRow = curRow;
338 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
339 if(!_selectingItem) {
340 qWarning() << "WARNING: ChatScene::updateSelection() has a null _selectingItem, this should never happen! Please report.";
343 _lines[curRow]->setSelected(false);
344 _isSelecting = false;
345 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
349 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
350 if(_isSelecting && event->buttons() == Qt::LeftButton) {
351 updateSelection(event->scenePos());
354 QGraphicsScene::mouseMoveEvent(event);
358 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
359 if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
360 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
361 _lines[l]->setSelected(false);
363 _selectionStart = -1;
364 QGraphicsScene::mousePressEvent(event); // so we can start a new local selection
366 QGraphicsScene::mousePressEvent(event);
370 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
371 if(_isSelecting && !event->buttons() & Qt::LeftButton) {
372 putToClipboard(selectionToString());
373 _isSelecting = false;
376 QGraphicsScene::mouseReleaseEvent(event);
380 void ChatScene::putToClipboard(const QString &selection) {
381 // TODO Configure clipboards
383 QApplication::clipboard()->setText(selection, QClipboard::Selection);
386 QApplication::clipboard()->setText(selection);
390 //!\brief Convert current selection to human-readable string.
391 QString ChatScene::selectionToString() const {
392 //TODO Make selection format configurable!
393 if(!_isSelecting) return QString();
394 int start = qMin(_selectionStart, _selectionEnd);
395 int end = qMax(_selectionStart, _selectionEnd);
396 if(start < 0 || end >= _lines.count()) {
397 qDebug() << "Invalid selection range:" << start << end;
401 for(int l = start; l <= end; l++) {
402 if(_selectionMinCol == ChatLineModel::TimestampColumn)
403 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
404 if(_selectionMinCol <= ChatLineModel::SenderColumn)
405 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
406 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
411 void ChatScene::requestBacklog() {
412 static const int REQUEST_COUNT = 50;
413 int backlogSize = model()->rowCount();
414 if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
415 QModelIndex msgIdx = model()->index(0, 0);
416 MsgId msgId = model()->data(msgIdx, ChatLineModel::MsgIdRole).value<MsgId>();
417 BufferId bufferId = model()->data(msgIdx, ChatLineModel::BufferIdRole).value<BufferId>();
418 _lastBacklogSize = backlogSize;
419 Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
423 int ChatScene::sectionByScenePos(int x) {
424 if(x < firstColHandle->x())
425 return ChatLineModel::TimestampColumn;
426 if(x < secondColHandle->x())
427 return ChatLineModel::SenderColumn;
429 return ChatLineModel::ContentsColumn;