MT Showcase SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Component.hpp
1 #pragma once
2 
3 #include "Export.hpp"
4 
5 #include "CachedGraph.hpp"
6 #include "CachedNode.hpp"
7 #include "Slot.hpp"
8 #include "Schema.hpp"
9 
10 #include "GraphTypes.hpp"
11 
12 #include <MultiWidgets/Widget.hpp>
13 
14 #include <QString>
15 #include <memory>
16 
17 namespace Showcase
18 {
19 
20  class ComponentInstance;
21  class Component;
22 
23  typedef std::shared_ptr<ComponentInstance> ComponentInstancePtr;
24  typedef std::list<ComponentInstancePtr>::iterator ComponentInstanceKey;
25 
26 
28  class SHOWCASE_API ComponentInstance : public std::enable_shared_from_this<ComponentInstance>
29  {
30  public:
33  ComponentInstance(std::shared_ptr<Component> component);
34 
37  virtual ~ComponentInstance();
38 
42 
46  virtual MultiWidgets::WidgetPtr widgetInstance();
47 
52  virtual MultiWidgets::OperatorPtr operatorInstance(MultiWidgets::Widget & host,
53  bool immediate = false);
54 
55  // Maybe for services
56  //virtual ServicePtr serviceInstance();
57 
62  std::shared_ptr<Component> creator() const;
63 
68  virtual void propertyReceived(const PropertyKey& key,
69  const PropertyValue& value);
70 
75  virtual void propertyLost(const PropertyKey& key,
76  const PropertyValue& value);
77 
78  protected:
82  virtual void removeInstance() = 0;
83 
88  void onDelete();
89 
90  virtual void setCache(CachedNode::Cache& cache);
91 
92  void setKey(ComponentInstanceKey key);
93 
94  private:
95  std::weak_ptr<Component> m_creator;
96  ComponentInstanceKey m_key;
97 
98  friend class Component;
99  };
100 
101  //------------------------------------------------------------------------
102 
103  class ComponentPlaceholder;
104  typedef std::shared_ptr<ComponentPlaceholder> ComponentPlaceholderPtr;
105 
112  class SHOWCASE_API Component : public std::enable_shared_from_this<Component>
113  {
114  public:
117  Component(CachedGraphPtr uiGraph=nullptr);
118  virtual ~Component();
119 
123  void setCachedGraph(CachedGraphPtr uiGraph);
124 
128  void init(const GraphNode& confNode);
129 
132  const QString& name() const;
133 
136  void setComponentName(const QString& name);
137 
140  void setSchema(const Schema& schema);
141 
144  const Schema& schema() const;
145 
150  ComponentInstancePtr createInstance();
151 
155  void instanceDestroyed(ComponentInstanceKey key);
156 
159  size_t numInstances() const;
160 
163  CachedGraphPtr cachedGraph() const;
164 
167  CachedNodePtr cachedNode() const;
168 
171  const NodeId& nodeId() const;
172 
175  const std::list<ComponentInstancePtr>& instances() const;
176 
177  protected:
178  void childFound(const NodeId& id);
179  void childLost(const NodeId& id);
180 
181  virtual void placeholderStatusChanged(const NodeId& id) = 0;
182 
183  void setConfigurationNode(const GraphNode& confNode);
184  void addInstance(ComponentInstancePtr instance);
185  virtual void propertyReceived(const Property& property);
186  virtual void propertyLost(const Property& property);
187 
188  virtual ComponentInstancePtr instanceImpl() = 0;
189 
190  ComponentPlaceholderPtr placeholder(const NodeId& id) const;
191  virtual void initPlaceholder(ComponentPlaceholderPtr placeholder);
192 
194  void applyForInstances(std::function<void(ComponentInstancePtr)>&& f);
195 
196  void removeInstances();
197 
198  private:
201  CachedGraphPtr m_cachedGraph;
202 
204  CachedNodePtr m_cachedNode;
205 
206  mutable std::mutex m_placeholderMutex;
207  std::unordered_map<NodeId, ComponentPlaceholderPtr> m_placeholders;
208 
209  QString m_name;
210  // is it needed here? it is assumed that this is write-once on the initialization
211  Schema m_schema;
212 
213  std::mutex m_instanceMutex;
214  std::list<ComponentInstancePtr> m_instances;
215 
216  friend class ComponentPlaceholder;
217  };
218  typedef std::shared_ptr<Component> ComponentPtr;
219 
220  //------------------------------------------------------------------------
221 
224  class ComponentPlaceholder : public std::enable_shared_from_this<ComponentPlaceholder>
225  {
226  public:
227  ComponentPlaceholder(const NodeId& id, std::weak_ptr<Component> parent);
228 
229  void init(CachedGraphPtr cgraph);
230  bool ready() const;
231 
232  void addRequiredProperty(const PropertyKey& propertyName);
233  PropertyValue propertyValue(const PropertyKey& key) const;
234 
235  void setOnReady(std::function<void(void)> func);
236 
237  NodeId targetId() const;
238 
239  private:
240  bool checkReady() const;
241 
242  void notify();
243  void onPropertyEvent(const PropertyEvent& event);
244  void onProperty(const Property& prop);
245 
246  StreamConsumerPtr<PropertyEvent> m_propertyConsumer;
247 
248  NodeId m_targetId;
249  std::weak_ptr<Component> m_parent;
250 
251  PropertyMap m_properties;
254  std::set<PropertyKey> m_requiredProperties;
255 
257  std::function<void(void)> m_onReady;
258 
259  bool m_ready;
260  };
261 
262 }