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 for(int i = 0; i < model->rowCount(); i++) {
49 ChatLine *line = new ChatLine(model->index(i, 0));
55 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
56 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
58 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
59 defaultFirstColHandlePos).toInt();
60 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
61 defaultSecondColHandlePos).toInt();
63 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
64 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
66 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
67 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
69 firstColHandle->setXPos(firstColHandlePos);
70 firstColHandle->setXLimits(0, secondColHandlePos);
71 secondColHandle->setXPos(secondColHandlePos);
72 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
74 emit heightChanged(height());
77 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::setWidth(qreal w) {
112 foreach(ChatLine *line, _lines) {
113 line->setPos(0, _height);
114 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
116 setSceneRect(QRectF(0, 0, w, _height));
117 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
118 emit heightChanged(_height);
121 void ChatScene::rectChanged(const QRectF &rect) {
122 firstColHandle->sceneRectChanged(rect);
123 secondColHandle->sceneRectChanged(rect);
126 void ChatScene::handlePositionChanged(qreal xpos) {
127 bool first = (sender() == firstColHandle);
130 oldx = firstColHandlePos;
131 firstColHandlePos = xpos;
133 oldx = secondColHandlePos;
134 secondColHandlePos = xpos;
137 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
138 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
139 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
140 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
142 setWidth(width()); // readjust all chatlines
143 // we get ugly redraw errors if we don't update this explicitly... :(
144 // width() should be the same for both handles, so just use firstColHandle regardless
145 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
148 void ChatScene::setSelectingItem(ChatItem *item) {
149 if(_selectingItem) _selectingItem->clearSelection();
150 _selectingItem = item;
153 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
154 _selectionStart = _selectionEnd = item->index().row();
155 _selectionStartCol = _selectionMinCol = item->index().column();
157 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
158 updateSelection(item->mapToScene(itemPos));
161 void ChatScene::updateSelection(const QPointF &pos) {
162 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
163 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
164 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
165 if(!contentItem) return;
167 int curRow = contentItem->index().row();
169 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
170 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
171 else curColumn = ChatLineModel::TimestampColumn;
173 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
174 if(minColumn != _selectionMinCol) {
175 _selectionMinCol = minColumn;
176 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
177 _lines[l]->setSelected(true, minColumn);
181 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
182 for(int l = _selectionEnd + 1; l <= curRow; l++) {
183 _lines[l]->setSelected(true, minColumn);
185 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
186 for(int l = _selectionEnd; l < curRow; l++) {
187 _lines[l]->setSelected(false);
189 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
190 for(int l = _selectionEnd; l > curRow; l--) {
191 _lines[l]->setSelected(false);
193 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
194 for(int l = _selectionEnd - 1; l >= curRow; l--) {
195 _lines[l]->setSelected(true, minColumn);
198 _selectionEnd = curRow;
200 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
201 _lines[curRow]->setSelected(false);
202 _isSelecting = false;
203 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
207 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
208 if(_isSelecting && event->buttons() & Qt::LeftButton) {
209 updateSelection(event->scenePos());
212 QGraphicsScene::mouseMoveEvent(event);
216 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
217 if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
218 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
219 _lines[l]->setSelected(false);
221 _selectionStart = -1;
224 QGraphicsScene::mousePressEvent(event);
228 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
231 QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
234 QApplication::clipboard()->setText(selectionToString());
236 _isSelecting = false;
239 QGraphicsScene::mouseReleaseEvent(event);
243 //!\brief Convert current selection to human-readable string.
244 QString ChatScene::selectionToString() const {
245 //TODO Make selection format configurable!
246 if(!_isSelecting) return "";
248 for(int l = _selectionStart; l <= _selectionEnd; l++) {
249 if(_selectionMinCol == ChatLineModel::TimestampColumn)
250 result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
251 if(_selectionMinCol <= ChatLineModel::SenderColumn)
252 result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
253 result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";