1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 ***************************************************************************/
25 #include <QGraphicsTextItem>
30 #include "bufferwidget.h"
32 #include "chatscene.h"
34 #include "clientignorelistmanager.h"
35 #include "messagefilter.h"
37 #include "qtuistyle.h"
39 ChatView::ChatView(BufferId bufferId, QWidget* parent)
40 : QGraphicsView(parent)
43 QList<BufferId> filterList;
44 filterList.append(bufferId);
45 auto* filter = new MessageFilter(Client::messageModel(), filterList, this);
49 ChatView::ChatView(MessageFilter* filter, QWidget* parent)
50 : QGraphicsView(parent)
56 void ChatView::init(MessageFilter* filter)
58 _bufferContainer = nullptr;
59 _currentScaleFactor = 1;
60 _invalidateFilter = false;
62 setAttribute(Qt::WA_AcceptTouchEvents);
63 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
64 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
65 setAlignment(Qt::AlignLeft | Qt::AlignBottom);
67 // setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
68 // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
69 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
70 // setTransformationAnchor(QGraphicsView::NoAnchor);
71 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
73 _scrollTimer.setInterval(100);
74 _scrollTimer.setSingleShot(true);
75 connect(&_scrollTimer, &QTimer::timeout, this, &ChatView::scrollTimerTimeout);
77 _scene = new ChatScene(filter, filter->idString(), viewport()->width(), this);
78 connect(_scene, &QGraphicsScene::sceneRectChanged, this, &ChatView::adjustSceneRect);
79 connect(_scene, &ChatScene::lastLineChanged, this, &ChatView::lastLineChanged);
80 connect(_scene, &ChatScene::mouseMoveWhileSelecting, this, &ChatView::mouseMoveWhileSelecting);
83 connect(verticalScrollBar(), &QAbstractSlider::valueChanged, this, &ChatView::verticalScrollbarChanged);
84 _lastScrollbarPos = verticalScrollBar()->maximum();
86 connect(Client::networkModel(), &NetworkModel::markerLineSet, this, &ChatView::markerLineSet);
88 // only connect if client is synched with a core
89 if (Client::isConnected())
90 connect(Client::ignoreListManager(), &ClientIgnoreListManager::ignoreListChanged, this, &ChatView::invalidateFilter);
93 bool ChatView::event(QEvent* event)
95 if (event->type() == QEvent::KeyPress) {
96 auto* 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::TouchBegin && ((QTouchEvent*)event)->device()->type() == QTouchDevice::TouchScreen) {
112 // Enable scrolling by draging, disable selecting/clicking content
113 setDragMode(QGraphicsView::ScrollHandDrag);
114 setInteractive(false);
115 // if scrollbar is not visible we need to request backlog below else we need to accept
116 // the event now (return true) so that we will receive TouchUpdate and TouchEnd/TouchCancel
117 if (verticalScrollBar()->isVisible())
121 if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
122 // End scroll and reset settings to default
123 setDragMode(QGraphicsView::NoDrag);
124 setInteractive(true);
125 _firstTouchUpdateHappened = false;
129 if (event->type() == QEvent::TouchUpdate) {
130 if (!_firstTouchUpdateHappened) {
131 // After the first movement of a Touch-Point, calculate the distance in both axis
132 // and if the point moved more horizontally abort scroll.
133 QTouchEvent::TouchPoint p = ((QTouchEvent*)event)->touchPoints().at(0);
134 double dx = qAbs(p.lastPos().x() - p.pos().x());
135 double dy = qAbs(p.lastPos().y() - p.pos().y());
137 setDragMode(QGraphicsView::NoDrag);
138 setInteractive(true);
140 _firstTouchUpdateHappened = true;
142 // Applying the movement happens automatically by the drag-mode
144 if (event->type() == QEvent::Wheel
145 || (event->type() == QEvent::TouchBegin && ((QTouchEvent*)event)->device()->type() == QTouchDevice::TouchScreen)
146 || event->type() == QEvent::TouchUpdate) {
147 if (!verticalScrollBar()->isVisible()) {
148 scene()->requestBacklog();
153 if (event->type() == QEvent::Show) {
154 if (_invalidateFilter)
158 return QGraphicsView::event(event);
161 void ChatView::resizeEvent(QResizeEvent* event)
163 // if view is currently scrolled to bottom, we want it that way after resizing
164 bool atBottom = (_lastScrollbarPos == verticalScrollBar()->maximum());
166 QGraphicsView::resizeEvent(event);
168 // if scrolling to bottom, do it immediately.
170 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
171 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
174 scene()->updateForViewport(viewport()->width(), viewport()->height());
178 _lastScrollbarPos = verticalScrollBar()->maximum();
179 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
181 checkChatLineCaches();
184 void ChatView::adjustSceneRect()
186 // Workaround for QTBUG-6322
187 // If the viewport's sceneRect() is (almost) as wide as as the viewport itself,
188 // Qt wants to reserve space for scrollbars even if they're turned off, resulting in
189 // an ugly white space at the bottom of the ChatView.
190 // Since the view's scene's width actually doesn't matter at all, we just adjust it
191 // by some hopefully large enough value to avoid this problem.
193 setSceneRect(scene()->sceneRect().adjusted(0, 0, -25, 0));
196 void ChatView::mouseMoveWhileSelecting(const QPointF& scenePos)
198 int y = (int)mapFromScene(scenePos).y();
202 else if (y > height())
203 _scrollOffset = y - height();
205 if (_scrollOffset && !_scrollTimer.isActive())
206 _scrollTimer.start();
209 void ChatView::scrollTimerTimeout()
212 QAbstractSlider* vbar = verticalScrollBar();
213 if (_scrollOffset < 0 && vbar->value() > 0)
214 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
215 else if (_scrollOffset > 0 && vbar->value() < vbar->maximum())
216 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
219 void ChatView::lastLineChanged(QGraphicsItem* chatLine, qreal offset)
222 // disabled until further testing/discussion
223 // if(!scene()->isScrollingAllowed())
226 QAbstractSlider* vbar = verticalScrollBar();
228 if (vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor) { // 5px grace area
229 vbar->setValue(vbar->maximum());
233 void ChatView::verticalScrollbarChanged(int newPos)
235 QAbstractSlider* vbar = verticalScrollBar();
238 // check for backlog request
239 if (newPos < _lastScrollbarPos) {
240 int relativePos = 100;
241 if (vbar->maximum() - vbar->minimum() != 0)
242 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
244 if (relativePos < 20) {
245 scene()->requestBacklog();
248 _lastScrollbarPos = newPos;
250 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
251 if (vbar->maximum() - newPos <= 2)
252 vbar->setValue(vbar->maximum());
255 MsgId ChatView::lastMsgId() const
260 QAbstractItemModel* model = scene()->model();
261 if (!model || model->rowCount() == 0)
264 return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
267 MsgId ChatView::lastVisibleMsgId() const
269 ChatLine* line = lastVisibleChatLine();
272 return line->msgId();
277 bool chatLinePtrLessThan(ChatLine* one, ChatLine* other)
279 return one->row() < other->row();
282 // TODO: figure out if it's cheaper to use a cached list (that we'd need to keep updated)
283 QSet<ChatLine*> ChatView::visibleChatLines(Qt::ItemSelectionMode mode) const
285 QSet<ChatLine*> result;
286 foreach (QGraphicsItem* item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) {
287 auto* line = qgraphicsitem_cast<ChatLine*>(item);
294 QList<ChatLine*> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const
296 QList<ChatLine*> result = visibleChatLines(mode).toList();
297 std::sort(result.begin(), result.end(), chatLinePtrLessThan);
301 ChatLine* ChatView::lastVisibleChatLine(bool ignoreDayChange) const
306 QAbstractItemModel* model = scene()->model();
307 if (!model || model->rowCount() == 0)
312 QSet<ChatLine*> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
313 foreach (ChatLine* line, visibleLines) {
314 if (line->row() > row && (ignoreDayChange ? line->msgType() != Message::DayChange : true))
319 return scene()->chatLine(row);
324 void ChatView::setMarkerLineVisible(bool visible)
326 scene()->setMarkerLineVisible(visible);
329 void ChatView::setMarkerLine(MsgId msgId)
331 if (!scene()->isSingleBufferScene())
334 BufferId bufId = scene()->singleBufferId();
335 Client::setMarkerLine(bufId, msgId);
338 void ChatView::markerLineSet(BufferId buffer, MsgId msgId)
340 if (!scene()->isSingleBufferScene() || scene()->singleBufferId() != buffer)
343 scene()->setMarkerLine(msgId);
344 scene()->setMarkerLineVisible(true);
347 void ChatView::jumpToMarkerLine(bool requestBacklog)
349 scene()->jumpToMarkerLine(requestBacklog);
352 void ChatView::addActionsToMenu(QMenu* menu, const QPointF& pos)
355 auto* bw = qobject_cast<BufferWidget*>(bufferContainer());
357 bw->addActionsToMenu(menu, pos);
358 menu->addSeparator();
362 void ChatView::zoomIn()
364 _currentScaleFactor *= 1.2;
366 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
369 void ChatView::zoomOut()
371 _currentScaleFactor /= 1.2;
372 scale(1 / 1.2, 1 / 1.2);
373 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
376 void ChatView::zoomOriginal()
378 scale(1 / _currentScaleFactor, 1 / _currentScaleFactor);
379 _currentScaleFactor = 1;
380 scene()->setWidth(viewport()->width() - 2);
383 void ChatView::invalidateFilter()
385 // if this is the currently selected chatview
386 // invalidate immediately
388 _scene->filter()->invalidateFilter();
389 _invalidateFilter = false;
391 // otherwise invalidate whenever the view is shown
393 _invalidateFilter = true;
397 void ChatView::scrollContentsBy(int dx, int dy)
399 QGraphicsView::scrollContentsBy(dx, dy);
400 checkChatLineCaches();
403 void ChatView::setHasCache(ChatLine* line, bool hasCache)
406 _linesWithCache.insert(line);
408 _linesWithCache.remove(line);
411 void ChatView::checkChatLineCaches()
413 qreal top = mapToScene(viewport()->rect().topLeft()).y() - 10; // some grace area to avoid premature cleaning
414 qreal bottom = mapToScene(viewport()->rect().bottomRight()).y() + 10;
415 QSet<ChatLine*>::iterator iter = _linesWithCache.begin();
416 while (iter != _linesWithCache.end()) {
417 ChatLine* line = *iter;
418 if (line->pos().y() + line->height() < top || line->pos().y() > bottom) {
420 iter = _linesWithCache.erase(iter);