fd31645f64be711ffc13f00d17127857da1f4aa8
[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 <QApplication>
22 #include <QGraphicsTextItem>
23 #include <QScrollBar>
24
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     _currentScaleFactor(1)
36 {
37   QList<BufferId> filterList;
38   filterList.append(bufferId);
39   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
40   init(filter);
41 }
42
43 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
44   : QGraphicsView(parent),
45     AbstractChatView(),
46     _currentScaleFactor(1)
47 {
48   init(filter);
49 }
50
51 void ChatView::init(MessageFilter *filter) {
52   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
53   setAlignment(Qt::AlignBottom);
54   setInteractive(true);
55   //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
56   // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
57   setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
58   // setTransformationAnchor(QGraphicsView::NoAnchor);
59   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
60
61   _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 2, this); // see below: resizeEvent()
62   connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
63   connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
64   setScene(_scene);
65   // installEventFilter(_scene);
66
67   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
68 }
69
70 void ChatView::resizeEvent(QResizeEvent *event) {
71   QGraphicsView::resizeEvent(event);
72
73   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
74   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
75
76   // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
77   // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
78   scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
79
80   _lastScrollbarPos = verticalScrollBar()->maximum();
81   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
82 }
83
84 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
85   Q_UNUSED(chatLine)
86   QAbstractSlider *vbar = verticalScrollBar();
87   Q_ASSERT(vbar);
88   if(QApplication::mouseButtons() == Qt::NoButton
89   && vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
90     vbar->setValue(vbar->maximum());
91   }
92 }
93
94 void ChatView::verticalScrollbarChanged(int newPos) {
95   QAbstractSlider *vbar = verticalScrollBar();
96   Q_ASSERT(vbar);
97
98   // check for backlog request
99   if(newPos < _lastScrollbarPos) {
100     int relativePos = 100;
101     if(vbar->maximum() - vbar->minimum() != 0)
102       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
103
104     if(relativePos < 20) {
105       scene()->requestBacklog();
106     }
107   }
108   _lastScrollbarPos = newPos;
109 }
110
111 MsgId ChatView::lastMsgId() const {
112   if(!scene())
113     return MsgId();
114
115   QAbstractItemModel *model = scene()->model();
116   if(!model || model->rowCount() == 0)
117     return MsgId();
118
119   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
120 }
121
122 void ChatView::zoomIn() {
123     _currentScaleFactor *= 1.2;
124     scale(1.2, 1.2);
125     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
126 }
127
128 void ChatView::zoomOut() {
129     _currentScaleFactor /= 1.2;
130     scale(1 / 1.2, 1 / 1.2);
131     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
132 }
133
134 void ChatView::zoomNormal() {
135     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
136     _currentScaleFactor = 1;
137     scene()->setWidth(viewport()->width() - 2);
138 }