1 /***************************************************************************
2 * Copyright (C) 2005-2015 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <QGraphicsTextItem>
26 #include "bufferwidget.h"
27 #include "chatscene.h"
30 #include "messagefilter.h"
32 #include "qtuistyle.h"
33 #include "clientignorelistmanager.h"
37 ChatView::ChatView(BufferId bufferId, QWidget *parent)
38 : QGraphicsView(parent),
41 QList<BufferId> filterList;
42 filterList.append(bufferId);
43 MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
48 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
49 : QGraphicsView(parent),
56 void ChatView::init(MessageFilter *filter)
59 _currentScaleFactor = 1;
60 _invalidateFilter = false;
62 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
64 setAlignment(Qt::AlignLeft|Qt::AlignBottom);
66 //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
67 // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
68 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
69 // setTransformationAnchor(QGraphicsView::NoAnchor);
70 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
72 _scrollTimer.setInterval(100);
73 _scrollTimer.setSingleShot(true);
74 connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
76 _scene = new ChatScene(filter, filter->idString(), viewport()->width(), this);
77 connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(adjustSceneRect()));
78 connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
79 connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
82 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
83 _lastScrollbarPos = verticalScrollBar()->maximum();
85 connect(Client::networkModel(), SIGNAL(markerLineSet(BufferId, MsgId)), SLOT(markerLineSet(BufferId, MsgId)));
87 // only connect if client is synched with a core
88 if (Client::isConnected())
89 connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
93 bool ChatView::event(QEvent *event)
95 if (event->type() == QEvent::KeyPress) {
96 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
97 switch (keyEvent->key()) {
101 case Qt::Key_PageDown:
102 if (!verticalScrollBar()->isVisible()) {
103 scene()->requestBacklog();
111 if (event->type() == QEvent::Wheel) {
112 if (!verticalScrollBar()->isVisible()) {
113 scene()->requestBacklog();
118 if (event->type() == QEvent::Show) {
119 if (_invalidateFilter)
123 return QGraphicsView::event(event);
127 void ChatView::resizeEvent(QResizeEvent *event)
129 // if view is currently scrolled to bottom, we want it that way after resizing
130 bool atBottom = (_lastScrollbarPos == verticalScrollBar()->maximum());
132 QGraphicsView::resizeEvent(event);
134 // if scrolling to bottom, do it immediately.
137 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
138 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
141 scene()->updateForViewport(viewport()->width(), viewport()->height());
146 _lastScrollbarPos = verticalScrollBar()->maximum();
147 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
149 checkChatLineCaches();
153 void ChatView::adjustSceneRect()
155 // Workaround for QTBUG-6322
156 // If the viewport's sceneRect() is (almost) as wide as as the viewport itself,
157 // Qt wants to reserve space for scrollbars even if they're turned off, resulting in
158 // an ugly white space at the bottom of the ChatView.
159 // Since the view's scene's width actually doesn't matter at all, we just adjust it
160 // by some hopefully large enough value to avoid this problem.
162 setSceneRect(scene()->sceneRect().adjusted(0, 0, -25, 0));
166 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos)
168 int y = (int)mapFromScene(scenePos).y();
172 else if (y > height())
173 _scrollOffset = y - height();
175 if (_scrollOffset && !_scrollTimer.isActive())
176 _scrollTimer.start();
180 void ChatView::scrollTimerTimeout()
183 QAbstractSlider *vbar = verticalScrollBar();
184 if (_scrollOffset < 0 && vbar->value() > 0)
185 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
186 else if (_scrollOffset > 0 && vbar->value() < vbar->maximum())
187 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
191 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset)
194 // disabled until further testing/discussion
195 //if(!scene()->isScrollingAllowed())
198 QAbstractSlider *vbar = verticalScrollBar();
200 if (vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor) { // 5px grace area
201 vbar->setValue(vbar->maximum());
206 void ChatView::verticalScrollbarChanged(int newPos)
208 QAbstractSlider *vbar = verticalScrollBar();
211 // check for backlog request
212 if (newPos < _lastScrollbarPos) {
213 int relativePos = 100;
214 if (vbar->maximum() - vbar->minimum() != 0)
215 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
217 if (relativePos < 20) {
218 scene()->requestBacklog();
221 _lastScrollbarPos = newPos;
223 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
224 if (vbar->maximum() - newPos <= 2)
225 vbar->setValue(vbar->maximum());
229 MsgId ChatView::lastMsgId() const
234 QAbstractItemModel *model = scene()->model();
235 if (!model || model->rowCount() == 0)
238 return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
242 MsgId ChatView::lastVisibleMsgId() const
244 ChatLine *line = lastVisibleChatLine();
247 return line->msgId();
253 bool chatLinePtrLessThan(ChatLine *one, ChatLine *other)
255 return one->row() < other->row();
259 // TODO: figure out if it's cheaper to use a cached list (that we'd need to keep updated)
260 QSet<ChatLine *> ChatView::visibleChatLines(Qt::ItemSelectionMode mode) const
262 QSet<ChatLine *> result;
263 foreach(QGraphicsItem *item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) {
264 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item);
272 QList<ChatLine *> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const
274 QList<ChatLine *> result = visibleChatLines(mode).toList();
275 qSort(result.begin(), result.end(), chatLinePtrLessThan);
280 ChatLine *ChatView::lastVisibleChatLine(bool ignoreDayChange) const
285 QAbstractItemModel *model = scene()->model();
286 if (!model || model->rowCount() == 0)
291 QSet<ChatLine *> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
292 foreach(ChatLine *line, visibleLines) {
293 if (line->row() > row && (ignoreDayChange ? line->msgType() != Message::DayChange : true))
298 return scene()->chatLine(row);
304 void ChatView::setMarkerLineVisible(bool visible)
306 scene()->setMarkerLineVisible(visible);
310 void ChatView::setMarkerLine(MsgId msgId)
312 if (!scene()->isSingleBufferScene())
315 BufferId bufId = scene()->singleBufferId();
316 Client::setMarkerLine(bufId, msgId);
320 void ChatView::markerLineSet(BufferId buffer, MsgId msgId)
322 if (!scene()->isSingleBufferScene() || scene()->singleBufferId() != buffer)
325 scene()->setMarkerLine(msgId);
326 scene()->setMarkerLineVisible(true);
330 void ChatView::jumpToMarkerLine(bool requestBacklog)
332 scene()->jumpToMarkerLine(requestBacklog);
336 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos)
339 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
341 bw->addActionsToMenu(menu, pos);
342 menu->addSeparator();
347 void ChatView::zoomIn()
349 _currentScaleFactor *= 1.2;
351 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
355 void ChatView::zoomOut()
357 _currentScaleFactor /= 1.2;
358 scale(1 / 1.2, 1 / 1.2);
359 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
363 void ChatView::zoomOriginal()
365 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
366 _currentScaleFactor = 1;
367 scene()->setWidth(viewport()->width() - 2);
371 void ChatView::invalidateFilter()
373 // if this is the currently selected chatview
374 // invalidate immediately
376 _scene->filter()->invalidateFilter();
377 _invalidateFilter = false;
379 // otherwise invalidate whenever the view is shown
381 _invalidateFilter = true;
386 void ChatView::scrollContentsBy(int dx, int dy)
388 QGraphicsView::scrollContentsBy(dx, dy);
389 checkChatLineCaches();
393 void ChatView::setHasCache(ChatLine *line, bool hasCache)
396 _linesWithCache.insert(line);
398 _linesWithCache.remove(line);
402 void ChatView::checkChatLineCaches()
404 qreal top = mapToScene(viewport()->rect().topLeft()).y() - 10; // some grace area to avoid premature cleaning
405 qreal bottom = mapToScene(viewport()->rect().bottomRight()).y() + 10;
406 QSet<ChatLine *>::iterator iter = _linesWithCache.begin();
407 while (iter != _linesWithCache.end()) {
408 ChatLine *line = *iter;
409 if (line->pos().y() + line->height() < top || line->pos().y() > bottom) {
411 iter = _linesWithCache.erase(iter);