From dbefd590650e9053c7a1513a5f49aad3e582108a Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Mon, 11 Aug 2008 00:32:15 +0200 Subject: [PATCH] Bring back dynamic backlog fetching (move scrollbar slider to the very top to get more lines) --- src/qtui/bufferwidget.cpp | 5 +++-- src/qtui/chatscene.cpp | 32 +++++++++++++++++++++++++++++++- src/qtui/chatscene.h | 33 ++++++++++++++++++++++++++++++++- src/qtui/chatview.cpp | 39 ++++++++++++++++++++++++++++++++++++--- src/qtui/chatview.h | 6 ++++++ 5 files changed, 108 insertions(+), 7 deletions(-) diff --git a/src/qtui/bufferwidget.cpp b/src/qtui/bufferwidget.cpp index d6dcfa25..c1e992dc 100644 --- a/src/qtui/bufferwidget.cpp +++ b/src/qtui/bufferwidget.cpp @@ -34,12 +34,13 @@ BufferWidget::~BufferWidget() { } 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); - return dynamic_cast(chatView); + chatView->setBufferForBacklogFetching(id); + return chatView; } void BufferWidget::removeChatView(BufferId id) { diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 43ff2480..a6578fa6 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -28,6 +28,8 @@ #include "chatline.h" #include "chatlinemodelitem.h" #include "chatscene.h" +#include "client.h" +#include "clientbacklogmanager.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; + _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))); @@ -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); } - + 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); } + + requestBacklogIfNeeded(); } void ChatScene::modelReset() { @@ -275,3 +281,27 @@ QString ChatScene::selectionToString() const { } 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(); + if(!_lastBacklogOffset.isValid() || (msgId < _lastBacklogOffset && _lastBacklogSize + REQUEST_COUNT <= model()->rowCount())) { + Client::backlogManager()->requestBacklog(bufferForBacklogFetching(), REQUEST_COUNT, msgId.toInt()); + _lastBacklogOffset = msgId; + _lastBacklogSize = model()->rowCount(); + } +} diff --git a/src/qtui/chatscene.h b/src/qtui/chatscene.h index 22dcb479..e72308e4 100644 --- a/src/qtui/chatscene.h +++ b/src/qtui/chatscene.h @@ -25,8 +25,11 @@ #include #include +#include "types.h" + class AbstractUiMsg; class Buffer; +class BufferId; class ChatItem; class ChatLine; class ColumnHandleItem; @@ -40,10 +43,13 @@ class ChatScene : public QGraphicsScene { 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 bool isFetchingBacklog() const; + inline bool isBacklogFetchingEnabled() const; + inline BufferId bufferForBacklogFetching() const; + 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); + void setIsFetchingBacklog(bool); + inline void setBufferForBacklogFetching(BufferId buffer); + signals: void heightChanged(qreal height); @@ -71,6 +80,7 @@ class ChatScene : public QGraphicsScene { private: void updateSelection(const QPointF &pos); QString selectionToString() const; + void requestBacklogIfNeeded(); QString _idString; qreal _width, _height; @@ -86,6 +96,27 @@ class ChatScene : public QGraphicsScene { 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 diff --git a/src/qtui/chatview.cpp b/src/qtui/chatview.cpp index be2cbc62..6d4e5ea2 100644 --- a/src/qtui/chatview.cpp +++ b/src/qtui/chatview.cpp @@ -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); + + 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() { } - 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()); @@ -72,6 +79,32 @@ void ChatView::sceneHeightChanged(qreal h) { 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()); + } } diff --git a/src/qtui/chatview.h b/src/qtui/chatview.h index 7d84e6ad..b9c1892a 100644 --- a/src/qtui/chatview.h +++ b/src/qtui/chatview.h @@ -45,11 +45,16 @@ class ChatView : public QGraphicsView, public AbstractChatView { void clear(); + void setBufferForBacklogFetching(BufferId buffer); + 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); @@ -57,4 +62,5 @@ class ChatView : public QGraphicsView, public AbstractChatView { ChatScene *_scene; }; + #endif -- 2.20.1