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"
31 #include "columnhandleitem.h"
33 #include "qtuisettings.h"
35 const qreal minContentsWidth = 200;
37 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
38 : QGraphicsScene(parent),
46 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
48 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
49 connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
50 for(int i = 0; i < model->rowCount(); i++) {
51 ChatLine *line = new ChatLine(i, model);
57 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
58 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
60 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
61 defaultFirstColHandlePos).toInt();
62 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
63 defaultSecondColHandlePos).toInt();
65 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
66 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
68 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
69 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
71 firstColHandle->setXPos(firstColHandlePos);
72 firstColHandle->setXLimits(0, secondColHandlePos);
73 secondColHandle->setXPos(secondColHandlePos);
74 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
76 emit heightChanged(height());
79 ChatScene::~ChatScene() {
83 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
85 // maybe make this more efficient by prepending stuff with negative yval
86 // dunno if that's worth not guranteeing that 0 is on the top...
87 // TODO bulk inserts, iterators
90 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
91 for(int i = start; i <= end; i++) {
92 ChatLine *line = new ChatLine(i, model());
93 _lines.insert(i, line);
97 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
100 // update existing items
101 for(int i = end+1; i < _lines.count(); i++) {
102 _lines[i]->setRow(i);
107 for(int i = end+1; i < _lines.count(); i++) {
108 _lines.value(i)->moveBy(0, h);
110 setSceneRect(QRectF(0, 0, _width, _height));
111 emit heightChanged(_height);
115 void ChatScene::modelReset() {
116 foreach(ChatLine *line, _lines) {
121 setSceneRect(QRectF(0, 0, _width, 0));
124 void ChatScene::setWidth(qreal w) {
127 foreach(ChatLine *line, _lines) {
128 line->setPos(0, _height);
129 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
131 setSceneRect(QRectF(0, 0, w, _height));
132 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
133 emit heightChanged(_height);
136 void ChatScene::rectChanged(const QRectF &rect) {
137 firstColHandle->sceneRectChanged(rect);
138 secondColHandle->sceneRectChanged(rect);
141 void ChatScene::handlePositionChanged(qreal xpos) {
142 bool first = (sender() == firstColHandle);
145 oldx = firstColHandlePos;
146 firstColHandlePos = xpos;
148 oldx = secondColHandlePos;
149 secondColHandlePos = xpos;
152 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
153 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
154 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
155 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
157 setWidth(width()); // readjust all chatlines
158 // we get ugly redraw errors if we don't update this explicitly... :(
159 // width() should be the same for both handles, so just use firstColHandle regardless
160 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
163 void ChatScene::setSelectingItem(ChatItem *item) {
164 if(_selectingItem) _selectingItem->clearSelection();
165 _selectingItem = item;
168 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
169 _selectionStart = _selectionEnd = item->row();
170 _selectionStartCol = _selectionMinCol = item->column();
172 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
173 updateSelection(item->mapToScene(itemPos));
176 void ChatScene::updateSelection(const QPointF &pos) {
177 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
178 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
179 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
180 if(!contentItem) return;
182 int curRow = contentItem->row();
184 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
185 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
186 else curColumn = ChatLineModel::TimestampColumn;
188 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
189 if(minColumn != _selectionMinCol) {
190 _selectionMinCol = minColumn;
191 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
192 _lines[l]->setSelected(true, minColumn);
196 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
197 for(int l = _selectionEnd + 1; l <= curRow; l++) {
198 _lines[l]->setSelected(true, minColumn);
200 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
201 for(int l = _selectionEnd; l < curRow; l++) {
202 _lines[l]->setSelected(false);
204 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
205 for(int l = _selectionEnd; l > curRow; l--) {
206 _lines[l]->setSelected(false);
208 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
209 for(int l = _selectionEnd - 1; l >= curRow; l--) {
210 _lines[l]->setSelected(true, minColumn);
213 _selectionEnd = curRow;
215 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
216 _lines[curRow]->setSelected(false);
217 _isSelecting = false;
218 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
222 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
223 if(_isSelecting && event->buttons() & Qt::LeftButton) {
224 updateSelection(event->scenePos());
227 QGraphicsScene::mouseMoveEvent(event);
231 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
232 if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
233 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
234 _lines[l]->setSelected(false);
236 _selectionStart = -1;
239 QGraphicsScene::mousePressEvent(event);
243 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
246 QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
249 QApplication::clipboard()->setText(selectionToString());
251 _isSelecting = false;
254 QGraphicsScene::mouseReleaseEvent(event);
258 //!\brief Convert current selection to human-readable string.
259 QString ChatScene::selectionToString() const {
260 //TODO Make selection format configurable!
261 if(!_isSelecting) return QString();
262 int start = qMin(_selectionStart, _selectionEnd);
263 int end = qMax(_selectionStart, _selectionEnd);
264 if(start < 0 || end >= _lines.count()) {
265 qDebug() << "Invalid selection range:" << start << end;
269 for(int l = start; l <= end; l++) {
270 if(_selectionMinCol == ChatLineModel::TimestampColumn)
271 result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
272 if(_selectionMinCol <= ChatLineModel::SenderColumn)
273 result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
274 result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";