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

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 };
0014 
0015 KColumnHeadersModel::KColumnHeadersModel(QObject *parent)
0016     : QAbstractListModel(parent)
0017     , d(new KColumnHeadersModelPrivate)
0018 {
0019 }
0020 
0021 KColumnHeadersModel::~KColumnHeadersModel()
0022 {
0023 }
0024 
0025 int KColumnHeadersModel::rowCount(const QModelIndex &parent) const
0026 {
0027     if (!d->sourceModel || parent.isValid()) {
0028         return 0;
0029     }
0030 
0031     return d->sourceModel->columnCount();
0032 }
0033 
0034 QVariant KColumnHeadersModel::data(const QModelIndex &index, int role) const
0035 {
0036     if (!d->sourceModel || !index.isValid()) {
0037         return QVariant{};
0038     }
0039 
0040     return sourceModel()->headerData(index.row(), Qt::Horizontal, role);
0041 }
0042 
0043 QHash<int, QByteArray> KColumnHeadersModel::roleNames() const
0044 {
0045     if (!d->sourceModel) {
0046         return QHash<int, QByteArray>{};
0047     }
0048 
0049     return d->sourceModel->roleNames();
0050 }
0051 
0052 QAbstractItemModel *KColumnHeadersModel::sourceModel() const
0053 {
0054     return d->sourceModel;
0055 }
0056 
0057 void KColumnHeadersModel::setSourceModel(QAbstractItemModel *newSourceModel)
0058 {
0059     if (newSourceModel == d->sourceModel) {
0060         return;
0061     }
0062 
0063     if (d->sourceModel) {
0064         d->sourceModel->disconnect(this);
0065     }
0066 
0067     beginResetModel();
0068     d->sourceModel = newSourceModel;
0069     endResetModel();
0070 
0071     if (newSourceModel) {
0072         connect(newSourceModel, &QAbstractItemModel::columnsAboutToBeInserted, this, [this](const QModelIndex &, int first, int last) {
0073             beginInsertRows(QModelIndex{}, first, last);
0074         });
0075         connect(newSourceModel, &QAbstractItemModel::columnsInserted, this, [this]() {
0076             endInsertRows();
0077         });
0078         connect(newSourceModel,
0079                 &QAbstractItemModel::columnsAboutToBeMoved,
0080                 this,
0081                 [this](const QModelIndex &, int start, int end, const QModelIndex &, int destination) {
0082                     beginMoveRows(QModelIndex{}, start, end, QModelIndex{}, destination);
0083                 });
0084         connect(newSourceModel, &QAbstractItemModel::columnsMoved, this, [this]() {
0085             endMoveRows();
0086         });
0087         connect(newSourceModel, &QAbstractItemModel::columnsAboutToBeRemoved, this, [this](const QModelIndex &, int first, int last) {
0088             beginRemoveRows(QModelIndex{}, first, last);
0089         });
0090         connect(newSourceModel, &QAbstractItemModel::columnsRemoved, this, [this]() {
0091             endRemoveRows();
0092         });
0093         connect(newSourceModel, &QAbstractItemModel::headerDataChanged, this, [this](Qt::Orientation orientation, int first, int last) {
0094             if (orientation == Qt::Horizontal) {
0095                 Q_EMIT dataChanged(index(first, 0), index(last, 0));
0096             }
0097         });
0098         connect(newSourceModel, &QAbstractItemModel::layoutAboutToBeChanged, this, &QAbstractItemModel::layoutAboutToBeChanged);
0099         connect(newSourceModel, &QAbstractItemModel::layoutChanged, this, &QAbstractItemModel::layoutChanged);
0100         connect(newSourceModel, &QAbstractItemModel::modelAboutToBeReset, this, [this]() {
0101             beginResetModel();
0102         });
0103         connect(newSourceModel, &QAbstractItemModel::modelReset, this, [this]() {
0104             endResetModel();
0105         });
0106     }
0107 }
0108 
0109 #include "moc_kcolumnheadersmodel.cpp"