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 <QGraphicsTextItem>
25 #include "chatlinemodelitem.h"
26 #include "chatscene.h"
29 #include "messagefilter.h"
30 #include "quasselui.h"
32 ChatView::ChatView(BufferId bufferId, QWidget *parent)
33 : QGraphicsView(parent),
36 QList<BufferId> filterList;
37 filterList.append(bufferId);
38 MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
43 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
44 : QGraphicsView(parent),
50 void ChatView::init(MessageFilter *filter) {
51 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52 setAlignment(Qt::AlignBottom);
55 _scene = new ChatScene(filter, filter->idString(), this);
56 connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
59 connect(verticalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
60 connect(verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
61 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
64 void ChatView::resizeEvent(QResizeEvent *event) {
65 scene()->setWidth(event->size().width() - 2); // FIXME figure out why we have to hardcode the -2 here
66 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
69 void ChatView::sceneHeightChanged(qreal h) {
71 bool scrollable = qAbs(verticalScrollBar()->value() - verticalScrollBar()->maximum()) <= 2; // be a bit tolerant here, also FIXME (why we need this?)
72 setSceneRect(scene()->sceneRect());
73 if(scrollable) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
76 void ChatView::setBufferForBacklogFetching(BufferId id) {
77 scene()->setBufferForBacklogFetching(id);
80 void ChatView::sliderPressed() {
81 verticalScrollbarChanged(verticalScrollBar()->value());
84 void ChatView::sliderReleased() {
85 if(scene()->isFetchingBacklog()) scene()->setIsFetchingBacklog(false);
88 void ChatView::verticalScrollbarChanged(int newPos) {
90 if(!scene()->isBacklogFetchingEnabled()) return;
92 QAbstractSlider *vbar = verticalScrollBar();
95 if(vbar->isSliderDown()) {
97 int relativePos = 100;
98 if(vbar->maximum() - vbar->minimum() != 0)
99 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
100 scene()->setIsFetchingBacklog(relativePos < 20);
102 scene()->setIsFetchingBacklog(vbar->value() == vbar->minimum());
106 MsgId ChatView::lastMsgId() const {
110 QAbstractItemModel *model = scene()->model();
111 if(!model || model->rowCount() == 0)
115 return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();