Columns are now moveable!
[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
31 const qreal minContentsWidth = 200;
32
33 ChatScene::ChatScene(QAbstractItemModel *model, QObject *parent) : QGraphicsScene(parent), _model(model) {
34   _width = 0;
35   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
36
37   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
38   for(int i = 0; i < model->rowCount(); i++) {
39     ChatLine *line = new ChatLine(model->index(i, 0));
40     _lines.append(line);
41     addItem(line);
42   }
43
44   firstColHandlePos = 80;
45   secondColHandlePos = 200;
46
47   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
48   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
49
50   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
51   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
52
53   firstColHandle->setXPos(firstColHandlePos);
54   firstColHandle->setXLimits(0, secondColHandlePos);
55   secondColHandle->setXPos(secondColHandlePos);
56   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
57
58   emit heightChanged(height());
59 }
60
61 ChatScene::~ChatScene() {
62
63
64 }
65
66 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
67   Q_UNUSED(index);
68   // maybe make this more efficient by prepending stuff with negative yval
69   // dunno if that's worth not guranteeing that 0 is on the top...
70   // TODO bulk inserts, iterators
71   qreal h = 0;
72   qreal y = 0;
73   if(_width && start > 0) y = _lines.value(start - 1)->y() + _lines.value(start - 1)->height();
74   for(int i = start; i <= end; i++) {
75     ChatLine *line = new ChatLine(model()->index(i, 0));
76     _lines.insert(i, line);
77     addItem(line);
78     if(_width > 0) {
79       line->setPos(0, y+h);
80       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
81     }
82   }
83   if(h > 0) {
84     _height += h;
85     for(int i = end+1; i < _lines.count(); i++) {
86       _lines.value(i)->moveBy(0, h);
87     }
88     setSceneRect(QRectF(0, 0, _width, _height));
89     emit heightChanged(_height);
90   }
91 }
92
93 void ChatScene::setWidth(qreal w) {
94   _width = w;
95   _height = 0;
96   foreach(ChatLine *line, _lines) {
97     line->setPos(0, _height);
98     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
99   }
100   setSceneRect(QRectF(0, 0, w, _height));
101   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
102   emit heightChanged(_height);
103 }
104
105 void ChatScene::rectChanged(const QRectF &rect) {
106   firstColHandle->sceneRectChanged(rect);
107   secondColHandle->sceneRectChanged(rect);
108 }
109
110 void ChatScene::handlePositionChanged(qreal xpos) {
111   bool first = (sender() == firstColHandle);
112   qreal oldx;
113   if(first) {
114     oldx = firstColHandlePos;
115     firstColHandlePos = xpos;
116   } else {
117     oldx = secondColHandlePos;
118     secondColHandlePos = xpos;
119   }
120   setWidth(width());  // readjust all chatlines
121   // we get ugly redraw errors if we don't update this explicitly... :(
122   // width() should be the same for both handles, so just use firstColHandle regardless
123   update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
124 }