Provide some new accessors for Chat{View|Scene}
[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 #include "chatline.h"
36
37 ChatView::ChatView(BufferId bufferId, QWidget *parent)
38   : QGraphicsView(parent),
39     AbstractChatView()
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 {
51   init(filter);
52 }
53
54 void ChatView::init(MessageFilter *filter) {
55   _bufferContainer = 0;
56   _currentScaleFactor = 1;
57   _invalidateFilter = false;
58
59   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60   setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
61   setAlignment(Qt::AlignLeft|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   _lastScrollbarPos = verticalScrollBar()->value();
81
82   // only connect if client is synched with a core
83   if(Client::isConnected())
84     connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
85 }
86
87 bool ChatView::event(QEvent *event) {
88   if(event->type() == QEvent::KeyPress) {
89     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
90     switch(keyEvent->key()) {
91     case Qt::Key_Up:
92     case Qt::Key_Down:
93     case Qt::Key_PageUp:
94     case Qt::Key_PageDown:
95       if(!verticalScrollBar()->isVisible()) {
96         scene()->requestBacklog();
97         return true;
98       }
99     default:
100       break;
101     }
102   }
103
104   if(event->type() == QEvent::Wheel) {
105     if(!verticalScrollBar()->isVisible()) {
106       scene()->requestBacklog();
107       return true;
108     }
109   }
110
111   if(event->type() == QEvent::Show) {
112     if(_invalidateFilter)
113       invalidateFilter();
114   }
115
116   return QGraphicsView::event(event);
117 }
118
119 void ChatView::resizeEvent(QResizeEvent *event) {
120   QGraphicsView::resizeEvent(event);
121
122   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
123   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
124   scene()->updateForViewport(viewport()->width(), viewport()->height());
125   adjustSceneRect();
126
127   _lastScrollbarPos = verticalScrollBar()->maximum();
128   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
129 }
130
131 void ChatView::adjustSceneRect() {
132   // Workaround for QTBUG-6322
133   // If the viewport's sceneRect() is (almost) as wide as as the viewport itself,
134   // Qt wants to reserve space for scrollbars even if they're turned off, resulting in
135   // an ugly white space at the bottom of the ChatView.
136   // Since the view's scene's width actually doesn't matter at all, we just adjust it
137   // by some hopefully large enough value to avoid this problem.
138
139   setSceneRect(scene()->sceneRect().adjusted(0, 0, -25 ,0));
140 }
141
142 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
143   int y = (int)mapFromScene(scenePos).y();
144   _scrollOffset = 0;
145   if(y < 0)
146     _scrollOffset = y;
147   else if(y > height())
148     _scrollOffset = y - height();
149
150   if(_scrollOffset && !_scrollTimer.isActive())
151     _scrollTimer.start();
152 }
153
154 void ChatView::scrollTimerTimeout() {
155   // scroll view
156   QAbstractSlider *vbar = verticalScrollBar();
157   if(_scrollOffset < 0 && vbar->value() > 0)
158     vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
159   else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
160     vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
161 }
162
163 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
164   Q_UNUSED(chatLine)
165   // disabled until further testing/discussion
166   //if(!scene()->isScrollingAllowed())
167   //  return;
168
169   QAbstractSlider *vbar = verticalScrollBar();
170   Q_ASSERT(vbar);
171   if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
172     vbar->setValue(vbar->maximum());
173   }
174 }
175
176 void ChatView::verticalScrollbarChanged(int newPos) {
177   QAbstractSlider *vbar = verticalScrollBar();
178   Q_ASSERT(vbar);
179
180   // check for backlog request
181   if(newPos < _lastScrollbarPos) {
182     int relativePos = 100;
183     if(vbar->maximum() - vbar->minimum() != 0)
184       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
185
186     if(relativePos < 20) {
187       scene()->requestBacklog();
188     }
189   }
190   _lastScrollbarPos = newPos;
191
192   // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
193   if(vbar->maximum() - newPos <= 2)
194     vbar->setValue(vbar->maximum());
195 }
196
197 MsgId ChatView::lastMsgId() const {
198   if(!scene())
199     return MsgId();
200
201   QAbstractItemModel *model = scene()->model();
202   if(!model || model->rowCount() == 0)
203     return MsgId();
204
205   return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
206 }
207
208 MsgId ChatView::lastVisibleMsgId() const {
209   ChatLine *line = lastVisibleChatLine();
210
211   if(line)
212     return line->msgId();
213
214   return MsgId();
215 }
216
217 bool chatLinePtrLessThan(ChatLine *one, ChatLine *other) {
218   return one->row() < other->row();
219 }
220
221 QSet<ChatLine *> ChatView::visibleChatLines(Qt::ItemSelectionMode mode) const {
222   QSet<ChatLine *> result;
223   foreach(QGraphicsItem *item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) {
224     ChatLine *line = qgraphicsitem_cast<ChatLine *>(item);
225     if(line)
226       result.insert(line);
227   }
228   return result;
229 }
230
231 QList<ChatLine *> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const {
232   QList<ChatLine *> result = visibleChatLines(mode).toList();
233   qSort(result.begin(), result.end(), chatLinePtrLessThan);
234   return result;
235 }
236
237 ChatLine *ChatView::lastVisibleChatLine() const {
238   if(!scene())
239     return 0;
240
241   QAbstractItemModel *model = scene()->model();
242   if(!model || model->rowCount() == 0)
243     return 0;
244
245   int row = -1;
246
247   QSet<ChatLine *> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
248   foreach(ChatLine *line, visibleLines) {
249     if(line->row() > row)
250       row = line->row();
251   }
252
253   if(row >= 0)
254     return scene()->chatLine(row);
255
256   return 0;
257 }
258
259 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
260   // zoom actions
261   BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
262   if(bw) {
263     bw->addActionsToMenu(menu, pos);
264     menu->addSeparator();
265   }
266 }
267
268 void ChatView::zoomIn() {
269     _currentScaleFactor *= 1.2;
270     scale(1.2, 1.2);
271     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
272 }
273
274 void ChatView::zoomOut() {
275     _currentScaleFactor /= 1.2;
276     scale(1 / 1.2, 1 / 1.2);
277     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
278 }
279
280 void ChatView::zoomOriginal() {
281     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
282     _currentScaleFactor = 1;
283     scene()->setWidth(viewport()->width() - 2);
284 }
285
286 void ChatView::invalidateFilter() {
287   // if this is the currently selected chatview
288   // invalidate immediately
289   if(isVisible()) {
290     _scene->filter()->invalidateFilter();
291     _invalidateFilter = false;
292   }
293   // otherwise invalidate whenever the view is shown
294   else {
295     _invalidateFilter = true;
296   }
297 }