correct tab order for the settingspages
[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   setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
61   setAlignment(Qt::AlignBottom);
62   setInteractive(true);
63   //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
64   // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
65   setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
66   // setTransformationAnchor(QGraphicsView::NoAnchor);
67   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
68
69   _scrollTimer.setInterval(100);
70   _scrollTimer.setSingleShot(true);
71   connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
72
73   _scene = new ChatScene(filter, filter->idString(), viewport()->width(), this);
74   connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(adjustSceneRect()));
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
81   // only connect if client is synched with a core
82   if(Client::isConnected())
83     connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
84 }
85
86 bool ChatView::event(QEvent *event) {
87   if(event->type() == QEvent::KeyPress) {
88     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
89     switch(keyEvent->key()) {
90     case Qt::Key_Up:
91     case Qt::Key_Down:
92     case Qt::Key_PageUp:
93     case Qt::Key_PageDown:
94       if(!verticalScrollBar()->isVisible()) {
95         scene()->requestBacklog();
96         return true;
97       }
98     default:
99       break;
100     }
101   }
102
103   if(event->type() == QEvent::Wheel) {
104     if(!verticalScrollBar()->isVisible()) {
105       scene()->requestBacklog();
106       return true;
107     }
108   }
109
110   if(event->type() == QEvent::Show) {
111     if(_invalidateFilter)
112       invalidateFilter();
113   }
114
115   return QGraphicsView::event(event);
116 }
117
118 void ChatView::resizeEvent(QResizeEvent *event) {
119   QGraphicsView::resizeEvent(event);
120
121   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
122   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
123   scene()->updateForViewport(viewport()->width(), viewport()->height());
124   adjustSceneRect();
125
126   _lastScrollbarPos = verticalScrollBar()->maximum();
127   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
128 }
129
130 // Workaround for QTBUG-6322
131 // The viewport rect gets some margins where it shouldn't, resulting in scrollbars to appear
132 void ChatView::adjustSceneRect() {
133   static qreal voffset = 0;
134   static bool offsetStable = false;
135
136   QRectF rect = scene()->sceneRect();
137   if(rect.height() <= viewport()->height()) {
138     setSceneRect(rect);
139     return;
140   }
141   if(offsetStable && rect.height() > viewport()->height())
142     setSceneRect(rect.adjusted(0, 0, 0, voffset));
143   else {
144     setSceneRect(rect);
145
146     QScrollBar *vbar = verticalScrollBar();
147     qreal sceneHeight = rect.height();
148     qreal viewHeight = vbar->maximum() + viewport()->height() - vbar->minimum();
149     if(sceneHeight != viewHeight) {
150       voffset = sceneHeight - viewHeight;
151       // qDebug() << "Adjusting ChatView offset to" << voffset << "(QTBUG-6322)";
152       if(sceneHeight + voffset <= viewport()->height()) {
153         setSceneRect(rect.adjusted(0, -voffset, 0, 0));
154         offsetStable = false;
155         return;
156       } else
157         setSceneRect(rect.adjusted(0, 0, 0, voffset));
158     }
159     if(vbar->maximum() + viewport()->height() - vbar->minimum() != sceneHeight)
160       qWarning() << "Workaround for QTBUG-6322 failed!1!!" << vbar->maximum() + viewport()->height() - vbar->minimum() << sceneHeight;
161     else
162       offsetStable = true;
163   }
164 }
165
166 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
167   int y = (int)mapFromScene(scenePos).y();
168   _scrollOffset = 0;
169   if(y < 0)
170     _scrollOffset = y;
171   else if(y > height())
172     _scrollOffset = y - height();
173
174   if(_scrollOffset && !_scrollTimer.isActive())
175     _scrollTimer.start();
176 }
177
178 void ChatView::scrollTimerTimeout() {
179   // scroll view
180   QAbstractSlider *vbar = verticalScrollBar();
181   if(_scrollOffset < 0 && vbar->value() > 0)
182     vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
183   else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
184     vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
185 }
186
187 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
188   Q_UNUSED(chatLine)
189   // disabled until further testing/discussion
190   //if(!scene()->isScrollingAllowed())
191   //  return;
192
193   QAbstractSlider *vbar = verticalScrollBar();
194   Q_ASSERT(vbar);
195   if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
196     vbar->setValue(vbar->maximum());
197   }
198 }
199
200 void ChatView::verticalScrollbarChanged(int newPos) {
201   QAbstractSlider *vbar = verticalScrollBar();
202   Q_ASSERT(vbar);
203
204   // check for backlog request
205   if(newPos < _lastScrollbarPos) {
206     int relativePos = 100;
207     if(vbar->maximum() - vbar->minimum() != 0)
208       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
209
210     if(relativePos < 20) {
211       scene()->requestBacklog();
212     }
213   }
214   _lastScrollbarPos = newPos;
215
216   // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
217   if(vbar->maximum() - newPos <= 2)
218     vbar->setValue(vbar->maximum());
219 }
220
221 MsgId ChatView::lastMsgId() const {
222   if(!scene())
223     return MsgId();
224
225   QAbstractItemModel *model = scene()->model();
226   if(!model || model->rowCount() == 0)
227     return MsgId();
228
229   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
230 }
231
232 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
233   // zoom actions
234   BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
235   if(bw) {
236     bw->addActionsToMenu(menu, pos);
237     menu->addSeparator();
238   }
239 }
240
241 void ChatView::zoomIn() {
242     _currentScaleFactor *= 1.2;
243     scale(1.2, 1.2);
244     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
245 }
246
247 void ChatView::zoomOut() {
248     _currentScaleFactor /= 1.2;
249     scale(1 / 1.2, 1 / 1.2);
250     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
251 }
252
253 void ChatView::zoomOriginal() {
254     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
255     _currentScaleFactor = 1;
256     scene()->setWidth(viewport()->width() - 2);
257 }
258
259 void ChatView::invalidateFilter() {
260   // if this is the currently selected chatview
261   // invalidate immediately
262   if(isVisible()) {
263     _scene->filter()->invalidateFilter();
264     _invalidateFilter = false;
265   }
266   // otherwise invalidate whenever the view is shown
267   else {
268     _invalidateFilter = true;
269   }
270 }