Salome HOME
Construction of vertices/edges/faces on the base of sketch: Widget Creator Factory
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetCreatorFactory.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        ModuleBase_WidgetCreatorFactory.cpp
4 // Created:     03 Dec 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #include <ModuleBase_WidgetCreatorFactory.h>
8 #include <ModuleBase_IWidgetCreator.h>
9
10 #include <Events_Error.h>
11
12 #include <QStringList>
13
14 /// Factory instance that will be initialized by first getting, one per application
15 std::shared_ptr<ModuleBase_WidgetCreatorFactory> MY_WIDGET_CREATOR_FACTORY;
16
17 std::shared_ptr<ModuleBase_WidgetCreatorFactory> ModuleBase_WidgetCreatorFactory::get()
18 {
19   if (!MY_WIDGET_CREATOR_FACTORY) {
20     MY_WIDGET_CREATOR_FACTORY =  std::shared_ptr<ModuleBase_WidgetCreatorFactory>(new ModuleBase_WidgetCreatorFactory());
21   }
22   return MY_WIDGET_CREATOR_FACTORY;
23 }
24
25 ModuleBase_WidgetCreatorFactory::ModuleBase_WidgetCreatorFactory()
26 {
27 }
28
29 ModuleBase_WidgetCreatorFactory::~ModuleBase_WidgetCreatorFactory()
30 {
31 }
32
33 void ModuleBase_WidgetCreatorFactory::registerCreator(const WidgetCreatorPtr& theCreator)
34 {
35   const std::set<std::string>& aTypes = theCreator->widgetTypes();
36   std::set<std::string>::const_iterator anIt = aTypes.begin(), aLast = aTypes.end();
37   for (; anIt != aLast; anIt++) {
38     std::string aKey = *anIt;
39     if (!myModelWidgets.contains(aKey))
40       myModelWidgets[aKey] = theCreator;
41     else {
42       Events_Error::send("The" + aKey + " widget XML definition has been already\
43                                         used by another widget creator");
44     }
45   }
46 }
47
48 ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType(
49                               const std::string& theType, QWidget* theParent)
50 {
51   ModuleBase_ModelWidget* aWidget = 0;
52
53   if (myModelWidgets.contains(theType)) {
54     WidgetCreatorPtr aCreator = myModelWidgets[theType];
55     aWidget = aCreator->createWidgetByType(theType, theParent);
56   }
57
58   return aWidget;
59 }