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 setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
61 setAlignment(Qt::AlignLeft|Qt::AlignBottom);
63 //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
64 // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
65 setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
66 // setTransformationAnchor(QGraphicsView::NoAnchor);
67 setTransformationAnchor(QGraphicsView::AnchorViewCenter);
69 _scrollTimer.setInterval(100);
70 _scrollTimer.setSingleShot(true);
71 connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
73 _scene = new ChatScene(filter, filter->idString(), viewport()->width(), this);
74 connect(_scene, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(adjustSceneRect()));
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)));
81 // only connect if client is synched with a core
82 if(Client::isConnected())
83 connect(Client::ignoreListManager(), SIGNAL(ignoreListChanged()), this, SLOT(invalidateFilter()));
86 bool ChatView::event(QEvent *event) {
87 if(event->type() == QEvent::KeyPress) {
88 QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
89 switch(keyEvent->key()) {
93 case Qt::Key_PageDown:
94 if(!verticalScrollBar()->isVisible()) {
95 scene()->requestBacklog();
103 if(event->type() == QEvent::Wheel) {
104 if(!verticalScrollBar()->isVisible()) {
105 scene()->requestBacklog();
110 if(event->type() == QEvent::Show) {
111 if(_invalidateFilter)
115 return QGraphicsView::event(event);
118 void ChatView::resizeEvent(QResizeEvent *event) {
119 QGraphicsView::resizeEvent(event);
121 // we can reduce viewport updates if we scroll to the bottom allready at the beginning
122 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
123 scene()->updateForViewport(viewport()->width(), viewport()->height());
126 _lastScrollbarPos = verticalScrollBar()->maximum();
127 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
130 void ChatView::adjustSceneRect() {
131 // Workaround for QTBUG-6322
132 // If the viewport's sceneRect() is (almost) as wide as as the viewport itself,
133 // Qt wants to reserve space for scrollbars even if they're turned off, resulting in
134 // an ugly white space at the bottom of the ChatView.
135 // Since the view's scene's width actually doesn't matter at all, we just adjust it
136 // by some hopefully large enough value to avoid this problem.
138 setSceneRect(scene()->sceneRect().adjusted(0, 0, -25 ,0));
141 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
142 int y = (int)mapFromScene(scenePos).y();
146 else if(y > height())
147 _scrollOffset = y - height();
149 if(_scrollOffset && !_scrollTimer.isActive())
150 _scrollTimer.start();
153 void ChatView::scrollTimerTimeout() {
155 QAbstractSlider *vbar = verticalScrollBar();
156 if(_scrollOffset < 0 && vbar->value() > 0)
157 vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
158 else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
159 vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
162 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
164 // disabled until further testing/discussion
165 //if(!scene()->isScrollingAllowed())
168 QAbstractSlider *vbar = verticalScrollBar();
170 if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
171 vbar->setValue(vbar->maximum());
175 void ChatView::verticalScrollbarChanged(int newPos) {
176 QAbstractSlider *vbar = verticalScrollBar();
179 // check for backlog request
180 if(newPos < _lastScrollbarPos) {
181 int relativePos = 100;
182 if(vbar->maximum() - vbar->minimum() != 0)
183 relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
185 if(relativePos < 20) {
186 scene()->requestBacklog();
189 _lastScrollbarPos = newPos;
191 // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
192 if(vbar->maximum() - newPos <= 2)
193 vbar->setValue(vbar->maximum());
196 MsgId ChatView::lastMsgId() const {
200 QAbstractItemModel *model = scene()->model();
201 if(!model || model->rowCount() == 0)
204 return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
207 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
209 BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
211 bw->addActionsToMenu(menu, pos);
212 menu->addSeparator();
216 void ChatView::zoomIn() {
217 _currentScaleFactor *= 1.2;
219 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
222 void ChatView::zoomOut() {
223 _currentScaleFactor /= 1.2;
224 scale(1 / 1.2, 1 / 1.2);
225 scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
228 void ChatView::zoomOriginal() {
229 scale(1/_currentScaleFactor, 1/_currentScaleFactor);
230 _currentScaleFactor = 1;
231 scene()->setWidth(viewport()->width() - 2);
234 void ChatView::invalidateFilter() {
235 // if this is the currently selected chatview
236 // invalidate immediately
238 _scene->filter()->invalidateFilter();
239 _invalidateFilter = false;
241 // otherwise invalidate whenever the view is shown
243 _invalidateFilter = true;