797290d7711a414835b78ba481b09a07c1312d8d
[quassel.git] / src / qtui / chatscene.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QGraphicsSceneMouseEvent>
22 #include <QPersistentModelIndex>
23
24 #include "buffer.h"
25 #include "chatitem.h"
26 #include "chatlinemodelitem.h"
27 #include "chatscene.h"
28 #include "columnhandleitem.h"
29 #include "qtui.h"
30 #include "qtuisettings.h"
31
32 const qreal minContentsWidth = 200;
33
34 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
35   : QGraphicsScene(parent),
36   _idString(idString),
37   _model(model)
38 {
39   _width = 0;
40   _selectingItem = 0;
41   _isSelecting = false;
42   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
43
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));
47     _lines.append(line);
48     addItem(line);
49   }
50
51   QtUiSettings s;
52   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
53   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
54
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();
59
60   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
61   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
62
63   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
64   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
65
66   firstColHandle->setXPos(firstColHandlePos);
67   firstColHandle->setXLimits(0, secondColHandlePos);
68   secondColHandle->setXPos(secondColHandlePos);
69   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
70
71   emit heightChanged(height());
72 }
73
74 ChatScene::~ChatScene() {
75
76
77 }
78
79 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
80   Q_UNUSED(index);
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
84   qreal h = 0;
85   qreal y = 0;
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);
90     addItem(line);
91     if(_width > 0) {
92       line->setPos(0, y+h);
93       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
94     }
95   }
96   if(h > 0) {
97     _height += h;
98     for(int i = end+1; i < _lines.count(); i++) {
99       _lines.value(i)->moveBy(0, h);
100     }
101     setSceneRect(QRectF(0, 0, _width, _height));
102     emit heightChanged(_height);
103   }
104 }
105
106 void ChatScene::setWidth(qreal w) {
107   _width = w;
108   _height = 0;
109   foreach(ChatLine *line, _lines) {
110     line->setPos(0, _height);
111     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
112   }
113   setSceneRect(QRectF(0, 0, w, _height));
114   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
115   emit heightChanged(_height);
116 }
117
118 void ChatScene::rectChanged(const QRectF &rect) {
119   firstColHandle->sceneRectChanged(rect);
120   secondColHandle->sceneRectChanged(rect);
121 }
122
123 void ChatScene::handlePositionChanged(qreal xpos) {
124   bool first = (sender() == firstColHandle);
125   qreal oldx;
126   if(first) {
127     oldx = firstColHandlePos;
128     firstColHandlePos = xpos;
129   } else {
130     oldx = secondColHandlePos;
131     secondColHandlePos = xpos;
132   }
133   QtUiSettings s;
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);
138
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());
143 }
144
145 void ChatScene::setSelectingItem(ChatItem *item) {
146   if(_selectingItem) _selectingItem->clearSelection();
147   _selectingItem = item;
148 }
149
150 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
151   _selectionStart = _selectionEnd = item->index().row();
152   _selectionStartCol = _selectionMinCol = item->index().column();
153   _isSelecting = true;
154   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
155   updateSelection(item->mapToScene(itemPos));
156 }
157
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;
163
164   int curRow = contentItem->index().row();
165   int curColumn;
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;
169
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);
175     }
176   }
177
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);
181     }
182   } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
183     for(int l = _selectionEnd; l < curRow; l++) {
184       _lines[l]->setSelected(false);
185     }
186   } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
187     for(int l = _selectionEnd; l > curRow; l--) {
188       _lines[l]->setSelected(false);
189     }
190   } else if(curRow < _selectionEnd && curRow < _selectionStart) {
191     for(int l = _selectionEnd - 1; l >= curRow; l--) {
192       _lines[l]->setSelected(true, minColumn);
193     }
194   }
195   _selectionEnd = curRow;
196
197   if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
198     _lines[curRow]->setSelected(false);
199     _isSelecting = false;
200     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
201   }
202 }
203
204 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
205   if(_isSelecting && event->buttons() & Qt::LeftButton) {
206     updateSelection(event->scenePos());
207     event->accept();
208   } else {
209     QGraphicsScene::mouseMoveEvent(event);
210   }
211 }
212
213 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
214   qDebug() << "pressed";
215   QGraphicsScene::mousePressEvent(event);
216 }
217
218 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
219   if(_isSelecting) {
220     _isSelecting = false;
221     event->accept();
222   } else {
223     QGraphicsScene::mouseReleaseEvent(event);
224   }
225 }
226