Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef CONFIGNODE_H
10 : #define CONFIGNODE_H
11 :
12 : #include <QObject>
13 : #include <QSharedPointer>
14 : #include <QVariant>
15 :
16 : #include <cassert>
17 : #include <kdb.hpp>
18 : #include <keyio.hpp>
19 :
20 : #include "printvisitor.hpp"
21 :
22 : class TreeViewModel;
23 : class PrintVisitor;
24 :
25 : /**
26 : * @brief The ConfigNode class
27 : */
28 : class ConfigNode : public QObject
29 : {
30 0 : Q_OBJECT
31 :
32 : public:
33 : /**
34 : * @brief ConfigNode
35 : * @param name
36 : * @param path
37 : * @param key
38 : * @param parentModel
39 : */
40 : explicit ConfigNode (QString name, QString path, const kdb::Key & key, TreeViewModel * parentModel);
41 : /// Needed by Qt. This copy constructor is supposed to create a DEEP COPY.
42 : ConfigNode (const ConfigNode & other);
43 : /// Needed by Qt/QSharedPtr
44 : ConfigNode ();
45 : ~ConfigNode ();
46 :
47 : /**
48 : * @brief dontDelete
49 : */
50 0 : void dontDelete ()
51 : {
52 0 : }
53 :
54 : /**
55 : * @brief Returns the number of children of this ConfigNode.
56 : *
57 : * @return The number of children of this ConfigNode.
58 : */
59 : int getChildCount () const;
60 :
61 : /**
62 : * @brief Returns the name of this ConfigNode.
63 : *
64 : * @return The name of this ConfigNode.
65 : */
66 : QString getName () const;
67 :
68 : /**
69 : * @brief Returns the path of this ConfigNode.
70 : *
71 : * @return The path of this ConfigNode.
72 : */
73 : QString getPath () const;
74 :
75 : /**
76 : * @brief Returns the value of this ConfigNode.
77 : *
78 : * @return The value of this ConfigNode.
79 : */
80 : QVariant getValue () const;
81 :
82 : /**
83 : * @brief Rename this ConfigNode.
84 : *
85 : * @param name The new name for this ConfigNode.
86 : */
87 : void setName (const QString & name);
88 :
89 : /**
90 : * @brief Change the value of this ConfigNode.
91 : *
92 : * @param value The new value for this ConfigNode.
93 : */
94 : void setValue (const QVariant & value);
95 :
96 : /**
97 : * @brief Append a new child to this ConfigNode.
98 : *
99 : * @param node The new child of this ConfigNode.
100 : */
101 : void appendChild (QSharedPointer<ConfigNode> node);
102 :
103 : /**
104 : * @brief Returns if this ConfigNode has a child with a certain name.
105 : *
106 : * @param name The name of the child node.
107 : *
108 : * @return True if this node has a child with a certain name.
109 : */
110 : bool hasChild (const QString & name) const;
111 :
112 : /**
113 : * @brief Get the children of this ConfigNode.
114 : *
115 : * @return The children of this ConfigNode as model.
116 : */
117 : TreeViewModel * getChildren () const;
118 :
119 : /**
120 : * @brief Get the metakeys of this ConfigNode.
121 : *
122 : * @return The metakeys of this ConfigNode as model.
123 : */
124 : TreeViewModel * getMetaKeys () const;
125 :
126 : /**
127 : * @brief Returns if the children of this ConfigNode have any children themselves.
128 : *
129 : * @return True if no child of this ConfigNode has any children.
130 : */
131 : bool childrenHaveNoChildren () const;
132 :
133 : /**
134 : * @brief Returns a child with a certain name.
135 : *
136 : * @param name The name of the child which is looked for.
137 : *
138 : * @return The child with the given name if it is a child of this ConfigNode.
139 : */
140 : QSharedPointer<ConfigNode> getChildByName (QString & name) const;
141 :
142 : /**
143 : * @brief Returns a child on a given index.
144 : *
145 : * @param index The index of the wanted child.
146 : *
147 : * @return The child on the given index.
148 : */
149 : Q_INVOKABLE QSharedPointer<ConfigNode> getChildByIndex (int index) const;
150 :
151 : /**
152 : * @brief Change the path of this ConfigNode.
153 : *
154 : * @param path The new path of this ConfigNode.
155 : */
156 : void setPath (const QString & path);
157 :
158 : /**
159 : * @brief Change or add a metakey of this ConfigNode. This method is used if this ConfigNode is a metakey.
160 : *
161 : * @param name The name of the metakey.
162 : * @param value The value of the metakey.
163 : */
164 : void setMeta (const QString & name, const QVariant & value);
165 :
166 : /**
167 : * @brief Change or add more than one metakeys of this ConfigNode. This method is used if this ConfigNode is a metakey.
168 : *
169 : * @param metaData The data consists of metanames and metavalues respectively.
170 : */
171 : Q_INVOKABLE void setMeta (const QVariantMap & metaData);
172 :
173 : /**
174 : * @brief Delete a metakey of this ConfigNode. This method is used if this ConfigNode is a metakey.
175 : *
176 : * @param name The name of the metakey that is supposed to be deleted.
177 : */
178 : Q_INVOKABLE void deleteMeta (const QString & name);
179 :
180 : /**
181 : * @brief This method accepts a visitor to support the Vistor Pattern.
182 : *
183 : * @param visitor The Visitor that visits this ConfigNode.
184 : */
185 : void accept (Visitor & visitor);
186 :
187 : /**
188 : * @brief Get the underlying Key of this ConfigNode.
189 : *
190 : * @return The underlying Key of this ConfigNode.
191 : */
192 : kdb::Key getKey () const;
193 :
194 : /**
195 : * @brief Set the underlying Key of this ConfigNode.
196 : *
197 : * @param key The new Key of this ConfigNode.
198 : */
199 : void setKey (kdb::Key key);
200 :
201 : /**
202 : * @brief Change the name of the underlying Key of this ConfigNode.
203 : *
204 : * @param name The new name of the underlying Key of this ConfigNode.
205 : */
206 : void setKeyName (const QString & name);
207 :
208 : /**
209 : * @brief Returns the index of a child ConfigNode, based on its name; if there is no child with this
210 : * name, the return index is -1.
211 : *
212 : * @param name The name of this ConfigNode.
213 : *
214 : * @return The index of this ConfigNode.
215 : */
216 : int getChildIndexByName (const QString & name);
217 :
218 : /**
219 : * @brief Returns a pointer to the TreeViewModel this ConfigNode is in.
220 : *
221 : * @return A pointer to the TreeViewModel this ConfigNode is in.
222 : */
223 : TreeViewModel * getParentModel ();
224 :
225 : /**
226 : * @brief Sets a pointer to the TreeViewModel this ConfigNode is in.
227 : *
228 : * @param parentModel The TreeViewModel this ConfigNode is in.
229 : */
230 : void setParentModel (TreeViewModel * parentModel);
231 :
232 : /**
233 : * @brief Returns if this ConfigNode is expanded.
234 : *
235 : * @return True if this ConfigNode is expanded.
236 : */
237 : bool isExpanded () const;
238 :
239 : bool isDirty () const;
240 : void setIsDirty (bool dirty);
241 : void updateNode (kdb::Key key);
242 :
243 : private:
244 : QString m_name;
245 : QString m_path;
246 : QVariant m_value;
247 :
248 : kdb::Key m_key;
249 : TreeViewModel * m_children;
250 : TreeViewModel * m_metaData;
251 : TreeViewModel * m_parentModel;
252 :
253 : bool m_isExpanded;
254 : bool m_isDirty;
255 :
256 : /**
257 : * @brief Populates the TreeViewModel which holds the metakeys of this ConfigNode.
258 : */
259 : void populateMetaModel ();
260 : void setValue ();
261 :
262 : signals:
263 : /**
264 : * @brief showMessage
265 : * @param title
266 : * @param text
267 : * @param detailedText
268 : */
269 : void showMessage (QString title, QString text, QString detailedText);
270 :
271 : public slots:
272 : /**
273 : * @brief setIsExpanded
274 : * @param value
275 : */
276 : void setIsExpanded (bool value);
277 : };
278 :
279 0 : Q_DECLARE_METATYPE (ConfigNode)
280 :
281 : typedef QSharedPointer<ConfigNode> ConfigNodePtr;
282 :
283 : #endif // CONFIGNODE_H
|