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 <QGraphicsSceneMouseEvent>
22 #include <QPersistentModelIndex>
26 #include "chatlinemodelitem.h"
27 #include "chatscene.h"
28 #include "columnhandleitem.h"
30 #include "qtuisettings.h"
32 const qreal minContentsWidth = 200;
34 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
35 : QGraphicsScene(parent),
42 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
44 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
45 for(int i = 0; i < model->rowCount(); i++) {
46 ChatLine *line = new ChatLine(model->index(i, 0));
52 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
53 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
55 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
56 defaultFirstColHandlePos).toInt();
57 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
58 defaultSecondColHandlePos).toInt();
60 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
61 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
63 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
64 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
66 firstColHandle->setXPos(firstColHandlePos);
67 firstColHandle->setXLimits(0, secondColHandlePos);
68 secondColHandle->setXPos(secondColHandlePos);
69 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
71 emit heightChanged(height());
74 ChatScene::~ChatScene() {
79 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
81 // maybe make this more efficient by prepending stuff with negative yval
82 // dunno if that's worth not guranteeing that 0 is on the top...
83 // TODO bulk inserts, iterators
86 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
87 for(int i = start; i <= end; i++) {
88 ChatLine *line = new ChatLine(model()->index(i, 0));
89 _lines.insert(i, line);
93 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
98 for(int i = end+1; i < _lines.count(); i++) {
99 _lines.value(i)->moveBy(0, h);
101 setSceneRect(QRectF(0, 0, _width, _height));
102 emit heightChanged(_height);
106 void ChatScene::setWidth(qreal w) {
109 foreach(ChatLine *line, _lines) {
110 line->setPos(0, _height);
111 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
113 setSceneRect(QRectF(0, 0, w, _height));
114 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
115 emit heightChanged(_height);
118 void ChatScene::rectChanged(const QRectF &rect) {
119 firstColHandle->sceneRectChanged(rect);
120 secondColHandle->sceneRectChanged(rect);
123 void ChatScene::handlePositionChanged(qreal xpos) {
124 bool first = (sender() == firstColHandle);
127 oldx = firstColHandlePos;
128 firstColHandlePos = xpos;
130 oldx = secondColHandlePos;
131 secondColHandlePos = xpos;
134 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
135 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
136 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
137 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
139 setWidth(width()); // readjust all chatlines
140 // we get ugly redraw errors if we don't update this explicitly... :(
141 // width() should be the same for both handles, so just use firstColHandle regardless
142 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
145 void ChatScene::setSelectingItem(ChatItem *item) {
146 if(_selectingItem) _selectingItem->clearSelection();
147 _selectingItem = item;
150 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
151 _selectionStart = _selectionEnd = item->index().row();
152 _selectionStartCol = _selectionMinCol = item->index().column();
154 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
155 updateSelection(item->mapToScene(itemPos));
158 void ChatScene::updateSelection(const QPointF &pos) {
159 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
160 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
161 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
162 if(!contentItem) return;
164 int curRow = contentItem->index().row();
166 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
167 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
168 else curColumn = ChatLineModel::TimestampColumn;
170 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
171 if(minColumn != _selectionMinCol) {
172 _selectionMinCol = minColumn;
173 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
174 _lines[l]->setSelected(true, minColumn);
178 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
179 for(int l = _selectionEnd + 1; l <= curRow; l++) {
180 _lines[l]->setSelected(true, minColumn);
182 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
183 for(int l = _selectionEnd; l < curRow; l++) {
184 _lines[l]->setSelected(false);
186 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
187 for(int l = _selectionEnd; l > curRow; l--) {
188 _lines[l]->setSelected(false);
190 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
191 for(int l = _selectionEnd - 1; l >= curRow; l--) {
192 _lines[l]->setSelected(true, minColumn);
195 _selectionEnd = curRow;
197 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
198 _lines[curRow]->setSelected(false);
199 _isSelecting = false;
200 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
204 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
205 if(_isSelecting && event->buttons() & Qt::LeftButton) {
206 updateSelection(event->scenePos());
209 QGraphicsScene::mouseMoveEvent(event);
213 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
214 qDebug() << "pressed";
215 QGraphicsScene::mousePressEvent(event);
218 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
220 _isSelecting = false;
223 QGraphicsScene::mouseReleaseEvent(event);