Clean up MessageModel on disconnect
[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 <QApplication>
22 #include <QClipboard>
23 #include <QGraphicsSceneMouseEvent>
24 #include <QPersistentModelIndex>
25
26 #include "buffer.h"
27 #include "chatitem.h"
28 #include "chatlinemodelitem.h"
29 #include "chatscene.h"
30 #include "columnhandleitem.h"
31 #include "qtui.h"
32 #include "qtuisettings.h"
33
34 const qreal minContentsWidth = 200;
35
36 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
37   : QGraphicsScene(parent),
38   _idString(idString),
39   _model(model)
40 {
41   _width = 0;
42   _selectingItem = 0;
43   _isSelecting = false;
44   _selectionStart = -1;
45   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
46
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));
51     _lines.append(line);
52     addItem(line);
53   }
54
55   QtUiSettings s;
56   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
57   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
58
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();
63
64   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
65   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
66
67   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
68   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
69
70   firstColHandle->setXPos(firstColHandlePos);
71   firstColHandle->setXLimits(0, secondColHandlePos);
72   secondColHandle->setXPos(secondColHandlePos);
73   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
74
75   emit heightChanged(height());
76 }
77
78 ChatScene::~ChatScene() {
79
80 }
81
82 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
83   Q_UNUSED(index);
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
87   qreal h = 0;
88   qreal y = 0;
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);
93     addItem(line);
94     if(_width > 0) {
95       line->setPos(0, y+h);
96       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
97     }
98   }
99   if(h > 0) {
100     _height += h;
101     for(int i = end+1; i < _lines.count(); i++) {
102       _lines.value(i)->moveBy(0, h);
103     }
104     setSceneRect(QRectF(0, 0, _width, _height));
105     emit heightChanged(_height);
106   }
107 }
108
109 void ChatScene::modelReset() {
110   foreach(ChatLine *line, _lines) {
111     removeItem(line);
112     delete line;
113   }
114   _lines.clear();
115   setSceneRect(QRectF(0, 0, _width, 0));
116 }
117
118 void ChatScene::setWidth(qreal w) {
119   _width = w;
120   _height = 0;
121   foreach(ChatLine *line, _lines) {
122     line->setPos(0, _height);
123     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
124   }
125   setSceneRect(QRectF(0, 0, w, _height));
126   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
127   emit heightChanged(_height);
128 }
129
130 void ChatScene::rectChanged(const QRectF &rect) {
131   firstColHandle->sceneRectChanged(rect);
132   secondColHandle->sceneRectChanged(rect);
133 }
134
135 void ChatScene::handlePositionChanged(qreal xpos) {
136   bool first = (sender() == firstColHandle);
137   qreal oldx;
138   if(first) {
139     oldx = firstColHandlePos;
140     firstColHandlePos = xpos;
141   } else {
142     oldx = secondColHandlePos;
143     secondColHandlePos = xpos;
144   }
145   QtUiSettings s;
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);
150
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());
155 }
156
157 void ChatScene::setSelectingItem(ChatItem *item) {
158   if(_selectingItem) _selectingItem->clearSelection();
159   _selectingItem = item;
160 }
161
162 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
163   _selectionStart = _selectionEnd = item->index().row();
164   _selectionStartCol = _selectionMinCol = item->index().column();
165   _isSelecting = true;
166   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
167   updateSelection(item->mapToScene(itemPos));
168 }
169
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;
175
176   int curRow = contentItem->index().row();
177   int curColumn;
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;
181
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);
187     }
188   }
189
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);
193     }
194   } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
195     for(int l = _selectionEnd; l < curRow; l++) {
196       _lines[l]->setSelected(false);
197     }
198   } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
199     for(int l = _selectionEnd; l > curRow; l--) {
200       _lines[l]->setSelected(false);
201     }
202   } else if(curRow < _selectionEnd && curRow < _selectionStart) {
203     for(int l = _selectionEnd - 1; l >= curRow; l--) {
204       _lines[l]->setSelected(true, minColumn);
205     }
206   }
207   _selectionEnd = curRow;
208
209   if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
210     _lines[curRow]->setSelected(false);
211     _isSelecting = false;
212     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
213   }
214 }
215
216 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
217   if(_isSelecting && event->buttons() & Qt::LeftButton) {
218     updateSelection(event->scenePos());
219     event->accept();
220   } else {
221     QGraphicsScene::mouseMoveEvent(event);
222   }
223 }
224
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);
229     }
230     _selectionStart = -1;
231     event->accept();
232   } else {
233     QGraphicsScene::mousePressEvent(event);
234   }
235 }
236
237 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
238   if(_isSelecting) {
239 #   ifdef Q_WS_X11
240       QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
241 #   endif
242 //# else
243       QApplication::clipboard()->setText(selectionToString());
244 //# endif
245     _isSelecting = false;
246     event->accept();
247   } else {
248     QGraphicsScene::mouseReleaseEvent(event);
249   }
250 }
251
252 //!\brief Convert current selection to human-readable string.
253 QString ChatScene::selectionToString() const {
254   //TODO Make selection format configurable!
255   if(!_isSelecting) return "";
256   QString result;
257   for(int l = _selectionStart; l <= _selectionEnd; l++) {
258     if(_selectionMinCol == ChatLineModel::TimestampColumn)
259       result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
260     if(_selectionMinCol <= ChatLineModel::SenderColumn)
261       result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
262     result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";
263   }
264   return result;
265 }