Debian Codegraph @

src/settings/dsettingsoption.cpp

raw
1 // SPDX-FileCopyrightText: 2016 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dsettingsoption.h"
6
7 #include <QVariant>
8 #include <QJsonObject>
9 #include <QJsonArray>
10
11 DCORE_BEGIN_NAMESPACE
12
13 class DSettingsOptionPrivate
14 {
15 public:
16 DSettingsOptionPrivate(DSettingsOption *parent) : q_ptr(parent) {}
17
18 void parseJson(const QString &prefixKey, const QJsonObject &option);
19
20 QPointer<DSettingsGroup> parent;
21
22 QString key;
23 QString name;
24 QString viewType;
25 QVariant defalutValue;
26 QVariant value;
27 QVariantMap datas;
28 bool canReset;
29 bool hidden;
30
31 DSettingsOption *q_ptr;
32 Q_DECLARE_PUBLIC(DSettingsOption)
33 };
34
35 /*!
36 @~english
37 @class Dtk::Core::DSettingsOption
38 \inmodule dtkcore
39 @brief DSettingsOption is the base key/value item of DSettings.
40 */
41
42 /*!
43 @~english
44 @fn void DSettingsOption::valueChanged(QVariant value);
45 @brief Emit when option value change.
46
47 \a value Changed data.
48 */
49
50 /*!
51 @~english
52 @fn void DSettingsOption::dataChanged(const QString &dataType, QVariant value);
53 @brief Emit when option data change.
54
55 \a dataType Changed data type, \a value Changed data.
56 */
57
58 /*!
59 @~english
60 @property Dtk::Core::DSettingsOption::value
61 @brief Current value of this option.
62 */
63
64 DSettingsOption::DSettingsOption(QObject *parent) :
65 QObject(parent), dd_ptr(new DSettingsOptionPrivate(this))
66 {
67 }
68
69 DSettingsOption::~DSettingsOption()
70 {
71
72 }
73
74 /*!
75 @~english
76 @brief Get direct parent group of this option.
77 @return Returns the direct parent group of the this option.
78 */
79 QPointer<DSettingsGroup> DSettingsOption::parentGroup() const
80 {
81 Q_D(const DSettingsOption);
82 return d->parent;
83 }
84
85 /*!
86 @~english
87 @brief Change the direct parent group of this option.
88
89 \a parentGroup parent group.
90 */
91 void DSettingsOption::setParentGroup(QPointer<DSettingsGroup> parentGroup)
92 {
93 Q_D(DSettingsOption);
94 d->parent = parentGroup;
95 }
96
97 /*!
98 @~english
99 @brief Return the full key of this option, include all parent.
100 @return Return the full key of this option, include all parent.
101 */
102 QString DSettingsOption::key() const
103 {
104 Q_D(const DSettingsOption);
105 return d->key;
106 }
107
108 /*!
109 @~english
110 @brief Get display name of the option, it may be translated.
111 @return Return the name of the option.
112 */
113 QString DSettingsOption::name() const
114 {
115 Q_D(const DSettingsOption);
116 return d->name;
117 }
118
119 /*!
120 @~english
121 @brief Check this option can be reset to default value. if false, reset action will not take effect.
122
123 @return true if can be reset.
124 */
125 bool DSettingsOption::canReset() const
126 {
127 Q_D(const DSettingsOption);
128 return d->canReset;
129 }
130
131 /*!
132 @~english
133 @brief Default value of this option, must config in this json desciption file.
134
135 @return Return the default value of this option.
136 */
137 QVariant DSettingsOption::defaultValue() const
138 {
139 Q_D(const DSettingsOption);
140 return d->defalutValue;
141 }
142
143 /*!
144 @~english
145 @brief Get current value of option.
146
147 @return Return the current value of this option.
148 */
149 QVariant DSettingsOption::value() const
150 {
151 Q_D(const DSettingsOption);
152 return (!d->value.isValid() || d->value.isNull()) ? d->defalutValue : d->value;
153 }
154
155 /*!
156 @~english
157 @brief Custom data of option, like QObject::property.
158 \a dataType Data type.
159
160 @return Data type Indicates the data.
161 @sa QObject::property
162 @sa Dtk::Core::DSettingsOption::setData
163 */
164 QVariant DSettingsOption::data(const QString &dataType) const
165 {
166 Q_D(const DSettingsOption);
167 return d->datas.value(dataType);
168 }
169
170 /*!
171 @~english
172 @brief UI widget type of this option.
173
174 @return Returns the UI widget type of the option.
175 @sa Dtk::Widget::DSettingsWidgetFactory
176 */
177 QString DSettingsOption::viewType() const
178 {
179 Q_D(const DSettingsOption);
180 return d->viewType;
181 }
182
183 /*!
184 @~english
185 @brief Check this option will show on DSettings dialog.
186
187 @return true if option not bind to ui element.
188 */
189 bool DSettingsOption::isHidden() const
190 {
191 Q_D(const DSettingsOption);
192 return d->hidden;
193 }
194
195 /*!
196 @~english
197 @brief Convert QJsonObject to DSettingsOption.
198
199 \a prefixKey instead parse prefix key from parent.
200 \a json is an QJsonObejct instance.
201
202 @return Return the option data after parsing.
203 */
204 QPointer<DSettingsOption> DSettingsOption::fromJson(const QString &prefixKey, const QJsonObject &json)
205 {
206 auto optionPtr = QPointer<DSettingsOption>(new DSettingsOption);
207 optionPtr->parseJson(prefixKey, json);
208 return optionPtr;
209 }
210
211 /*!
212 @~english
213 @brief Set current value of option.
214
215 \a value Current value of option.
216 */
217 void DSettingsOption::setValue(QVariant value)
218 {
219 Q_D(DSettingsOption);
220
221 // 默认没有设置value时比较默认值。防止reset时出现所有的option都发射valueChanged
222 if (this->value() == value) {
223 return;
224 }
225
226 d->value = value;
227 Q_EMIT valueChanged(value);
228 }
229
230 ///*!
231 // * @brief Override default value of json
232 // * \a value
233 // */
234 //void DSettingsOption::setDefault(QVariant value)
235 //{
236 // Q_D(DSettingsOption);
237 // d->defalutValue = value;
238 //}
239
240 /*!
241 @~english
242 @brief Set custom data.
243 \a dataType is data id, just a unique string.
244 \a value of the data id.
245 @sa Dtk::Core::DSettingsOption::data
246 */
247 void DSettingsOption::setData(const QString &dataType, QVariant value)
248 {
249 Q_D(DSettingsOption);
250
251 if (d->datas.value(dataType) == value) {
252 return;
253 }
254
255 d->datas.insert(dataType, value);
256
257 Q_EMIT dataChanged(dataType, value);
258 }
259
260 /*!
261 @~english
262 @brief Parse QJsonObject to DSettingsOption.
263 \a prefixKey instead parse prefix key from parent.
264 \a option is an QJsonObejct instance.
265 @sa QPointer<DSettingsOption> Dtk::Core::DSettingsOption::fromJson(const QString &prefixKey, const QJsonObject &json)
266 */
267 void DSettingsOption::parseJson(const QString &prefixKey, const QJsonObject &option)
268 {
269 Q_D(DSettingsOption);
270 d->parseJson(prefixKey, option);
271 }
272
273 void DSettingsOptionPrivate::parseJson(const QString &prefixKey, const QJsonObject &option)
274 {
275 // Q_Q(Option);
276 key = option.value("key").toString();
277 Q_ASSERT(!key.isEmpty());
278 Q_ASSERT(!prefixKey.isEmpty());
279 key = prefixKey + "." + key;
280 name = option.value("name").toString();
281
282 canReset = !option.contains("reset") ? true : option.value("reset").toBool();
283 defalutValue = option.value("default").toVariant();
284 hidden = !option.contains("hide") ? false : option.value("hide").toBool();
285 viewType = option.value("type").toString();
286
287 QStringList revserdKeys;
288 revserdKeys << "key" << "name" << "reset"
289 << "default" << "hide" << "type";
290
291 auto allKeys = option.keys();
292 for (auto key : revserdKeys) {
293 allKeys.removeAll(key);
294 }
295
296 for (auto key : allKeys) {
297 auto value = option.value(key);
298 if (value.isArray()) {
299 QStringList stringlist;
300 for (auto va : value.toArray()) {
301 stringlist << QString("%1").arg(va.toString());
302 }
303 datas.insert(key, stringlist);
304 } else {
305 datas.insert(key, value.toVariant());
306 }
307 }
308 }
309
310 DCORE_END_NAMESPACE
311
312