]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_WidgetCreatorFactory.cpp
Salome HOME
Provide On Plane filter and reading of validators for filters GUI definition
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetCreatorFactory.cpp
1 // Copyright (C) 2014-2019  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 {
104   QWidget* aPanel = 0;
105   if (myPanelToCreator.contains(theType)) {
106     WidgetCreatorPtr aCreator = myPanelToCreator[theType];
107     aPanel = aCreator->createPanelByType(theType, theParent, theFeature);
108   }
109   return aPanel;
110 }
111
112 bool ModuleBase_WidgetCreatorFactory::hasPageWidget(const std::string& theType)
113 {
114   return myPageToCreator.contains(theType);
115 }
116
117 ModuleBase_PageBase* ModuleBase_WidgetCreatorFactory::createPageByType(
118                                           const std::string& theType, QWidget* theParent,
119                                           Config_WidgetAPI* theWidgetApi)
120 {
121   ModuleBase_PageBase* aPage = 0;
122
123   if (myPageToCreator.contains(theType)) {
124     WidgetCreatorPtr aCreator = myPageToCreator[theType];
125     aPage = aCreator->createPageByType(theType, theParent, theWidgetApi);
126   }
127
128   return aPage;
129 }
130
131
132 ModuleBase_ModelWidget* ModuleBase_WidgetCreatorFactory::createWidgetByType(
133                                                 const std::string& theType, QWidget* theParent,
134                                                 Config_WidgetAPI* theWidgetApi,
135                                                 ModuleBase_IWorkshop* theWorkshop)
136 {
137   ModuleBase_ModelWidget* aWidget = 0;
138
139   if (myCreators.contains(theType)) {
140     WidgetCreatorPtr aCreator = myCreators[theType];
141     aWidget = aCreator->createWidgetByType(theType, theParent, theWidgetApi, theWorkshop);
142   }
143
144   return aWidget;
145 }