Bring back dynamic backlog fetching (move scrollbar slider to the very top to get...
authorManuel Nickschas <sputnick@quassel-irc.org>
Sun, 10 Aug 2008 22:32:15 +0000 (00:32 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 11 Aug 2008 11:06:32 +0000 (13:06 +0200)
src/qtui/bufferwidget.cpp
src/qtui/chatscene.cpp
src/qtui/chatscene.h
src/qtui/chatview.cpp
src/qtui/chatview.h

index d6dcfa2..c1e992d 100644 (file)
@@ -34,12 +34,13 @@ BufferWidget::~BufferWidget() {
 }
 
 AbstractChatView *BufferWidget::createChatView(BufferId id) {
 }
 
 AbstractChatView *BufferWidget::createChatView(BufferId id) {
-  QWidget *chatView;
+  ChatView *chatView;
   chatView = new ChatView(Client::buffer(id), this);
   _chatViews[id] = chatView;
   ui.stackedWidget->addWidget(chatView);
   chatView->setFocusProxy(this);
   chatView = new ChatView(Client::buffer(id), this);
   _chatViews[id] = chatView;
   ui.stackedWidget->addWidget(chatView);
   chatView->setFocusProxy(this);
-  return dynamic_cast<AbstractChatView*>(chatView);
+  chatView->setBufferForBacklogFetching(id);
+  return chatView;
 }
 
 void BufferWidget::removeChatView(BufferId id) {
 }
 
 void BufferWidget::removeChatView(BufferId id) {
index 43ff248..a6578fa 100644 (file)
@@ -28,6 +28,8 @@
 #include "chatline.h"
 #include "chatlinemodelitem.h"
 #include "chatscene.h"
 #include "chatline.h"
 #include "chatlinemodelitem.h"
 #include "chatscene.h"
+#include "client.h"
+#include "clientbacklogmanager.h"
 #include "columnhandleitem.h"
 #include "qtui.h"
 #include "qtuisettings.h"
 #include "columnhandleitem.h"
 #include "qtui.h"
 #include "qtuisettings.h"
@@ -43,6 +45,8 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject
   _selectingItem = 0;
   _isSelecting = false;
   _selectionStart = -1;
   _selectingItem = 0;
   _isSelecting = false;
   _selectionStart = -1;
+  _fetchingBacklog = false;
+
   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
 
   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
   connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
 
   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
@@ -101,7 +105,7 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
   for(int i = end+1; i < _lines.count(); i++) {
     _lines[i]->setRow(i);
   }
   for(int i = end+1; i < _lines.count(); i++) {
     _lines[i]->setRow(i);
   }
-  
+
   if(h > 0) {
     _height += h;
     for(int i = end+1; i < _lines.count(); i++) {
   if(h > 0) {
     _height += h;
     for(int i = end+1; i < _lines.count(); i++) {
@@ -110,6 +114,8 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) {
     setSceneRect(QRectF(0, 0, _width, _height));
     emit heightChanged(_height);
   }
     setSceneRect(QRectF(0, 0, _width, _height));
     emit heightChanged(_height);
   }
+
+  requestBacklogIfNeeded();
 }
 
 void ChatScene::modelReset() {
 }
 
 void ChatScene::modelReset() {
@@ -275,3 +281,27 @@ QString ChatScene::selectionToString() const {
   }
   return result;
 }
   }
   return result;
 }
+
+void ChatScene::setIsFetchingBacklog(bool fetch) {
+  if(!isBacklogFetchingEnabled()) return;
+
+  if(!fetch) {
+    _fetchingBacklog = false;
+  } else {
+    _fetchingBacklog = true;
+    requestBacklogIfNeeded();
+  }
+}
+
+void ChatScene::requestBacklogIfNeeded() {
+  const int REQUEST_COUNT = 50;
+
+  if(!isBacklogFetchingEnabled() || !isFetchingBacklog() || !model()->rowCount()) return;
+
+  MsgId msgId = model()->data(model()->index(0, 0), ChatLineModel::MsgIdRole).value<MsgId>();
+  if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) {
+    Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt());
+    _lastBacklogOffset = msgId;
+    _lastBacklogSize = model()->rowCount();
+  }
+}
index 22dcb47..e72308e 100644 (file)
 #include <QGraphicsScene>
 #include <QSet>
 
 #include <QGraphicsScene>
 #include <QSet>
 
+#include "types.h"
+
 class AbstractUiMsg;
 class Buffer;
 class AbstractUiMsg;
 class Buffer;
+class BufferId;
 class ChatItem;
 class ChatLine;
 class ColumnHandleItem;
 class ChatItem;
 class ChatLine;
 class ColumnHandleItem;
@@ -40,10 +43,13 @@ class ChatScene : public QGraphicsScene {
     ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent);
     virtual ~ChatScene();
 
     ChatScene(QAbstractItemModel *model, const QString &idString, QObject *parent);
     virtual ~ChatScene();
 
-    Buffer *buffer() const;
     inline QAbstractItemModel *model() const { return _model; }
     inline QString idString() const { return _idString; }
 
     inline QAbstractItemModel *model() const { return _model; }
     inline QString idString() const { return _idString; }
 
+    inline bool isFetchingBacklog() const;
+    inline bool isBacklogFetchingEnabled() const;
+    inline BufferId bufferForBacklogFetching() const;
+
   public slots:
     void setWidth(qreal);
 
   public slots:
     void setWidth(qreal);
 
@@ -52,6 +58,9 @@ class ChatScene : public QGraphicsScene {
     ChatItem *selectingItem() const { return _selectingItem; }
     void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
 
     ChatItem *selectingItem() const { return _selectingItem; }
     void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
 
+    void setIsFetchingBacklog(bool);
+    inline void setBufferForBacklogFetching(BufferId buffer);
+
   signals:
     void heightChanged(qreal height);
 
   signals:
     void heightChanged(qreal height);
 
@@ -71,6 +80,7 @@ class ChatScene : public QGraphicsScene {
   private:
     void updateSelection(const QPointF &pos);
     QString selectionToString() const;
   private:
     void updateSelection(const QPointF &pos);
     QString selectionToString() const;
+    void requestBacklogIfNeeded();
 
     QString _idString;
     qreal _width, _height;
 
     QString _idString;
     qreal _width, _height;
@@ -86,6 +96,27 @@ class ChatScene : public QGraphicsScene {
     int _selectionStart;
     int _selectionEnd;
     bool _isSelecting;
     int _selectionStart;
     int _selectionEnd;
     bool _isSelecting;
+
+    bool _fetchingBacklog;
+    BufferId _backlogFetchingBuffer;
+    MsgId _lastBacklogOffset;
+    int _lastBacklogSize;
 };
 
 };
 
+bool ChatScene::isFetchingBacklog() const {
+  return _fetchingBacklog;
+}
+
+bool ChatScene::isBacklogFetchingEnabled() const {
+  return _backlogFetchingBuffer.isValid();
+}
+
+BufferId ChatScene::bufferForBacklogFetching() const {
+  return _backlogFetchingBuffer;
+}
+
+void ChatScene::setBufferForBacklogFetching(BufferId buf) {
+  _backlogFetchingBuffer = buf;
+}
+
 #endif
 #endif
index be2cbc6..6d4e5ea 100644 (file)
@@ -49,17 +49,24 @@ void ChatView::init(MessageFilter *filter) {
   _scene = new ChatScene(filter, filter->idString(), this);
   connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
   setScene(_scene);
   _scene = new ChatScene(filter, filter->idString(), this);
   connect(_scene, SIGNAL(heightChanged(qreal)), this, SLOT(sceneHeightChanged(qreal)));
   setScene(_scene);
+
+  connect(verticalScrollBar(), SIGNAL(sliderPressed()), this, SLOT(sliderPressed()));
+  connect(verticalScrollBar(), SIGNAL(sliderReleased()), this, SLOT(sliderReleased()));
+  connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(verticalScrollbarChanged(int)));
 }
 
 ChatView::~ChatView() {
 
 }
 
 }
 
 ChatView::~ChatView() {
 
 }
 
-
 ChatScene *ChatView::scene() const {
   return _scene;
 }
 
 ChatScene *ChatView::scene() const {
   return _scene;
 }
 
+void ChatView::clear() {
+
+}
+
 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());
 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());
@@ -72,6 +79,32 @@ void ChatView::sceneHeightChanged(qreal h) {
   if(scrollable) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
 }
 
   if(scrollable) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
 }
 
-void ChatView::clear()
-{
+void ChatView::setBufferForBacklogFetching(BufferId id) {
+  scene()->setBufferForBacklogFetching(id);
+}
+
+void ChatView::sliderPressed() {
+  verticalScrollbarChanged(verticalScrollBar()->value());
+}
+
+void ChatView::sliderReleased() {
+  if(scene()->isFetchingBacklog()) scene()->setIsFetchingBacklog(false);
+}
+
+void ChatView::verticalScrollbarChanged(int newPos) {
+  Q_UNUSED(newPos);
+  if(!scene()->isBacklogFetchingEnabled()) return;
+
+  QAbstractSlider *vbar = verticalScrollBar();
+  if(!vbar)
+    return;
+  if(vbar->isSliderDown()) {
+    /*
+    int relativePos = 100;
+    if(vbar->maximum() - vbar->minimum() != 0)
+      relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
+    scene()->setIsFetchingBacklog(relativePos < 20);
+    */
+    scene()->setIsFetchingBacklog(vbar->value() == vbar->minimum());
+  }
 }
 }
index 7d84e6a..b9c1892 100644 (file)
@@ -45,11 +45,16 @@ class ChatView : public QGraphicsView, public AbstractChatView {
 
     void clear();
 
 
     void clear();
 
+    void setBufferForBacklogFetching(BufferId buffer);
+
   protected:
     virtual void resizeEvent(QResizeEvent *event);
 
   protected slots:
     virtual void sceneHeightChanged(qreal height);
   protected:
     virtual void resizeEvent(QResizeEvent *event);
 
   protected slots:
     virtual void sceneHeightChanged(qreal height);
+    virtual void verticalScrollbarChanged(int);
+    virtual void sliderPressed();
+    virtual void sliderReleased();
 
   private:
     void init(MessageFilter *filter);
 
   private:
     void init(MessageFilter *filter);
@@ -57,4 +62,5 @@ class ChatView : public QGraphicsView, public AbstractChatView {
     ChatScene *_scene;
 };
 
     ChatScene *_scene;
 };
 
+
 #endif
 #endif