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));
145 void ChatScene::setWidth(qreal w) {
148 foreach(ChatLine *line, _lines) {
149 line->setPos(0, _height);
150 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
152 setSceneRect(QRectF(0, 0, w, _height));
153 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
154 emit heightChanged(_height);
157 void ChatScene::rectChanged(const QRectF &rect) {
158 firstColHandle->sceneRectChanged(rect);
159 secondColHandle->sceneRectChanged(rect);
162 void ChatScene::handlePositionChanged(qreal xpos) {
163 bool first = (sender() == firstColHandle);
166 oldx = firstColHandlePos;
167 firstColHandlePos = xpos;
169 oldx = secondColHandlePos;
170 secondColHandlePos = xpos;
173 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
174 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
175 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
176 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
178 setWidth(width()); // readjust all chatlines
179 // we get ugly redraw errors if we don't update this explicitly... :(
180 // width() should be the same for both handles, so just use firstColHandle regardless
181 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
184 void ChatScene::setSelectingItem(ChatItem *item) {
185 if(_selectingItem) _selectingItem->clearSelection();
186 _selectingItem = item;
189 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
190 _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
191 _selectionStartCol = _selectionMinCol = item->column();
193 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
194 updateSelection(item->mapToScene(itemPos));
197 void ChatScene::updateSelection(const QPointF &pos) {
198 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
199 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
200 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
201 if(!contentItem) return;
203 int curRow = contentItem->row();
205 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
206 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
207 else curColumn = ChatLineModel::TimestampColumn;
209 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
210 if(minColumn != _selectionMinCol) {
211 _selectionMinCol = minColumn;
212 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
213 _lines[l]->setSelected(true, minColumn);
216 int newstart = qMin(curRow, _firstSelectionRow);
217 int newend = qMax(curRow, _firstSelectionRow);
218 if(newstart < _selectionStart) {
219 for(int l = newstart; l < _selectionStart; l++)
220 _lines[l]->setSelected(true, minColumn);
222 if(newstart > _selectionStart) {
223 for(int l = _selectionStart; l < newstart; l++)
224 _lines[l]->setSelected(false);
226 if(newend > _selectionEnd) {
227 for(int l = _selectionEnd+1; l <= newend; l++)
228 _lines[l]->setSelected(true, minColumn);
230 if(newend < _selectionEnd) {
231 for(int l = newend+1; l <= _selectionEnd; l++)
232 _lines[l]->setSelected(false);
235 _selectionStart = newstart;
236 _selectionEnd = newend;
237 _lastSelectionRow = curRow;
239 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
240 _lines[curRow]->setSelected(false);
241 _isSelecting = false;
242 Q_ASSERT(_selectingItem); // this seems to not always be true, but I have no idea why
243 // adding this assert to make sure the occasional segfault is caused by this
244 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
248 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
249 if(_isSelecting && event->buttons() == Qt::LeftButton) {
250 updateSelection(event->scenePos());
253 QGraphicsScene::mouseMoveEvent(event);
257 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
258 if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
259 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
260 _lines[l]->setSelected(false);
262 _selectionStart = -1;
265 QGraphicsScene::mousePressEvent(event);
269 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
270 if(_isSelecting && !event->buttons() & Qt::LeftButton) {
271 putToClipboard(selectionToString());
272 _isSelecting = false;
275 QGraphicsScene::mouseReleaseEvent(event);
279 void ChatScene::putToClipboard(const QString &selection) {
280 // TODO Configure clipboards
282 QApplication::clipboard()->setText(selection, QClipboard::Selection);
285 QApplication::clipboard()->setText(selection);
289 //!\brief Convert current selection to human-readable string.
290 QString ChatScene::selectionToString() const {
291 //TODO Make selection format configurable!
292 if(!_isSelecting) return QString();
293 int start = qMin(_selectionStart, _selectionEnd);
294 int end = qMax(_selectionStart, _selectionEnd);
295 if(start < 0 || end >= _lines.count()) {
296 qDebug() << "Invalid selection range:" << start << end;
300 for(int l = start; l <= end; l++) {
301 if(_selectionMinCol == ChatLineModel::TimestampColumn)
302 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
303 if(_selectionMinCol <= ChatLineModel::SenderColumn)
304 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
305 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
310 void ChatScene::setIsFetchingBacklog(bool fetch) {
311 if(!isBacklogFetchingEnabled()) return;
314 _fetchingBacklog = false;
316 _fetchingBacklog = true;
317 requestBacklogIfNeeded();
321 void ChatScene::requestBacklogIfNeeded() {
322 const int REQUEST_COUNT = 50;
324 if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
326 MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
327 if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
328 Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
329 _lastBacklogOffset = msgId;
330 _lastBacklogSize = model()->rowCount();
334 int ChatScene::sectionByScenePos(int x) {
335 if(x < firstColHandlePos)
336 return ChatLineModel::TimestampColumn;
337 if(x < secondColHandlePos)
338 return ChatLineModel::SenderColumn;
340 return ChatLineModel::ContentsColumn;