From f9ee60eeb807b94393ec7b48c059af2af107bff1 Mon Sep 17 00:00:00 2001 From: Digital Artifex <7929434+DigitalArtifex@users.noreply.github.com> Date: Mon, 8 Jun 2026 21:52:09 -0400 Subject: [PATCH] Added reset method and updated comments --- KomplexHubPlugin/common/paginator.h | 131 ++++++++++++++++++++++++---- 1 file changed, 112 insertions(+), 19 deletions(-) diff --git a/KomplexHubPlugin/common/paginator.h b/KomplexHubPlugin/common/paginator.h index d946cfb..6fc58de 100644 --- a/KomplexHubPlugin/common/paginator.h +++ b/KomplexHubPlugin/common/paginator.h @@ -20,19 +20,23 @@ #define PAGINATOR_H #include +#include +#include #include "komplex_global.h" #include "slidingcachecontroller.h" /** - * @brief The PaginatorNotifier class + * @brief The PaginationNotifier class * * Qt does not support templated QObjects. Pushing it to a notifier class * that the template can inherit fixes this */ -class KOMPLEX_EXPORT PaginatorNotifier : public QObject +class KOMPLEX_EXPORT PaginationNotifier : public QObject { Q_OBJECT +protected: + explicit PaginationNotifier(QObject *parent = nullptr) : QObject(parent) {} signals: auto dataChanged() -> void; auto resultsPerPageChanged() -> void; @@ -47,9 +51,14 @@ signals: * the SlidingCacheController as it's backend */ template -class KOMPLEX_EXPORT Paginator : public PaginatorNotifier +class KOMPLEX_EXPORT Paginator : public PaginationNotifier { public: + explicit Paginator(QObject *parent = nullptr) : PaginationNotifier(parent) + { + + } + /** * @brief next * @@ -58,7 +67,6 @@ public: auto next() const -> void { setOffset(m_offset + m_resultsPerPage); - Q_EMIT static_cast(this)->pageChanged(); } /** @@ -69,7 +77,6 @@ public: auto previous() const -> void { setOffset(m_offset - m_resultsPerPage); - Q_EMIT static_cast(this)->pageChanged(); } /** @@ -87,16 +94,23 @@ public: */ auto setResultsPerPage(qsizetype resultsPerPage) -> void { + QMutexLocker dataLocker(&m_dataMutex); + + if(resultsPerPage == m_resultsPerPage) + { + return; + } + m_resultsPerPage = resultsPerPage; - Q_EMIT static_cast(this)->resultsPerPageChanged(); + Q_EMIT static_cast(this)->resultsPerPageChanged(); m_data = m_cacheController.chunk(m_offset, m_resultsPerPage); - Q_EMIT static_cast(this)->dataChanged(); + Q_EMIT static_cast(this)->dataChanged(); } /** * @brief data - * @return a copy of the list of pointers + * @return */ auto data() const -> QList { @@ -117,8 +131,10 @@ public: * @param index * @return */ - auto at(qsizetype index) const -> T + auto at(qsizetype index) -> T { + QMutexLocker dataLocker(&m_dataMutex); + if(index < 0 || index >= m_data.count()) { return {}; @@ -140,6 +156,7 @@ public: /** * @brief page + * Calculates the current page * @return */ auto page() const -> qsizetype @@ -151,6 +168,7 @@ public: /** * @brief totalPages + * Calculates the total number of pages available * @return */ auto totalPages() const -> qsizetype @@ -162,18 +180,33 @@ public: protected: + /** + * @brief controller + * Reference to the underlying cache controller + * @return + */ auto controller() -> SlidingCacheController* { return &m_cacheController; } + /** + * @brief setOffset + * Sets the dataset to the specified offset + * @param offset + */ auto setOffset(qsizetype offset) -> void { + if(offset == m_offset) + { + return; + } + m_offset = offset; - if(m_offset >= m_totalResults) + if(m_offset >= totalResults()) { - m_offset = m_totalResults - m_resultsPerPage; + m_offset = totalResults() - m_resultsPerPage; } if(m_offset < 0) @@ -181,28 +214,88 @@ protected: m_offset = 0; } - m_data = m_cacheController.chunk(m_offset, m_resultsPerPage); - Q_EMIT this->countChanged(); - Q_EMIT this->dataChanged(); + QList data = m_cacheController.chunk(m_offset, m_resultsPerPage); + setData(data); + Q_EMIT this->pageChanged(); - Q_EMIT this->totalPagesChanged(); } + /** + * @brief setData + * @param data + */ + auto setData(const QList &data) + { + m_dataMutex.lock(); + + m_data.clear(); + m_data.append(std::move(data)); + m_dataMutex.unlock(); + + Q_EMIT this->countChanged(); + Q_EMIT this->dataChanged(); + } + + /** + * @brief onControllerTotalResultsChanged + * Slot used to forward totalResultsChanged signal + */ auto onControllerTotalResultsChanged() -> void { Q_EMIT this->totalResultsChanged(); } + /** + * @brief reset + * Resets the paginator and underlying controller + */ + auto reset() -> void + { + m_dataMutex.lock(); + + m_offset = std::numeric_limits::infinity(); + m_data.clear(); + controller()->reset(); + m_dataMutex.unlock(); + + Q_EMIT totalPagesChanged(); + Q_EMIT dataChanged(); + Q_EMIT pageChanged(); + } + private: - qsizetype m_page = 0; - qsizetype m_totalPages = 0; + /** + * @brief m_offset + * Current absolute offset. Used in fetch operations + */ + qsizetype m_offset = std::numeric_limits::infinity(); - qsizetype m_offset = 0; + /** + * @brief m_resultsPerPage + * Results per page to use when calculating current and total pages. + * Needs to be set by the view + */ qsizetype m_resultsPerPage = 0; - qsizetype m_totalResults = 0; + /** + * @brief m_cacheController + * Internal cache controller. + * + * TODO: make the cache controller generic so it can be used in other projects + * and with other cache methods + */ SlidingCacheController m_cacheController; + + /** + * @brief m_data + * Internal data copy + */ QList m_data; + + /** + * @brief m_dataMutex + */ + QMutex m_dataMutex; }; #endif // PAGINATOR_H