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"
30 #include "columnhandleitem.h"
32 #include "qtuisettings.h"
34 const qreal minContentsWidth = 200;
36 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
37 : QGraphicsScene(parent),
45 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
47 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
48 connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
49 for(int i = 0; i < model->rowCount(); i++) {
50 ChatLine *line = new ChatLine(model->index(i, 0));
56 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
57 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
59 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
60 defaultFirstColHandlePos).toInt();
61 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
62 defaultSecondColHandlePos).toInt();
64 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
65 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
67 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
68 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
70 firstColHandle->setXPos(firstColHandlePos);
71 firstColHandle->setXLimits(0, secondColHandlePos);
72 secondColHandle->setXPos(secondColHandlePos);
73 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
75 emit heightChanged(height());
78 ChatScene::~ChatScene() {
82 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
84 // maybe make this more efficient by prepending stuff with negative yval
85 // dunno if that's worth not guranteeing that 0 is on the top...
86 // TODO bulk inserts, iterators
89 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
90 for(int i = start; i <= end; i++) {
91 ChatLine *line = new ChatLine(model()->index(i, 0));
92 _lines.insert(i, line);
96 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
101 for(int i = end+1; i < _lines.count(); i++) {
102 _lines.value(i)->moveBy(0, h);
104 setSceneRect(QRectF(0, 0, _width, _height));
105 emit heightChanged(_height);
109 void ChatScene::modelReset() {
110 foreach(ChatLine *line, _lines) {
115 setSceneRect(QRectF(0, 0, _width, 0));
118 void ChatScene::setWidth(qreal w) {
121 foreach(ChatLine *line, _lines) {
122 line->setPos(0, _height);
123 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
125 setSceneRect(QRectF(0, 0, w, _height));
126 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
127 emit heightChanged(_height);
130 void ChatScene::rectChanged(const QRectF &rect) {
131 firstColHandle->sceneRectChanged(rect);
132 secondColHandle->sceneRectChanged(rect);
135 void ChatScene::handlePositionChanged(qreal xpos) {
136 bool first = (sender() == firstColHandle);
139 oldx = firstColHandlePos;
140 firstColHandlePos = xpos;
142 oldx = secondColHandlePos;
143 secondColHandlePos = xpos;
146 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
147 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
148 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
149 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
151 setWidth(width()); // readjust all chatlines
152 // we get ugly redraw errors if we don't update this explicitly... :(
153 // width() should be the same for both handles, so just use firstColHandle regardless
154 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
157 void ChatScene::setSelectingItem(ChatItem *item) {
158 if(_selectingItem) _selectingItem->clearSelection();
159 _selectingItem = item;
162 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
163 _selectionStart = _selectionEnd = item->index().row();
164 _selectionStartCol = _selectionMinCol = item->index().column();
166 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
167 updateSelection(item->mapToScene(itemPos));
170 void ChatScene::updateSelection(const QPointF &pos) {
171 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
172 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
173 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
174 if(!contentItem) return;
176 int curRow = contentItem->index().row();
178 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
179 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
180 else curColumn = ChatLineModel::TimestampColumn;
182 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
183 if(minColumn != _selectionMinCol) {
184 _selectionMinCol = minColumn;
185 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
186 _lines[l]->setSelected(true, minColumn);
190 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
191 for(int l = _selectionEnd + 1; l <= curRow; l++) {
192 _lines[l]->setSelected(true, minColumn);
194 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
195 for(int l = _selectionEnd; l < curRow; l++) {
196 _lines[l]->setSelected(false);
198 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
199 for(int l = _selectionEnd; l > curRow; l--) {
200 _lines[l]->setSelected(false);
202 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
203 for(int l = _selectionEnd - 1; l >= curRow; l--) {
204 _lines[l]->setSelected(true, minColumn);
207 _selectionEnd = curRow;
209 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
210 _lines[curRow]->setSelected(false);
211 _isSelecting = false;
212 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
216 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
217 if(_isSelecting && event->buttons() & Qt::LeftButton) {
218 updateSelection(event->scenePos());
221 QGraphicsScene::mouseMoveEvent(event);
225 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
226 if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
227 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
228 _lines[l]->setSelected(false);
230 _selectionStart = -1;
233 QGraphicsScene::mousePressEvent(event);
237 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
240 QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
243 QApplication::clipboard()->setText(selectionToString());
245 _isSelecting = false;
248 QGraphicsScene::mouseReleaseEvent(event);
252 //!\brief Convert current selection to human-readable string.
253 QString ChatScene::selectionToString() const {
254 //TODO Make selection format configurable!
255 if(!_isSelecting) return QString();
256 int start = qMin(_selectionStart, _selectionEnd);
257 int end = qMax(_selectionStart, _selectionEnd);
258 if(start < 0 || end >= _lines.count()) {
259 qDebug() << "Invalid selection range:" << start << end;
263 for(int l = start; l <= end; l++) {
264 if(_selectionMinCol == ChatLineModel::TimestampColumn)
265 result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
266 if(_selectionMinCol <= ChatLineModel::SenderColumn)
267 result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
268 result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";