ImagingTools Core SDK
CTreeItemModel.h
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ImtCore-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QAbstractListModel>
7#include <QtCore/QJsonDocument>
8#include <QtCore/QJsonObject>
9#include <QtCore/QJsonArray>
10#include <QtCore/QtDebug>
11#include <QtCore/QVariantMap>
12
13// ACF includes
14#include <iser/ISerializable.h>
15#include <istd/TSmartPtr.h>
16
17
18namespace imtbase
19{
20
21
25class CTreeItemModel: public QAbstractListModel
26{
27 Q_OBJECT
28 Q_PROPERTY(QString state READ State WRITE SetState NOTIFY stateChanged)
29
30public:
31 typedef QAbstractListModel BaseClass2;
32
33 Q_INVOKABLE inline bool beginChanges(){ return BeginChanges(); }
34 Q_INVOKABLE inline bool endChanges(){ return EndChanges(); }
35 Q_INVOKABLE inline bool copy(const CTreeItemModel* object){ return Copy(object); }
36 Q_INVOKABLE inline void setParent(QObject* parent){ SetParent(parent); }
37 Q_INVOKABLE inline imtbase::CTreeItemModel* copyMe(){ return CopyMe(); }
38 Q_INVOKABLE inline bool isEqualWithModel(CTreeItemModel* modelPtr){ return IsEqualWithModel(modelPtr); }
39 Q_INVOKABLE inline void insertNewItemWithParameters(int index, const QVariantMap& map){ InsertNewItemWithParameters(index, map); }
40 Q_INVOKABLE inline int insertNewItem(){ return InsertNewItem(); }
41 Q_INVOKABLE inline int insertNewItem(int index){ return InsertNewItem(index); }
42 Q_INVOKABLE inline int removeItem(int index){ return RemoveItem(index); }
43 Q_INVOKABLE inline bool swapItems(int index1, int index2){ return SwapItems(index1, index2); }
44 Q_INVOKABLE inline imtbase::CTreeItemModel* addTreeModel(const QByteArray& key, int index = 0){ return AddTreeModel(key, index); }
45 Q_INVOKABLE inline bool setExternTreeModel(const QByteArray& key, CTreeItemModel* externTreeModel, int index = 0){ return SetExternTreeModel(key, externTreeModel, index); }
46 Q_INVOKABLE inline bool copyItemDataFromModel(int index, const CTreeItemModel* externTreeModel, int externIndex = 0){ return CopyItemDataFromModel(index, externTreeModel, externIndex); }
47 Q_INVOKABLE inline bool copyItemDataFromModel(int index, CTreeItemModel* externTreeModel, int externIndex = 0){ return CopyItemDataFromModel(index, externTreeModel, externIndex); }
48 Q_INVOKABLE inline bool copyItemDataToModel(int index, CTreeItemModel* externTreeModel, int externIndex = 0) const{ return CopyItemDataToModel(index, externTreeModel, externIndex); }
49 Q_INVOKABLE inline bool setData(const QByteArray& key, const QVariant& value, int index = 0){ return SetData(key, value, index); }
50 Q_INVOKABLE inline bool removeData(const QByteArray& key, int index = 0){ return RemoveData(key, index); }
51 Q_INVOKABLE inline QVariant getData(const QByteArray& key, int index = 0) const{ return GetData(key, index); }
52 Q_INVOKABLE inline imtbase::CTreeItemModel* getParent() const{ return GetParent(); }
53 Q_INVOKABLE inline bool isTreeModel(const QByteArray& key, int index = 0) const{ return IsTreeModel(key, index); }
54 Q_INVOKABLE inline bool containsKey(const QByteArray& key, int index = 0) const{ return ContainsKey(key, index); }
55 Q_INVOKABLE inline bool isValidData(const QByteArray& key, int index = 0) const{ return IsValidData(key, index); }
56 Q_INVOKABLE inline imtbase::CTreeItemModel* getTreeItemModel(const QByteArray& key, int index = 0) const{ return GetTreeItemModel(key, index); }
57 Q_INVOKABLE inline imtbase::CTreeItemModel* getModelFromItem(int itemIndex = 0) const{ return GetModelFromItem(itemIndex); }
58 Q_INVOKABLE inline int getItemsCount() const{ return GetItemsCount(); }
59 Q_INVOKABLE inline void getKeys(QList<QByteArray>& keys, int index = 0) const{ return GetKeys(keys, index); }
60 Q_INVOKABLE inline QList<QString> getKeys(int index = 0) const{ return GetKeys(index); }
61 Q_INVOKABLE inline void clear(){ Clear(); }
62 Q_INVOKABLE inline bool isArray(){ return IsArray(); }
63 Q_INVOKABLE inline void setIsArray(const bool& isArray){ SetIsArray(isArray); }
64 Q_INVOKABLE inline bool createFromJson(const QByteArray& jsonContent){ return CreateFromJson(jsonContent); }
65 Q_INVOKABLE inline void setQueryParam(const QByteArray& key, const QByteArray& value){ SetQueryParam(key, value); }
66 Q_INVOKABLE inline QByteArray getQueryParam(const QByteArray& key){ return GetQueryParam(key); }
67 Q_INVOKABLE inline QByteArray takeQueryParam(const QByteArray& key){ return TakeQueryParam(key); }
68 Q_INVOKABLE inline QMap<QByteArray, QByteArray>& getQueryParams(){ return GetQueryParams(); }
69 Q_INVOKABLE inline void clearQueryParams(const QByteArray& key){ ClearQueryParams(key); }
70 Q_INVOKABLE inline void setUpdateEnabled(bool updateEnabled){ SetUpdateEnabled(updateEnabled); }
71 Q_INVOKABLE inline void refresh(){ Refresh(); }
72 Q_INVOKABLE inline QString toJson(){ return ToJson(); }
73
74 explicit CTreeItemModel(QObject* parent = nullptr);
76
77 const QString& State() const;
78 void SetState(const QString& newState);
79
80 virtual bool SerializeModel(iser::IArchive& archive);
81
82 bool BeginChanges();
83 bool EndChanges();
84
85 void SetParent(QObject* parent);
86 bool Copy(const CTreeItemModel* object);
87 imtbase::CTreeItemModel* CopyMe() const;
88 bool IsEqualWithModel(CTreeItemModel* modelPtr) const;
89 bool IsEqualWithModel(const CTreeItemModel* modelPtr) const;
90 void InsertNewItemWithParameters(int index, const QVariantMap& map);
91 int InsertNewItem();
92 int InsertNewItem(int index);
93 int RemoveItem(int index);
94 bool SwapItems(int index1, int index2);
95 imtbase::CTreeItemModel* AddTreeModel(const QByteArray& key, int index = 0);
96 bool SetExternTreeModel(const QByteArray& key, CTreeItemModel* externTreeModel, int index = 0);
97 bool CopyItemDataFromModel(int index, const CTreeItemModel* externTreeModel, int externIndex = 0);
98 bool CopyItemDataFromModel(int index, CTreeItemModel* externTreeModel, int externIndex = 0);
99 bool CopyItemDataToModel(int index, CTreeItemModel* externTreeModel, int externIndex = 0) const;
100 bool SetData(const QByteArray&key, const QVariant& value, int index = 0);
101 bool RemoveData(const QByteArray& key, int index = 0);
102 QVariant GetData(const QByteArray& key, int index = 0) const;
103 imtbase::CTreeItemModel* GetParent() const;
104 bool IsTreeModel(const QByteArray& key, int index = 0) const;
105 bool ContainsKey(const QByteArray& key, int index = 0) const;
106 bool IsValidData(const QByteArray& key, int index = 0) const;
107 imtbase::CTreeItemModel* GetTreeItemModel(const QByteArray& key, int index = 0) const;
108 imtbase::CTreeItemModel* GetModelFromItem(int itemIndex = 0) const;
109 int GetItemsCount() const;
110 void GetKeys(QList<QByteArray>& keys, int index = 0) const;
111 QList<QString> GetKeys(int index = 0) const;
112 void Clear();
113 bool IsArray();
114 void SetIsArray(const bool& isArray);
115 bool CreateFromJson(const QByteArray& jsonContent);
116 void SetQueryParam(const QByteArray& key, const QByteArray& value);
117 QByteArray GetQueryParam(const QByteArray& key);
118 QByteArray TakeQueryParam(const QByteArray& key);
119 QMap<QByteArray, QByteArray>& GetQueryParams();
120 void ClearQueryParams(const QByteArray& key);
121 void SetUpdateEnabled(bool updateEnabled);
122 void Refresh();
123 QString ToJson();
124
125 // reimplemented (QAbstractListModel)
126 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
127 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
128 virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
129 virtual QHash<int, QByteArray> roleNames() const override;
130
131public Q_SLOTS:
132 void OnModelChanged();
133
134Q_SIGNALS:
135 void stateChanged(const QString& state);
136 void needsReload();
137 void modelChanged();
138
139protected:
140 bool SerializeRecursive(iser::IArchive& archive, const QByteArray& tagName);
141
142private:
143 class Item
144 {
145 public:
146 Item(){}
147 ~Item(){}
148
149 void SetValue(const QByteArray& key, const QVariant& value){
150 if (!m_map.contains(key)){
151 m_keys.append(key);
152 }
153 m_map.insert(key,value);
154 }
155 void RemoveValue(const QByteArray& key){
156 m_map.remove(key); m_keys.removeAll(key);
157 }
158 QVariant Value(const QByteArray& key){
159 QVariant res;
160 if(m_map.contains(key))
161 res = m_map.value(key);
162 return res;
163 }
164 void GetKeys(QList<QByteArray>& keys){ keys = m_keys; }
165 bool Contains(const QByteArray& key){ return m_map.contains(key); }
166 bool ContainsKey(const QByteArray& key){ return m_keys.contains(key); }
167 private:
168 QMap<QByteArray,QVariant> m_map;
169 QList<QByteArray> m_keys;
170 };
171
172 QList<Item*> m_items;
173 QHash<int, QByteArray> m_roleNames;
174 QMap<QByteArray,QByteArray> m_queryParams;
175 bool m_isArray;
176
177 QString m_state;
178 bool m_isUpdateEnabled;
179
180 int m_countTransaction;
181 int m_countChanges;
182
183protected:
184 virtual int GetKeyRole(const QByteArray& key) const;
185 virtual bool ParseRecursive(const QJsonObject& jsonObject, int index = 0);
186
187private:
188 inline int GetVariantTypeId(const QVariant& value);
189 bool SerializeVariantValue(iser::IArchive& archive, const QVariant& value);
190};
191
192
193typedef istd::TSmartPtr<imtbase::CTreeItemModel> CHierarchicalItemModelPtr;
194
195
196} // namespace imtbase
197
198
199Q_DECLARE_METATYPE(imtbase::CTreeItemModel*)
200
201