1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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);
47 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
48 : QGraphicsView(parent),
54 void ChatView::init(MessageFilter *filter) {
56 _currentScaleFactor = 1;
57 _invalidateFilter = false;
58 _markerLineVisible = true;
61 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
62 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
63 setAlignment(Qt::AlignLeft|Qt::AlignBottom);
65 //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
66 // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
67 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
68 // setTransformationAnchor(QGraphicsView::NoAnchor);
69 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
71 _scrollTimer.setInterval(100);
72 _scrollTimer.setSingleShot(true);
73 connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
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 &)));
81 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
82 _lastScrollbarPos = verticalScrollBar()->value();
84 connect(Client::networkModel(), SIGNAL(markerLineSet(BufferId,MsgId)), SLOT(markerLineSet(BufferId,MsgId)));
86 // only connect if client is synched with a core
87 if(Client::isConnected())
88 connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
91 bool ChatView::event(QEvent *event) {
92 if(event->type() == QEvent::KeyPress) {
93 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
94 switch(keyEvent->key()) {
98 case Qt::Key_PageDown:
99 if(!verticalScrollBar()->isVisible()) {
100 scene()->requestBacklog();
108 if(event->type() == QEvent::Wheel) {
109 if(!verticalScrollBar()->isVisible()) {
110 scene()->requestBacklog();
115 if(event->type() == QEvent::Show) {
116 if(_invalidateFilter)
120 return QGraphicsView::event(event);
123 void ChatView::resizeEvent(QResizeEvent *event) {
124 QGraphicsView::resizeEvent(event);
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());
131 _lastScrollbarPos = verticalScrollBar()->maximum();
132 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
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.
143 setSceneRect(scene()->sceneRect().adjusted(0, 0, -25 ,0));
146 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
147 int y = (int)mapFromScene(scenePos).y();
151 else if(y > height())
152 _scrollOffset = y - height();
154 if(_scrollOffset && !_scrollTimer.isActive())
155 _scrollTimer.start();
158 void ChatView::scrollTimerTimeout() {
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()));
167 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
169 // disabled until further testing/discussion
170 //if(!scene()->isScrollingAllowed())
173 QAbstractSlider *vbar = verticalScrollBar();
175 if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
176 vbar->setValue(vbar->maximum());
180 void ChatView::verticalScrollbarChanged(int newPos) {
181 QAbstractSlider *vbar = verticalScrollBar();
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());
190 if(relativePos < 20) {
191 scene()->requestBacklog();
194 _lastScrollbarPos = newPos;
196 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
197 if(vbar->maximum() - newPos <= 2)
198 vbar->setValue(vbar->maximum());
201 MsgId ChatView::lastMsgId() const {
205 QAbstractItemModel *model = scene()->model();
206 if(!model || model->rowCount() == 0)
209 return model->index(model->rowCount() - 1, 0).data(MessageModel::MsgIdRole).value<MsgId>();
212 MsgId ChatView::lastVisibleMsgId() const {
213 ChatLine *line = lastVisibleChatLine();
216 return line->msgId();
221 bool chatLinePtrLessThan(ChatLine *one, ChatLine *other) {
222 return one->row() < other->row();
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);
235 QList<ChatLine *> ChatView::visibleChatLinesSorted(Qt::ItemSelectionMode mode) const {
236 QList<ChatLine *> result = visibleChatLines(mode).toList();
237 qSort(result.begin(), result.end(), chatLinePtrLessThan);
241 ChatLine *ChatView::lastVisibleChatLine() const {
245 QAbstractItemModel *model = scene()->model();
246 if(!model || model->rowCount() == 0)
251 QSet<ChatLine *> visibleLines = visibleChatLines(Qt::ContainsItemBoundingRect);
252 foreach(ChatLine *line, visibleLines) {
253 if(line->row() > row)
258 return scene()->chatLine(row);
263 void ChatView::setMarkerLineVisible(bool visible) {
264 if(visible != _markerLineVisible) {
265 _markerLineVisible = visible;
269 void ChatView::setMarkedLine(ChatLine *line) {
270 if(_markedLine == line)
273 if(!scene()->isSingleBufferScene())
277 BufferId bufId = scene()->singleBufferId();
278 Client::setMarkerLine(bufId, line->msgId());
282 void ChatView::markerLineSet(BufferId buffer, MsgId msg) {
283 if(!scene()->isSingleBufferScene() || scene()->singleBufferId() != buffer)
286 ChatLine *newLine = scene()->chatLine(msg);
287 if(_markedLine == newLine)
290 ChatLine *oldLine = _markedLine;
291 _markedLine = newLine;
297 setMarkerLineVisible(true);
302 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
304 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
306 bw->addActionsToMenu(menu, pos);
307 menu->addSeparator();
311 void ChatView::zoomIn() {
312 _currentScaleFactor *= 1.2;
314 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
317 void ChatView::zoomOut() {
318 _currentScaleFactor /= 1.2;
319 scale(1 / 1.2, 1 / 1.2);
320 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
323 void ChatView::zoomOriginal() {
324 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
325 _currentScaleFactor = 1;
326 scene()->setWidth(viewport()->width() - 2);
329 void ChatView::invalidateFilter() {
330 // if this is the currently selected chatview
331 // invalidate immediately
333 _scene->filter()->invalidateFilter();
334 _invalidateFilter = false;
336 // otherwise invalidate whenever the view is shown
338 _invalidateFilter = true;