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 #include "kcolumnheadersmodel.h"
0008 
0009 class KColumnHeadersModelPrivate
0010 {
0011 public:
0012     QAbstractItemModel *sourceModel = nullptr;
0013     int sortColumn = -1;
0014     Qt::SortOrder sortOrder = Qt::AscendingOrder;
0015 };
0016 
0017 KColumnHeadersModel::KColumnHeadersModel(QObject *parent)
0018     : QAbstractListModel(parent)
0019     , d(new KColumnHeadersModelPrivate)
0020 {
0021 }
0022 
0023 KColumnHeadersModel::~KColumnHeadersModel()
0024 {
0025 }
0026 
0027 int KColumnHeadersModel::rowCount(const QModelIndex &parent) const
0028 {
0029     if (!d->sourceModel || parent.isValid()) {
0030         return 0;
0031     }
0032 
0033     return d->sourceModel->columnCount();
0034 }
0035 
0036 QVariant KColumnHeadersModel::data(const QModelIndex &index, int role) const
0037 {
0038     if (!d->sourceModel || !index.isValid()) {
0039         return QVariant{};
0040     }
0041 
0042     if (role == SortRole) {
0043         if (index.row() == d->sortColumn) {
0044             return d->sortOrder;
0045         } else {
0046             return QVariant{};
0047         }
0048     }
0049 
0050     return sourceModel()->headerData(index.row(), Qt::Horizontal, role);
0051 }
0052 
0053 QHash<int, QByteArray> KColumnHeadersModel::roleNames() const
0054 {
0055     if (!d->sourceModel) {
0056         return QHash<int, QByteArray>{};
0057     }
0058 
0059     auto names = d->sourceModel->roleNames();
0060     names.insert(SortRole, "sort");
0061     return names;
0062 }
0063 
0064 QAbstractItemModel *KColumnHeadersModel::sourceModel() const
0065 {
0066     return d->sourceModel;
0067 }
0068 
0069 void KColumnHeadersModel::setSourceModel(QAbstractItemModel *newSourceModel)
0070 {
0071     if (newSourceModel == d->sourceModel) {
0072         return;
0073     }
0074 
0075     if (d->sourceModel) {
0076         d->sourceModel->disconnect(this);
0077     }
0078 
0079     beginResetModel();
0080     d->sourceModel = newSourceModel;
0081     endResetModel();
0082 
0083     if (newSourceModel) {
0084         connect(newSourceModel, &QAbstractItemModel::columnsAboutToBeInserted, this, [this](const QModelIndex &, int first, int last) {
0085             beginInsertRows(QModelIndex{}, first, last);
0086         });
0087         connect(newSourceModel, &QAbstractItemModel::columnsInserted, this, [this]() {
0088             endInsertRows();
0089         });
0090         connect(newSourceModel,
0091                 &QAbstractItemModel::columnsAboutToBeMoved,
0092                 this,
0093                 [this](const QModelIndex &, int start, int end, const QModelIndex &, int destination) {
0094                     beginMoveRows(QModelIndex{}, start, end, QModelIndex{}, destination);
0095                 });
0096         connect(newSourceModel, &QAbstractItemModel::columnsMoved, this, [this]() {
0097             endMoveRows();
0098         });
0099         connect(newSourceModel, &QAbstractItemModel::columnsAboutToBeRemoved, this, [this](const QModelIndex &, int first, int last) {
0100             beginRemoveRows(QModelIndex{}, first, last);
0101         });
0102         connect(newSourceModel, &QAbstractItemModel::columnsRemoved, this, [this]() {
0103             endRemoveRows();
0104         });
0105         connect(newSourceModel, &QAbstractItemModel::headerDataChanged, this, [this](Qt::Orientation orientation, int first, int last) {
0106             if (orientation == Qt::Horizontal) {
0107                 Q_EMIT dataChanged(index(first, 0), index(last, 0));
0108             }
0109         });
0110         connect(newSourceModel, &QAbstractItemModel::modelAboutToBeReset, this, [this]() {
0111             beginResetModel();
0112         });
0113         connect(newSourceModel, &QAbstractItemModel::modelReset, this, [this]() {
0114             endResetModel();
0115         });
0116     }
0117 }
0118 
0119 int KColumnHeadersModel::sortColumn() const
0120 {
0121     return d->sortColumn;
0122 }
0123 
0124 void KColumnHeadersModel::setSortColumn(int newSortColumn)
0125 {
0126     if (newSortColumn == d->sortColumn) {
0127         return;
0128     }
0129 
0130     auto previousSortColumn = d->sortColumn;
0131 
0132     d->sortColumn = newSortColumn;
0133 
0134     if (previousSortColumn >= 0) {
0135         Q_EMIT dataChanged(index(previousSortColumn), index(previousSortColumn), {SortRole});
0136     }
0137 
0138     if (newSortColumn >= 0) {
0139         Q_EMIT dataChanged(index(newSortColumn), index(newSortColumn), {SortRole});
0140     }
0141 
0142     Q_EMIT sortColumnChanged();
0143 }
0144 
0145 Qt::SortOrder KColumnHeadersModel::sortOrder() const
0146 {
0147     return d->sortOrder;
0148 }
0149 
0150 void KColumnHeadersModel::setSortOrder(Qt::SortOrder newSortOrder)
0151 {
0152     if (newSortOrder == d->sortOrder) {
0153         return;
0154     }
0155 
0156     d->sortOrder = newSortOrder;
0157 
0158     if (d->sortColumn >= 0) {
0159         Q_EMIT dataChanged(index(d->sortColumn), index(d->sortColumn), {SortRole});
0160     }
0161 
0162     Q_EMIT sortOrderChanged();
0163 }
0164 
0165 #include "moc_kcolumnheadersmodel.cpp"