Improve marker line behavior; allow manual setting (Ctrl+R)
[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   _markerLineVisible = true;
59   _markedLine = 0;
60
61   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62   setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
63   setAlignment(Qt::AlignLeft|Qt::AlignBottom);
64   setInteractive(true);
65   //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
66   // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
67   setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
68   // setTransformationAnchor(QGraphicsView::NoAnchor);
69   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
70
71   _scrollTimer.setInterval(100);
72   _scrollTimer.setSingleShot(true);
73   connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
74
75   _scene = new ChatScene(filter, filter->idString(), viewport()->width(), this);
76   connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(adjustSceneRect()));
77   connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
78   connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
79   setScene(_scene);
80
81   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
82   _lastScrollbarPos = verticalScrollBar()->value();
83
84   connect(Client::networkModel(), SIGNAL(markerLineSet(BufferId,MsgId)), SLOT(markerLineSet(BufferId,MsgId)));
85
86   // only connect if client is synched with a core
87   if(Client::isConnected())
88     connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
89 }
90
91 bool ChatView::event(QEvent *event) {
92   if(event->type() == QEvent::KeyPress) {
93     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
94     switch(keyEvent->key()) {
95     case Qt::Key_Up:
96     case Qt::Key_Down:
97     case Qt::Key_PageUp:
98     case Qt::Key_PageDown:
99       if(!verticalScrollBar()->isVisible()) {
100         scene()->requestBacklog();
101         return true;
102       }
103     default:
104       break;
105     }
106   }
107
108   if(event->type() == QEvent::Wheel) {
109     if(!verticalScrollBar()->isVisible()) {
110       scene()->requestBacklog();
111       return true;
112     }
113   }
114
115   if(event->type() == QEvent::Show) {
116     if(_invalidateFilter)
117       invalidateFilter();
118   }
119
120   return QGraphicsView::event(event);
121 }
122
123 void ChatView::resizeEvent(QResizeEvent *event) {
124   QGraphicsView::resizeEvent(event);
125
126   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
127   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
128   scene()->updateForViewport(viewport()->width(), viewport()->height());
129   adjustSceneRect();
130
131   _lastScrollbarPos = verticalScrollBar()->maximum();
132   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
133 }
134
135 void ChatView::adjustSceneRect() {
136   // Workaround for QTBUG-6322
137   // If the viewport's sceneRect() is (almost) as wide as as the viewport itself,
138   // Qt wants to reserve space for scrollbars even if they're turned off, resulting in
139   // an ugly white space at the bottom of the ChatView.
140   // Since the view's scene's width actually doesn't matter at all, we just adjust it
141   // by some hopefully large enough value to avoid this problem.
142
143   setSceneRect(scene()->sceneRect().adjusted(0, 0, -25 ,0));
144 }
145
146 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
147   int y = (int)mapFromScene(scenePos).y();
148   _scrollOffset = 0;
149   if(y < 0)
150     _scrollOffset = y;
151   else if(y > height())
152     _scrollOffset = y - height();
153
154   if(_scrollOffset && !_scrollTimer.isActive())
155     _scrollTimer.start();
156 }
157
158 void ChatView::scrollTimerTimeout() {
159   // scroll view
160   QAbstractSlider *vbar = verticalScrollBar();
161   if(_scrollOffset < 0 && vbar->value() > 0)
162     vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
163   else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
164     vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
165 }
166
167 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
168   Q_UNUSED(chatLine)
169   // disabled until further testing/discussion
170   //if(!scene()->isScrollingAllowed())
171   //  return;
172
173   QAbstractSlider *vbar = verticalScrollBar();
174   Q_ASSERT(vbar);
175   if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
176     vbar->setValue(vbar->maximum());
177   }
178 }
179
180 void ChatView::verticalScrollbarChanged(int newPos) {
181   QAbstractSlider *vbar = verticalScrollBar();
182   Q_ASSERT(vbar);
183
184   // check for backlog request
185   if(newPos < _lastScrollbarPos) {
186     int relativePos = 100;
187     if(vbar->maximum() - vbar->minimum() != 0)
188       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
189
190     if(relativePos < 20) {
191       scene()->requestBacklog();
192     }
193   }
194   _lastScrollbarPos = newPos;
195
196   // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
197   if(vbar->maximum() - newPos <= 2)
198     vbar->setValue(vbar->maximum());
199 }
200
201 MsgId ChatView::lastMsgId() const {
202   if(!scene())
203     return MsgId();
204
205   QAbstractItemModel *model = scene()->model();
206   if(!model || model->rowCount() == 0)
207     return MsgId();
208
209   return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
210 }
211
212 MsgId ChatView::lastVisibleMsgId() const {
213   ChatLine *line = lastVisibleChatLine();
214
215   if(line)
216     return line->msgId();
217
218   return MsgId();
219 }
220
221 bool chatLinePtrLessThan(ChatLine *one, ChatLine *other) {
222   return one->row() < other->row();
223 }
224
225 QSet<ChatLine *> ChatView::visibleChatLines(Qt::ItemSelectionMode mode) const {
226   QSet<ChatLine *> result;
227   foreach(QGraphicsItem *item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) {
228     ChatLine *line = qgraphicsitem_cast<ChatLine *>(item);
229     if(line)
230       result.insert(line);
231   }
232   return result;
233 }
234
235 QList<ChatLine *> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const {
236   QList<ChatLine *> result = visibleChatLines(mode).toList();
237   qSort(result.begin(), result.end(), chatLinePtrLessThan);
238   return result;
239 }
240
241 ChatLine *ChatView::lastVisibleChatLine() const {
242   if(!scene())
243     return 0;
244
245   QAbstractItemModel *model = scene()->model();
246   if(!model || model->rowCount() == 0)
247     return 0;
248
249   int row = -1;
250
251   QSet<ChatLine *> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
252   foreach(ChatLine *line, visibleLines) {
253     if(line->row() > row)
254       row = line->row();
255   }
256
257   if(row >= 0)
258     return scene()->chatLine(row);
259
260   return 0;
261 }
262
263 void ChatView::setMarkerLineVisible(bool visible) {
264   if(visible != _markerLineVisible) {
265     _markerLineVisible = visible;
266   }
267 }
268
269 void ChatView::setMarkedLine(ChatLine *line) {
270   if(_markedLine == line)
271     return;
272
273   if(!scene()->isSingleBufferScene())
274     return;
275
276   if(line) {
277     BufferId bufId = scene()->singleBufferId();
278     Client::setMarkerLine(bufId, line->msgId());
279   }
280 }
281
282 void ChatView::markerLineSet(BufferId buffer, MsgId msg) {
283   if(!scene()->isSingleBufferScene() || scene()->singleBufferId() != buffer)
284     return;
285
286   ChatLine *newLine = scene()->chatLine(msg);
287   if(_markedLine == newLine)
288     return;
289
290   ChatLine *oldLine = _markedLine;
291   _markedLine = newLine;
292
293   if(oldLine)
294     oldLine->update();
295
296   if(newLine) {
297     setMarkerLineVisible(true);
298     newLine->update();
299   }
300 }
301
302 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
303   // zoom actions
304   BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
305   if(bw) {
306     bw->addActionsToMenu(menu, pos);
307     menu->addSeparator();
308   }
309 }
310
311 void ChatView::zoomIn() {
312     _currentScaleFactor *= 1.2;
313     scale(1.2, 1.2);
314     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
315 }
316
317 void ChatView::zoomOut() {
318     _currentScaleFactor /= 1.2;
319     scale(1 / 1.2, 1 / 1.2);
320     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
321 }
322
323 void ChatView::zoomOriginal() {
324     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
325     _currentScaleFactor = 1;
326     scene()->setWidth(viewport()->width() - 2);
327 }
328
329 void ChatView::invalidateFilter() {
330   // if this is the currently selected chatview
331   // invalidate immediately
332   if(isVisible()) {
333     _scene->filter()->invalidateFilter();
334     _invalidateFilter = false;
335   }
336   // otherwise invalidate whenever the view is shown
337   else {
338     _invalidateFilter = true;
339   }
340 }