Salome HOME
3f2ff93079c0e633f0caf23126abbd48b14d3e29
[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 <Config_WidgetAPI.h>
11
12 #include <Events_Error.h>
13
14 #include <QStringList>
15
16 /// Factory instance that will be initialized by first getting, one per application
17 std::shared_ptr<ModuleBase_WidgetCreatorFactory> MY_WIDGET_CREATOR_FACTORY;
18
19 std::shared_ptr<ModuleBase_WidgetCreatorFactory> ModuleBase_WidgetCreatorFactory::get()
20 {
21   if (!MY_WIDGET_CREATOR_FACTORY) {
22     MY_WIDGET_CREATOR_FACTORY =  std::shared_ptr<ModuleBase_WidgetCreatorFactory>(new ModuleBase_WidgetCreatorFactory());
23   }
24   return MY_WIDGET_CREATOR_FACTORY;
25 }
26
27 ModuleBase_WidgetCreatorFactory::ModuleBase_WidgetCreatorFactory()
28 {
29 }
30
31 ModuleBase_WidgetCreatorFactory::~ModuleBase_WidgetCreatorFactory()
32 {
33 }
34
35 void ModuleBase_WidgetCreatorFactory::registerCreator(const WidgetCreatorPtr& theCreator)
36 {
37   const std::set<std::string>& aTypes = theCreator->widgetTypes();
38   std::set<std::string>::const_iterator anIt = aTypes.begin(), aLast = aTypes.end();
39   for (; anIt != aLast; anIt++) {
40     std::string aKey = *anIt;
41     if (!myCreators.contains(aKey))
42       myCreators[aKey] = theCreator;
43     else {
44       Events_Error::send("The" + aKey + " widget XML definition has been already \
45 used by another widget creator");
46     }
47   }
48
49   const std::set<std::string>& aPTypes = theCreator->pageTypes();
50   for (anIt = aPTypes.begin(), aLast = aPTypes.end(); anIt != aLast; anIt++) {
51     std::string aKey = *anIt;
52     if (!myPageToCreator.contains(aKey))
53       myPageToCreator[aKey] = theCreator;
54     else {
55       Events_Error::send("The" + aKey + " page XML definition has been already \
56 used by another widget creator");
57     }
58   }
59 }
60
61 bool ModuleBase_WidgetCreatorFactory::hasPageWidget(const std::string& theType)
62 {
63   return myPageToCreator.contains(theType);
64 }
65
66 ModuleBase_PageBase* ModuleBase_WidgetCreatorFactory::createPageByType(
67                                           const std::string& theType, QWidget* theParent,
68                                           Config_WidgetAPI* theWidgetApi)
69 {
70   ModuleBase_PageBase* aPage = 0;
71
72   if (myPageToCreator.contains(theType)) {
73     WidgetCreatorPtr aCreator = myPageToCreator[theType];
74     aPage = aCreator->createPageByType(theType, theParent, theWidgetApi);
75   }
76
77   return aPage;
78 }
79
80
81 ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType(
82                                                 const std::string& theType, QWidget* theParent,
83                                                 Config_WidgetAPI* theWidgetApi,
84                                                 ModuleBase_IWorkshop* theWorkshop)
85 {
86   ModuleBase_ModelWidget* aWidget = 0;
87
88   if (myCreators.contains(theType)) {
89     WidgetCreatorPtr aCreator = myCreators[theType];
90     aWidget = aCreator->createWidgetByType(theType, theParent, theWidgetApi, theWorkshop);
91   }
92
93   return aWidget;
94 }