Salome HOME
Copyright update 2022
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetCreatorFactory.cpp
1 // Copyright (C) 2014-2022  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <ModuleBase_WidgetCreatorFactory.h>
21 #include <ModuleBase_IWidgetCreator.h>
22
23 #include <Config_WidgetAPI.h>
24
25 #include <Events_InfoMessage.h>
26
27 #include <QStringList>
28
29 /// Factory instance that will be initialized by first getting, one per application
30 std::shared_ptr<ModuleBase_WidgetCreatorFactory> MY_WIDGET_CREATOR_FACTORY;
31
32 std::shared_ptr<ModuleBase_WidgetCreatorFactory> ModuleBase_WidgetCreatorFactory::get()
33 {
34   if (!MY_WIDGET_CREATOR_FACTORY) {
35     MY_WIDGET_CREATOR_FACTORY =
36       std::shared_ptr<ModuleBase_WidgetCreatorFactory>(new ModuleBase_WidgetCreatorFactory());
37   }
38   return MY_WIDGET_CREATOR_FACTORY;
39 }
40
41 ModuleBase_WidgetCreatorFactory::ModuleBase_WidgetCreatorFactory()
42 {
43 }
44
45 ModuleBase_WidgetCreatorFactory::~ModuleBase_WidgetCreatorFactory()
46 {
47 }
48
49 void ModuleBase_WidgetCreatorFactory::registerCreator(const WidgetCreatorPtr& theCreator)
50 {
51   std::set<std::string>::const_iterator anIt, aLast;
52   /// fill map of panels
53   std::set<std::string> aPanelTypes;
54   theCreator->panelTypes(aPanelTypes);
55   for (anIt = aPanelTypes.begin(), aLast = aPanelTypes.end(); anIt != aLast; anIt++) {
56     std::string aKey = *anIt;
57     if (!myPanelToCreator.contains(aKey))
58       myPanelToCreator[aKey] = theCreator;
59     else {
60       Events_InfoMessage("ModuleBase_WidgetCreatorFactory",
61         "The %1 panel XML definition has been already used by another widget creator")
62         .arg(aKey).send();
63     }
64   }
65
66   /// fill map of widgets
67   std::set<std::string> aTypes;
68   theCreator->widgetTypes(aTypes);
69   for (anIt = aTypes.begin(), aLast = aTypes.end(); anIt != aLast; anIt++) {
70     std::string aKey = *anIt;
71     if (!myCreators.contains(aKey))
72       myCreators[aKey] = theCreator;
73     else {
74       Events_InfoMessage("ModuleBase_WidgetCreatorFactory",
75         "The %1 widget XML definition has been already used by another widget creator")
76         .arg(aKey).send();
77     }
78   }
79
80   /// fill map of pages
81   std::set<std::string> aPTypes;
82   theCreator->pageTypes(aPTypes);
83   for (anIt = aPTypes.begin(), aLast = aPTypes.end(); anIt != aLast; anIt++) {
84     std::string aKey = *anIt;
85     if (!myPageToCreator.contains(aKey))
86       myPageToCreator[aKey] = theCreator;
87     else {
88       Events_InfoMessage("ModuleBase_WidgetCreatorFactory",
89         "The %1 page XML definition has been already used by another widget creator")
90         .arg(aKey).send();
91     }
92   }
93 }
94
95 bool ModuleBase_WidgetCreatorFactory::hasPanelWidget(const std::string& theType)
96 {
97   return myPanelToCreator.contains(theType);
98 }
99
100 QWidget* ModuleBase_WidgetCreatorFactory::createPanelByType(const std::string& theType,
101                                                             QWidget* theParent,
102                                                             const FeaturePtr& theFeature,
103                                                             Config_WidgetAPI* myWidgetApi)
104 {
105   QWidget* aPanel = 0;
106   if (myPanelToCreator.contains(theType)) {
107     WidgetCreatorPtr aCreator = myPanelToCreator[theType];
108     aPanel = aCreator->createPanelByType(theType, theParent, theFeature, myWidgetApi);
109   }
110   return aPanel;
111 }
112
113 bool ModuleBase_WidgetCreatorFactory::hasPageWidget(const std::string& theType)
114 {
115   return myPageToCreator.contains(theType);
116 }
117
118 ModuleBase_PageBase* ModuleBase_WidgetCreatorFactory::createPageByType(
119                                           const std::string& theType, QWidget* theParent,
120                                           Config_WidgetAPI* theWidgetApi)
121 {
122   ModuleBase_PageBase* aPage = 0;
123
124   if (myPageToCreator.contains(theType)) {
125     WidgetCreatorPtr aCreator = myPageToCreator[theType];
126     aPage = aCreator->createPageByType(theType, theParent, theWidgetApi);
127   }
128
129   return aPage;
130 }
131
132
133 ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType(
134                                                 const std::string& theType, QWidget* theParent,
135                                                 Config_WidgetAPI* theWidgetApi,
136                                                 ModuleBase_IWorkshop* theWorkshop)
137 {
138   ModuleBase_ModelWidget* aWidget = 0;
139
140   if (myCreators.contains(theType)) {
141     WidgetCreatorPtr aCreator = myCreators[theType];
142     aWidget = aCreator->createWidgetByType(theType, theParent, theWidgetApi, theWorkshop);
143   }
144
145   return aWidget;
146 }