File indexing completed on 2024-05-12 05:36:50
0001 /* 0002 * SPDX-FileCopyrightText: 2020 David Redondo <kde@david-redondo.de> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "FacesModel.h" 0008 0009 #include <QQmlListProperty> 0010 #include <QQuickItem> 0011 0012 #include <KLocalizedString> 0013 0014 #include <faces/SensorFaceController.h> 0015 0016 #include "FaceLoader.h" 0017 #include "PageDataObject.h" 0018 0019 FacesModel::FacesModel(QObject *parent) 0020 : QAbstractListModel(parent) 0021 { 0022 } 0023 0024 QHash<int, QByteArray> FacesModel::roleNames() const 0025 { 0026 return {{Qt::DisplayRole, "display"}, {IdRole, "id"}}; 0027 } 0028 0029 int FacesModel::rowCount(const QModelIndex &parent) const 0030 { 0031 if (parent.isValid()) { 0032 return 0; 0033 } 0034 return m_faceLoaders.size() + 1; 0035 } 0036 0037 QVariant FacesModel::data(const QModelIndex &index, int role) const 0038 { 0039 if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) { 0040 return QVariant(); 0041 } 0042 0043 switch (role) { 0044 case Qt::DisplayRole: 0045 if (index.row() == m_faceLoaders.count()) { 0046 return i18nc("@info:placeholder", "No Chart"); 0047 } else { 0048 return m_faceLoaders[index.row()]->controller()->title(); 0049 } 0050 case IdRole: 0051 if (index.row() == m_faceLoaders.count()) { 0052 return ""; 0053 } else { 0054 return m_faceLoaders[index.row()]->dataObject()->value(QStringLiteral("face")).toString(); 0055 } 0056 } 0057 return QVariant(); 0058 } 0059 0060 QQuickItem *FacesModel::faceAtIndex(int row) const 0061 { 0062 if (row == m_faceLoaders.count()) { 0063 return nullptr; 0064 } 0065 0066 auto loader = m_faceLoaders.at(row); 0067 if (loader->controller()) { 0068 return loader->controller()->fullRepresentation(); 0069 } 0070 0071 return nullptr; 0072 } 0073 0074 PageDataObject *FacesModel::pageData() const 0075 { 0076 return m_pageData; 0077 } 0078 0079 void FacesModel::setPageData(PageDataObject *pageData) 0080 { 0081 if (pageData == m_pageData) { 0082 return; 0083 } 0084 beginResetModel(); 0085 if (m_pageData) { 0086 disconnect(m_pageData, &PageDataObject::dirtyChanged, this, nullptr); 0087 } 0088 m_faceLoaders.clear(); 0089 m_pageData = pageData; 0090 Q_EMIT pageDataChanged(); 0091 if (pageData) { 0092 findFaceLoaders(pageData); 0093 connect(m_pageData, &PageDataObject::dirtyChanged, this, [this] { 0094 beginResetModel(); 0095 m_faceLoaders.clear(); 0096 findFaceLoaders(m_pageData); 0097 endResetModel(); 0098 }); 0099 } 0100 endResetModel(); 0101 } 0102 0103 void FacesModel::findFaceLoaders(PageDataObject *pageData) 0104 { 0105 if (pageData->faceLoader()) { 0106 m_faceLoaders.append(pageData->faceLoader()); 0107 } else { 0108 for (auto child : pageData->children()) { 0109 findFaceLoaders(child); 0110 } 0111 } 0112 } 0113 0114 #include "moc_FacesModel.cpp"