6#include <QtCore/QJsonDocument>
9#include <imod/TSingleModelObserverBase.h>
12#include <imtqml/CQmlModelEditorCompBase.h>
19template<
typename ModelInterface>
20class TQmlModelEditorCompBase:
21 public CQmlModelEditorCompBase,
22 public imod::TSingleModelObserverBase<ModelInterface>
25 typedef CQmlModelEditorCompBase BaseClass;
26 typedef imod::TSingleModelObserverBase<ModelInterface> BaseClass2;
28 I_BEGIN_BASE_COMPONENT(TQmlModelEditorCompBase)
31 TQmlModelEditorCompBase();
37 virtual QJsonDocument CreateRepresentationFromObject(
const ModelInterface&
object)
const = 0;
42 virtual void UpdateObjectFromRepresentation(
const QJsonDocument& representation, ModelInterface&
object)
const = 0;
45 virtual void OnUpdate(
const istd::IChangeable::ChangeSet& changeSet)
override;
48 virtual void OnGuiCreated()
override;
54 explicit UpdateBlocker(
const TQmlModelEditorCompBase<ModelInterface>* parentPtr);
58 const TQmlModelEditorCompBase& m_parent;
62 bool IsUpdateBlocked()
const;
65 virtual void OnRepresentationChanged()
override;
68 bool m_isUpdatePending =
false;
69 mutable int m_ignoreUpdatesCounter = 0;
75template<
typename ModelInterface>
76TQmlModelEditorCompBase<ModelInterface>::TQmlModelEditorCompBase()
85template<
typename ModelInterface>
86inline void TQmlModelEditorCompBase<ModelInterface>::OnUpdate(
const istd::IChangeable::ChangeSet& )
88 if (IsUpdateBlocked()){
92 UpdateBlocker blocker(
this);
94 ModelInterface* objectPtr = BaseClass2::GetObservedObject();
95 Q_ASSERT(objectPtr !=
nullptr);
97 QQuickItem* quickItemPtr = m_quickWidget->rootObject();
98 if (quickItemPtr !=
nullptr){
99 QJsonDocument jsonDoc = CreateRepresentationFromObject(*objectPtr);
100 if (jsonDoc.isObject()){
101 QString jsonRepresentation = QString::fromUtf8(jsonDoc.toJson(QJsonDocument::Compact));
103 QMetaObject::invokeMethod(
104 quickItemPtr,
"setRepresentation", Q_ARG(QVariant, QVariant::fromValue(jsonRepresentation)));
108 m_isUpdatePending =
true;
115template<
typename ModelInterface>
116inline void TQmlModelEditorCompBase<ModelInterface>::OnGuiCreated()
118 BaseClass::OnGuiCreated();
120 if (m_isUpdatePending){
121 m_isUpdatePending =
false;
123 OnUpdate(istd::IChangeable::GetAnyChange());
130template<
typename ModelInterface>
131inline bool TQmlModelEditorCompBase<ModelInterface>::IsUpdateBlocked()
const
133 return (m_ignoreUpdatesCounter > 0);
139template<
typename ModelInterface>
140inline void TQmlModelEditorCompBase<ModelInterface>::OnRepresentationChanged()
142 if (IsUpdateBlocked()){
146 UpdateBlocker blocker(
this);
148 ModelInterface* objectPtr = BaseClass2::GetObservedObject();
149 Q_ASSERT(objectPtr !=
nullptr);
151 QQuickItem* quickItemPtr = m_quickWidget->rootObject();
152 if (quickItemPtr !=
nullptr){
154 if (QMetaObject::invokeMethod(
155 quickItemPtr,
"getRepresentation", Qt::DirectConnection, Q_RETURN_ARG(QVariant, var)))
157 if (var.typeId() == QMetaType::QString){
158 QString jsonRepresentation = var.toString();
160 QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonRepresentation.toUtf8());
162 UpdateObjectFromRepresentation(jsonDocument, *objectPtr);
171template<
typename ModelInterface>
172TQmlModelEditorCompBase<ModelInterface>::UpdateBlocker::UpdateBlocker(
const TQmlModelEditorCompBase<ModelInterface>* parentPtr)
173 :m_parent(*parentPtr)
175 ++m_parent.m_ignoreUpdatesCounter;
179template<
typename ModelInterface>
180TQmlModelEditorCompBase<ModelInterface>::UpdateBlocker::~UpdateBlocker()
182 --m_parent.m_ignoreUpdatesCounter;