Salome HOME
Construction of vertices/edges/faces on the base of sketch: a special group box in...
[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 (!myCreators.contains(aKey))
40       myCreators[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   const std::set<std::string>& aPTypes = theCreator->pageTypes();
48   for (anIt = aPTypes.begin(), aLast = aPTypes.end(); anIt != aLast; anIt++) {
49     std::string aKey = *anIt;
50     if (!myPageToCreator.contains(aKey))
51       myPageToCreator[aKey] = theCreator;
52     else {
53       Events_Error::send("The" + aKey + " page XML definition has been already \
54 used by another widget creator");
55     }
56   }
57 }
58
59 bool ModuleBase_WidgetCreatorFactory::hasPageWidget(const std::string& theType)
60 {
61   return myPageToCreator.contains(theType);
62 }
63
64 ModuleBase_PageBase* ModuleBase_WidgetCreatorFactory::createPageByType(
65                               const std::string& theType, QWidget* theParent)
66 {
67   ModuleBase_PageBase* aPage = 0;
68
69   if (myPageToCreator.contains(theType)) {
70     WidgetCreatorPtr aCreator = myPageToCreator[theType];
71     aPage = aCreator->createPageByType(theType, theParent);
72   }
73
74   return aPage;
75 }
76
77
78 ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType(
79                               const std::string& theType, QWidget* theParent)
80 {
81   ModuleBase_ModelWidget* aWidget = 0;
82
83   if (myCreators.contains(theType)) {
84     WidgetCreatorPtr aCreator = myCreators[theType];
85     aWidget = aCreator->createWidgetByType(theType, theParent);
86   }
87
88   return aWidget;
89 }