1 /***************************************************************************
2 * Copyright (C) 2005-2014 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()->value();
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 QGraphicsView::resizeEvent(event);
131 scene()->updateForViewport(viewport()->width(), viewport()->height());
134 checkChatLineCaches();
138 void ChatView::adjustSceneRect()
140 // Workaround for QTBUG-6322
141 // If the viewport's sceneRect() is (almost) as wide as as the viewport itself,
142 // Qt wants to reserve space for scrollbars even if they're turned off, resulting in
143 // an ugly white space at the bottom of the ChatView.
144 // Since the view's scene's width actually doesn't matter at all, we just adjust it
145 // by some hopefully large enough value to avoid this problem.
147 setSceneRect(scene()->sceneRect().adjusted(0, 0, -25, 0));
151 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos)
153 int y = (int)mapFromScene(scenePos).y();
157 else if (y > height())
158 _scrollOffset = y - height();
160 if (_scrollOffset && !_scrollTimer.isActive())
161 _scrollTimer.start();
165 void ChatView::scrollTimerTimeout()
168 QAbstractSlider *vbar = verticalScrollBar();
169 if (_scrollOffset < 0 && vbar->value() > 0)
170 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
171 else if (_scrollOffset > 0 && vbar->value() < vbar->maximum())
172 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
176 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset)
179 // disabled until further testing/discussion
180 //if(!scene()->isScrollingAllowed())
183 QAbstractSlider *vbar = verticalScrollBar();
185 if (vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor) { // 5px grace area
186 vbar->setValue(vbar->maximum());
191 void ChatView::verticalScrollbarChanged(int newPos)
193 QAbstractSlider *vbar = verticalScrollBar();
196 // check for backlog request
197 if (newPos < _lastScrollbarPos) {
198 int relativePos = 100;
199 if (vbar->maximum() - vbar->minimum() != 0)
200 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
202 if (relativePos < 20) {
203 scene()->requestBacklog();
206 _lastScrollbarPos = newPos;
208 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
209 if (vbar->maximum() - newPos <= 2)
210 vbar->setValue(vbar->maximum());
214 MsgId ChatView::lastMsgId() const
219 QAbstractItemModel *model = scene()->model();
220 if (!model || model->rowCount() == 0)
223 return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
227 MsgId ChatView::lastVisibleMsgId() const
229 ChatLine *line = lastVisibleChatLine();
232 return line->msgId();
238 bool chatLinePtrLessThan(ChatLine *one, ChatLine *other)
240 return one->row() < other->row();
244 // TODO: figure out if it's cheaper to use a cached list (that we'd need to keep updated)
245 QSet<ChatLine *> ChatView::visibleChatLines(Qt::ItemSelectionMode mode) const
247 QSet<ChatLine *> result;
248 foreach(QGraphicsItem *item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) {
249 ChatLine *line = qgraphicsitem_cast<ChatLine *>(item);
257 QList<ChatLine *> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const
259 QList<ChatLine *> result = visibleChatLines(mode).toList();
260 qSort(result.begin(), result.end(), chatLinePtrLessThan);
265 ChatLine *ChatView::lastVisibleChatLine(bool ignoreDayChange) const
270 QAbstractItemModel *model = scene()->model();
271 if (!model || model->rowCount() == 0)
276 QSet<ChatLine *> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
277 foreach(ChatLine *line, visibleLines) {
278 if (line->row() > row && (ignoreDayChange ? line->msgType() != Message::DayChange : true))
283 return scene()->chatLine(row);
289 void ChatView::setMarkerLineVisible(bool visible)
291 scene()->setMarkerLineVisible(visible);
295 void ChatView::setMarkerLine(MsgId msgId)
297 if (!scene()->isSingleBufferScene())
300 BufferId bufId = scene()->singleBufferId();
301 Client::setMarkerLine(bufId, msgId);
305 void ChatView::markerLineSet(BufferId buffer, MsgId msgId)
307 if (!scene()->isSingleBufferScene() || scene()->singleBufferId() != buffer)
310 scene()->setMarkerLine(msgId);
311 scene()->setMarkerLineVisible(true);
315 void ChatView::jumpToMarkerLine(bool requestBacklog)
317 scene()->jumpToMarkerLine(requestBacklog);
321 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos)
324 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
326 bw->addActionsToMenu(menu, pos);
327 menu->addSeparator();
332 void ChatView::zoomIn()
334 _currentScaleFactor *= 1.2;
336 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
340 void ChatView::zoomOut()
342 _currentScaleFactor /= 1.2;
343 scale(1 / 1.2, 1 / 1.2);
344 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
348 void ChatView::zoomOriginal()
350 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
351 _currentScaleFactor = 1;
352 scene()->setWidth(viewport()->width() - 2);
356 void ChatView::invalidateFilter()
358 // if this is the currently selected chatview
359 // invalidate immediately
361 _scene->filter()->invalidateFilter();
362 _invalidateFilter = false;
364 // otherwise invalidate whenever the view is shown
366 _invalidateFilter = true;
371 void ChatView::scrollContentsBy(int dx, int dy)
373 QGraphicsView::scrollContentsBy(dx, dy);
374 checkChatLineCaches();
378 void ChatView::setHasCache(ChatLine *line, bool hasCache)
381 _linesWithCache.insert(line);
383 _linesWithCache.remove(line);
387 void ChatView::checkChatLineCaches()
389 qreal top = mapToScene(viewport()->rect().topLeft()).y() - 10; // some grace area to avoid premature cleaning
390 qreal bottom = mapToScene(viewport()->rect().bottomRight()).y() + 10;
391 QSet<ChatLine *>::iterator iter = _linesWithCache.begin();
392 while (iter != _linesWithCache.end()) {
393 ChatLine *line = *iter;
394 if (line->pos().y() + line->height() < top || line->pos().y() > bottom) {
396 iter = _linesWithCache.erase(iter);