Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetCreatorFactory.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <ModuleBase_WidgetCreatorFactory.h>
22 #include <ModuleBase_IWidgetCreator.h>
23
24 #include <Config_WidgetAPI.h>
25
26 #include <Events_InfoMessage.h>
27
28 #include <QStringList>
29
30 /// Factory instance that will be initialized by first getting, one per application
31 std::shared_ptr<ModuleBase_WidgetCreatorFactory> MY_WIDGET_CREATOR_FACTORY;
32
33 std::shared_ptr<ModuleBase_WidgetCreatorFactory> ModuleBase_WidgetCreatorFactory::get()
34 {
35   if (!MY_WIDGET_CREATOR_FACTORY) {
36     MY_WIDGET_CREATOR_FACTORY =
37       std::shared_ptr<ModuleBase_WidgetCreatorFactory>(new ModuleBase_WidgetCreatorFactory());
38   }
39   return MY_WIDGET_CREATOR_FACTORY;
40 }
41
42 ModuleBase_WidgetCreatorFactory::ModuleBase_WidgetCreatorFactory()
43 {
44 }
45
46 ModuleBase_WidgetCreatorFactory::~ModuleBase_WidgetCreatorFactory()
47 {
48 }
49
50 void ModuleBase_WidgetCreatorFactory::registerCreator(const WidgetCreatorPtr& theCreator)
51 {
52   std::set<std::string>::const_iterator anIt, aLast;
53   /// fill map of panels
54   std::set<std::string> aPanelTypes;
55   theCreator->panelTypes(aPanelTypes);
56   for (anIt = aPanelTypes.begin(), aLast = aPanelTypes.end(); anIt != aLast; anIt++) {
57     std::string aKey = *anIt;
58     if (!myPanelToCreator.contains(aKey))
59       myPanelToCreator[aKey] = theCreator;
60     else {
61       Events_InfoMessage("ModuleBase_WidgetCreatorFactory",
62         "The %1 panel XML definition has been already used by another widget creator")
63         .arg(aKey).send();
64     }
65   }
66
67   /// fill map of widgets
68   std::set<std::string> aTypes;
69   theCreator->widgetTypes(aTypes);
70   for (anIt = aTypes.begin(), aLast = aTypes.end(); anIt != aLast; anIt++) {
71     std::string aKey = *anIt;
72     if (!myCreators.contains(aKey))
73       myCreators[aKey] = theCreator;
74     else {
75       Events_InfoMessage("ModuleBase_WidgetCreatorFactory",
76         "The %1 widget XML definition has been already used by another widget creator")
77         .arg(aKey).send();
78     }
79   }
80
81   /// fill map of pages
82   std::set<std::string> aPTypes;
83   theCreator->pageTypes(aPTypes);
84   for (anIt = aPTypes.begin(), aLast = aPTypes.end(); anIt != aLast; anIt++) {
85     std::string aKey = *anIt;
86     if (!myPageToCreator.contains(aKey))
87       myPageToCreator[aKey] = theCreator;
88     else {
89       Events_InfoMessage("ModuleBase_WidgetCreatorFactory",
90         "The %1 page XML definition has been already used by another widget creator")
91         .arg(aKey).send();
92     }
93   }
94 }
95
96 bool ModuleBase_WidgetCreatorFactory::hasPanelWidget(const std::string& theType)
97 {
98   return myPanelToCreator.contains(theType);
99 }
100
101 QWidget* ModuleBase_WidgetCreatorFactory::createPanelByType(const std::string& theType,
102                                                             QWidget* theParent,
103                                                             const FeaturePtr& theFeature)
104 {
105   QWidget* aPanel = 0;
106   if (myPanelToCreator.contains(theType)) {
107     WidgetCreatorPtr aCreator = myPanelToCreator[theType];
108     aPanel = aCreator->createPanelByType(theType, theParent, theFeature);
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 }