File indexing completed on 2024-05-12 17:08:28
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 "PageDataModel.h" 0008 0009 #include <QDebug> 0010 0011 #include <KConfig> 0012 #include <KConfigGroup> 0013 0014 #include "PageDataObject.h" 0015 0016 PageDataModel::PageDataModel(QObject *parent) 0017 : QAbstractListModel(parent) 0018 { 0019 } 0020 0021 QHash<int, QByteArray> PageDataModel::roleNames() const 0022 { 0023 static QHash<int, QByteArray> roles{ 0024 {DataRole, "data"}, 0025 }; 0026 return roles; 0027 } 0028 0029 PageDataObject *PageDataModel::dataObject() const 0030 { 0031 return m_data; 0032 } 0033 0034 void PageDataModel::setDataObject(PageDataObject *newDataObject) 0035 { 0036 if (newDataObject == m_data) { 0037 return; 0038 } 0039 0040 if (m_data) { 0041 m_data->disconnect(this); 0042 } 0043 0044 beginResetModel(); 0045 m_data = newDataObject; 0046 endResetModel(); 0047 0048 if (m_data) { 0049 connect(m_data, &PageDataObject::childInserted, this, [this](int index) { 0050 beginInsertRows(QModelIndex{}, index, index); 0051 endInsertRows(); 0052 }); 0053 connect(m_data, &PageDataObject::childRemoved, this, [this](int index) { 0054 beginRemoveRows(QModelIndex{}, index, index); 0055 endRemoveRows(); 0056 }); 0057 connect(m_data, &PageDataObject::childMoved, this, [this](int from, int to) { 0058 if (from < to) { 0059 beginMoveRows(QModelIndex{}, from, from, QModelIndex{}, to + 1); 0060 } else { 0061 beginMoveRows(QModelIndex{}, from, from, QModelIndex{}, to); 0062 } 0063 endMoveRows(); 0064 }); 0065 connect(m_data, &PageDataObject::loaded, this, [this] { 0066 beginResetModel(); 0067 endResetModel(); 0068 }); 0069 } 0070 0071 Q_EMIT dataObjectChanged(); 0072 } 0073 0074 int PageDataModel::rowCount(const QModelIndex &parent) const 0075 { 0076 if (parent.isValid() || !m_data) { 0077 return 0; 0078 } 0079 0080 return m_data->childCount(); 0081 } 0082 0083 QVariant PageDataModel::data(const QModelIndex &index, int role) const 0084 { 0085 if (!m_data || !checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::DoNotUseParent)) { 0086 return QVariant{}; 0087 } 0088 0089 auto data = m_data->childAt(index.row()); 0090 if (!data) { 0091 return QVariant{}; 0092 } 0093 0094 switch (role) { 0095 case DataRole: 0096 return QVariant::fromValue(data); 0097 default: 0098 return QVariant{}; 0099 } 0100 } 0101 0102 int PageDataModel::countObjects(const QVariantMap &properties) 0103 { 0104 if (!m_data) { 0105 return 0; 0106 } 0107 0108 if (properties.isEmpty()) { 0109 return m_data->childCount(); 0110 } 0111 0112 int result = 0; 0113 const auto children = m_data->children(); 0114 for (auto child : children) { 0115 auto match = std::all_of(properties.keyValueBegin(), properties.keyValueEnd(), [child](const std::pair<QString, QVariant> &entry) { 0116 return child->value(entry.first) == entry.second; 0117 }); 0118 if (match) { 0119 result += 1; 0120 } 0121 } 0122 0123 return result; 0124 }