From 9e5f187f9eecdf3ae217361dfb01f56a6917ecd8 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Thu, 22 Jun 2023 12:54:12 +0530 Subject: [PATCH] Implemented dynamic row adding The view only adds as many rows as they can be displayed now. Only scrolling, it adds more --- src/contentmanagermodel.cpp | 20 +++++++++++++++++++- src/contentmanagermodel.h | 5 +++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index ddfa54f..06e5632 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -82,7 +82,7 @@ QModelIndex ContentManagerModel::parent(const QModelIndex &index) const int ContentManagerModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - return m_data.size(); + return zimCount; } QVariant ContentManagerModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -150,3 +150,21 @@ bool ContentManagerModel::hasChildren(const QModelIndex &parent) const return true; } +bool ContentManagerModel::canFetchMore(const QModelIndex &parent) const +{ + if (parent.isValid()) + return false; + return (zimCount < m_data.size()); +} + +void ContentManagerModel::fetchMore(const QModelIndex &parent) +{ + if (parent.isValid()) + return; + int remainder = m_data.size() - zimCount; + int zimsToFetch = qMin(5, remainder); + beginInsertRows(QModelIndex(), zimCount, zimCount + zimsToFetch - 1); + zimCount += zimsToFetch; + endInsertRows(); +} + diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h index 994f456..6d127b4 100644 --- a/src/contentmanagermodel.h +++ b/src/contentmanagermodel.h @@ -28,9 +28,14 @@ public: void setupNodes(); bool hasChildren(const QModelIndex &parent) const override; +protected: + bool canFetchMore(const QModelIndex &parent) const override; + void fetchMore(const QModelIndex &parent) override; + private: QList> m_data; Node *rootNode; + int zimCount = 0; }; #endif // CONTENTMANAGERMODEL_H