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);
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 _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 4, this); // see below: resizeEvent()
70 connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(sceneRectChanged(const QRectF &)));
71 connect(_scene, SIGNAL(lastLineChanged(QGraphicsItem *, qreal)), this, SLOT(lastLineChanged(QGraphicsItem *, qreal)));
72 connect(_scene, SIGNAL(mouseMoveWhileSelecting(const QPointF &)), this, SLOT(mouseMoveWhileSelecting(const QPointF &)));
75 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
76 connect(QtUi::style(), SIGNAL(changed()), this, SLOT(styleChanged()));
79 bool ChatView::event(QEvent *event) {
80 if(event->type() == QEvent::KeyPress) {
81 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
82 switch(keyEvent->key()) {
86 case Qt::Key_PageDown:
87 if(!verticalScrollBar()->isVisible()) {
88 scene()->requestBacklog();
96 if(event->type() == QEvent::Wheel) {
97 if(!verticalScrollBar()->isVisible()) {
98 scene()->requestBacklog();
103 return QGraphicsView::event(event);
106 void ChatView::resizeEvent(QResizeEvent *event) {
107 QGraphicsView::resizeEvent(event);
109 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
110 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
112 // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
113 // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
114 scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
116 _lastScrollbarPos = verticalScrollBar()->maximum();
117 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
120 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
121 int y = (int)mapFromScene(scenePos).y();
125 else if(y > height())
126 _scrollOffset = y - height();
128 if(_scrollOffset && !_scrollTimer.isActive())
129 _scrollTimer.start();
132 void ChatView::scrollTimerTimeout() {
134 QAbstractSlider *vbar = verticalScrollBar();
135 if(_scrollOffset < 0 && vbar->value() > 0)
136 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
137 else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
138 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
141 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
143 // disabled until further testing/discussion
144 //if(!scene()->isScrollingAllowed())
147 QAbstractSlider *vbar = verticalScrollBar();
149 if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
150 vbar->setValue(vbar->maximum());
154 void ChatView::verticalScrollbarChanged(int newPos) {
155 QAbstractSlider *vbar = verticalScrollBar();
158 // check for backlog request
159 if(newPos < _lastScrollbarPos) {
160 int relativePos = 100;
161 if(vbar->maximum() - vbar->minimum() != 0)
162 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
164 if(relativePos < 20) {
165 scene()->requestBacklog();
168 _lastScrollbarPos = newPos;
170 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
171 if(vbar->maximum() - newPos <= 2)
172 vbar->setValue(vbar->maximum());
175 void ChatView::styleChanged() {
179 MsgId ChatView::lastMsgId() const {
183 QAbstractItemModel *model = scene()->model();
184 if(!model || model->rowCount() == 0)
187 return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
190 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
192 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
194 bw->addActionsToMenu(menu, pos);
195 menu->addSeparator();
199 void ChatView::zoomIn() {
200 _currentScaleFactor *= 1.2;
202 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
205 void ChatView::zoomOut() {
206 _currentScaleFactor /= 1.2;
207 scale(1 / 1.2, 1 / 1.2);
208 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
211 void ChatView::zoomOriginal() {
212 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
213 _currentScaleFactor = 1;
214 scene()->setWidth(viewport()->width() - 2);