File indexing completed on 2024-12-29 05:06:04
0001 // SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org> 0002 // SPDX-License-Identifier: GPL-2.0-or-later 0003 0004 #include "pagelistmodel.h" 0005 #include "homescreenstate.h" 0006 0007 #include <QJsonArray> 0008 #include <QJsonDocument> 0009 0010 PageListModel *PageListModel::self() 0011 { 0012 static PageListModel *model = new PageListModel; 0013 return model; 0014 } 0015 0016 PageListModel::PageListModel(QObject *parent) 0017 : QAbstractListModel{parent} 0018 { 0019 } 0020 0021 int PageListModel::rowCount(const QModelIndex &parent) const 0022 { 0023 Q_UNUSED(parent) 0024 return m_pages.size(); 0025 } 0026 0027 QVariant PageListModel::data(const QModelIndex &index, int role) const 0028 { 0029 if (!index.isValid()) { 0030 return QVariant(); 0031 } 0032 0033 switch (role) { 0034 case PageRole: 0035 return QVariant::fromValue(m_pages.at(index.row())); 0036 } 0037 0038 return QVariant(); 0039 } 0040 0041 QHash<int, QByteArray> PageListModel::roleNames() const 0042 { 0043 return {{PageRole, "delegate"}}; 0044 } 0045 0046 int PageListModel::length() 0047 { 0048 return m_pages.size(); 0049 } 0050 0051 PageModel *PageListModel::getPage(int index) 0052 { 0053 if (index < 0 || index >= m_pages.size()) { 0054 return nullptr; 0055 } 0056 0057 return m_pages[index]; 0058 } 0059 0060 void PageListModel::removePage(int index) 0061 { 0062 if (index < 0 || index >= m_pages.size()) { 0063 return; 0064 } 0065 0066 beginRemoveRows(QModelIndex(), index, index); 0067 m_pages[index]->deleteLater(); 0068 m_pages.removeAt(index); 0069 endRemoveRows(); 0070 0071 Q_EMIT lengthChanged(); 0072 0073 save(); 0074 } 0075 0076 Q_INVOKABLE void PageListModel::addPageAtEnd() 0077 { 0078 beginInsertRows(QModelIndex(), m_pages.size(), m_pages.size()); 0079 0080 PageModel *page = new PageModel{{}, this}; 0081 connect(page, &PageModel::saveRequested, this, &PageListModel::save); 0082 0083 m_pages.append(page); 0084 0085 endInsertRows(); 0086 0087 Q_EMIT lengthChanged(); 0088 0089 save(); 0090 } 0091 0092 bool PageListModel::isLastPageEmpty() 0093 { 0094 return m_pages.size() == 0 ? true : m_pages[m_pages.size() - 1]->isPageEmpty(); 0095 } 0096 0097 QJsonArray PageListModel::exportToJson() 0098 { 0099 QJsonArray arr; 0100 for (auto &page : m_pages) { 0101 arr.push_back(page->toJson()); 0102 } 0103 return arr; 0104 } 0105 0106 void PageListModel::save() 0107 { 0108 if (!m_containment) { 0109 return; 0110 } 0111 0112 QJsonArray arr = exportToJson(); 0113 QByteArray data = QJsonDocument(arr).toJson(QJsonDocument::Compact); 0114 0115 m_containment->config().writeEntry("Pages", QString::fromStdString(data.toStdString())); 0116 Q_EMIT m_containment->configNeedsSaving(); 0117 } 0118 0119 void PageListModel::load() 0120 { 0121 if (!m_containment) { 0122 return; 0123 } 0124 0125 QJsonDocument doc = QJsonDocument::fromJson(m_containment->config().readEntry("Pages", "{}").toUtf8()); 0126 loadFromJson(doc.array()); 0127 } 0128 0129 void PageListModel::loadFromJson(QJsonArray arr) 0130 { 0131 beginResetModel(); 0132 0133 m_pages.clear(); 0134 0135 for (QJsonValueRef r : arr) { 0136 QJsonArray obj = r.toArray(); 0137 0138 PageModel *page = PageModel::fromJson(obj, this); 0139 if (page) { 0140 connect(page, &PageModel::saveRequested, this, &PageListModel::save); 0141 m_pages.append(page); 0142 } 0143 } 0144 0145 endResetModel(); 0146 0147 Q_EMIT lengthChanged(); 0148 0149 // add page if there are no pages 0150 if (m_pages.size() == 0) { 0151 addPageAtEnd(); 0152 } 0153 } 0154 0155 void PageListModel::setContainment(Plasma::Containment *containment) 0156 { 0157 m_containment = containment; 0158 }