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 _verticalOffsetStable(false)
44 QList<BufferId> filterList;
45 filterList.append(bufferId);
46 MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
50 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
51 : QGraphicsView(parent),
54 _currentScaleFactor(1),
55 _invalidateFilter(false)
60 void ChatView::init(MessageFilter *filter) {
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)));
83 // only connect if client is synched with a core
84 if(Client::isConnected())
85 connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
88 bool ChatView::event(QEvent *event) {
89 if(event->type() == QEvent::KeyPress) {
90 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
91 switch(keyEvent->key()) {
95 case Qt::Key_PageDown:
96 if(!verticalScrollBar()->isVisible()) {
97 scene()->requestBacklog();
105 if(event->type() == QEvent::Wheel) {
106 if(!verticalScrollBar()->isVisible()) {
107 scene()->requestBacklog();
112 if(event->type() == QEvent::Show) {
113 if(_invalidateFilter)
117 return QGraphicsView::event(event);
120 void ChatView::resizeEvent(QResizeEvent *event) {
121 QGraphicsView::resizeEvent(event);
123 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
124 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
125 scene()->updateForViewport(viewport()->width(), viewport()->height());
128 _lastScrollbarPos = verticalScrollBar()->maximum();
129 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
132 // Workaround for QTBUG-6322
133 // The viewport rect gets some margins where it shouldn't, resulting in scrollbars to appear
134 void ChatView::adjustSceneRect() {
135 QRectF rect = scene()->sceneRect(); // qDebug() << "sceneRect" << rect;
136 if(rect.height() <= viewport()->height()) {
141 if(_verticalOffsetStable && rect.height() > viewport()->height())
142 setSceneRect(rect.adjusted(0, 0, 0, _verticalOffset));
146 QScrollBar *vbar = verticalScrollBar();
147 qreal sceneHeight = rect.height();
148 qreal viewHeight = vbar->maximum() + viewport()->height() - vbar->minimum();
149 if(sceneHeight != viewHeight) {
150 qreal voffset = sceneHeight - viewHeight; //voffset *= _currentScaleFactor;
151 // qDebug() << "Adjusting ChatView offset to" << voffset << "(QTBUG-6322)";
152 if(sceneHeight + voffset <= viewport()->height()) {
153 setSceneRect(rect.adjusted(0, -voffset, 0, 0));
154 _verticalOffsetStable = false;
157 _verticalOffsetStable = true;
158 _verticalOffset = voffset;
159 setSceneRect(rect.adjusted(0, 0, 0, voffset));
161 if(vbar->maximum() + viewport()->height() - vbar->minimum() != sceneHeight) {
162 //qWarning() << "Workaround for QTBUG-6322 failed!1!!" << vbar->maximum() + viewport()->height() - vbar->minimum() << sceneHeight;
163 _verticalOffsetStable = false;
167 if(voffset == _verticalOffset)
168 _verticalOffsetStable = true;
171 _verticalOffset = voffset;
174 //_verticalOffsetStable = true;
177 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
178 int y = (int)mapFromScene(scenePos).y();
182 else if(y > height())
183 _scrollOffset = y - height();
185 if(_scrollOffset && !_scrollTimer.isActive())
186 _scrollTimer.start();
189 void ChatView::scrollTimerTimeout() {
191 QAbstractSlider *vbar = verticalScrollBar();
192 if(_scrollOffset < 0 && vbar->value() > 0)
193 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
194 else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
195 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
198 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
200 // disabled until further testing/discussion
201 //if(!scene()->isScrollingAllowed())
204 QAbstractSlider *vbar = verticalScrollBar();
206 if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
207 vbar->setValue(vbar->maximum());
211 void ChatView::verticalScrollbarChanged(int newPos) {
212 QAbstractSlider *vbar = verticalScrollBar();
215 // check for backlog request
216 if(newPos < _lastScrollbarPos) {
217 int relativePos = 100;
218 if(vbar->maximum() - vbar->minimum() != 0)
219 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
221 if(relativePos < 20) {
222 scene()->requestBacklog();
225 _lastScrollbarPos = newPos;
227 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
228 if(vbar->maximum() - newPos <= 2)
229 vbar->setValue(vbar->maximum());
232 MsgId ChatView::lastMsgId() const {
236 QAbstractItemModel *model = scene()->model();
237 if(!model || model->rowCount() == 0)
240 return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
243 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
245 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
247 bw->addActionsToMenu(menu, pos);
248 menu->addSeparator();
252 void ChatView::zoomIn() {
253 _currentScaleFactor *= 1.2;
255 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
258 void ChatView::zoomOut() {
259 _currentScaleFactor /= 1.2;
260 scale(1 / 1.2, 1 / 1.2);
261 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
264 void ChatView::zoomOriginal() {
265 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
266 _currentScaleFactor = 1;
267 scene()->setWidth(viewport()->width() - 2);
270 void ChatView::invalidateFilter() {
271 // if this is the currently selected chatview
272 // invalidate immediately
274 _scene->filter()->invalidateFilter();
275 _invalidateFilter = false;
277 // otherwise invalidate whenever the view is shown
279 _invalidateFilter = true;