File indexing completed on 2024-05-12 15:42:57

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
0003     SPDX-FileContributor: David Faure <david.faure@kdab.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KCONCATENATEROWSPROXYMODEL_H
0009 #define KCONCATENATEROWSPROXYMODEL_H
0010 
0011 #include "kitemmodels_export.h"
0012 #include <QAbstractItemModel>
0013 
0014 #include <memory>
0015 
0016 #if KITEMMODELS_ENABLE_DEPRECATED_SINCE(5, 80)
0017 
0018 class KConcatenateRowsProxyModelPrivate;
0019 
0020 /**
0021  * @class KConcatenateRowsProxyModel kconcatenaterowsproxymodel.h KConcatenateRowsProxyModel
0022  *
0023  * This proxy takes multiple source models and concatenates their rows.
0024  *
0025  * In other words, the proxy will have all rows of the first source model,
0026  * followed by all rows of the second source model, etc.
0027  *
0028  * Only flat models (lists and tables) are supported, no trees.
0029  *
0030  * All models are assumed to have the same number of columns.
0031  * More precisely, the number of columns of the first source model is used,
0032  * so all source models should have at least as many columns as the first source model,
0033  * and additional columns in other source models will simply be ignored.
0034  *
0035  * Source models can be added and removed at runtime, including the first source
0036  * model (but it should keep the same number of columns).
0037  *
0038  * Dynamic insertion and removal of rows and columns in any source model is supported.
0039  * dataChanged, layoutChanged and reset coming from the source models are supported.
0040  *
0041  * At the moment this model doesn't support editing, drag-n-drop.
0042  * It could be added though, nothing prevents it.
0043  *
0044  * This proxy does not inherit from QAbstractProxyModel because it uses multiple source
0045  * models, rather than a single one.
0046  *
0047  * Author: David Faure, KDAB
0048  * @since 5.14
0049  *
0050  * @deprecated Since 5.80 use QConcatenateTablesProxyModel instead, which is
0051  * part of Qt >= 5.13.
0052  */
0053 class KITEMMODELS_EXPORT KConcatenateRowsProxyModel : public QAbstractItemModel
0054 {
0055     Q_OBJECT
0056 
0057 public:
0058     /**
0059      * Creates a KConcatenateRowsProxyModel.
0060      * @param parent optional parent
0061      */
0062     KITEMMODELS_DEPRECATED_VERSION(5, 80, "Use QConcatenateTablesProxyModel")
0063     explicit KConcatenateRowsProxyModel(QObject *parent = nullptr);
0064     /**
0065      * Destructor.
0066      */
0067     ~KConcatenateRowsProxyModel() override;
0068 
0069     /**
0070      * Adds a source model @p sourceModel, after all existing source models.
0071      * @param sourceModel the source model
0072      *
0073      * The ownership of @p sourceModel is not affected by this.
0074      * The same source model cannot be added more than once.
0075      */
0076     Q_SCRIPTABLE void addSourceModel(QAbstractItemModel *sourceModel);
0077 
0078     /**
0079      * Removes the source model @p sourceModel.
0080      * @param sourceModel a source model previously added to this proxy
0081      *
0082      * The ownership of @sourceModel is not affected by this.
0083      */
0084     Q_SCRIPTABLE void removeSourceModel(QAbstractItemModel *sourceModel);
0085 
0086     /**
0087      * The currently set source models
0088      */
0089     QList<QAbstractItemModel *> sources() const;
0090 
0091     /**
0092      * Returns the proxy index for a given source index
0093      * @param sourceIndex an index coming from any of the source models
0094      * @return a proxy index
0095      * Calling this method with an index not from a source model is undefined behavior.
0096      */
0097     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
0098 
0099     /**
0100      * Returns the source index for a given proxy index.
0101      * @param proxyIndex an index for this proxy model
0102      * @return a source index
0103      */
0104     QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
0105 
0106     /// @reimp
0107     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0108     /// @reimp
0109     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
0110     /// @reimp
0111     QMap<int, QVariant> itemData(const QModelIndex &proxyIndex) const override;
0112     /// @reimp
0113     Qt::ItemFlags flags(const QModelIndex &index) const override;
0114     /// @reimp
0115     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0116     /// @reimp
0117     QModelIndex parent(const QModelIndex &index) const override;
0118     /// @reimp
0119     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0120 
0121     /**
0122      * The horizontal header data for the first source model is returned here.
0123      * @reimp
0124      */
0125     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0126     /**
0127      * The column count for the first source model is returned here.
0128      * @reimp
0129      */
0130     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0131 
0132     /**
0133      * The roles names for the first source model is returned here
0134      * @reimp
0135      */
0136     QHash<int, QByteArray> roleNames() const override;
0137 
0138 private:
0139     Q_PRIVATE_SLOT(d, void slotRowsAboutToBeInserted(const QModelIndex &, int start, int end))
0140     Q_PRIVATE_SLOT(d, void slotRowsInserted(const QModelIndex &, int start, int end))
0141     Q_PRIVATE_SLOT(d, void slotRowsAboutToBeRemoved(const QModelIndex &, int start, int end))
0142     Q_PRIVATE_SLOT(d, void slotRowsRemoved(const QModelIndex &, int start, int end))
0143     Q_PRIVATE_SLOT(d, void slotColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end))
0144     Q_PRIVATE_SLOT(d, void slotColumnsInserted(const QModelIndex &parent, int, int))
0145     Q_PRIVATE_SLOT(d, void slotColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end))
0146     Q_PRIVATE_SLOT(d, void slotColumnsRemoved(const QModelIndex &parent, int, int))
0147     Q_PRIVATE_SLOT(d, void slotDataChanged(const QModelIndex &from, const QModelIndex &to, const QVector<int> &roles))
0148     Q_PRIVATE_SLOT(d, void slotSourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint))
0149     Q_PRIVATE_SLOT(d, void slotSourceLayoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint))
0150     Q_PRIVATE_SLOT(d, void slotModelAboutToBeReset())
0151     Q_PRIVATE_SLOT(d, void slotModelReset())
0152 
0153 private:
0154     friend class KConcatenateRowsProxyModelPrivate;
0155     std::unique_ptr<KConcatenateRowsProxyModelPrivate> const d;
0156 };
0157 
0158 #endif
0159 
0160 #endif