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),
43 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
45 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
46 for(int i = 0; i < model->rowCount(); i++) {
47 ChatLine *line = new ChatLine(model->index(i, 0));
53 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
54 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
56 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
57 defaultFirstColHandlePos).toInt();
58 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
59 defaultSecondColHandlePos).toInt();
61 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
62 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
64 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
65 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
67 firstColHandle->setXPos(firstColHandlePos);
68 firstColHandle->setXLimits(0, secondColHandlePos);
69 secondColHandle->setXPos(secondColHandlePos);
70 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
72 emit heightChanged(height());
75 ChatScene::~ChatScene() {
80 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
82 // maybe make this more efficient by prepending stuff with negative yval
83 // dunno if that's worth not guranteeing that 0 is on the top...
84 // TODO bulk inserts, iterators
87 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
88 for(int i = start; i <= end; i++) {
89 ChatLine *line = new ChatLine(model()->index(i, 0));
90 _lines.insert(i, line);
94 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
99 for(int i = end+1; i < _lines.count(); i++) {
100 _lines.value(i)->moveBy(0, h);
102 setSceneRect(QRectF(0, 0, _width, _height));
103 emit heightChanged(_height);
107 void ChatScene::setWidth(qreal w) {
110 foreach(ChatLine *line, _lines) {
111 line->setPos(0, _height);
112 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
114 setSceneRect(QRectF(0, 0, w, _height));
115 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
116 emit heightChanged(_height);
119 void ChatScene::rectChanged(const QRectF &rect) {
120 firstColHandle->sceneRectChanged(rect);
121 secondColHandle->sceneRectChanged(rect);
124 void ChatScene::handlePositionChanged(qreal xpos) {
125 bool first = (sender() == firstColHandle);
128 oldx = firstColHandlePos;
129 firstColHandlePos = xpos;
131 oldx = secondColHandlePos;
132 secondColHandlePos = xpos;
135 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
136 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
137 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
138 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
140 setWidth(width()); // readjust all chatlines
141 // we get ugly redraw errors if we don't update this explicitly... :(
142 // width() should be the same for both handles, so just use firstColHandle regardless
143 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
146 void ChatScene::setSelectingItem(ChatItem *item) {
147 if(_selectingItem) _selectingItem->clearSelection();
148 _selectingItem = item;
151 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
152 _selectionStart = _selectionEnd = item->index().row();
153 _selectionStartCol = _selectionMinCol = item->index().column();
155 _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
156 updateSelection(item->mapToScene(itemPos));
159 void ChatScene::updateSelection(const QPointF &pos) {
160 // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
161 // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
162 ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
163 if(!contentItem) return;
165 int curRow = contentItem->index().row();
167 if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
168 else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
169 else curColumn = ChatLineModel::TimestampColumn;
171 ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
172 if(minColumn != _selectionMinCol) {
173 _selectionMinCol = minColumn;
174 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
175 _lines[l]->setSelected(true, minColumn);
179 if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
180 for(int l = _selectionEnd + 1; l <= curRow; l++) {
181 _lines[l]->setSelected(true, minColumn);
183 } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
184 for(int l = _selectionEnd; l < curRow; l++) {
185 _lines[l]->setSelected(false);
187 } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
188 for(int l = _selectionEnd; l > curRow; l--) {
189 _lines[l]->setSelected(false);
191 } else if(curRow < _selectionEnd && curRow < _selectionStart) {
192 for(int l = _selectionEnd - 1; l >= curRow; l--) {
193 _lines[l]->setSelected(true, minColumn);
196 _selectionEnd = curRow;
198 if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
199 _lines[curRow]->setSelected(false);
200 _isSelecting = false;
201 _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
205 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
206 if(_isSelecting && event->buttons() & Qt::LeftButton) {
207 updateSelection(event->scenePos());
210 QGraphicsScene::mouseMoveEvent(event);
214 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
215 if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
216 for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
217 _lines[l]->setSelected(false);
219 _selectionStart = -1;
222 QGraphicsScene::mousePressEvent(event);
226 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
228 _isSelecting = false;
231 QGraphicsScene::mouseReleaseEvent(event);