ImagingTools Core SDK
TModelUpdateBinder.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/QtGlobal>
7
8// ACF includes
9#include <imod/CMultiModelDispatcherBase.h>
10
11
12namespace imtbase
13{
14
15
58template <typename ModelInterface, typename Parent>
59class TModelUpdateBinder: protected imod::CMultiModelDispatcherBase
60{
61public:
62 typedef void (Parent::*CallbackMethod)(const istd::IChangeable::ChangeSet& changeSet, const ModelInterface* objectPtr);
63 typedef imod::CMultiModelDispatcherBase BaseClass;
64
65 explicit TModelUpdateBinder(Parent& parent);
66
67 bool RegisterObject(const ModelInterface* dataPtr, CallbackMethod callbackMethod, int modelId = 0);
68 bool RegisterObject(ModelInterface* dataPtr, CallbackMethod callbackMethod, int modelId = 0);
69 void UnregisterObject(int modelId);
70 void UnregisterAllObjects();
71
72 ModelInterface* GetObjectAt(int modelId) const;
73
74 // reimplemented (imod::CMultiModelDispatcherBase)
75 virtual void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet& changeSet) override;
76
77private:
78 Parent& m_parent;
79
80 typedef QMap<int, CallbackMethod> CallbackMap;
81 CallbackMap m_callbackMap;
82};
83
84
85template <typename ModelInterface, typename Parent>
87 :m_parent(parent)
88{
89}
90
91
92template <typename ModelInterface, typename Parent>
93bool TModelUpdateBinder<ModelInterface, Parent>::RegisterObject(const ModelInterface* dataPtr, CallbackMethod callbackMethod, int modelId)
94{
95 return RegisterObject(const_cast<ModelInterface*>(dataPtr), callbackMethod, modelId);
96}
97
98
99template <typename ModelInterface, typename Parent>
100bool TModelUpdateBinder<ModelInterface, Parent>::RegisterObject(ModelInterface* dataPtr, CallbackMethod callbackMethod, int modelId)
101{
102 auto modelPtr = dynamic_cast<imod::IModel*>(dataPtr);
103 if (modelPtr != nullptr){
104 m_callbackMap[modelId] = callbackMethod;
105 if (BaseClass::RegisterModel(modelPtr, modelId)){
106 return true;
107 }
108
109 m_callbackMap.remove(modelId);
110 }
111
112 return false;
113}
114
115
116template<typename ModelInterface, typename Parent>
117void TModelUpdateBinder<ModelInterface, Parent>::UnregisterObject(int modelId)
118{
119 BaseClass::UnregisterModel(modelId);
120
121 m_callbackMap.remove(modelId);
122}
123
124
125template<typename ModelInterface, typename Parent>
126void TModelUpdateBinder<ModelInterface, Parent>::UnregisterAllObjects()
127{
128 BaseClass::UnregisterAllModels();
129
130 m_callbackMap.clear();
131}
132
133
134template<typename ModelInterface, typename Parent>
135ModelInterface* TModelUpdateBinder<ModelInterface, Parent>::GetObjectAt(int modelId) const
136{
137 return BaseClass::GetObjectAt<ModelInterface>(modelId);
138}
139
140
141// reimplemented (imod::CMultiModelDispatcherBase)
142
143template <typename ModelInterface, typename Parent>
144void TModelUpdateBinder<ModelInterface, Parent>::OnModelChanged(int modelId, const istd::IChangeable::ChangeSet& changeSet)
145{
146 if (m_callbackMap.contains(modelId)){
147 const CallbackMethod& method = m_callbackMap[modelId];
148
149 (m_parent.*method)(changeSet, GetObjectAt(modelId));
150 }
151}
152
153
154} // namespace imtbase
155