e8a06f13fab59856b1f9fae4bf5bdbad7a174392
[quassel.git] / src / qtui / chatview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 <QKeyEvent>
23 #include <QMenu>
24 #include <QScrollBar>
25
26 #include "bufferwidget.h"
27 #include "chatscene.h"
28 #include "chatview.h"
29 #include "client.h"
30 #include "messagefilter.h"
31 #include "qtui.h"
32 #include "qtuistyle.h"
33
34 ChatView::ChatView(BufferId bufferId, QWidget *parent)
35   : QGraphicsView(parent),
36     AbstractChatView(),
37     _bufferContainer(0),
38     _currentScaleFactor(1)
39 {
40   QList<BufferId> filterList;
41   filterList.append(bufferId);
42   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
43   init(filter);
44 }
45
46 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
47   : QGraphicsView(parent),
48     AbstractChatView(),
49     _bufferContainer(0),
50     _currentScaleFactor(1)
51 {
52   init(filter);
53 }
54
55 void ChatView::init(MessageFilter *filter) {
56   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57   setAlignment(Qt::AlignBottom|Qt::AlignLeft);
58   setInteractive(true);
59   //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
60   // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
61   setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
62   // setTransformationAnchor(QGraphicsView::NoAnchor);
63   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
64
65   _scrollTimer.setInterval(100);
66   _scrollTimer.setSingleShot(true);
67   connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
68
69   _resizeTimer.setInterval(100);
70   _resizeTimer.setSingleShot(true);
71   connect(&_resizeTimer, SIGNAL(timeout()), SLOT(resizeTimerTimeout()));
72
73   _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent()
74   connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
75   connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
76   connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
77   setScene(_scene);
78
79   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
80   connect(QtUi::style(), SIGNAL(changed()), this, SLOT(styleChanged()));
81 }
82
83 bool ChatView::event(QEvent *event) {
84   if(event->type() == QEvent::KeyPress) {
85     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
86     switch(keyEvent->key()) {
87     case Qt::Key_Up:
88     case Qt::Key_Down:
89     case Qt::Key_PageUp:
90     case Qt::Key_PageDown:
91       if(!verticalScrollBar()->isVisible()) {
92         scene()->requestBacklog();
93         return true;
94       }
95     default:
96       break;
97     }
98   }
99
100   if(event->type() == QEvent::Wheel) {
101     if(!verticalScrollBar()->isVisible()) {
102       scene()->requestBacklog();
103       return true;
104     }
105   }
106
107   return QGraphicsView::event(event);
108 }
109
110 void ChatView::resizeEvent(QResizeEvent *event) {
111   QGraphicsView::resizeEvent(event);
112   _resizeTimer.start();
113 }
114
115 void ChatView::resizeTimerTimeout() {
116
117   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
118   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
119
120   // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
121   // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
122   scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
123
124   _lastScrollbarPos = verticalScrollBar()->maximum();
125   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
126 }
127
128 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
129   int y = (int)mapFromScene(scenePos).y();
130   _scrollOffset = 0;
131   if(y < 0)
132     _scrollOffset = y;
133   else if(y > height())
134     _scrollOffset = y - height();
135
136   if(_scrollOffset && !_scrollTimer.isActive())
137     _scrollTimer.start();
138 }
139
140 void ChatView::scrollTimerTimeout() {
141   // scroll view
142   QAbstractSlider *vbar = verticalScrollBar();
143   if(_scrollOffset < 0 && vbar->value() > 0)
144     vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
145   else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
146     vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
147 }
148
149 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
150   Q_UNUSED(chatLine)
151   // disabled until further testing/discussion
152   //if(!scene()->isScrollingAllowed())
153   //  return;
154
155   QAbstractSlider *vbar = verticalScrollBar();
156   Q_ASSERT(vbar);
157   if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
158     vbar->setValue(vbar->maximum());
159   }
160 }
161
162 void ChatView::verticalScrollbarChanged(int newPos) {
163   QAbstractSlider *vbar = verticalScrollBar();
164   Q_ASSERT(vbar);
165
166   // check for backlog request
167   if(newPos < _lastScrollbarPos) {
168     int relativePos = 100;
169     if(vbar->maximum() - vbar->minimum() != 0)
170       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
171
172     if(relativePos < 20) {
173       scene()->requestBacklog();
174     }
175   }
176   _lastScrollbarPos = newPos;
177
178   // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
179   if(vbar->maximum() - newPos <= 2)
180     vbar->setValue(vbar->maximum());
181 }
182
183 void ChatView::styleChanged() {
184   scene()->layout();
185 }
186
187 MsgId ChatView::lastMsgId() const {
188   if(!scene())
189     return MsgId();
190
191   QAbstractItemModel *model = scene()->model();
192   if(!model || model->rowCount() == 0)
193     return MsgId();
194
195   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
196 }
197
198 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
199   // zoom actions
200   BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
201   if(bw) {
202     bw->addActionsToMenu(menu, pos);
203     menu->addSeparator();
204   }
205 }
206
207 void ChatView::zoomIn() {
208     _currentScaleFactor *= 1.2;
209     scale(1.2, 1.2);
210     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
211 }
212
213 void ChatView::zoomOut() {
214     _currentScaleFactor /= 1.2;
215     scale(1 / 1.2, 1 / 1.2);
216     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
217 }
218
219 void ChatView::zoomOriginal() {
220     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
221     _currentScaleFactor = 1;
222     scene()->setWidth(viewport()->width() - 2);
223 }