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 "ColumnDisplayModel.h"
0008 
0009 #include <QDebug>
0010 
0011 ColumnDisplayModel::ColumnDisplayModel(QObject *parent)
0012     : QIdentityProxyModel(parent)
0013 {
0014 }
0015 
0016 void ColumnDisplayModel::setSourceModel(QAbstractItemModel *newSourceModel)
0017 {
0018     m_idRoleNumber = -1;
0019     QIdentityProxyModel::setSourceModel(newSourceModel);
0020 }
0021 
0022 QHash<int, QByteArray> ColumnDisplayModel::roleNames() const
0023 {
0024     auto roles = QIdentityProxyModel::roleNames();
0025     roles.insert(DisplayStyleRole, "displayStyle");
0026     return roles;
0027 }
0028 
0029 QVariant ColumnDisplayModel::data(const QModelIndex &index, int role) const
0030 {
0031     if (role == DisplayStyleRole && sourceModel()) {
0032         auto id = sourceModel()->data(mapToSource(index), idRoleNumber()).toString();
0033         return m_columnDisplay.value(id, QStringLiteral("hidden"));
0034     }
0035 
0036     return QIdentityProxyModel::data(index, role);
0037 }
0038 
0039 void ColumnDisplayModel::setDisplay(int row, const QString &display)
0040 {
0041     auto id = sourceModel()->data(sourceModel()->index(row, 0), idRoleNumber()).toString();
0042     if (id.isEmpty()) {
0043         return;
0044     }
0045 
0046     auto changed = false;
0047     if (!m_columnDisplay.contains(id)) {
0048         m_columnDisplay.insert(id, display);
0049         changed = true;
0050     } else if (m_columnDisplay.value(id) != display) {
0051         m_columnDisplay[id] = display;
0052         changed = true;
0053     }
0054 
0055     if (changed) {
0056         auto i = index(row, 0);
0057         Q_EMIT dataChanged(i, i, {DisplayStyleRole});
0058         Q_EMIT columnDisplayChanged();
0059     }
0060 }
0061 
0062 QVariantMap ColumnDisplayModel::columnDisplay() const
0063 {
0064     QVariantMap result;
0065     for (auto itr = m_columnDisplay.cbegin(); itr != m_columnDisplay.cend(); ++itr) {
0066         result.insert(itr.key(), itr.value());
0067     }
0068     return result;
0069 }
0070 
0071 void ColumnDisplayModel::setColumnDisplay(const QVariantMap &newColumnDisplay)
0072 {
0073     QHash<QString, QString> display;
0074     for (auto itr = newColumnDisplay.begin(); itr != newColumnDisplay.end(); ++itr) {
0075         display.insert(itr.key(), itr.value().toString());
0076     }
0077 
0078     m_columnDisplay = display;
0079     Q_EMIT columnDisplayChanged();
0080     if (sourceModel()) {
0081         Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
0082     }
0083 }
0084 
0085 QStringList ColumnDisplayModel::visibleColumnIds() const
0086 {
0087     QHash<QString, int> sourceRowMapping;
0088     for (int i = 0; i < rowCount(); ++i) {
0089         auto id = sourceModel()->data(sourceModel()->index(i, 0), idRoleNumber()).toString();
0090         sourceRowMapping.insert(id, i);
0091     }
0092 
0093     QStringList result;
0094     std::copy_if(m_columnDisplay.keyBegin(), m_columnDisplay.keyEnd(), std::back_inserter(result), [this](const auto &key) {
0095         return m_columnDisplay.value(key) != QStringLiteral("hidden");
0096     });
0097 
0098     std::stable_sort(result.begin(), result.end(), [sourceRowMapping](const QString &first, const QString &second) {
0099         return sourceRowMapping.value(first) < sourceRowMapping.value(second);
0100     });
0101 
0102     return result;
0103 }
0104 
0105 QString ColumnDisplayModel::idRole() const
0106 {
0107     return m_idRole;
0108 }
0109 
0110 void ColumnDisplayModel::setIdRole(const QString &newIdRole)
0111 {
0112     if (newIdRole == m_idRole) {
0113         return;
0114     }
0115 
0116     beginResetModel();
0117     m_idRole = newIdRole;
0118     m_idRoleNumber = -1;
0119     endResetModel();
0120     Q_EMIT idRoleChanged();
0121 }
0122 
0123 int ColumnDisplayModel::idRoleNumber() const
0124 {
0125     if (m_idRoleNumber == -1 && !m_idRole.isEmpty() && sourceModel()) {
0126         m_idRoleNumber = sourceModel()->roleNames().key(m_idRole.toUtf8());
0127     }
0128     return m_idRoleNumber;
0129 }
0130 
0131 #include "moc_ColumnDisplayModel.cpp"