CanvasCV: CanvasCV-doxygen/src/canvascv/widgets/widgetfactory.h Source File
CanvasCV  1.0.0
widgetfactory.h
1 #ifndef WIDGETFACTORY_H
2 #define WIDGETFACTORY_H
3 
4 #include <opencv2/core.hpp>
5 #include <map>
6 #include <memory>
7 #include <functional>
8 #include "layout.h"
9 #include "canvascv/themes/themerepository.h"
10 
11 namespace canvascv
12 {
13 
14 class Widget;
15 class Layout;
16 
25 {
26 public:
37  static std::shared_ptr<Widget> newWidget(std::string type, Layout &layoutVal, const cv::Point &pos);
38 
39 protected:
40  typedef std::function<Widget*(const cv::Point &)> Allocator;
41  static void addWidget(std::string name, Allocator a);
42 
43 private:
44  template <class T> friend class WidgetFactoryT;
45 
46  typedef std::map<std::string,Allocator> AllocatorsMap;
47  static AllocatorsMap *allocators;
48 };
49 
57 template <class T>
59 {
60 public:
68  static bool addType(std::string name);
69 
79  static std::shared_ptr<T> newWidget(Layout &layoutVal, const cv::Point &pos = cv::Point(0,0));
80 };
81 
82 template <class T>
83 bool WidgetFactoryT<T>::addType(std::string name)
84 {
85  static bool doneOnce;
86  if (! doneOnce)
87  {
88  addWidget(name, [](const cv::Point& pos)->Widget*{return new T(pos);});
89  doneOnce = true;
90  }
91  return true;
92 }
93 
94 template <class T>
95 std::shared_ptr<T> WidgetFactoryT<T>::newWidget(Layout &layoutVal, const cv::Point &pos)
96 {
97  std::shared_ptr<T> widget(new T(pos));
99  layoutVal.addWidget(widget);
100  return widget;
101 }
102 
103 }
104 
105 #define CCV_REGISTER_WIDGET(X) static bool regWidget##X = canvascv::WidgetFactoryT<X>::addType(#X)
106 
107 
112 #endif // WIDGETFACTORY_H
The WidgetFactory class.
Definition: widgetfactory.h:24
static std::shared_ptr< Widget > newWidget(std::string type, Layout &layoutVal, const cv::Point &pos)
newWidget
static void applyCurrentTheme(Shape *shape)
used automatically by the ShapeFactory when creating a shape
This namespace holds all the classes of the CanvasCV library.
Definition: canvas.h:20
The Layout class.
Definition: layout.h:17
static std::shared_ptr< T > newWidget(Layout &layoutVal, const cv::Point &pos=cv::Point(0, 0))
newWidget
Definition: widgetfactory.h:95
static bool addType(std::string name)
addType adds type &#39;T&#39; under name &#39;name&#39;
Definition: widgetfactory.h:83
The WidgetFactoryT class.
Definition: widgetfactory.h:58
The Widget class.
Definition: widget.h:28
virtual void addWidget(const std::shared_ptr< Widget > &widget)=0
adds the widget to this Layout after removing it from it&#39;s previous layout