Temporarily disable topic to make quassel compile again after sputdev merge
[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   for(int i = 0; i < model->rowCount(); i++) {
49     ChatLine *line = new ChatLine(model->index(i, 0));
50     _lines.append(line);
51     addItem(line);
52   }
53
54   QtUiSettings s;
55   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
56   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
57
58   firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
59                                defaultFirstColHandlePos).toInt();
60   secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
61                                 defaultSecondColHandlePos).toInt();
62
63   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
64   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
65
66   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
67   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
68
69   firstColHandle->setXPos(firstColHandlePos);
70   firstColHandle->setXLimits(0, secondColHandlePos);
71   secondColHandle->setXPos(secondColHandlePos);
72   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
73
74   emit heightChanged(height());
75 }
76
77 ChatScene::~ChatScene() {
78
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::setWidth(qreal w) {
110   _width = w;
111   _height = 0;
112   foreach(ChatLine *line, _lines) {
113     line->setPos(0, _height);
114     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
115   }
116   setSceneRect(QRectF(0, 0, w, _height));
117   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
118   emit heightChanged(_height);
119 }
120
121 void ChatScene::rectChanged(const QRectF &rect) {
122   firstColHandle->sceneRectChanged(rect);
123   secondColHandle->sceneRectChanged(rect);
124 }
125
126 void ChatScene::handlePositionChanged(qreal xpos) {
127   bool first = (sender() == firstColHandle);
128   qreal oldx;
129   if(first) {
130     oldx = firstColHandlePos;
131     firstColHandlePos = xpos;
132   } else {
133     oldx = secondColHandlePos;
134     secondColHandlePos = xpos;
135   }
136   QtUiSettings s;
137   s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
138   s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
139   s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
140   s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
141
142   setWidth(width());  // readjust all chatlines
143   // we get ugly redraw errors if we don't update this explicitly... :(
144   // width() should be the same for both handles, so just use firstColHandle regardless
145   update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
146 }
147
148 void ChatScene::setSelectingItem(ChatItem *item) {
149   if(_selectingItem) _selectingItem->clearSelection();
150   _selectingItem = item;
151 }
152
153 void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
154   _selectionStart = _selectionEnd = item->index().row();
155   _selectionStartCol = _selectionMinCol = item->index().column();
156   _isSelecting = true;
157   _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
158   updateSelection(item->mapToScene(itemPos));
159 }
160
161 void ChatScene::updateSelection(const QPointF &pos) {
162   // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
163   // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
164   ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
165   if(!contentItem) return;
166
167   int curRow = contentItem->index().row();
168   int curColumn;
169   if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
170   else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
171   else curColumn = ChatLineModel::TimestampColumn;
172
173   ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
174   if(minColumn != _selectionMinCol) {
175     _selectionMinCol = minColumn;
176     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
177       _lines[l]->setSelected(true, minColumn);
178     }
179   }
180
181   if(curRow > _selectionEnd && curRow > _selectionStart) {  // select further towards bottom
182     for(int l = _selectionEnd + 1; l <= curRow; l++) {
183       _lines[l]->setSelected(true, minColumn);
184     }
185   } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
186     for(int l = _selectionEnd; l < curRow; l++) {
187       _lines[l]->setSelected(false);
188     }
189   } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
190     for(int l = _selectionEnd; l > curRow; l--) {
191       _lines[l]->setSelected(false);
192     }
193   } else if(curRow < _selectionEnd && curRow < _selectionStart) {
194     for(int l = _selectionEnd - 1; l >= curRow; l--) {
195       _lines[l]->setSelected(true, minColumn);
196     }
197   }
198   _selectionEnd = curRow;
199
200   if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
201     _lines[curRow]->setSelected(false);
202     _isSelecting = false;
203     _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
204   }
205 }
206
207 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
208   if(_isSelecting && event->buttons() & Qt::LeftButton) {
209     updateSelection(event->scenePos());
210     event->accept();
211   } else {
212     QGraphicsScene::mouseMoveEvent(event);
213   }
214 }
215
216 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
217   if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) {
218     for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
219       _lines[l]->setSelected(false);
220     }
221     _selectionStart = -1;
222     event->accept();
223   } else {
224     QGraphicsScene::mousePressEvent(event);
225   }
226 }
227
228 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
229   if(_isSelecting) {
230 #   ifdef Q_WS_X11
231       QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
232 #   endif
233 //# else
234       QApplication::clipboard()->setText(selectionToString());
235 //# endif
236     _isSelecting = false;
237     event->accept();
238   } else {
239     QGraphicsScene::mouseReleaseEvent(event);
240   }
241 }
242
243 //!\brief Convert current selection to human-readable string.
244 QString ChatScene::selectionToString() const {
245   //TODO Make selection format configurable!
246   if(!_isSelecting) return "";
247   QString result;
248   for(int l = _selectionStart; l <= _selectionEnd; l++) {
249     if(_selectionMinCol == ChatLineModel::TimestampColumn)
250       result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " ";
251     if(_selectionMinCol <= ChatLineModel::SenderColumn)
252       result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " ";
253     result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n";
254   }
255   return result;
256 }