Bring back workaround for the unwanted scroll-on-bufferswitch
[quassel.git] / src / qtui / chatview.cpp
index be2cbc6..4a5e286 100644 (file)
@@ -21,7 +21,6 @@
 #include <QGraphicsTextItem>
 #include <QScrollBar>
 
-#include "buffer.h"
 #include "chatlinemodelitem.h"
 #include "chatscene.h"
 #include "chatview.h"
 #include "messagefilter.h"
 #include "quasselui.h"
 
-ChatView::ChatView(Buffer *buf, QWidget *parent) : QGraphicsView(parent), AbstractChatView() {
+ChatView::ChatView(BufferId bufferId, QWidget *parent)
+  : QGraphicsView(parent),
+    AbstractChatView()
+{
   QList<BufferId> filterList;
-  filterList.append(buf->bufferInfo().bufferId());
+  filterList.append(bufferId);
   MessageFilter *filter = new MessageFilter(Client::messageModel(), filterList, this);
   init(filter);
-
 }
 
-ChatView::ChatView(MessageFilter *filter, QWidget *parent) : QGraphicsView(parent), AbstractChatView() {
+ChatView::ChatView(MessageFilter *filter, QWidget *parent)
+  : QGraphicsView(parent),
+    AbstractChatView()
+{
   init(filter);
 }
 
@@ -46,32 +50,53 @@ void ChatView::init(MessageFilter *filter) {
   setAlignment(Qt::AlignBottom);
   setInteractive(true);
 
-  _scene = new ChatScene(filter, filter->idString(), this);
-  connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
+  _scene = new ChatScene(filter, filter->idString(), viewport()->width() - 2, this); // see below: resizeEvent()
+  connect(_scene, SIGNAL(sceneHeightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
   setScene(_scene);
+
+  _lastScrollbarPos = verticalScrollBar()->maximum();
+  connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
 }
 
-ChatView::~ChatView() {
+void ChatView::resizeEvent(QResizeEvent *event) {
+  QGraphicsView::resizeEvent(event);
+  scene()->setWidth(viewport()->width() - 2);  // FIXME figure out why we have to hardcode the -2 here -> Qt-Bug most probably
+  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
+}
 
+void ChatView::sceneHeightChanged(qreal dh) {
+  QAbstractSlider *vbar = verticalScrollBar();
+  Q_ASSERT(vbar);
+  if(vbar->maximum() - vbar->value() <= dh + 5) // in case we had scrolled only about half a line to the bottom we allow a grace of 5
+    vbar->setValue(vbar->maximum());
 }
 
+void ChatView::verticalScrollbarChanged(int newPos) {
+  QAbstractSlider *vbar = verticalScrollBar();
+  Q_ASSERT(vbar);
 
-ChatScene *ChatView::scene() const {
-  return _scene;
-}
+  if(vbar->maximum() - vbar->value() <= 5) // FIXME workaround the fact that the view gets scrolled up a few px on buffer change
+    vbar->setValue(vbar->maximum());
 
-void ChatView::resizeEvent(QResizeEvent *event) {
-  scene()->setWidth(event->size().width() - 2);  // FIXME figure out why we have to hardcode the -2 here
-  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
-}
+  if(newPos < _lastScrollbarPos) {
+    int relativePos = 100;
+    if(vbar->maximum() - vbar->minimum() != 0)
+      relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
 
-void ChatView::sceneHeightChanged(qreal h) {
-  Q_UNUSED(h)
-  bool scrollable = qAbs(verticalScrollBar()->value() - verticalScrollBar()->maximum()) <= 2; // be a bit tolerant here, also FIXME (why we need this?)
-  setSceneRect(scene()->sceneRect());
-  if(scrollable) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
+    if(relativePos < 20) {
+      scene()->requestBacklog();
+    }
+  }
+  _lastScrollbarPos = newPos;
 }
 
-void ChatView::clear()
-{
+MsgId ChatView::lastMsgId() const {
+  if(!scene())
+    return MsgId();
+
+  QAbstractItemModel *model = scene()->model();
+  if(!model || model->rowCount() == 0)
+    return MsgId();
+
+  return model->data(model->index(model->rowCount() - 1, 0), MessageModel::MsgIdRole).value<MsgId>();
 }