the alias engine can now expand nicks to their hostnames
[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 "chatline.h"
29 #include "chatlinemodelitem.h"
30 #include "chatscene.h"
31 #include "client.h"
32 #include "clientbacklogmanager.h"
33 #include "columnhandleitem.h"
34 #include "qtui.h"
35 #include "qtuisettings.h"
36
37 const qreal minContentsWidth = 200;
38
39 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
40   : QGraphicsScene(parent),
41   _idString(idString),
42   _model(model)
43 {
44   _width = 0;
45   _selectingItem = 0;
46   _isSelecting = false;
47   _selectionStart = -1;
48   _fetchingBacklog = false;
49
50   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
51
52   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
53   connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset()));
54   for(int i = 0; i < model->rowCount(); i++) {
55     ChatLine *line = new ChatLine(i, model);
56     _lines.append(line);
57     addItem(line);
58   }
59
60   QtUiSettings s;
61   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
62   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
63
64   firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
65                                defaultFirstColHandlePos).toInt();
66   secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
67                                 defaultSecondColHandlePos).toInt();
68
69   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
70   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
71
72   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
73   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
74
75   firstColHandle->setXPos(firstColHandlePos);
76   firstColHandle->setXLimits(0, secondColHandlePos);
77   secondColHandle->setXPos(secondColHandlePos);
78   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
79
80   emit heightChanged(height());
81 }
82
83 ChatScene::~ChatScene() {
84
85 }
86
87 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
88   Q_UNUSED(index);
89   // maybe make this more efficient by prepending stuff with negative yval
90   // dunno if that's worth not guranteeing that 0 is on the top...
91   // TODO bulk inserts, iterators
92   qreal h = 0;
93   qreal y = 0;
94   if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
95   for(int i = start; i <= end; i++) {
96     ChatLine *line = new ChatLine(i, model());
97     _lines.insert(i, line);
98     addItem(line);
99     if(_width > 0) {
100       line->setPos(0, y+h);
101       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
102     }
103   }
104   // update existing items
105   for(int i = end+1; i < _lines.count(); i++) {
106     _lines[i]->setRow(i);
107   }
108
109   if(h > 0) {
110     _height += h;
111     for(int i = end+1; i < _lines.count(); i++) {
112       _lines.value(i)->moveBy(0, h);
113     }
114     setSceneRect(QRectF(0, 0, _width, _height));
115     emit heightChanged(_height);
116   }
117
118   requestBacklogIfNeeded();
119 }
120
121 void ChatScene::modelReset() {
122   foreach(ChatLine *line, _lines) {
123     removeItem(line);
124     delete line;
125   }
126   _lines.clear();
127   setSceneRect(QRectF(0, 0, _width, 0));
128 }
129
130 void ChatScene::setWidth(qreal w) {
131   _width = w;
132   _height = 0;
133   foreach(ChatLine *line, _lines) {
134     line->setPos(0, _height);
135     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
136   }
137   setSceneRect(QRectF(0, 0, w, _height));
138   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
139   emit heightChanged(_height);
140 }
141
142 void ChatScene::rectChanged(const QRectF &rect) {
143   firstColHandle->sceneRectChanged(rect);
144   secondColHandle->sceneRectChanged(rect);
145 }
146
147 void ChatScene::handlePositionChanged(qreal xpos) {
148   bool first = (sender() == firstColHandle);
149   qreal oldx;
150   if(first) {
151     oldx = firstColHandlePos;
152     firstColHandlePos = xpos;
153   } else {
154     oldx = secondColHandlePos;
155     secondColHandlePos = xpos;
156   }
157   QtUiSettings s;
158   s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
159   s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
160   s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
161   s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
162
163   setWidth(width());  // readjust all chatlines
164   // we get ugly redraw errors if we don't update this explicitly... :(
165   // width() should be the same for both handles, so just use firstColHandle regardless
166   update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
167 }
168
169 void ChatScene::setSelectingItem(ChatItem *item) {
170   if(_selectingItem) _selectingItem->clearSelection();
171   _selectingItem = item;
172 }
173
174 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
175   _selectionStart = _selectionEnd = item->row();
176   _selectionStartCol = _selectionMinCol = item->column();
177   _isSelecting = true;
178   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
179   updateSelection(item->mapToScene(itemPos));
180 }
181
182 void ChatScene::updateSelection(const QPointF &pos) {
183   // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
184   // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
185   ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
186   if(!contentItem) return;
187
188   int curRow = contentItem->row();
189   int curColumn;
190   if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
191   else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
192   else curColumn = ChatLineModel::TimestampColumn;
193
194   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
195   if(minColumn != _selectionMinCol) {
196     _selectionMinCol = minColumn;
197     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
198       _lines[l]->setSelected(true, minColumn);
199     }
200   }
201
202   if(curRow > _selectionEnd && curRow > _selectionStart) {  // select further towards bottom
203     for(int l = _selectionEnd + 1; l <= curRow; l++) {
204       _lines[l]->setSelected(true, minColumn);
205     }
206   } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
207     for(int l = _selectionEnd; l < curRow; l++) {
208       _lines[l]->setSelected(false);
209     }
210   } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
211     for(int l = _selectionEnd; l > curRow; l--) {
212       _lines[l]->setSelected(false);
213     }
214   } else if(curRow < _selectionEnd && curRow < _selectionStart) {
215     for(int l = _selectionEnd - 1; l >= curRow; l--) {
216       _lines[l]->setSelected(true, minColumn);
217     }
218   }
219   _selectionEnd = curRow;
220
221   if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
222     _lines[curRow]->setSelected(false);
223     _isSelecting = false;
224     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
225   }
226 }
227
228 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
229   if(_isSelecting && event->buttons() & Qt::LeftButton) {
230     updateSelection(event->scenePos());
231     event->accept();
232   } else {
233     QGraphicsScene::mouseMoveEvent(event);
234   }
235 }
236
237 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
238   if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
239     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
240       _lines[l]->setSelected(false);
241     }
242     _selectionStart = -1;
243     event->accept();
244   } else {
245     QGraphicsScene::mousePressEvent(event);
246   }
247 }
248
249 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
250   if(_isSelecting) {
251 #   ifdef Q_WS_X11
252       QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
253 #   endif
254 //# else
255       QApplication::clipboard()->setText(selectionToString());
256 //# endif
257     _isSelecting = false;
258     event->accept();
259   } else {
260     QGraphicsScene::mouseReleaseEvent(event);
261   }
262 }
263
264 //!\brief Convert current selection to human-readable string.
265 QString ChatScene::selectionToString() const {
266   //TODO Make selection format configurable!
267   if(!_isSelecting) return QString();
268   int start = qMin(_selectionStart, _selectionEnd);
269   int end = qMax(_selectionStart, _selectionEnd);
270   if(start < 0 || end >= _lines.count()) {
271     qDebug() << "Invalid selection range:" << start << end;
272     return QString();
273   }
274   QString result;
275   for(int l = start; l <= end; l++) {
276     if(_selectionMinCol == ChatLineModel::TimestampColumn)
277       result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
278     if(_selectionMinCol <= ChatLineModel::SenderColumn)
279       result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
280     result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
281   }
282   return result;
283 }
284
285 void ChatScene::setIsFetchingBacklog(bool fetch) {
286   if(!isBacklogFetchingEnabled()) return;
287
288   if(!fetch) {
289     _fetchingBacklog = false;
290   } else {
291     _fetchingBacklog = true;
292     requestBacklogIfNeeded();
293   }
294 }
295
296 void ChatScene::requestBacklogIfNeeded() {
297   const int REQUEST_COUNT = 50;
298
299   if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
300
301   MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
302   if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
303     Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
304     _lastBacklogOffset = msgId;
305     _lastBacklogSize = model()->rowCount();
306   }
307 }
308
309 int ChatScene::sectionByScenePos(int x) {
310   if(x < firstColHandlePos)
311     return ChatLineModel::TimestampColumn;
312   if(x < secondColHandlePos)
313     return ChatLineModel::SenderColumn;
314
315   return ChatLineModel::ContentsColumn;
316 }