File indexing completed on 2024-05-12 05:36:51

0001 /*
0002  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "ColumnSortModel.h"
0008 
0009 #include <QDebug>
0010 
0011 ColumnSortModel::ColumnSortModel(QObject *parent)
0012     : QIdentityProxyModel(parent)
0013 {
0014 }
0015 
0016 QVariant ColumnSortModel::data(const QModelIndex &index, int role) const
0017 {
0018     return QIdentityProxyModel::data(mapToSource(index), role);
0019 }
0020 
0021 void ColumnSortModel::setSourceModel(QAbstractItemModel *newSourceModel)
0022 {
0023     m_rowMapping.clear();
0024     if (newSourceModel) {
0025         for (int i = 0; i < newSourceModel->rowCount(); ++i) {
0026             m_rowMapping.append(i);
0027         }
0028     }
0029 
0030     QIdentityProxyModel::setSourceModel(newSourceModel);
0031 }
0032 
0033 QModelIndex ColumnSortModel::mapFromSource(const QModelIndex &sourceIndex) const
0034 {
0035     if (!sourceIndex.isValid()) {
0036         return QModelIndex();
0037     }
0038 
0039     return createIndex(m_rowMapping.indexOf(sourceIndex.row()), sourceIndex.column());
0040 }
0041 
0042 QModelIndex ColumnSortModel::mapToSource(const QModelIndex &proxyIndex) const
0043 {
0044     if (!proxyIndex.isValid()) {
0045         return QModelIndex();
0046     }
0047 
0048     return sourceModel()->index(m_rowMapping.at(proxyIndex.row()), proxyIndex.column());
0049 }
0050 
0051 void ColumnSortModel::move(int fromRow, int toRow)
0052 {
0053     beginMoveRows(QModelIndex(), fromRow, fromRow, QModelIndex(), fromRow < toRow ? toRow + 1 : toRow);
0054     m_rowMapping.move(fromRow, toRow);
0055     endMoveRows();
0056     Q_EMIT sortedColumnsChanged();
0057 }
0058 
0059 QStringList ColumnSortModel::sortedColumns() const
0060 {
0061     QStringList result;
0062     auto source = sourceModel();
0063     for (auto sourceRow : std::as_const(m_rowMapping)) {
0064         result.append(source->data(source->index(sourceRow, 0), idRoleNumber()).toString());
0065     }
0066     return result;
0067 }
0068 
0069 void ColumnSortModel::setSortedColumns(const QStringList &newSortedColumns)
0070 {
0071     auto source = sourceModel();
0072     QHash<QString, int> sourceRowMapping;
0073     for (int i = 0; i < rowCount(); ++i) {
0074         auto id = source->data(source->index(i, 0), idRoleNumber()).toString();
0075         sourceRowMapping.insert(id, i);
0076     }
0077 
0078     beginResetModel();
0079     m_rowMapping.clear();
0080     for (const auto &id : newSortedColumns) {
0081         auto row = sourceRowMapping.value(id, -1);
0082         if (row != -1) {
0083             m_rowMapping.append(row);
0084         }
0085     }
0086 
0087     for (int i = 0; i < rowCount(); ++i) {
0088         if (!m_rowMapping.contains(i)) {
0089             m_rowMapping.append(i);
0090         }
0091     }
0092     endResetModel();
0093     Q_EMIT sortedColumnsChanged();
0094 }
0095 
0096 QString ColumnSortModel::idRole() const
0097 {
0098     return m_idRole;
0099 }
0100 
0101 void ColumnSortModel::setIdRole(const QString &newIdRole)
0102 {
0103     if (newIdRole == m_idRole) {
0104         return;
0105     }
0106 
0107     m_idRole = newIdRole;
0108     m_idRoleNumber = -1;
0109     Q_EMIT idRoleChanged();
0110 }
0111 
0112 int ColumnSortModel::idRoleNumber() const
0113 {
0114     if (m_idRoleNumber == -1 && !m_idRole.isEmpty() && sourceModel()) {
0115         m_idRoleNumber = sourceModel()->roleNames().key(m_idRole.toUtf8());
0116     }
0117 
0118     return m_idRoleNumber;
0119 }
0120 
0121 #include "moc_ColumnSortModel.cpp"