]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetCreatorFactory.cpp
Salome HOME
Issue #1368: Creation of a Qt panel. Widget creator interface correction to manage...
[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   std::set<std::string>::const_iterator anIt, aLast;
38   /// fill map of panels
39   const std::set<std::string>& aPanelTypes = theCreator->panelTypes();
40   for (anIt = aPanelTypes.begin(), aLast = aPanelTypes.end(); anIt != aLast; anIt++) {
41     std::string aKey = *anIt;
42     if (!myPanelToCreator.contains(aKey))
43       myPanelToCreator[aKey] = theCreator;
44     else {
45       Events_Error::send("The" + aKey + " panel XML definition has been already \
46 used by another widget creator");
47     }
48   }
49
50   /// fill map of widgets
51   const std::set<std::string>& aTypes = theCreator->widgetTypes();
52   for (anIt = aTypes.begin(), aLast = aTypes.end(); anIt != aLast; anIt++) {
53     std::string aKey = *anIt;
54     if (!myCreators.contains(aKey))
55       myCreators[aKey] = theCreator;
56     else {
57       Events_Error::send("The" + aKey + " widget XML definition has been already \
58 used by another widget creator");
59     }
60   }
61
62   /// fill map of pages
63   const std::set<std::string>& aPTypes = theCreator->pageTypes();
64   for (anIt = aPTypes.begin(), aLast = aPTypes.end(); anIt != aLast; anIt++) {
65     std::string aKey = *anIt;
66     if (!myPageToCreator.contains(aKey))
67       myPageToCreator[aKey] = theCreator;
68     else {
69       Events_Error::send("The" + aKey + " page XML definition has been already \
70 used by another widget creator");
71     }
72   }
73 }
74
75 bool ModuleBase_WidgetCreatorFactory::hasPanelWidget(const std::string& theType)
76 {
77   return myPanelToCreator.contains(theType);
78 }
79
80 QWidget* ModuleBase_WidgetCreatorFactory::createPanel(const std::string& theType, QWidget* theParent)
81 {
82   QWidget* aPanel = 0;
83   if (myPanelToCreator.contains(theType)) {
84     WidgetCreatorPtr aCreator = myPanelToCreator[theType];
85     aPanel = aCreator->createPanelByType(theType, theParent);
86   }
87   return aPanel;
88 }
89
90 bool ModuleBase_WidgetCreatorFactory::hasPageWidget(const std::string& theType)
91 {
92   return myPageToCreator.contains(theType);
93 }
94
95 ModuleBase_PageBase* ModuleBase_WidgetCreatorFactory::createPageByType(
96                                           const std::string& theType, QWidget* theParent,
97                                           Config_WidgetAPI* theWidgetApi)
98 {
99   ModuleBase_PageBase* aPage = 0;
100
101   if (myPageToCreator.contains(theType)) {
102     WidgetCreatorPtr aCreator = myPageToCreator[theType];
103     aPage = aCreator->createPageByType(theType, theParent, theWidgetApi);
104   }
105
106   return aPage;
107 }
108
109
110 ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType(
111                                                 const std::string& theType, QWidget* theParent,
112                                                 Config_WidgetAPI* theWidgetApi,
113                                                 ModuleBase_IWorkshop* theWorkshop)
114 {
115   ModuleBase_ModelWidget* aWidget = 0;
116
117   if (myCreators.contains(theType)) {
118     WidgetCreatorPtr aCreator = myCreators[theType];
119     aWidget = aCreator->createWidgetByType(theType, theParent, theWidgetApi, theWorkshop);
120   }
121
122   return aWidget;
123 }