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