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, QObject *parent)
40 : QGraphicsScene(parent),
45 _singleBufferScene(false),
51 MessageFilter *filter = qobject_cast<MessageFilter*>(model);
53 _singleBufferScene = filter->isSingleBufferFilter();
56 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
58 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
59 this, SLOT(rowsInserted(const QModelIndex &, int, int)));
60 connect(model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int)),
61 this, SLOT(rowsAboutToBeRemoved(const QModelIndex &, int, int)));
63 for(int i = 0; i < model->rowCount(); i++) {
64 ChatLine *line = new ChatLine(i, model);
70 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
71 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
73 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
74 defaultFirstColHandlePos).toInt();
75 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
76 defaultSecondColHandlePos).toInt();
78 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator());
79 addItem(firstColHandle);
81 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator());
82 addItem(secondColHandle);
84 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
85 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
87 firstColHandle->setXPos(firstColHandlePos);
88 secondColHandle->setXPos(secondColHandlePos);
91 emit heightChanged(height());
94 ChatScene::~ChatScene() {
97 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
99 // maybe make this more efficient by prepending stuff with negative yval
100 // dunno if that's worth not guranteeing that 0 is on the top...
101 // TODO bulk inserts, iterators
104 if(_width && start > 0)
105 y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
107 for(int i = start; i <= end; i++) {
108 ChatLine *line = new ChatLine(i, model());
109 _lines.insert(i, line);
112 line->setPos(0, y+h);
113 h += line->setGeometry(_width);
116 // update existing items
117 for(int i = end+1; i < _lines.count(); i++) {
118 _lines[i]->setRow(i);
122 if(_selectionStart >= 0) {
123 int offset = end - start + 1;
124 if(_selectionStart >= start) _selectionStart += offset;
125 if(_selectionEnd >= start) _selectionEnd += offset;
126 if(_firstSelectionRow >= start) _firstSelectionRow += offset;
127 if(_lastSelectionRow >= start) _lastSelectionRow += offset;
132 for(int i = end+1; i < _lines.count(); i++) {
133 _lines.at(i)->moveBy(0, h);
135 setSceneRect(QRectF(0, 0, _width, _height));
136 emit heightChanged(_height);
140 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
143 qreal h = 0; // total height of removed items;
145 // remove items from scene
146 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
147 int lineCount = start;
148 while(lineIter != _lines.end() && lineCount <= end) {
149 h += (*lineIter)->height();
151 lineIter = _lines.erase(lineIter);
155 // update rows of remaining chatlines
156 for(int i = start; i < _lines.count(); i++) {
157 _lines.at(i)->setRow(i);
161 if(_selectionStart >= 0) {
162 int offset = end - start + 1;
163 if(_selectionStart >= start)
164 _selectionStart -= offset;
165 if(_selectionEnd >= start)
166 _selectionEnd -= offset;
167 if(_firstSelectionRow >= start)
168 _firstSelectionRow -= offset;
169 if(_lastSelectionRow >= start)
170 _lastSelectionRow -= offset;
173 // reposition remaining chatlines
175 Q_ASSERT(_height >= h);
177 for(int i = start; i < _lines.count(); i++) {
178 _lines.at(i)->moveBy(0, -h);
180 setSceneRect(QRectF(0, 0, _width, _height));
181 emit heightChanged(_height);
185 void ChatScene::setWidth(qreal w) {
188 foreach(ChatLine *line, _lines) {
189 line->setPos(0, _height);
190 _height += line->setGeometry(_width);
192 setSceneRect(QRectF(0, 0, w, _height));
194 emit heightChanged(_height);
197 void ChatScene::rectChanged(const QRectF &rect) {
198 firstColHandle->sceneRectChanged(rect);
199 secondColHandle->sceneRectChanged(rect);
202 void ChatScene::handlePositionChanged(qreal xpos) {
203 bool first = (sender() == firstColHandle);
206 oldx = firstColHandlePos;
207 firstColHandlePos = xpos;
209 oldx = secondColHandlePos;
210 secondColHandlePos = xpos;
213 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
214 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
215 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
216 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
218 setWidth(width()); // readjust all chatlines
219 // we get ugly redraw errors if we don't update this explicitly... :(
220 // width() should be the same for both handles, so just use firstColHandle regardless
221 //update(qMin(oldx, xpos), 0, qMax(oldx, xpos) + firstColHandle->width(), height());
224 void ChatScene::setHandleXLimits() {
225 firstColHandle->setXLimits(0, secondColumnHandleRect().left());
226 secondColHandle->setXLimits(firstColumnHandleRect().right(), width() - minContentsWidth);
229 void ChatScene::setSelectingItem(ChatItem *item) {
230 if(_selectingItem) _selectingItem->clearSelection();
231 _selectingItem = item;
234 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
235 _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
236 _selectionStartCol = _selectionMinCol = item->column();
238 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
239 updateSelection(item->mapToScene(itemPos));
242 void ChatScene::updateSelection(const QPointF &pos) {
243 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
244 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
245 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColumnHandleRect().right() + 1, pos.y())));
246 if(!contentItem) return;
248 int curRow = contentItem->row();
250 if(pos.x() > secondColumnHandleRect().right()) curColumn = ChatLineModel::ContentsColumn;
251 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
252 else curColumn = ChatLineModel::TimestampColumn;
254 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
255 if(minColumn != _selectionMinCol) {
256 _selectionMinCol = minColumn;
257 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
258 _lines[l]->setSelected(true, minColumn);
261 int newstart = qMin(curRow, _firstSelectionRow);
262 int newend = qMax(curRow, _firstSelectionRow);
263 if(newstart < _selectionStart) {
264 for(int l = newstart; l < _selectionStart; l++)
265 _lines[l]->setSelected(true, minColumn);
267 if(newstart > _selectionStart) {
268 for(int l = _selectionStart; l < newstart; l++)
269 _lines[l]->setSelected(false);
271 if(newend > _selectionEnd) {
272 for(int l = _selectionEnd+1; l <= newend; l++)
273 _lines[l]->setSelected(true, minColumn);
275 if(newend < _selectionEnd) {
276 for(int l = newend+1; l <= _selectionEnd; l++)
277 _lines[l]->setSelected(false);
280 _selectionStart = newstart;
281 _selectionEnd = newend;
282 _lastSelectionRow = curRow;
284 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
285 if(!_selectingItem) {
286 qWarning() << "WARNING: ChatScene::updateSelection() has a null _selectingItem, this should never happen! Please report.";
289 _lines[curRow]->setSelected(false);
290 _isSelecting = false;
291 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
295 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
296 if(_isSelecting && event->buttons() == Qt::LeftButton) {
297 updateSelection(event->scenePos());
300 QGraphicsScene::mouseMoveEvent(event);
304 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
305 if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
306 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
307 _lines[l]->setSelected(false);
309 _selectionStart = -1;
310 QGraphicsScene::mousePressEvent(event); // so we can start a new local selection
312 QGraphicsScene::mousePressEvent(event);
316 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
317 if(_isSelecting && !event->buttons() & Qt::LeftButton) {
318 putToClipboard(selectionToString());
319 _isSelecting = false;
322 QGraphicsScene::mouseReleaseEvent(event);
326 void ChatScene::putToClipboard(const QString &selection) {
327 // TODO Configure clipboards
329 QApplication::clipboard()->setText(selection, QClipboard::Selection);
332 QApplication::clipboard()->setText(selection);
336 //!\brief Convert current selection to human-readable string.
337 QString ChatScene::selectionToString() const {
338 //TODO Make selection format configurable!
339 if(!_isSelecting) return QString();
340 int start = qMin(_selectionStart, _selectionEnd);
341 int end = qMax(_selectionStart, _selectionEnd);
342 if(start < 0 || end >= _lines.count()) {
343 qDebug() << "Invalid selection range:" << start << end;
347 for(int l = start; l <= end; l++) {
348 if(_selectionMinCol == ChatLineModel::TimestampColumn)
349 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
350 if(_selectionMinCol <= ChatLineModel::SenderColumn)
351 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
352 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
357 void ChatScene::requestBacklog() {
358 static const int REQUEST_COUNT = 50;
359 int backlogSize = model()->rowCount();
360 if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
361 QModelIndex msgIdx = model()->index(0, 0);
362 MsgId msgId = model()->data(msgIdx, ChatLineModel::MsgIdRole).value<MsgId>();
363 BufferId bufferId = model()->data(msgIdx, ChatLineModel::BufferIdRole).value<BufferId>();
364 _lastBacklogSize = backlogSize;
365 Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
369 int ChatScene::sectionByScenePos(int x) {
370 if(x < firstColHandle->x())
371 return ChatLineModel::TimestampColumn;
372 if(x < secondColHandle->x())
373 return ChatLineModel::SenderColumn;
375 return ChatLineModel::ContentsColumn;