Introduce IgnoreList backend
[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 {
41   QList<BufferId> filterList;
42   filterList.append(bufferId);
43   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
44   init(filter);
45 }
46
47 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
48   : QGraphicsView(parent),
49     AbstractChatView(),
50     _bufferContainer(0),
51     _currentScaleFactor(1)
52 {
53   init(filter);
54 }
55
56 void ChatView::init(MessageFilter *filter) {
57   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
58   setAlignment(Qt::AlignBottom);
59   setInteractive(true);
60   //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
61   // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
62   setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
63   // setTransformationAnchor(QGraphicsView::NoAnchor);
64   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
65
66   _scrollTimer.setInterval(100);
67   _scrollTimer.setSingleShot(true);
68   connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
69
70   _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent()
71   connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
72   connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
73   connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
74   setScene(_scene);
75
76   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
77
78   // only connect if client is synched with a core
79   if(Client::isSynced())
80     connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), filter, SLOT(invalidateFilter()));
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
113   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
114   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
115
116   // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
117   // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
118   scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
119
120   _lastScrollbarPos = verticalScrollBar()->maximum();
121   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
122 }
123
124 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
125   int y = (int)mapFromScene(scenePos).y();
126   _scrollOffset = 0;
127   if(y < 0)
128     _scrollOffset = y;
129   else if(y > height())
130     _scrollOffset = y - height();
131
132   if(_scrollOffset && !_scrollTimer.isActive())
133     _scrollTimer.start();
134 }
135
136 void ChatView::scrollTimerTimeout() {
137   // scroll view
138   QAbstractSlider *vbar = verticalScrollBar();
139   if(_scrollOffset < 0 && vbar->value() > 0)
140     vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
141   else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
142     vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
143 }
144
145 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
146   Q_UNUSED(chatLine)
147   // disabled until further testing/discussion
148   //if(!scene()->isScrollingAllowed())
149   //  return;
150
151   QAbstractSlider *vbar = verticalScrollBar();
152   Q_ASSERT(vbar);
153   if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
154     vbar->setValue(vbar->maximum());
155   }
156 }
157
158 void ChatView::verticalScrollbarChanged(int newPos) {
159   QAbstractSlider *vbar = verticalScrollBar();
160   Q_ASSERT(vbar);
161
162   // check for backlog request
163   if(newPos < _lastScrollbarPos) {
164     int relativePos = 100;
165     if(vbar->maximum() - vbar->minimum() != 0)
166       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
167
168     if(relativePos < 20) {
169       scene()->requestBacklog();
170     }
171   }
172   _lastScrollbarPos = newPos;
173
174   // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
175   if(vbar->maximum() - newPos <= 2)
176     vbar->setValue(vbar->maximum());
177 }
178
179 MsgId ChatView::lastMsgId() const {
180   if(!scene())
181     return MsgId();
182
183   QAbstractItemModel *model = scene()->model();
184   if(!model || model->rowCount() == 0)
185     return MsgId();
186
187   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
188 }
189
190 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
191   // zoom actions
192   BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
193   if(bw) {
194     bw->addActionsToMenu(menu, pos);
195     menu->addSeparator();
196   }
197 }
198
199 void ChatView::zoomIn() {
200     _currentScaleFactor *= 1.2;
201     scale(1.2, 1.2);
202     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
203 }
204
205 void ChatView::zoomOut() {
206     _currentScaleFactor /= 1.2;
207     scale(1 / 1.2, 1 / 1.2);
208     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
209 }
210
211 void ChatView::zoomOriginal() {
212     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
213     _currentScaleFactor = 1;
214     scene()->setWidth(viewport()->width() - 2);
215 }