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>
29 #include "chatlinemodelitem.h"
30 #include "chatscene.h"
32 #include "clientbacklogmanager.h"
33 #include "columnhandleitem.h"
35 #include "qtuisettings.h"
37 const qreal minContentsWidth = 200;
39 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
40 : QGraphicsScene(parent),
48 _fetchingBacklog = false;
50 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
52 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
53 connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
54 for(int i = 0; i < model->rowCount(); i++) {
55 ChatLine *line = new ChatLine(i, model);
61 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
62 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
64 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
65 defaultFirstColHandlePos).toInt();
66 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
67 defaultSecondColHandlePos).toInt();
69 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
70 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
72 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
73 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
75 firstColHandle->setXPos(firstColHandlePos);
76 firstColHandle->setXLimits(0, secondColHandlePos);
77 secondColHandle->setXPos(secondColHandlePos);
78 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
80 emit heightChanged(height());
83 ChatScene::~ChatScene() {
87 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
89 // maybe make this more efficient by prepending stuff with negative yval
90 // dunno if that's worth not guranteeing that 0 is on the top...
91 // TODO bulk inserts, iterators
94 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
95 for(int i = start; i <= end; i++) {
96 ChatLine *line = new ChatLine(i, model());
97 _lines.insert(i, line);
100 line->setPos(0, y+h);
101 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
104 // update existing items
105 for(int i = end+1; i < _lines.count(); i++) {
106 _lines[i]->setRow(i);
111 for(int i = end+1; i < _lines.count(); i++) {
112 _lines.value(i)->moveBy(0, h);
114 setSceneRect(QRectF(0, 0, _width, _height));
115 emit heightChanged(_height);
118 requestBacklogIfNeeded();
121 void ChatScene::modelReset() {
122 foreach(ChatLine *line, _lines) {
127 setSceneRect(QRectF(0, 0, _width, 0));
130 void ChatScene::setWidth(qreal w) {
133 foreach(ChatLine *line, _lines) {
134 line->setPos(0, _height);
135 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
137 setSceneRect(QRectF(0, 0, w, _height));
138 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
139 emit heightChanged(_height);
142 void ChatScene::rectChanged(const QRectF &rect) {
143 firstColHandle->sceneRectChanged(rect);
144 secondColHandle->sceneRectChanged(rect);
147 void ChatScene::handlePositionChanged(qreal xpos) {
148 bool first = (sender() == firstColHandle);
151 oldx = firstColHandlePos;
152 firstColHandlePos = xpos;
154 oldx = secondColHandlePos;
155 secondColHandlePos = xpos;
158 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
159 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
160 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
161 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
163 setWidth(width()); // readjust all chatlines
164 // we get ugly redraw errors if we don't update this explicitly... :(
165 // width() should be the same for both handles, so just use firstColHandle regardless
166 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
169 void ChatScene::setSelectingItem(ChatItem *item) {
170 if(_selectingItem) _selectingItem->clearSelection();
171 _selectingItem = item;
174 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
175 _selectionStart = _selectionEnd = item->row();
176 _selectionStartCol = _selectionMinCol = item->column();
178 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
179 updateSelection(item->mapToScene(itemPos));
182 void ChatScene::updateSelection(const QPointF &pos) {
183 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
184 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
185 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
186 if(!contentItem) return;
188 int curRow = contentItem->row();
190 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
191 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
192 else curColumn = ChatLineModel::TimestampColumn;
194 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
195 if(minColumn != _selectionMinCol) {
196 _selectionMinCol = minColumn;
197 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
198 _lines[l]->setSelected(true, minColumn);
202 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
203 for(int l = _selectionEnd + 1; l <= curRow; l++) {
204 _lines[l]->setSelected(true, minColumn);
206 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
207 for(int l = _selectionEnd; l < curRow; l++) {
208 _lines[l]->setSelected(false);
210 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
211 for(int l = _selectionEnd; l > curRow; l--) {
212 _lines[l]->setSelected(false);
214 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
215 for(int l = _selectionEnd - 1; l >= curRow; l--) {
216 _lines[l]->setSelected(true, minColumn);
219 _selectionEnd = curRow;
221 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
222 _lines[curRow]->setSelected(false);
223 _isSelecting = false;
224 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
228 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
229 if(_isSelecting && event->buttons() & Qt::LeftButton) {
230 updateSelection(event->scenePos());
233 QGraphicsScene::mouseMoveEvent(event);
237 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
238 if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
239 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
240 _lines[l]->setSelected(false);
242 _selectionStart = -1;
245 QGraphicsScene::mousePressEvent(event);
249 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
252 QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
255 QApplication::clipboard()->setText(selectionToString());
257 _isSelecting = false;
260 QGraphicsScene::mouseReleaseEvent(event);
264 //!\brief Convert current selection to human-readable string.
265 QString ChatScene::selectionToString() const {
266 //TODO Make selection format configurable!
267 if(!_isSelecting) return QString();
268 int start = qMin(_selectionStart, _selectionEnd);
269 int end = qMax(_selectionStart, _selectionEnd);
270 if(start < 0 || end >= _lines.count()) {
271 qDebug() << "Invalid selection range:" << start << end;
275 for(int l = start; l <= end; l++) {
276 if(_selectionMinCol == ChatLineModel::TimestampColumn)
277 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
278 if(_selectionMinCol <= ChatLineModel::SenderColumn)
279 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
280 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
285 void ChatScene::setIsFetchingBacklog(bool fetch) {
286 if(!isBacklogFetchingEnabled()) return;
289 _fetchingBacklog = false;
291 _fetchingBacklog = true;
292 requestBacklogIfNeeded();
296 void ChatScene::requestBacklogIfNeeded() {
297 const int REQUEST_COUNT = 50;
299 if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
301 MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
302 if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
303 Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
304 _lastBacklogOffset = msgId;
305 _lastBacklogSize = model()->rowCount();