File indexing completed on 2024-05-12 05:35:46

0001 /*
0002     SPDX-FileCopyrightText: 2015 Klarälvdalens Datakonsult AB a KDAB Group company <info@kdab.com>
0003     SPDX-FileCopyrightText: 2015 David Faure <david.faure@kdab.com>
0004     SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0007 */
0008 
0009 #pragma once
0010 
0011 #include <QAbstractItemModel>
0012 #include <memory>
0013 
0014 /*
0015  * This class is based on KConcatenateRowsProxyModel adapted to handle trees with two levels.
0016  */
0017 
0018 class ShortcutsModelPrivate;
0019 
0020 class ShortcutsModel : public QAbstractItemModel
0021 {
0022     Q_OBJECT
0023 
0024 public:
0025     /**
0026      * Creates a ShortcutsModel.
0027      * @param parent optional parent
0028      */
0029     explicit ShortcutsModel(QObject *parent = nullptr);
0030     /**
0031      * Destructor.
0032      */
0033     ~ShortcutsModel() override;
0034 
0035     /**
0036      * Adds a source model @p sourceModel, after all existing source models.
0037      * @param sourceModel the source model
0038      *
0039      * The ownership of @p sourceModel is not affected by this.
0040      * The same source model cannot be added more than once.
0041      */
0042     Q_SCRIPTABLE void addSourceModel(QAbstractItemModel *sourceModel);
0043 
0044     /**
0045      * Removes the source model @p sourceModel.
0046      * @param sourceModel a source model previously added to this proxy
0047      *
0048      * The ownership of @sourceModel is not affected by this.
0049      */
0050     Q_SCRIPTABLE void removeSourceModel(QAbstractItemModel *sourceModel);
0051 
0052     /**
0053      * The currently set source models
0054      */
0055     QList<QAbstractItemModel *> sources() const;
0056 
0057     /**
0058      * Returns the proxy index for a given source index
0059      * @param sourceIndex an index coming from any of the source models
0060      * @return a proxy index
0061      * Calling this method with an index not from a source model is undefined behavior.
0062      */
0063     QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
0064 
0065     /**
0066      * Returns the source index for a given proxy index.
0067      * @param proxyIndex an index for this proxy model
0068      * @return a source index
0069      */
0070     Q_INVOKABLE QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
0071 
0072     /// @reimp
0073     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0074     /// @reimp
0075     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
0076     /// @reimp
0077     QMap<int, QVariant> itemData(const QModelIndex &proxyIndex) const override;
0078     /// @reimp
0079     Qt::ItemFlags flags(const QModelIndex &index) const override;
0080     /// @reimp
0081     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0082     /// @reimp
0083     QModelIndex parent(const QModelIndex &index) const override;
0084     /// @reimp
0085     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0086 
0087     /**
0088      * The horizontal header data for the first source model is returned here.
0089      * @reimp
0090      */
0091     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0092     /**
0093      * The column count for the first source model is returned here.
0094      * @reimp
0095      */
0096     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0097 
0098     /**
0099      * The roles names for the first source model is returned here
0100      * @reimp
0101      */
0102     QHash<int, QByteArray> roleNames() const override;
0103 
0104 private:
0105     Q_PRIVATE_SLOT(d, void slotRowsAboutToBeInserted(const QModelIndex &, int start, int end))
0106     Q_PRIVATE_SLOT(d, void slotRowsInserted(const QModelIndex &, int start, int end))
0107     Q_PRIVATE_SLOT(d, void slotRowsAboutToBeRemoved(const QModelIndex &, int start, int end))
0108     Q_PRIVATE_SLOT(d, void slotRowsRemoved(const QModelIndex &, int start, int end))
0109     Q_PRIVATE_SLOT(d, void slotColumnsAboutToBeInserted(const QModelIndex &parent, int start, int end))
0110     Q_PRIVATE_SLOT(d, void slotColumnsInserted(const QModelIndex &parent, int, int))
0111     Q_PRIVATE_SLOT(d, void slotColumnsAboutToBeRemoved(const QModelIndex &parent, int start, int end))
0112     Q_PRIVATE_SLOT(d, void slotColumnsRemoved(const QModelIndex &parent, int, int))
0113     Q_PRIVATE_SLOT(d, void slotDataChanged(const QModelIndex &from, const QModelIndex &to, const QList<int> &roles))
0114     Q_PRIVATE_SLOT(d, void slotSourceLayoutAboutToBeChanged(QList<QPersistentModelIndex>, QAbstractItemModel::LayoutChangeHint))
0115     Q_PRIVATE_SLOT(d, void slotSourceLayoutChanged(const QList<QPersistentModelIndex> &, QAbstractItemModel::LayoutChangeHint))
0116     Q_PRIVATE_SLOT(d, void slotModelAboutToBeReset())
0117     Q_PRIVATE_SLOT(d, void slotModelReset())
0118 
0119 private:
0120     friend class ShortcutsModelPrivate;
0121     const std::unique_ptr<ShortcutsModelPrivate> d;
0122 };