f6a5e33a4a49f8f28046a20d96bdc1ac69ba68dd
[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   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
42
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));
46     _lines.append(line);
47     addItem(line);
48   }
49
50   QtUiSettings s;
51   int defaultFirstColHandlePos = s.value("ChatView/DefaultFirstColumnHandlePos", 80).toInt();
52   int defaultSecondColHandlePos = s.value("ChatView/DefaultSecondColumnHandlePos", 200).toInt();
53
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();
58
59   firstColHandle = new ColumnHandleItem(QtUi::style()->firstColumnSeparator()); addItem(firstColHandle);
60   secondColHandle = new ColumnHandleItem(QtUi::style()->secondColumnSeparator()); addItem(secondColHandle);
61
62   connect(firstColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
63   connect(secondColHandle, SIGNAL(positionChanged(qreal)), this, SLOT(handlePositionChanged(qreal)));
64
65   firstColHandle->setXPos(firstColHandlePos);
66   firstColHandle->setXLimits(0, secondColHandlePos);
67   secondColHandle->setXPos(secondColHandlePos);
68   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
69
70   emit heightChanged(height());
71 }
72
73 ChatScene::~ChatScene() {
74
75
76 }
77
78 void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
79   Q_UNUSED(index);
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
83   qreal h = 0;
84   qreal y = 0;
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);
89     addItem(line);
90     if(_width > 0) {
91       line->setPos(0, y+h);
92       h += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
93     }
94   }
95   if(h > 0) {
96     _height += h;
97     for(int i = end+1; i < _lines.count(); i++) {
98       _lines.value(i)->moveBy(0, h);
99     }
100     setSceneRect(QRectF(0, 0, _width, _height));
101     emit heightChanged(_height);
102   }
103 }
104
105 void ChatScene::setWidth(qreal w) {
106   _width = w;
107   _height = 0;
108   foreach(ChatLine *line, _lines) {
109     line->setPos(0, _height);
110     _height += line->setGeometry(_width, firstColHandlePos, secondColHandlePos);
111   }
112   setSceneRect(QRectF(0, 0, w, _height));
113   secondColHandle->setXLimits(firstColHandlePos, width() - minContentsWidth);
114   emit heightChanged(_height);
115 }
116
117 void ChatScene::rectChanged(const QRectF &rect) {
118   firstColHandle->sceneRectChanged(rect);
119   secondColHandle->sceneRectChanged(rect);
120 }
121
122 void ChatScene::handlePositionChanged(qreal xpos) {
123   bool first = (sender() == firstColHandle);
124   qreal oldx;
125   if(first) {
126     oldx = firstColHandlePos;
127     firstColHandlePos = xpos;
128   } else {
129     oldx = secondColHandlePos;
130     secondColHandlePos = xpos;
131   }
132   QtUiSettings s;
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);
137
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());
142 }
143
144 void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
145
146   QGraphicsScene::mouseMoveEvent(event);
147 }
148
149 void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
150
151   QGraphicsScene::mousePressEvent(event);
152 }
153
154 void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
155
156   QGraphicsScene::mouseReleaseEvent(event);
157 }
158
159 void ChatScene::setSelectingItem(ChatItem *item) {
160   if(_selectingItem) _selectingItem->clearSelection();
161   _selectingItem = item;
162 }
163
164 void ChatScene::startGlobalSelection(ChatItem *item) {
165
166
167 }
168