Update ChatView properly on style changes
[quassel.git] / src / qtui / chatview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QGraphicsTextItem>
22 #include <QKeyEvent>
23 #include <QMenu>
24 #include <QScrollBar>
25
26 #include "bufferwidget.h"
27 #include "chatscene.h"
28 #include "chatview.h"
29 #include "client.h"
30 #include "messagefilter.h"
31 #include "qtui.h"
32 #include "qtuistyle.h"
33
34 ChatView::ChatView(BufferId bufferId, QWidget *parent)
35   : QGraphicsView(parent),
36     AbstractChatView(),
37     _bufferContainer(0),
38     _currentScaleFactor(1)
39 {
40   QList<BufferId> filterList;
41   filterList.append(bufferId);
42   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
43   init(filter);
44 }
45
46 ChatView::ChatView(MessageFilter *filter, QWidget *parent)
47   : QGraphicsView(parent),
48     AbstractChatView(),
49     _bufferContainer(0),
50     _currentScaleFactor(1)
51 {
52   init(filter);
53 }
54
55 void ChatView::init(MessageFilter *filter) {
56   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
57   setAlignment(Qt::AlignBottom);
58   setInteractive(true);
59   //setOptimizationFlags(QGraphicsView::DontClipPainter | QGraphicsView::DontAdjustForAntialiasing);
60   // setOptimizationFlags(QGraphicsView::DontAdjustForAntialiasing);
61   setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
62   // setTransformationAnchor(QGraphicsView::NoAnchor);
63   setTransformationAnchor(QGraphicsView::AnchorViewCenter);
64
65   _scrollTimer.setInterval(100);
66   _scrollTimer.setSingleShot(true);
67   connect(&_scrollTimer, SIGNAL(timeout()), SLOT(scrollTimerTimeout()));
68
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 &)));
73   setScene(_scene);
74
75   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
76 }
77
78 bool ChatView::event(QEvent *event) {
79   if(event->type() == QEvent::KeyPress) {
80     QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
81     switch(keyEvent->key()) {
82     case Qt::Key_Up:
83     case Qt::Key_Down:
84     case Qt::Key_PageUp:
85     case Qt::Key_PageDown:
86       if(!verticalScrollBar()->isVisible()) {
87         scene()->requestBacklog();
88         return true;
89       }
90     default:
91       break;
92     }
93   }
94
95   if(event->type() == QEvent::Wheel) {
96     if(!verticalScrollBar()->isVisible()) {
97       scene()->requestBacklog();
98       return true;
99     }
100   }
101
102   return QGraphicsView::event(event);
103 }
104
105 void ChatView::resizeEvent(QResizeEvent *event) {
106   QGraphicsView::resizeEvent(event);
107
108   // we can reduce viewport updates if we scroll to the bottom allready at the beginning
109   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
110
111   // FIXME: without the hardcoded -4 Qt reserves space for a horizontal scrollbar even though it's disabled permanently.
112   // this does only occur on QtX11 (at least not on Qt for Mac OS). Seems like a Qt Bug.
113   scene()->updateForViewport(viewport()->width() - 4, viewport()->height());
114
115   _lastScrollbarPos = verticalScrollBar()->maximum();
116   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
117 }
118
119 void ChatView::mouseMoveWhileSelecting(const QPointF &scenePos) {
120   int y = (int)mapFromScene(scenePos).y();
121   _scrollOffset = 0;
122   if(y < 0)
123     _scrollOffset = y;
124   else if(y > height())
125     _scrollOffset = y - height();
126
127   if(_scrollOffset && !_scrollTimer.isActive())
128     _scrollTimer.start();
129 }
130
131 void ChatView::scrollTimerTimeout() {
132   // scroll view
133   QAbstractSlider *vbar = verticalScrollBar();
134   if(_scrollOffset < 0 && vbar->value() > 0)
135     vbar->setValue(qMax(vbar->value() + _scrollOffset, 0));
136   else if(_scrollOffset > 0 && vbar->value() < vbar->maximum())
137     vbar->setValue(qMin(vbar->value() + _scrollOffset, vbar->maximum()));
138 }
139
140 void ChatView::lastLineChanged(QGraphicsItem *chatLine, qreal offset) {
141   Q_UNUSED(chatLine)
142   // disabled until further testing/discussion
143   //if(!scene()->isScrollingAllowed())
144   //  return;
145
146   QAbstractSlider *vbar = verticalScrollBar();
147   Q_ASSERT(vbar);
148   if(vbar->maximum() - vbar->value() <= (offset + 5) * _currentScaleFactor ) { // 5px grace area
149     vbar->setValue(vbar->maximum());
150   }
151 }
152
153 void ChatView::verticalScrollbarChanged(int newPos) {
154   QAbstractSlider *vbar = verticalScrollBar();
155   Q_ASSERT(vbar);
156
157   // check for backlog request
158   if(newPos < _lastScrollbarPos) {
159     int relativePos = 100;
160     if(vbar->maximum() - vbar->minimum() != 0)
161       relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
162
163     if(relativePos < 20) {
164       scene()->requestBacklog();
165     }
166   }
167   _lastScrollbarPos = newPos;
168
169   // FIXME: Fugly workaround for the ChatView scrolling up 1px on buffer switch
170   if(vbar->maximum() - newPos <= 2)
171     vbar->setValue(vbar->maximum());
172 }
173
174 MsgId ChatView::lastMsgId() const {
175   if(!scene())
176     return MsgId();
177
178   QAbstractItemModel *model = scene()->model();
179   if(!model || model->rowCount() == 0)
180     return MsgId();
181
182   return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
183 }
184
185 void ChatView::addActionsToMenu(QMenu *menu, const QPointF &pos) {
186   // zoom actions
187   BufferWidget *bw = qobject_cast<BufferWidget *>(bufferContainer());
188   if(bw) {
189     bw->addActionsToMenu(menu, pos);
190     menu->addSeparator();
191   }
192 }
193
194 void ChatView::zoomIn() {
195     _currentScaleFactor *= 1.2;
196     scale(1.2, 1.2);
197     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
198 }
199
200 void ChatView::zoomOut() {
201     _currentScaleFactor /= 1.2;
202     scale(1 / 1.2, 1 / 1.2);
203     scene()->setWidth(viewport()->width() / _currentScaleFactor - 2);
204 }
205
206 void ChatView::zoomOriginal() {
207     scale(1/_currentScaleFactor, 1/_currentScaleFactor);
208     _currentScaleFactor = 1;
209     scene()->setWidth(viewport()->width() - 2);
210 }