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

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 #pragma once
0008 
0009 #include <QQmlListProperty>
0010 #include <QQmlPropertyMap>
0011 #include <qqmlregistration.h>
0012 
0013 #include <KSharedConfig>
0014 
0015 class FaceLoader;
0016 
0017 class PageDataObject : public QQmlPropertyMap
0018 {
0019     Q_OBJECT
0020     Q_PROPERTY(QQmlListProperty<PageDataObject> children READ childrenProperty NOTIFY childrenChanged)
0021     Q_PROPERTY(bool dirty READ dirty NOTIFY dirtyChanged)
0022     Q_PROPERTY(QString fileName READ fileName CONSTANT)
0023     QML_ELEMENT
0024     QML_UNCREATABLE("Used for data storage")
0025 
0026 public:
0027     explicit PageDataObject(const KSharedConfig::Ptr &config, QObject *parent = nullptr);
0028 
0029     QQmlListProperty<PageDataObject> childrenProperty() const;
0030     QList<PageDataObject *> children() const;
0031 
0032     KSharedConfig::Ptr config() const;
0033 
0034     void setValue(const QString &name, const QVariant &value);
0035 
0036     Q_INVOKABLE bool resetPage();
0037     Q_INVOKABLE bool savePage();
0038     Q_INVOKABLE void saveAs(const QUrl &destination);
0039 
0040     bool load(const KConfigBase &config, const QString &groupName);
0041     Q_SIGNAL void loaded();
0042     bool save(KConfigBase &config, const QString &groupName, const QStringList &ignoreProperties = {QStringLiteral("children")});
0043     Q_SIGNAL void saved();
0044 
0045     void reset();
0046 
0047     Q_INVOKABLE PageDataObject *insertChild(int index, const QVariantMap &properties);
0048     Q_INVOKABLE void removeChild(int index);
0049     Q_INVOKABLE void moveChild(int from, int to);
0050 
0051     PageDataObject *childAt(int index) const;
0052     int childCount() const;
0053 
0054     Q_SIGNAL void childrenChanged();
0055     Q_SIGNAL void childInserted(int index);
0056     Q_SIGNAL void childRemoved(int index);
0057     Q_SIGNAL void childMoved(int from, int to);
0058 
0059     bool dirty() const;
0060     Q_INVOKABLE void markDirty();
0061     Q_INVOKABLE void markClean();
0062     Q_SIGNAL void dirtyChanged();
0063 
0064     FaceLoader *faceLoader();
0065     void setFaceLoader(FaceLoader *faceLoader);
0066 
0067     QString fileName() const;
0068 
0069 private:
0070     bool isGroupEmpty(const KConfigGroup &group);
0071     void updateNames();
0072 
0073     QQmlListProperty<PageDataObject> m_childrenProperty;
0074     QList<PageDataObject *> m_children;
0075     KSharedConfig::Ptr m_config;
0076     bool m_dirty = false;
0077     FaceLoader *m_faceLoader = nullptr;
0078 };