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"
35 ChatView::ChatView(BufferId bufferId, QWidget *parent)
36 : QGraphicsView(parent),
39 _currentScaleFactor(1),
40 _invalidateFilter(false)
42 QList<BufferId> filterList;
43 filterList.append(bufferId);
44 MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
48 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
49 : QGraphicsView(parent),
52 _currentScaleFactor(1),
53 _invalidateFilter(false)
58 void ChatView::init(MessageFilter *filter) {
59 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60 setAlignment(Qt::AlignBottom);
62 //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
63 // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
64 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
65 // setTransformationAnchor(QGraphicsView::NoAnchor);
66 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
68 _scrollTimer.setInterval(100);
69 _scrollTimer.setSingleShot(true);
70 connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
72 _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent()
73 connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
74 connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
75 connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
78 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
80 // only connect if client is synched with a core
81 if(Client::isSynced())
82 connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
85 bool ChatView::event(QEvent *event) {
86 if(event->type() == QEvent::KeyPress) {
87 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
88 switch(keyEvent->key()) {
92 case Qt::Key_PageDown:
93 if(!verticalScrollBar()->isVisible()) {
94 scene()->requestBacklog();
102 if(event->type() == QEvent::Wheel) {
103 if(!verticalScrollBar()->isVisible()) {
104 scene()->requestBacklog();
109 if(event->type() == QEvent::Show) {
110 if(_invalidateFilter)
114 return QGraphicsView::event(event);
117 void ChatView::resizeEvent(QResizeEvent *event) {
118 QGraphicsView::resizeEvent(event);
120 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
121 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
123 // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
124 // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
125 scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
127 _lastScrollbarPos = verticalScrollBar()->maximum();
128 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
131 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
132 int y = (int)mapFromScene(scenePos).y();
136 else if(y > height())
137 _scrollOffset = y - height();
139 if(_scrollOffset && !_scrollTimer.isActive())
140 _scrollTimer.start();
143 void ChatView::scrollTimerTimeout() {
145 QAbstractSlider *vbar = verticalScrollBar();
146 if(_scrollOffset < 0 && vbar->value() > 0)
147 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
148 else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
149 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
152 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
154 // disabled until further testing/discussion
155 //if(!scene()->isScrollingAllowed())
158 QAbstractSlider *vbar = verticalScrollBar();
160 if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
161 vbar->setValue(vbar->maximum());
165 void ChatView::verticalScrollbarChanged(int newPos) {
166 QAbstractSlider *vbar = verticalScrollBar();
169 // check for backlog request
170 if(newPos < _lastScrollbarPos) {
171 int relativePos = 100;
172 if(vbar->maximum() - vbar->minimum() != 0)
173 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
175 if(relativePos < 20) {
176 scene()->requestBacklog();
179 _lastScrollbarPos = newPos;
181 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
182 if(vbar->maximum() - newPos <= 2)
183 vbar->setValue(vbar->maximum());
186 MsgId ChatView::lastMsgId() const {
190 QAbstractItemModel *model = scene()->model();
191 if(!model || model->rowCount() == 0)
194 return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
197 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
199 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
201 bw->addActionsToMenu(menu, pos);
202 menu->addSeparator();
206 void ChatView::zoomIn() {
207 _currentScaleFactor *= 1.2;
209 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
212 void ChatView::zoomOut() {
213 _currentScaleFactor /= 1.2;
214 scale(1 / 1.2, 1 / 1.2);
215 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
218 void ChatView::zoomOriginal() {
219 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
220 _currentScaleFactor = 1;
221 scene()->setWidth(viewport()->width() - 2);
224 void ChatView::invalidateFilter() {
225 // if this is the currently selected chatview
226 // invalidate immediately
228 _scene->filter()->invalidateFilter();
229 _invalidateFilter = false;
231 // otherwise invalidate whenever the view is shown
233 _invalidateFilter = true;