77a0976f42d3b71ea7b30c432a06b952534bc724
[quassel.git] / src / qtui / chatview.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 <QGraphicsTextItem>
22 #include <QScrollBar>
23
24 #include "buffer.h"
25 #include "chatlinemodelitem.h"
26 #include "chatscene.h"
27 #include "chatview.h"
28 #include "client.h"
29 #include "messagefilter.h"
30 #include "quasselui.h"
31
32 ChatView::ChatView(BufferId bufferId, QWidget *parent)
33   : QGraphicsView(parent),
34     AbstractChatView()
35 {
36   QList<BufferId> filterList;
37   filterList.append(bufferId);
38   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
39   init(filter);
40
41 }
42
43 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
44   : QGraphicsView(parent),
45     AbstractChatView()
46 {
47   init(filter);
48 }
49
50 void ChatView::init(MessageFilter *filter) {
51   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52   setAlignment(Qt::AlignBottom);
53   setInteractive(true);
54
55   _scene = new ChatScene(filter, filter->idString(), this);
56   connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
57   setScene(_scene);
58
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)));
62 }
63
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());
67 }
68
69 void ChatView::sceneHeightChanged(qreal h) {
70   Q_UNUSED(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());
74 }
75
76 void ChatView::setBufferForBacklogFetching(BufferId id) {
77   scene()->setBufferForBacklogFetching(id);
78 }
79
80 void ChatView::sliderPressed() {
81   verticalScrollbarChanged(verticalScrollBar()->value());
82 }
83
84 void ChatView::sliderReleased() {
85   if(scene()->isFetchingBacklog()) scene()->setIsFetchingBacklog(false);
86 }
87
88 void ChatView::verticalScrollbarChanged(int newPos) {
89   Q_UNUSED(newPos);
90   if(!scene()->isBacklogFetchingEnabled()) return;
91
92   QAbstractSlider *vbar = verticalScrollBar();
93   if(!vbar)
94     return;
95   if(vbar->isSliderDown()) {
96     /*
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);
101     */
102     scene()->setIsFetchingBacklog(vbar->value() == vbar->minimum());
103   }
104 }
105
106 MsgId ChatView::lastMsgId() const {
107   if(!scene())
108     return MsgId();
109
110   QAbstractItemModel *model = scene()->model();
111   if(!model || model->rowCount() == 0)
112     return MsgId();
113
114   
115   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
116 }