Quassel IRC  Pre-Release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
chatview.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2015 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  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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"
34 
35 #include "chatline.h"
36 
37 ChatView::ChatView(BufferId bufferId, QWidget *parent)
38  : QGraphicsView(parent),
40 {
41  QList<BufferId> filterList;
42  filterList.append(bufferId);
43  MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
44  init(filter);
45 }
46 
47 
48 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
49  : QGraphicsView(parent),
51 {
52  init(filter);
53 }
54 
55 
57 {
58  _bufferContainer = 0;
60  _invalidateFilter = false;
61 
62  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
63  setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
64  setAlignment(Qt::AlignLeft|Qt::AlignBottom);
65  setInteractive(true);
66  //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
67  // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
68  setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
69  // setTransformationAnchor(QGraphicsView::NoAnchor);
70  setTransformationAnchor(QGraphicsView::AnchorViewCenter);
71 
72  _scrollTimer.setInterval(100);
73  _scrollTimer.setSingleShot(true);
74  connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
75 
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 &)));
80  setScene(_scene);
81 
82  connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
83  _lastScrollbarPos = verticalScrollBar()->maximum();
84 
86 
87  // only connect if client is synched with a core
88  if (Client::isConnected())
89  connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
90 }
91 
92 
93 bool ChatView::event(QEvent *event)
94 {
95  if (event->type() == QEvent::KeyPress) {
96  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
97  switch (keyEvent->key()) {
98  case Qt::Key_Up:
99  case Qt::Key_Down:
100  case Qt::Key_PageUp:
101  case Qt::Key_PageDown:
102  if (!verticalScrollBar()->isVisible()) {
103  scene()->requestBacklog();
104  return true;
105  }
106  default:
107  break;
108  }
109  }
110 
111  if (event->type() == QEvent::Wheel) {
112  if (!verticalScrollBar()->isVisible()) {
113  scene()->requestBacklog();
114  return true;
115  }
116  }
117 
118  if (event->type() == QEvent::Show) {
119  if (_invalidateFilter)
121  }
122 
123  return QGraphicsView::event(event);
124 }
125 
126 
127 void ChatView::resizeEvent(QResizeEvent *event)
128 {
129  // if view is currently scrolled to bottom, we want it that way after resizing
130  bool atBottom = (_lastScrollbarPos == verticalScrollBar()->maximum());
131 
133 
134  // if scrolling to bottom, do it immediately.
135  if(atBottom)
136  {
137  // we can reduce viewport updates if we scroll to the bottom allready at the beginning
138  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
139  }
140 
141  scene()->updateForViewport(viewport()->width(), viewport()->height());
142  adjustSceneRect();
143 
144  if(atBottom)
145  {
146  _lastScrollbarPos = verticalScrollBar()->maximum();
147  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
148  }
150 }
151 
152 
154 {
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.
161 
162  setSceneRect(scene()->sceneRect().adjusted(0, 0, -25, 0));
163 }
164 
165 
166 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos)
167 {
168  int y = (int)mapFromScene(scenePos).y();
169  _scrollOffset = 0;
170  if (y < 0)
171  _scrollOffset = y;
172  else if (y > height())
173  _scrollOffset = y - height();
174 
175  if (_scrollOffset && !_scrollTimer.isActive())
176  _scrollTimer.start();
177 }
178 
179 
181 {
182  // scroll view
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()));
188 }
189 
190 
191 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset)
192 {
193  Q_UNUSED(chatLine)
194  // disabled until further testing/discussion
195  //if(!scene()->isScrollingAllowed())
196  // return;
197 
198  QAbstractSlider *vbar = verticalScrollBar();
199  Q_ASSERT(vbar);
200  if (vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor) { // 5px grace area
201  vbar->setValue(vbar->maximum());
202  }
203 }
204 
205 
207 {
208  QAbstractSlider *vbar = verticalScrollBar();
209  Q_ASSERT(vbar);
210 
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());
216 
217  if (relativePos < 20) {
218  scene()->requestBacklog();
219  }
220  }
221  _lastScrollbarPos = newPos;
222 
223  // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
224  if (vbar->maximum() - newPos <= 2)
225  vbar->setValue(vbar->maximum());
226 }
227 
228 
230 {
231  if (!scene())
232  return MsgId();
233 
234  QAbstractItemModel *model = scene()->model();
235  if (!model || model->rowCount() == 0)
236  return MsgId();
237 
238  return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
239 }
240 
241 
243 {
244  ChatLine *line = lastVisibleChatLine();
245 
246  if (line)
247  return line->msgId();
248 
249  return MsgId();
250 }
251 
252 
254 {
255  return one->row() < other->row();
256 }
257 
258 
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
261 {
262  QSet<ChatLine *> result;
263  foreach(QGraphicsItem *item, items(viewport()->rect().adjusted(-1, -1, 1, 1), mode)) {
264  ChatLine *line = qgraphicsitem_cast<ChatLine *>(item);
265  if (line)
266  result.insert(line);
267  }
268  return result;
269 }
270 
271 
272 QList<ChatLine *> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const
273 {
274  QList<ChatLine *> result = visibleChatLines(mode).toList();
275  qSort(result.begin(), result.end(), chatLinePtrLessThan);
276  return result;
277 }
278 
279 
280 ChatLine *ChatView::lastVisibleChatLine(bool ignoreDayChange) const
281 {
282  if (!scene())
283  return 0;
284 
285  QAbstractItemModel *model = scene()->model();
286  if (!model || model->rowCount() == 0)
287  return 0;
288 
289  int row = -1;
290 
291  QSet<ChatLine *> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
292  foreach(ChatLine *line, visibleLines) {
293  if (line->row() > row && (ignoreDayChange ? line->msgType() != Message::DayChange : true))
294  row = line->row();
295  }
296 
297  if (row >= 0)
298  return scene()->chatLine(row);
299 
300  return 0;
301 }
302 
303 
305 {
306  scene()->setMarkerLineVisible(visible);
307 }
308 
309 
311 {
312  if (!scene()->isSingleBufferScene())
313  return;
314 
315  BufferId bufId = scene()->singleBufferId();
316  Client::setMarkerLine(bufId, msgId);
317 }
318 
319 
321 {
322  if (!scene()->isSingleBufferScene() || scene()->singleBufferId() != buffer)
323  return;
324 
325  scene()->setMarkerLine(msgId);
326  scene()->setMarkerLineVisible(true);
327 }
328 
329 
330 void ChatView::jumpToMarkerLine(bool requestBacklog)
331 {
332  scene()->jumpToMarkerLine(requestBacklog);
333 }
334 
335 
336 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos)
337 {
338  // zoom actions
339  BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
340  if (bw) {
341  bw->addActionsToMenu(menu, pos);
342  menu->addSeparator();
343  }
344 }
345 
346 
348 {
349  _currentScaleFactor *= 1.2;
350  scale(1.2, 1.2);
351  scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
352 }
353 
354 
356 {
357  _currentScaleFactor /= 1.2;
358  scale(1 / 1.2, 1 / 1.2);
359  scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
360 }
361 
362 
364 {
367  scene()->setWidth(viewport()->width() - 2);
368 }
369 
370 
372 {
373  // if this is the currently selected chatview
374  // invalidate immediately
375  if (isVisible()) {
377  _invalidateFilter = false;
378  }
379  // otherwise invalidate whenever the view is shown
380  else {
381  _invalidateFilter = true;
382  }
383 }
384 
385 
386 void ChatView::scrollContentsBy(int dx, int dy)
387 {
390 }
391 
392 
393 void ChatView::setHasCache(ChatLine *line, bool hasCache)
394 {
395  if (hasCache)
396  _linesWithCache.insert(line);
397  else
398  _linesWithCache.remove(line);
399 }
400 
401 
403 {
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) {
410  line->clearCache();
411  iter = _linesWithCache.erase(iter);
412  }
413  else
414  ++iter;
415  }
416 }