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