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),
41 connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
43 connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
44 for(int i = 0; i < model->rowCount(); i++) {
45 ChatLine *line = new ChatLine(model->index(i, 0));
51 int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
52 int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
54 firstColHandlePos = s.value(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString),
55 defaultFirstColHandlePos).toInt();
56 secondColHandlePos = s.value(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString),
57 defaultSecondColHandlePos).toInt();
59 firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
60 secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
62 connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
63 connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
65 firstColHandle->setXPos(firstColHandlePos);
66 firstColHandle->setXLimits(0, secondColHandlePos);
67 secondColHandle->setXPos(secondColHandlePos);
68 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
70 emit heightChanged(height());
73 ChatScene::~ChatScene() {
78 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
80 // maybe make this more efficient by prepending stuff with negative yval
81 // dunno if that's worth not guranteeing that 0 is on the top...
82 // TODO bulk inserts, iterators
85 if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
86 for(int i = start; i <= end; i++) {
87 ChatLine *line = new ChatLine(model()->index(i, 0));
88 _lines.insert(i, line);
92 h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
97 for(int i = end+1; i < _lines.count(); i++) {
98 _lines.value(i)->moveBy(0, h);
100 setSceneRect(QRectF(0, 0, _width, _height));
101 emit heightChanged(_height);
105 void ChatScene::setWidth(qreal w) {
108 foreach(ChatLine *line, _lines) {
109 line->setPos(0, _height);
110 _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
112 setSceneRect(QRectF(0, 0, w, _height));
113 secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
114 emit heightChanged(_height);
117 void ChatScene::rectChanged(const QRectF &rect) {
118 firstColHandle->sceneRectChanged(rect);
119 secondColHandle->sceneRectChanged(rect);
122 void ChatScene::handlePositionChanged(qreal xpos) {
123 bool first = (sender() == firstColHandle);
126 oldx = firstColHandlePos;
127 firstColHandlePos = xpos;
129 oldx = secondColHandlePos;
130 secondColHandlePos = xpos;
133 s.setValue(QString("ChatView/%1/FirstColumnHandlePos").arg(_idString), firstColHandlePos);
134 s.setValue(QString("ChatView/%1/SecondColumnHandlePos").arg(_idString), secondColHandlePos);
135 s.setValue(QString("ChatView/DefaultFirstColumnHandlePos"), firstColHandlePos);
136 s.setValue(QString("ChatView/DefaultSecondColumnHandlePos"), secondColHandlePos);
138 setWidth(width()); // readjust all chatlines
139 // we get ugly redraw errors if we don't update this explicitly... :(
140 // width() should be the same for both handles, so just use firstColHandle regardless
141 update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
144 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
146 QGraphicsScene::mouseMoveEvent(event);
149 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
151 QGraphicsScene::mousePressEvent(event);
154 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
156 QGraphicsScene::mouseReleaseEvent(event);
159 void ChatScene::setSelectingItem(ChatItem *item) {
160 if(_selectingItem) _selectingItem->clearSelection();
161 _selectingItem = item;
164 void ChatScene::startGlobalSelection(ChatItem *item) {