File indexing completed on 2024-07-21 06:38:21

0001 /*
0002     SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KCOLUMNHEADERSMODEL_H
0008 #define KCOLUMNHEADERSMODEL_H
0009 
0010 #include "kitemmodels_export.h"
0011 
0012 #include <QAbstractListModel>
0013 
0014 #include <memory>
0015 
0016 class KColumnHeadersModelPrivate;
0017 
0018 /**
0019  * A model that converts a model's headers into a list model.
0020  *
0021  * This model will expose the source model's headers as a simple list. This is
0022  * mostly useful as a helper for QML applications that want to display a model's
0023  * headers.
0024  *
0025  * Each columns's header will be presented as a row in this model. Roles are
0026  * forwarded directly to the source model's headerData() method.
0027  *
0028  * @since 5.66
0029  */
0030 class KITEMMODELS_EXPORT KColumnHeadersModel : public QAbstractListModel
0031 {
0032     Q_OBJECT
0033     Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE setSourceModel NOTIFY sourceModelChanged)
0034     Q_PROPERTY(int sortColumn READ sortColumn WRITE setSortColumn NOTIFY sortColumnChanged)
0035     Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder NOTIFY sortOrderChanged)
0036 
0037 public:
0038     enum ExtraRoles { SortRole = 0x011D910E };
0039     Q_ENUM(ExtraRoles)
0040 
0041     explicit KColumnHeadersModel(QObject *parent = nullptr);
0042     ~KColumnHeadersModel() override;
0043 
0044     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0045     QVariant data(const QModelIndex &index, int role) const override;
0046     QHash<int, QByteArray> roleNames() const override;
0047 
0048     QAbstractItemModel *sourceModel() const;
0049     void setSourceModel(QAbstractItemModel *newSourceModel);
0050 
0051     int sortColumn() const;
0052     void setSortColumn(int newSortColumn);
0053 
0054     Qt::SortOrder sortOrder() const;
0055     void setSortOrder(Qt::SortOrder newSortOrder);
0056 
0057 Q_SIGNALS:
0058     void sourceModelChanged();
0059     void sortColumnChanged();
0060     void sortOrderChanged();
0061 
0062 private:
0063     void onLayoutAboutToBeChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint);
0064     void onLayoutChanged(const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint);
0065 
0066     const std::unique_ptr<KColumnHeadersModelPrivate> d;
0067 };
0068 
0069 #endif