File indexing completed on 2024-05-12 17:07:45

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