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