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"
34 ChatView::ChatView(BufferId bufferId, QWidget *parent)
35 : QGraphicsView(parent),
38 _currentScaleFactor(1)
40 QList<BufferId> filterList;
41 filterList.append(bufferId);
42 MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
46 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
47 : QGraphicsView(parent),
50 _currentScaleFactor(1)
55 void ChatView::init(MessageFilter *filter) {
56 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57 setAlignment(Qt::AlignBottom|Qt::AlignLeft);
59 //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
60 // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
61 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
62 // setTransformationAnchor(QGraphicsView::NoAnchor);
63 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
65 _scrollTimer.setInterval(100);
66 _scrollTimer.setSingleShot(true);
67 connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
69 _resizeTimer.setInterval(100);
70 _resizeTimer.setSingleShot(true);
71 connect(&_resizeTimer, SIGNAL(timeout()), SLOT(resizeTimerTimeout()));
73 _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent()
74 connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
75 connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
76 connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
79 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
80 connect(QtUi::style(), SIGNAL(changed()), this, SLOT(styleChanged()));
83 bool ChatView::event(QEvent *event) {
84 if(event->type() == QEvent::KeyPress) {
85 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
86 switch(keyEvent->key()) {
90 case Qt::Key_PageDown:
91 if(!verticalScrollBar()->isVisible()) {
92 scene()->requestBacklog();
100 if(event->type() == QEvent::Wheel) {
101 if(!verticalScrollBar()->isVisible()) {
102 scene()->requestBacklog();
107 return QGraphicsView::event(event);
110 void ChatView::resizeEvent(QResizeEvent *event) {
111 QGraphicsView::resizeEvent(event);
112 _resizeTimer.start();
115 void ChatView::resizeTimerTimeout() {
117 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
118 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
120 // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
121 // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
122 scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
124 _lastScrollbarPos = verticalScrollBar()->maximum();
125 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
128 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
129 int y = (int)mapFromScene(scenePos).y();
133 else if(y > height())
134 _scrollOffset = y - height();
136 if(_scrollOffset && !_scrollTimer.isActive())
137 _scrollTimer.start();
140 void ChatView::scrollTimerTimeout() {
142 QAbstractSlider *vbar = verticalScrollBar();
143 if(_scrollOffset < 0 && vbar->value() > 0)
144 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
145 else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
146 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
149 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
151 // disabled until further testing/discussion
152 //if(!scene()->isScrollingAllowed())
155 QAbstractSlider *vbar = verticalScrollBar();
157 if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
158 vbar->setValue(vbar->maximum());
162 void ChatView::verticalScrollbarChanged(int newPos) {
163 QAbstractSlider *vbar = verticalScrollBar();
166 // check for backlog request
167 if(newPos < _lastScrollbarPos) {
168 int relativePos = 100;
169 if(vbar->maximum() - vbar->minimum() != 0)
170 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
172 if(relativePos < 20) {
173 scene()->requestBacklog();
176 _lastScrollbarPos = newPos;
178 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
179 if(vbar->maximum() - newPos <= 2)
180 vbar->setValue(vbar->maximum());
183 void ChatView::styleChanged() {
189 MsgId ChatView::lastMsgId() const {
193 QAbstractItemModel *model = scene()->model();
194 if(!model || model->rowCount() == 0)
197 return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
200 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
202 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
204 bw->addActionsToMenu(menu, pos);
205 menu->addSeparator();
209 void ChatView::zoomIn() {
210 _currentScaleFactor *= 1.2;
212 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
215 void ChatView::zoomOut() {
216 _currentScaleFactor /= 1.2;
217 scale(1 / 1.2, 1 / 1.2);
218 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
221 void ChatView::zoomOriginal() {
222 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
223 _currentScaleFactor = 1;
224 scene()->setWidth(viewport()->width() - 2);