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 = _lastSelectionRow = _firstSelectionRow = 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 int newstart = qMin(curRow, _firstSelectionRow);
210 int newend = qMax(curRow, _firstSelectionRow);
212 if(newstart < _selectionStart) {
213 for(int l = newstart; l < _selectionStart; l++)
214 _lines[l]->setSelected(true, minColumn);
216 if(newstart > _selectionStart) {
217 for(int l = _selectionStart; l < newstart; l++)
218 _lines[l]->setSelected(false);
220 if(newend > _selectionEnd) {
221 for(int l = _selectionEnd+1; l <= newend; l++)
222 _lines[l]->setSelected(true, minColumn);
224 if(newend < _selectionEnd) {
225 for(int l = newend+1; l <= _selectionEnd; l++)
226 _lines[l]->setSelected(false);
229 _selectionStart = newstart;
230 _selectionEnd = newend;
231 _lastSelectionRow = curRow;
233 if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
234 _lines[curRow]->setSelected(false);
235 _isSelecting = false;
236 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
240 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
241 if(_isSelecting && event->buttons() == Qt::LeftButton) {
242 updateSelection(event->scenePos());
245 QGraphicsScene::mouseMoveEvent(event);
249 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
250 if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
251 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
252 _lines[l]->setSelected(false);
254 _selectionStart = -1;
257 QGraphicsScene::mousePressEvent(event);
261 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
262 if(_isSelecting && !event->buttons() & Qt::LeftButton) {
264 QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
267 QApplication::clipboard()->setText(selectionToString());
269 _isSelecting = false;
272 QGraphicsScene::mouseReleaseEvent(event);
276 //!\brief Convert current selection to human-readable string.
277 QString ChatScene::selectionToString() const {
278 //TODO Make selection format configurable!
279 if(!_isSelecting) return QString();
280 int start = qMin(_selectionStart, _selectionEnd);
281 int end = qMax(_selectionStart, _selectionEnd);
282 if(start < 0 || end >= _lines.count()) {
283 qDebug() << "Invalid selection range:" << start << end;
287 for(int l = start; l <= end; l++) {
288 if(_selectionMinCol == ChatLineModel::TimestampColumn)
289 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
290 if(_selectionMinCol <= ChatLineModel::SenderColumn)
291 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
292 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
297 void ChatScene::setIsFetchingBacklog(bool fetch) {
298 if(!isBacklogFetchingEnabled()) return;
301 _fetchingBacklog = false;
303 _fetchingBacklog = true;
304 requestBacklogIfNeeded();
308 void ChatScene::requestBacklogIfNeeded() {
309 const int REQUEST_COUNT = 50;
311 if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
313 MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
314 if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
315 Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
316 _lastBacklogOffset = msgId;
317 _lastBacklogSize = model()->rowCount();
321 int ChatScene::sectionByScenePos(int x) {
322 if(x < firstColHandlePos)
323 return ChatLineModel::TimestampColumn;
324 if(x < secondColHandlePos)
325 return ChatLineModel::SenderColumn;
327 return ChatLineModel::ContentsColumn;