DtkDeclarative
DTK Declarative module
载入中...
搜索中...
未找到
dsettingscontainer_p.h
1// SPDX-FileCopyrightText: 2021 - 2022 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5#ifndef DSETTINGSCONTAINER_P_H
6#define DSETTINGSCONTAINER_P_H
7
8#include <dtkdeclarative_global.h>
9
10#include <QQmlParserStatus>
11#include <QQmlComponent>
12#include <private/qqmlobjectmodel_p.h>
13#include "dconfigwrapper_p.h"
14
15DQUICK_BEGIN_NAMESPACE
16
17class SettingsOption : public QObject
18{
19 Q_OBJECT
20 Q_PROPERTY(QString key READ key WRITE setKey NOTIFY keyChanged)
21 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
22 Q_PROPERTY(QVariant value READ value WRITE setValue RESET resetValue NOTIFY valueChanged)
23 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged)
24 Q_CLASSINFO("DefaultProperty", "delegate")
25#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
26 QML_NAMED_ELEMENT(SettingsOption)
27#endif
28
29public:
30 explicit SettingsOption(QObject *parent = nullptr);
31 virtual ~SettingsOption() override;
32
33 QString key() const;
34 void setKey(QString key);
35 QString name() const;
36 void setName(QString name);
37 QVariant value() ;
38 void setValue(QVariant value);
39 void resetValue();
40
41 QQmlComponent *delegate() const;
42 void setDelegate(QQmlComponent *delegate);
43 void setConfig(DConfigWrapper *config);
44
45 static SettingsOption *qmlAttachedProperties(QObject *object);
46
47Q_SIGNALS:
48 void keyChanged(QString key);
49 void nameChanged(QString name);
50 void valueChanged(QVariant value);
51 void delegateChanged();
52
53private Q_SLOTS:
54 void onConfigValueChanged();
55
56private:
57 QString m_key;
58 QString m_name;
59 QVariant m_value;
60 bool m_valueInitialized = false;
61 QQmlComponent *m_delegate = nullptr;
62 DConfigWrapper *m_config = nullptr;
63};
64
65class SettingsGroup : public QObject
66{
67 Q_OBJECT
68 Q_PROPERTY(QString key READ key WRITE setKey NOTIFY keyChanged)
69 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
70 Q_PROPERTY(int level READ level CONSTANT)
71 Q_PROPERTY(bool visible READ visible WRITE setVisible NOTIFY visibleChanged)
72 Q_PROPERTY(int index READ index NOTIFY indexChanged)
73 Q_PROPERTY(QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsOption> options READ options NOTIFY optionsChanged)
74 Q_PROPERTY(QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> children READ children NOTIFY childrenChanged)
75 Q_PROPERTY(QQmlComponent *background READ background WRITE setBackground NOTIFY backgroundChanged)
76 Q_CLASSINFO("DefaultProperty", "options")
77#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
78 QML_NAMED_ELEMENT(SettingsGroup)
79 QML_ATTACHED(SettingsGroup)
80#endif
81
82public:
83 explicit SettingsGroup(QObject * parent = nullptr);
84 virtual ~SettingsGroup() override;
85
86 QString key() const;
87 void setKey(const QString &key);
88 QString name() const;
89 void setName(QString name);
90 int level() const;
91 void setLevel(int level);
92 bool visible() const;
93 void setVisible(bool visible);
94 QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsOption> options();
95 QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> children();
96 QQmlComponent *background() const;
97 void setBackground(QQmlComponent *background);
98 void setConfig(DConfigWrapper *config);
99 SettingsGroup *parentGroup() const;
100 void setParentGroup(SettingsGroup *parentGroup);
101 int index() const;
102 void setIndex(const int index);
103
104 static SettingsGroup *qmlAttachedProperties(QObject *object);
105
106Q_SIGNALS:
107 void keyChanged(QString key);
108 void nameChanged(QString name);
109 void visibleChanged(bool visible);
110 void optionsChanged();
111 void childrenChanged();
112 void backgroundChanged();
113 void indexChanged(int index);
114
115private:
116
117 QString m_key;
118 QString m_name;
119 int m_level = -1;
120 bool m_visible = true;
121 int m_index;
122 QList<SettingsOption*> m_options;
123 QList<SettingsGroup*> m_children;
124 QQmlComponent * m_background = nullptr;
125 SettingsGroup* m_parentGroup = nullptr;
126};
127
130class SettingsInstanceModel : public QQmlInstanceModel
131{
132 Q_OBJECT
133public:
134 explicit SettingsInstanceModel(SettingsContainer *container);
135
136public:
137 virtual int count() const override;
138#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
139 virtual QVariant variantValue(int index, const QString &name) override;
140 virtual ReleaseFlags release(QObject *object, ReusableFlag reusableFlag = NotReusable) override;
141#else
142 virtual QString stringValue(int index, const QString &name) override;
143 virtual ReleaseFlags release(QObject *object) override;
144#endif
145 virtual void setWatchedRoles(const QList<QByteArray> &roles) override;
146 virtual QQmlIncubator::Status incubationStatus(int index) override;
147 virtual int indexOf(QObject *object, QObject *objectContext) const override;
148
149 void updateModel();
150
151public:
152 void insert(SettingsGroup *group);
153 void remove(SettingsGroup *group);
154
155protected:
156 Q_DECLARE_PRIVATE(SettingsInstanceModel)
157};
158
160{
161 Q_OBJECT
162public:
163 explicit SettingsContentModel(SettingsContainer *container = nullptr);
164
165public:
166 bool isValid() const override;
167 virtual QObject *object(int index, QQmlIncubator::IncubationMode incubationMode) override;
168
169private:
170 QQmlComponent *contentBackgroundByGroup(const SettingsGroup *group);
171 static const QByteArray groupComponentData();
172 static const QByteArray optionsLayoutComponentData();
173};
174
176 Q_OBJECT
177public:
178 SettingsNavigationModel(SettingsContainer *container = nullptr);
179
180public:
181 virtual bool isValid() const override;
182 virtual QObject *object(int index, QQmlIncubator::IncubationMode incubationMode) override;
183};
184
186class SettingsModelAttached;
187class SettingsContainer : public QObject, public QQmlParserStatus
188{
189 Q_OBJECT
190 Q_INTERFACES(QQmlParserStatus)
191 Q_PROPERTY(DConfigWrapper *config READ config WRITE setConfig NOTIFY configChanged)
192 Q_PROPERTY(QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> groups READ groups NOTIFY groupsChanged)
193 Q_PROPERTY(SettingsContentModel *contentModel READ contentModel NOTIFY contentModelChanged)
194 Q_PROPERTY(QQmlComponent *contentTitle READ contentTitle WRITE setContentTitle NOTIFY contentTitleChanged)
195 Q_PROPERTY(QQmlComponent *contentBackground READ contentBackground WRITE setContentBackground NOTIFY contentBackgroundChanged)
196 Q_PROPERTY(SettingsNavigationModel *navigationModel READ navigationModel NOTIFY navigationModelChanged)
197 Q_PROPERTY(QQmlComponent *navigationTitle READ navigationTitle WRITE setNavigationTitle NOTIFY navigationTitleChanged)
198 Q_CLASSINFO("DefaultProperty", "groups")
199#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
200 QML_NAMED_ELEMENT(SettingsContainer)
201#endif
202
203public:
204 explicit SettingsContainer(QObject *parent = nullptr);
205 virtual ~SettingsContainer() override;
206
207 DConfigWrapper *config() const;
208 void setConfig(DConfigWrapper *config);
209 QQmlListProperty<DTK_QUICK_NAMESPACE::SettingsGroup> groups();
210 SettingsContentModel *contentModel() const;
211 SettingsNavigationModel *navigationModel() const;
212 QQmlComponent *contentTitle() const;
213 void setContentTitle(QQmlComponent *delegate);
214 QQmlComponent *navigationTitle() const;
215 void setNavigationTitle(QQmlComponent *delegate);
216 void setContentBackground(QQmlComponent * contentBackground);
217 QQmlComponent *contentBackground() const;
218
219 QVector<SettingsGroup *> groupList() const;
220 QQmlContext *creationContext();
221
222 virtual void classBegin() override;
223 virtual void componentComplete() override;
224
225public Q_SLOTS:
226 void setGroupVisible(const QString &key, bool visible);
227 bool groupVisible(const QString &key) const;
228 void resetSettings();
229
230private Q_SLOTS:
231 void onGroupVisibleChanged(bool visible);
232
233Q_SIGNALS:
234 void groupsChanged();
235 void navigationTitleChanged();
236 void contentTitleChanged();
237 void contentModelChanged();
238 void navigationModelChanged();
239 void configChanged();
240 void contentBackgroundChanged();
241
242private:
243 QList<SettingsGroup*> m_groups;
244 SettingsNavigationModel *m_navigationModel = nullptr;
245 SettingsContentModel *m_contentModel = nullptr;
246 QQmlComponent *m_contentTitle = nullptr;
247 QQmlComponent *m_navigationTitle = nullptr;
248 QQmlComponent * m_contentBackground = nullptr;
249 DConfigWrapper *m_config = nullptr;
250};
251
252DQUICK_END_NAMESPACE
253
254QML_DECLARE_TYPEINFO(DTK_QUICK_NAMESPACE::SettingsOption, QML_HAS_ATTACHED_PROPERTIES)
255QML_DECLARE_TYPEINFO(DTK_QUICK_NAMESPACE::SettingsGroup, QML_HAS_ATTACHED_PROPERTIES)
256
257#endif // DSETTINGSCONTAINER_P_H
Definition dconfigwrapper_p.h:18
Definition dsettingscontainer_p.h:188
Definition dsettingscontainer_p.h:160
Definition dsettingscontainer_p.h:66
Definition dsettingscontainer.cpp:596
Definition dsettingscontainer_p.h:131
Definition dsettingscontainer_p.h:175
Definition dsettingscontainer_p.h:18