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),
49 _fetchingBacklog(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)), 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);
67 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
68 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
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();
75 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
76 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
78 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
79 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
81 firstColHandle->setXPos(firstColHandlePos);
82 firstColHandle->setXLimits(0, secondColHandlePos);
83 secondColHandle->setXPos(secondColHandlePos);
84 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
86 emit heightChanged(height());
89 ChatScene::~ChatScene() {
93 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
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
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);
106 line->setPos(0, y+h);
107 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
110 // update existing items
111 for(int i = end+1; i < _lines.count(); i++) {
112 _lines[i]->setRow(i);
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;
126 for(int i = end+1; i < _lines.count(); i++) {
127 _lines.value(i)->moveBy(0, h);
129 setSceneRect(QRectF(0, 0, _width, _height));
130 emit heightChanged(_height);
133 requestBacklogIfNeeded();
136 void ChatScene::modelReset() {
137 foreach(ChatLine *line, _lines) {
142 setSceneRect(QRectF(0, 0, _width, 0));
143 emit heightChanged(0);
146 void ChatScene::setWidth(qreal w) {
149 foreach(ChatLine *line, _lines) {
150 line->setPos(0, _height);
151 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
153 setSceneRect(QRectF(0, 0, w, _height));
154 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
155 emit heightChanged(_height);
158 void ChatScene::rectChanged(const QRectF &rect) {
159 firstColHandle->sceneRectChanged(rect);
160 secondColHandle->sceneRectChanged(rect);
163 void ChatScene::handlePositionChanged(qreal xpos) {
164 bool first = (sender() == firstColHandle);
167 oldx = firstColHandlePos;
168 firstColHandlePos = xpos;
170 oldx = secondColHandlePos;
171 secondColHandlePos = xpos;
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);
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());
185 void ChatScene::setSelectingItem(ChatItem *item) {
186 if(_selectingItem) _selectingItem->clearSelection();
187 _selectingItem = item;
190 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
191 _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
192 _selectionStartCol = _selectionMinCol = item->column();
194 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
195 updateSelection(item->mapToScene(itemPos));
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;
204 int curRow = contentItem->row();
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;
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);
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);
223 if(newstart > _selectionStart) {
224 for(int l = _selectionStart; l < newstart; l++)
225 _lines[l]->setSelected(false);
227 if(newend > _selectionEnd) {
228 for(int l = _selectionEnd+1; l <= newend; l++)
229 _lines[l]->setSelected(true, minColumn);
231 if(newend < _selectionEnd) {
232 for(int l = newend+1; l <= _selectionEnd; l++)
233 _lines[l]->setSelected(false);
236 _selectionStart = newstart;
237 _selectionEnd = newend;
238 _lastSelectionRow = curRow;
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));
249 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
250 if(_isSelecting && event->buttons() == Qt::LeftButton) {
251 updateSelection(event->scenePos());
254 QGraphicsScene::mouseMoveEvent(event);
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);
263 _selectionStart = -1;
266 QGraphicsScene::mousePressEvent(event);
270 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
271 if(_isSelecting && !event->buttons() & Qt::LeftButton) {
272 putToClipboard(selectionToString());
273 _isSelecting = false;
276 QGraphicsScene::mouseReleaseEvent(event);
280 void ChatScene::putToClipboard(const QString &selection) {
281 // TODO Configure clipboards
283 QApplication::clipboard()->setText(selection, QClipboard::Selection);
286 QApplication::clipboard()->setText(selection);
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;
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";
311 void ChatScene::setIsFetchingBacklog(bool fetch) {
312 if(!isBacklogFetchingEnabled()) return;
315 _fetchingBacklog = false;
317 _fetchingBacklog = true;
318 requestBacklogIfNeeded();
322 void ChatScene::requestBacklogIfNeeded() {
323 const int REQUEST_COUNT = 50;
325 if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
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();
335 int ChatScene::sectionByScenePos(int x) {
336 if(x < firstColHandlePos)
337 return ChatLineModel::TimestampColumn;
338 if(x < secondColHandlePos)
339 return ChatLineModel::SenderColumn;
341 return ChatLineModel::ContentsColumn;