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),
50 _fetchingBacklog(false)
52 MessageFilter *filter = qobject_cast<MessageFilter*>(model);
54 _singleBufferScene = filter->isSingleBufferFilter();
57 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
59 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
60 connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
61 for(int i = 0; i < model->rowCount(); i++) {
62 ChatLine *line = new ChatLine(i, model);
68 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
69 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
71 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
72 defaultFirstColHandlePos).toInt();
73 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
74 defaultSecondColHandlePos).toInt();
76 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
77 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
79 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
80 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
82 firstColHandle->setXPos(firstColHandlePos);
83 firstColHandle->setXLimits(0, secondColHandlePos);
84 secondColHandle->setXPos(secondColHandlePos);
85 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
87 emit heightChanged(height());
90 ChatScene::~ChatScene() {
94 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
96 // maybe make this more efficient by prepending stuff with negative yval
97 // dunno if that's worth not guranteeing that 0 is on the top...
98 // TODO bulk inserts, iterators
101 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
102 for(int i = start; i <= end; i++) {
103 ChatLine *line = new ChatLine(i, model());
104 _lines.insert(i, line);
107 line->setPos(0, y+h);
108 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
111 // update existing items
112 for(int i = end+1; i < _lines.count(); i++) {
113 _lines[i]->setRow(i);
118 for(int i = end+1; i < _lines.count(); i++) {
119 _lines.value(i)->moveBy(0, h);
121 setSceneRect(QRectF(0, 0, _width, _height));
122 emit heightChanged(_height);
125 requestBacklogIfNeeded();
128 void ChatScene::modelReset() {
129 foreach(ChatLine *line, _lines) {
134 setSceneRect(QRectF(0, 0, _width, 0));
137 void ChatScene::setWidth(qreal w) {
140 foreach(ChatLine *line, _lines) {
141 line->setPos(0, _height);
142 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
144 setSceneRect(QRectF(0, 0, w, _height));
145 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
146 emit heightChanged(_height);
149 void ChatScene::rectChanged(const QRectF &rect) {
150 firstColHandle->sceneRectChanged(rect);
151 secondColHandle->sceneRectChanged(rect);
154 void ChatScene::handlePositionChanged(qreal xpos) {
155 bool first = (sender() == firstColHandle);
158 oldx = firstColHandlePos;
159 firstColHandlePos = xpos;
161 oldx = secondColHandlePos;
162 secondColHandlePos = xpos;
165 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
166 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
167 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
168 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
170 setWidth(width()); // readjust all chatlines
171 // we get ugly redraw errors if we don't update this explicitly... :(
172 // width() should be the same for both handles, so just use firstColHandle regardless
173 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
176 void ChatScene::setSelectingItem(ChatItem *item) {
177 if(_selectingItem) _selectingItem->clearSelection();
178 _selectingItem = item;
181 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
182 _selectionStart = _selectionEnd = item->row();
183 _selectionStartCol = _selectionMinCol = item->column();
185 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
186 updateSelection(item->mapToScene(itemPos));
189 void ChatScene::updateSelection(const QPointF &pos) {
190 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
191 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
192 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
193 if(!contentItem) return;
195 int curRow = contentItem->row();
197 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
198 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
199 else curColumn = ChatLineModel::TimestampColumn;
201 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
202 if(minColumn != _selectionMinCol) {
203 _selectionMinCol = minColumn;
204 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
205 _lines[l]->setSelected(true, minColumn);
209 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
210 for(int l = _selectionEnd + 1; l <= curRow; l++) {
211 _lines[l]->setSelected(true, minColumn);
213 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
214 for(int l = _selectionEnd; l < curRow; l++) {
215 _lines[l]->setSelected(false);
217 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
218 for(int l = _selectionEnd; l > curRow; l--) {
219 _lines[l]->setSelected(false);
221 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
222 for(int l = _selectionEnd - 1; l >= curRow; l--) {
223 _lines[l]->setSelected(true, minColumn);
226 _selectionEnd = curRow;
228 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
229 _lines[curRow]->setSelected(false);
230 _isSelecting = false;
231 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
235 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
236 if(_isSelecting && event->buttons() & Qt::LeftButton) {
237 updateSelection(event->scenePos());
240 QGraphicsScene::mouseMoveEvent(event);
244 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
245 if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
246 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
247 _lines[l]->setSelected(false);
249 _selectionStart = -1;
252 QGraphicsScene::mousePressEvent(event);
256 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
259 QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
262 QApplication::clipboard()->setText(selectionToString());
264 _isSelecting = false;
267 QGraphicsScene::mouseReleaseEvent(event);
271 //!\brief Convert current selection to human-readable string.
272 QString ChatScene::selectionToString() const {
273 //TODO Make selection format configurable!
274 if(!_isSelecting) return QString();
275 int start = qMin(_selectionStart, _selectionEnd);
276 int end = qMax(_selectionStart, _selectionEnd);
277 if(start < 0 || end >= _lines.count()) {
278 qDebug() << "Invalid selection range:" << start << end;
282 for(int l = start; l <= end; l++) {
283 if(_selectionMinCol == ChatLineModel::TimestampColumn)
284 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
285 if(_selectionMinCol <= ChatLineModel::SenderColumn)
286 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
287 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
292 void ChatScene::setIsFetchingBacklog(bool fetch) {
293 if(!isBacklogFetchingEnabled()) return;
296 _fetchingBacklog = false;
298 _fetchingBacklog = true;
299 requestBacklogIfNeeded();
303 void ChatScene::requestBacklogIfNeeded() {
304 const int REQUEST_COUNT = 50;
306 if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
308 MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
309 if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
310 Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
311 _lastBacklogOffset = msgId;
312 _lastBacklogSize = model()->rowCount();
316 int ChatScene::sectionByScenePos(int x) {
317 if(x < firstColHandlePos)
318 return ChatLineModel::TimestampColumn;
319 if(x < secondColHandlePos)
320 return ChatLineModel::SenderColumn;
322 return ChatLineModel::ContentsColumn;