1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QGraphicsSceneMouseEvent>
22 #include <QPersistentModelIndex>
26 #include "chatlinemodelitem.h"
27 #include "chatscene.h"
28 #include "columnhandleitem.h"
30 #include "qtuisettings.h"
32 const qreal minContentsWidth = 200;
34 ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent)
35 : QGraphicsScene(parent),
40 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
42 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
43 for(int i = 0; i < model->rowCount(); i++) {
44 ChatLine *line = new ChatLine(model->index(i, 0));
50 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
51 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
53 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
54 defaultFirstColHandlePos).toInt();
55 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
56 defaultSecondColHandlePos).toInt();
58 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
59 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
61 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
62 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
64 firstColHandle->setXPos(firstColHandlePos);
65 firstColHandle->setXLimits(0, secondColHandlePos);
66 secondColHandle->setXPos(secondColHandlePos);
67 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
69 emit heightChanged(height());
72 ChatScene::~ChatScene() {
77 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
79 // maybe make this more efficient by prepending stuff with negative yval
80 // dunno if that's worth not guranteeing that 0 is on the top...
81 // TODO bulk inserts, iterators
84 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
85 for(int i = start; i <= end; i++) {
86 ChatLine *line = new ChatLine(model()->index(i, 0));
87 _lines.insert(i, line);
91 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
96 for(int i = end+1; i < _lines.count(); i++) {
97 _lines.value(i)->moveBy(0, h);
99 setSceneRect(QRectF(0, 0, _width, _height));
100 emit heightChanged(_height);
104 void ChatScene::setWidth(qreal w) {
107 foreach(ChatLine *line, _lines) {
108 line->setPos(0, _height);
109 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
111 setSceneRect(QRectF(0, 0, w, _height));
112 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
113 emit heightChanged(_height);
116 void ChatScene::rectChanged(const QRectF &rect) {
117 firstColHandle->sceneRectChanged(rect);
118 secondColHandle->sceneRectChanged(rect);
121 void ChatScene::handlePositionChanged(qreal xpos) {
122 bool first = (sender() == firstColHandle);
125 oldx = firstColHandlePos;
126 firstColHandlePos = xpos;
128 oldx = secondColHandlePos;
129 secondColHandlePos = xpos;
132 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
133 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
134 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
135 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
137 setWidth(width()); // readjust all chatlines
138 // we get ugly redraw errors if we don't update this explicitly... :(
139 // width() should be the same for both handles, so just use firstColHandle regardless
140 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());