From 49239dad1f77d967cdda80e01fe2ebb2f18037c4 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 11 Apr 2013 17:45:14 +0200 Subject: [PATCH] ExtendedTableWidget: Make scrolling less awkward with lazy population Improve the lazy population fatchData() calls when using the vertical scrollbar to make scrolling a lot smoother. Still not perfect but definitely a lot better than before. --- src/ExtendedTableWidget.cpp | 35 +++++++++++++++++++++++++++++++++++ src/ExtendedTableWidget.h | 4 ++++ 2 files changed, 39 insertions(+) diff --git a/src/ExtendedTableWidget.cpp b/src/ExtendedTableWidget.cpp index cb0f64f2..29426793 100644 --- a/src/ExtendedTableWidget.cpp +++ b/src/ExtendedTableWidget.cpp @@ -3,10 +3,14 @@ #include #include #include "ExtendedTableWidget.h" +#include "sqlitetablemodel.h" +#include +#include ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) : QTableView(parent) { + connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(vscrollbarChanged(int))); } void ExtendedTableWidget::copy() @@ -53,3 +57,34 @@ void ExtendedTableWidget::keyPressEvent(QKeyEvent* event) else QTableView::keyPressEvent(event); } + +void ExtendedTableWidget::updateGeometries() +{ + // Call the parent implementation first - it does most of the actual logic + QTableView::updateGeometries(); + + // Check if a model has already been set yet + if(model()) + { + // If so and if it is a SqliteTableModel and if the parent implementation of this method decided that a scrollbar is needed, update its maximum value + SqliteTableModel* m = qobject_cast(model()); + if(m && verticalScrollBar()->maximum()) + verticalScrollBar()->setMaximum(m->totalRowCount()); + } +} + +void ExtendedTableWidget::vscrollbarChanged(int value) +{ + // Cancel if there is no model set yet - this shouldn't happen (because without a model there should be no scrollbar) but just to be sure... + if(!model()) + return; + + // How many rows are visible right now? + int row_top = rowAt(0) == -1 ? 0 : rowAt(0); + int row_bottom = rowAt(height()) == -1 ? model()->rowCount() : rowAt(height()); + int num_visible_rows = row_bottom - row_top; + + // Fetch more data from the DB if necessary + if((value + num_visible_rows) >= model()->rowCount() && model()->canFetchMore(QModelIndex())) + model()->fetchMore(QModelIndex()); +} diff --git a/src/ExtendedTableWidget.h b/src/ExtendedTableWidget.h index 7e8adbbf..8cec83d8 100644 --- a/src/ExtendedTableWidget.h +++ b/src/ExtendedTableWidget.h @@ -13,8 +13,12 @@ public: private: void copy(); +private slots: + void vscrollbarChanged(int value); + protected: virtual void keyPressEvent(QKeyEvent* event); + virtual void updateGeometries(); }; #endif