MT Showcase SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
WidgetComponent.hpp
1 #pragma once
2 
3 #include "OperatorComponent.hpp"
4 #include "Showcase.hpp"
5 #include "UIComponent.hpp"
6 #include "ui/PinnableWidget.hpp"
7 
8 #include <MultiWidgets/Plugins.hpp>
9 
10 namespace Showcase
11 {
12  class WidgetInstance;
13 
14  typedef std::shared_ptr<WidgetInstance> WidgetInstancePtr;
15  typedef std::weak_ptr<WidgetInstance> WidgetInstanceWeakPtr;
16 
18  class SHOWCASE_API WidgetInstance : public UIComponentInstance
19  {
20  public:
21  typedef QPair<MultiWidgets::WidgetWeakPtr, WidgetInstanceWeakPtr> Source;
22  typedef QMap<Valuable::Node::Uuid, Source> Sources;
23  typedef QMap<QByteArray, Sources> EventSources;
24  typedef std::function<void(QByteArray, WidgetInstance&, MultiWidgets::Widget&)> EventFoundFunc;
25  typedef std::function<void(QByteArray, MultiWidgets::Widget&)> EventLostFunc;
26 
32  WidgetInstance(ComponentPtr comp, MultiWidgets::WidgetPtr instance);
33  virtual ~WidgetInstance();
34 
35  virtual void removeInstance() override;
36 
37  void init();
38 
39  virtual MultiWidgets::WidgetPtr widgetInstance() override;
40  MultiWidgets::WidgetPtr widgetInstanceWeak();
41 
42  virtual void propertyReceived(const PropertyKey &key,
43  const PropertyValue &value) override;
44  virtual void propertyLost(const PropertyKey &key,
45  const PropertyValue &value) override;
46 
50  virtual void componentAddedToSlot(const QByteArray& slotName,
51  std::shared_ptr<Component> comp) override;
53  MultiWidgets::WidgetPtr createInstanceWidget(const QByteArray& slotName,
54  std::shared_ptr<Component> comp,
55  bool addToThisWidget = true);
58  virtual void componentRemovedFromSlot(const QByteArray& slotName,
59  std::shared_ptr<Component> comp) override;
60 
61  virtual void operatorFound(const QByteArray& slotName,
62  std::shared_ptr<OperatorComponent> comp);
63  virtual void operatorLost(const QByteArray& slotName,
64  std::shared_ptr<OperatorComponent> comp);
65 
66  //-------------- Event passing functions ---------------------------------
67 
69  void setupEvents(WidgetInstance & instance, MultiWidgets::Widget & source);
71  void removeEvents(WidgetInstance & instance, MultiWidgets::Widget & source);
72 
73  void eventFound(const QByteArray & event, WidgetInstance & instance,
74  MultiWidgets::Widget & source, EventDirection type);
75  void eventLost(const QByteArray & event, MultiWidgets::Widget & source,
76  EventDirection type);
77 
78  void setOnEventFound(EventFoundFunc && func,EventDirection type);
79  void setOnEventLost(EventLostFunc && func,EventDirection type);
80 
81  Events allEvents(EventDirection type) const;
82  Events localEvents(EventDirection type) const;
83 
84  bool passEvents(EventDirection type) const;
85  bool isLocalEvent(const QByteArray & event, EventDirection type) const;
86 
88  static void setupEvents(MultiWidgets::Widget & host,
89  MultiWidgets::Widget & child);
90 
91  private:
92  void setupEvents(WidgetInstance & instance, MultiWidgets::Widget & source,
93  EventDirection type);
94  void removeEvents(WidgetInstance & instance, MultiWidgets::Widget & source,
95  EventDirection type);
96 
97  void applyProperty(MultiWidgets::WidgetPtr w, const PropertyKey & key,
98  const PropertyValue & value);
99  void clearProperty(MultiWidgets::WidgetPtr w, const PropertyKey & key,
100  const PropertyValue & value);
101 
102  MultiWidgets::WidgetWeakPtr m_instanceWidget;
103  Valuable::Node::Uuid m_instanceWidgetId;
104  MultiWidgets::WidgetPtr m_strongRef;
105 
106  EventSources m_externEventsIn;
107  EventSources m_externEventsOut;
108 
109  EventFoundFunc m_onEventInFound;
110  EventFoundFunc m_onEventOutFound;
111 
112  EventLostFunc m_onEventInLost;
113  EventLostFunc m_onEventOutLost;
114 
115  std::set<QByteArray> m_lazyAttributes;
116  };
117 
118  //------------------------------------------------------------------------
119 
121  class SHOWCASE_API WidgetComponent : public UIComponent
122  {
123  public:
124  WidgetComponent(std::function<MultiWidgets::WidgetPtr(void)> create);
125 
126  MultiWidgets::WidgetPtr createWidget();
127 
128  protected:
129  virtual ComponentInstancePtr instanceImpl() override;
130 
131  private:
132  std::function<MultiWidgets::WidgetPtr(void)> m_createFunc;
133  };
134 
135  // ------------------------------------------------------------------
136 
140  class SHOWCASE_API CustomWidgetInstanceComponent : public WidgetComponent
141  {
142  public:
143  typedef std::function<WidgetInstancePtr(ComponentPtr, MultiWidgets::WidgetPtr)> InstanceFunc;
144 
145  CustomWidgetInstanceComponent(std::function<MultiWidgets::WidgetPtr(void)> createWidget,
146  InstanceFunc createInst);
147 
148  protected:
149  virtual ComponentInstancePtr instanceImpl() override;
150 
151  private:
152  std::function<MultiWidgets::WidgetPtr(void)> m_createFunc;
153  InstanceFunc m_createInstanceFunc;
154  };
155 
156 }
157 
158 #define EXPORT_CORNERSTONE_PLUGIN(name, pluginName) \
159 namespace {\
160  Showcase::FactoryRegistrant<Showcase::UIComponent> MW_PLUGIN_CONCAT(component, __LINE__) \
161  (name, [] { return std::make_shared<Showcase::WidgetComponent>(\
162  [] { return MultiWidgets::Plugins::instance()->createPluginWidget(pluginName); } ); }); \
163 }
164 
165 #define SHOWCASE_WIDGET_COMPONENT(name, type) \
166 namespace {\
167 Showcase::FactoryRegistrant<Showcase::WidgetComponent> MW_PLUGIN_CONCAT(component, __LINE__) \
168 (name, [] { return std::make_shared<Showcase::WidgetComponent>(\
169  [] { return MultiWidgets::create<type>(); } ); }); \
170 }
171 
172 #define SHOWCASE_CUSTOM_WIDGET_INSTANCE_COMPONENT(name, type, instanceType) \
173 namespace {\
174 Showcase::FactoryRegistrant<Showcase::CustomWidgetInstanceComponent> MW_PLUGIN_CONCAT(component, __LINE__) \
175 (name, [] { return std::make_shared<Showcase::CustomWidgetInstanceComponent>(\
176  [] { return MultiWidgets::create<type>(); },\
177  [] (Showcase::ComponentPtr c, MultiWidgets::WidgetPtr w){\
178  return std::make_shared<instanceType>(c,w);\
179  });\
180  });\
181 }
182 
183 inline Radiant::IntrusivePtr<Showcase::PinnableWidget> createPinnableWidgetFromPlugin(const QString & pluginName)
184 {
185  auto child = MultiWidgets::Plugins::instance()->createPluginWidget(pluginName);
186  if (!child)
187  return nullptr;
188 
189  return MultiWidgets::create<Showcase::PinnableWidget>(child);
190 }
191 
192 #define EXPORT_CORNERSTONE_PLUGIN_AS_PINNABLE(name, pluginName) \
193 namespace {\
194  Showcase::FactoryRegistrant<Showcase::UIComponent> MW_PLUGIN_CONCAT(component, __LINE__) \
195  (name, [] { return std::make_shared<Showcase::WidgetComponent>(\
196  [] { return createPinnableWidgetFromPlugin(pluginName); });\
197  }); \
198 }
199 
200 #define SHOWCASE_WIDGET_COMPONENT_AS_PINNABLE(name, type) \
201 namespace {\
202  Showcase::FactoryRegistrant<Showcase::UIComponent> MW_PLUGIN_CONCAT(component, __LINE__) \
203  (name, [] { return std::make_shared<Showcase::WidgetComponent>(\
204  [] { return MultiWidgets::create<Showcase::PinnableWidget>(MultiWidgets::create<type>()); });\
205  });\
206 }