File indexing completed on 2024-05-05 05:29:12

0001 /*
0002  *   SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractListModel>
0010 
0011 /**
0012  * @class PaginateModel
0013  *
0014  * This class can be used to create representations of only a chunk of a model.
0015  *
0016  * With this component it will be possible to create views that only show a page
0017  * of a model, instead of drawing all the elements in the model.
0018  */
0019 class PaginateModel : public QAbstractListModel
0020 {
0021     Q_OBJECT
0022     /** Holds the number of elements that will fit in a page */
0023     Q_PROPERTY(int pageSize READ pageSize WRITE setPageSize NOTIFY pageSizeChanged)
0024 
0025     /** Tells what is the first row shown in the model */
0026     Q_PROPERTY(int firstItem READ firstItem WRITE setFirstItem NOTIFY firstItemChanged)
0027 
0028     /** The model we will be proxying */
0029     Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE setSourceModel NOTIFY sourceModelChanged)
0030 
0031     /** Among the totality of elements, indicates the one we're currently offering */
0032     Q_PROPERTY(int currentPage READ currentPage NOTIFY firstItemChanged)
0033 
0034     /** Provides the number of pages available, given the sourceModel size */
0035     Q_PROPERTY(int pageCount READ pageCount NOTIFY pageCountChanged)
0036 
0037     /** If enabled, ensures that pageCount and pageSize are the same. */
0038     Q_PROPERTY(bool staticRowCount READ hasStaticRowCount WRITE setStaticRowCount NOTIFY staticRowCountChanged)
0039 
0040 public:
0041     explicit PaginateModel(QObject *object = nullptr);
0042     ~PaginateModel() override;
0043 
0044     int pageSize() const;
0045     void setPageSize(int count);
0046 
0047     int firstItem() const;
0048     void setFirstItem(int row);
0049 
0050     /**
0051      * @returns Last visible item.
0052      *
0053      * Convenience function
0054      */
0055     int lastItem() const;
0056 
0057     QAbstractItemModel *sourceModel() const;
0058     void setSourceModel(QAbstractItemModel *model);
0059 
0060     QModelIndex mapToSource(const QModelIndex &idx) const;
0061     QModelIndex mapFromSource(const QModelIndex &idx) const;
0062     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0063     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0064 
0065     int currentPage() const;
0066     int pageCount() const;
0067     QHash<int, QByteArray> roleNames() const override;
0068 
0069     void setStaticRowCount(bool src);
0070     bool hasStaticRowCount() const;
0071 
0072     /** Display the first rows of the model */
0073     Q_SCRIPTABLE void firstPage();
0074 
0075     /** Display the rows right after the ones that are currently being served */
0076     Q_SCRIPTABLE void nextPage();
0077 
0078     /** Display the rows right before the ones that are currently being served */
0079     Q_SCRIPTABLE void previousPage();
0080 
0081     /** Display the last set of rows of the source model */
0082     Q_SCRIPTABLE void lastPage();
0083 
0084 private Q_SLOTS:
0085     void _k_sourceRowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
0086     void _k_sourceRowsInserted(const QModelIndex &parent, int start, int end);
0087     void _k_sourceRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
0088     void _k_sourceRowsRemoved(const QModelIndex &parent, int start, int end);
0089     void _k_sourceRowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
0090     void _k_sourceRowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
0091 
0092     void _k_sourceColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end);
0093     void _k_sourceColumnsInserted(const QModelIndex &parent, int start, int end);
0094     void _k_sourceColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
0095     void _k_sourceColumnsRemoved(const QModelIndex &parent, int start, int end);
0096     void _k_sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
0097     void _k_sourceColumnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
0098 
0099     void _k_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
0100     void _k_sourceHeaderDataChanged(Qt::Orientation orientation, int first, int last);
0101 
0102     void _k_sourceModelAboutToBeReset();
0103     void _k_sourceModelReset();
0104 
0105 Q_SIGNALS:
0106     void pageSizeChanged();
0107     void firstItemChanged();
0108     void sourceModelChanged();
0109     void pageCountChanged();
0110     void staticRowCountChanged();
0111 
0112 private:
0113     bool canSizeChange() const;
0114     bool isIntervalValid(const QModelIndex &parent, int start, int end) const;
0115     int rowsByPageSize(int size) const;
0116 
0117     class PaginateModelPrivate;
0118     QScopedPointer<PaginateModelPrivate> d;
0119 };