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());
92 emit heightChangedAt(0, height());
95 ChatScene::~ChatScene() {
98 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
100 // maybe make this more efficient by prepending stuff with negative yval
101 // dunno if that's worth not guranteeing that 0 is on the top...
102 // TODO bulk inserts, iterators
105 if(_width && start > 0)
106 y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
108 for(int i = start; i <= end; i++) {
109 ChatLine *line = new ChatLine(i, model());
110 _lines.insert(i, line);
113 line->setPos(0, y+h);
114 h += line->setGeometry(_width);
117 // update existing items
118 for(int i = end+1; i < _lines.count(); i++) {
119 _lines[i]->setRow(i);
123 if(_selectionStart >= 0) {
124 int offset = end - start + 1;
125 if(_selectionStart >= start) _selectionStart += offset;
126 if(_selectionEnd >= start) _selectionEnd += offset;
127 if(_firstSelectionRow >= start) _firstSelectionRow += offset;
128 if(_lastSelectionRow >= start) _lastSelectionRow += offset;
133 for(int i = end+1; i < _lines.count(); i++) {
134 _lines.at(i)->moveBy(0, h);
136 setSceneRect(QRectF(0, 0, _width, _height));
137 emit heightChanged(_height);
138 emit heightChangedAt(_lines.at(start)->y(), h);
142 void ChatScene::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) {
145 qreal h = 0; // total height of removed items;
147 // remove items from scene
148 QList<ChatLine *>::iterator lineIter = _lines.begin() + start;
149 int lineCount = start;
150 while(lineIter != _lines.end() && lineCount <= end) {
151 h += (*lineIter)->height();
153 lineIter = _lines.erase(lineIter);
157 // update rows of remaining chatlines
158 for(int i = start; i < _lines.count(); i++) {
159 _lines.at(i)->setRow(i);
163 if(_selectionStart >= 0) {
164 int offset = end - start + 1;
165 if(_selectionStart >= start)
166 _selectionStart -= offset;
167 if(_selectionEnd >= start)
168 _selectionEnd -= offset;
169 if(_firstSelectionRow >= start)
170 _firstSelectionRow -= offset;
171 if(_lastSelectionRow >= start)
172 _lastSelectionRow -= offset;
175 // reposition remaining chatlines
177 Q_ASSERT(_height >= h);
179 for(int i = start; i < _lines.count(); i++) {
180 _lines.at(i)->moveBy(0, -h);
182 setSceneRect(QRectF(0, 0, _width, _height));
183 emit heightChanged(_height);
184 Q_ASSERT(_lines.isEmpty() || (start < _lines.count())); // if _lines isn't empty it better contain start
185 qreal changePos = (_lines.isEmpty()) ? 0 : _lines.at(start)->y();
186 emit heightChangedAt(changePos, -h);
190 void ChatScene::setWidth(qreal w) {
191 qreal oldh = _height;
194 foreach(ChatLine *line, _lines) {
195 line->setPos(0, _height);
196 _height += line->setGeometry(_width);
198 setSceneRect(QRectF(0, 0, w, _height));
200 emit heightChanged(_height);
201 emit heightChangedAt(0, _height - oldh);
205 void ChatScene::rectChanged(const QRectF &rect) {
206 firstColHandle->sceneRectChanged(rect);
207 secondColHandle->sceneRectChanged(rect);
210 void ChatScene::handlePositionChanged(qreal xpos) {
211 bool first = (sender() == firstColHandle);
214 oldx = firstColHandlePos;
215 firstColHandlePos = xpos;
217 oldx = secondColHandlePos;
218 secondColHandlePos = xpos;
221 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
222 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
223 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
224 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
226 setWidth(width()); // readjust all chatlines
227 // we get ugly redraw errors if we don't update this explicitly... :(
228 // width() should be the same for both handles, so just use firstColHandle regardless
229 //update(qMin(oldx, xpos), 0, qMax(oldx, xpos) + firstColHandle->width(), height());
232 void ChatScene::setHandleXLimits() {
233 firstColHandle->setXLimits(0, secondColumnHandleRect().left());
234 secondColHandle->setXLimits(firstColumnHandleRect().right(), width() - minContentsWidth);
237 void ChatScene::setSelectingItem(ChatItem *item) {
238 if(_selectingItem) _selectingItem->clearSelection();
239 _selectingItem = item;
242 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
243 _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
244 _selectionStartCol = _selectionMinCol = item->column();
246 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
247 updateSelection(item->mapToScene(itemPos));
250 void ChatScene::updateSelection(const QPointF &pos) {
251 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
252 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
253 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColumnHandleRect().right() + 1, pos.y())));
254 if(!contentItem) return;
256 int curRow = contentItem->row();
258 if(pos.x() > secondColumnHandleRect().right()) curColumn = ChatLineModel::ContentsColumn;
259 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
260 else curColumn = ChatLineModel::TimestampColumn;
262 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
263 if(minColumn != _selectionMinCol) {
264 _selectionMinCol = minColumn;
265 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
266 _lines[l]->setSelected(true, minColumn);
269 int newstart = qMin(curRow, _firstSelectionRow);
270 int newend = qMax(curRow, _firstSelectionRow);
271 if(newstart < _selectionStart) {
272 for(int l = newstart; l < _selectionStart; l++)
273 _lines[l]->setSelected(true, minColumn);
275 if(newstart > _selectionStart) {
276 for(int l = _selectionStart; l < newstart; l++)
277 _lines[l]->setSelected(false);
279 if(newend > _selectionEnd) {
280 for(int l = _selectionEnd+1; l <= newend; l++)
281 _lines[l]->setSelected(true, minColumn);
283 if(newend < _selectionEnd) {
284 for(int l = newend+1; l <= _selectionEnd; l++)
285 _lines[l]->setSelected(false);
288 _selectionStart = newstart;
289 _selectionEnd = newend;
290 _lastSelectionRow = curRow;
292 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
293 if(!_selectingItem) {
294 qWarning() << "WARNING: ChatScene::updateSelection() has a null _selectingItem, this should never happen! Please report.";
297 _lines[curRow]->setSelected(false);
298 _isSelecting = false;
299 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
303 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
304 if(_isSelecting && event->buttons() == Qt::LeftButton) {
305 updateSelection(event->scenePos());
308 QGraphicsScene::mouseMoveEvent(event);
312 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
313 if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
314 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
315 _lines[l]->setSelected(false);
317 _selectionStart = -1;
318 QGraphicsScene::mousePressEvent(event); // so we can start a new local selection
320 QGraphicsScene::mousePressEvent(event);
324 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
325 if(_isSelecting && !event->buttons() & Qt::LeftButton) {
326 putToClipboard(selectionToString());
327 _isSelecting = false;
330 QGraphicsScene::mouseReleaseEvent(event);
334 void ChatScene::putToClipboard(const QString &selection) {
335 // TODO Configure clipboards
337 QApplication::clipboard()->setText(selection, QClipboard::Selection);
340 QApplication::clipboard()->setText(selection);
344 //!\brief Convert current selection to human-readable string.
345 QString ChatScene::selectionToString() const {
346 //TODO Make selection format configurable!
347 if(!_isSelecting) return QString();
348 int start = qMin(_selectionStart, _selectionEnd);
349 int end = qMax(_selectionStart, _selectionEnd);
350 if(start < 0 || end >= _lines.count()) {
351 qDebug() << "Invalid selection range:" << start << end;
355 for(int l = start; l <= end; l++) {
356 if(_selectionMinCol == ChatLineModel::TimestampColumn)
357 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
358 if(_selectionMinCol <= ChatLineModel::SenderColumn)
359 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
360 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
365 void ChatScene::requestBacklog() {
366 static const int REQUEST_COUNT = 50;
367 int backlogSize = model()->rowCount();
368 if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
369 QModelIndex msgIdx = model()->index(0, 0);
370 MsgId msgId = model()->data(msgIdx, ChatLineModel::MsgIdRole).value<MsgId>();
371 BufferId bufferId = model()->data(msgIdx, ChatLineModel::BufferIdRole).value<BufferId>();
372 _lastBacklogSize = backlogSize;
373 Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
377 int ChatScene::sectionByScenePos(int x) {
378 if(x < firstColHandle->x())
379 return ChatLineModel::TimestampColumn;
380 if(x < secondColHandle->x())
381 return ChatLineModel::SenderColumn;
383 return ChatLineModel::ContentsColumn;