8d830b6f01acae3ba655ecf6159b0678e08bde3d
[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   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
41
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));
45     _lines.append(line);
46     addItem(line);
47   }
48
49   QtUiSettings s;
50   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
51   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
52
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();
57
58   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
59   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
60
61   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
62   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
63
64   firstColHandle->setXPos(firstColHandlePos);
65   firstColHandle->setXLimits(0, secondColHandlePos);
66   secondColHandle->setXPos(secondColHandlePos);
67   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
68
69   emit heightChanged(height());
70 }
71
72 ChatScene::~ChatScene() {
73
74
75 }
76
77 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
78   Q_UNUSED(index);
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
82   qreal h = 0;
83   qreal y = 0;
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);
88     addItem(line);
89     if(_width > 0) {
90       line->setPos(0, y+h);
91       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
92     }
93   }
94   if(h > 0) {
95     _height += h;
96     for(int i = end+1; i < _lines.count(); i++) {
97       _lines.value(i)->moveBy(0, h);
98     }
99     setSceneRect(QRectF(0, 0, _width, _height));
100     emit heightChanged(_height);
101   }
102 }
103
104 void ChatScene::setWidth(qreal w) {
105   _width = w;
106   _height = 0;
107   foreach(ChatLine *line, _lines) {
108     line->setPos(0, _height);
109     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
110   }
111   setSceneRect(QRectF(0, 0, w, _height));
112   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
113   emit heightChanged(_height);
114 }
115
116 void ChatScene::rectChanged(const QRectF &rect) {
117   firstColHandle->sceneRectChanged(rect);
118   secondColHandle->sceneRectChanged(rect);
119 }
120
121 void ChatScene::handlePositionChanged(qreal xpos) {
122   bool first = (sender() == firstColHandle);
123   qreal oldx;
124   if(first) {
125     oldx = firstColHandlePos;
126     firstColHandlePos = xpos;
127   } else {
128     oldx = secondColHandlePos;
129     secondColHandlePos = xpos;
130   }
131   QtUiSettings s;
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);
136
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());
141 }