Salome HOME
Correct compilation on Linux.
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "ModuleBase_IModule.h"
5 #include "ModuleBase_IViewer.h"
6 #include "ModuleBase_ViewerPrs.h"
7 #include "ModuleBase_Operation.h"
8 #include "ModuleBase_ISelection.h"
9 #include "ModuleBase_OperationDescription.h"
10 #include "ModuleBase_OperationFeature.h"
11 #include "ModuleBase_ModelWidget.h"
12
13 #include <Events_Loop.h>
14
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <ModelAPI_Session.h>
19 #include "ModelAPI_Tools.h"
20
21 #include <Config_PointerMessage.h>
22 #include <Config_WidgetReader.h>
23 #include <Config_ModuleReader.h>
24
25 #include <QAction>
26
27 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
28   : QObject(theParent), myWorkshop(theParent) 
29 {
30   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
31
32
33   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
34   //        SLOT(onMousePressed(QMouseEvent*)));
35   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
36   //        SLOT(onMouseReleased(QMouseEvent*)));
37   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
38   //        SLOT(onMouseMoved(QMouseEvent*)));
39   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
40   //        SLOT(onKeyRelease(QKeyEvent*)));
41   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
42   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
43 }
44
45 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
46 {
47   if (!myWorkshop->canStartOperation(theCmdId))
48     return;
49
50   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
51                                              (createOperation(theCmdId.toStdString()));
52   if (aFOperation) {
53     ModuleBase_ISelection* aSelection = myWorkshop->selection();
54     // Initialise operation with preliminary selection
55     aFOperation->initSelection(aSelection, myWorkshop->viewer());
56     sendOperation(aFOperation);
57   }
58 }
59
60
61 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
62 {
63   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
64   std::shared_ptr<Config_PointerMessage> aMessage =
65       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
66   aMessage->setPointer(theOperation);
67   Events_Loop::loop()->send(aMessage);
68 }
69
70 const char* toString(ModelAPI_ExecState theExecState) 
71 {
72 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
73   switch (theExecState) {
74   TO_STRING(ModelAPI_StateDone)
75   TO_STRING(ModelAPI_StateMustBeUpdated)
76   TO_STRING(ModelAPI_StateExecFailed)
77   TO_STRING(ModelAPI_StateInvalidArgument)
78   TO_STRING(ModelAPI_StateNothing)
79   default: return "Unknown ExecState.";
80   }
81 #undef TO_STRING
82 }
83
84 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
85 {
86   return ModelAPI_Tools::getFeatureError(theFeature).c_str();
87 }
88
89 QString ModuleBase_IModule::getWidgetError(ModuleBase_ModelWidget* theWidget)
90 {
91   QString anError;
92
93   if (!theWidget || !theWidget->feature().get())
94     return anError;
95
96   std::string anAttributeID = theWidget->attributeID();
97   AttributePtr anAttribute = theWidget->feature()->attribute(anAttributeID);
98   if (!anAttribute.get())
99     return anError;
100
101   std::string aValidatorID;
102   std::string anErrorMsg;
103
104   static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
105   if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
106     if (anErrorMsg.empty())
107       anErrorMsg = "unknown error.";
108     anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
109   }
110
111   anError = QString::fromStdString(anErrorMsg);
112   if (anError.isEmpty())
113     anError = theWidget->getValueStateError();
114
115   return anError;
116 }
117
118 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
119                                              QStringList& theIds) const
120 {
121 }
122
123 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
124 {
125   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
126 }
127
128 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
129 {
130   return false;
131 }
132
133 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
134 {
135   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
136                                                           (getNewOperation(theFeatureId));
137   // If the operation is launched as sub-operation of another then we have to initialize
138   // parent feature
139   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
140                                                          (myWorkshop->currentOperation());
141   if (aCurOperation) {
142     FeaturePtr aFeature = aCurOperation->feature();
143     CompositeFeaturePtr aCompFeature =
144         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
145     if (aCompFeature) {
146       aFOperation->setParentFeature(aCompFeature);
147     }
148   }
149
150   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
151   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
152   aWdgReader.readAll();
153   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
154   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
155
156   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
157   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
158
159   return aFOperation;
160 }
161
162 void ModuleBase_IModule::createFeatures()
163 {
164   registerValidators();
165   registerFilters();
166   registerProperties();
167
168   Config_ModuleReader aXMLReader = Config_ModuleReader();
169   aXMLReader.readAll();
170   myFeaturesInFiles = aXMLReader.featuresInFiles();
171 }
172
173
174 void ModuleBase_IModule::actionCreated(QAction* theFeature)
175 {
176   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
177 }
178
179 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
180 {
181   return true;
182 }
183
184 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
185 {
186   return true;
187 }
188
189 bool ModuleBase_IModule::canUndo() const
190 {
191   SessionPtr aMgr = ModelAPI_Session::get();
192   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
193 }
194
195 bool ModuleBase_IModule::canRedo() const
196 {
197   SessionPtr aMgr = ModelAPI_Session::get();
198   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
199 }
200
201 void ModuleBase_IModule::onFeatureTriggered()
202 {
203   QAction* aCmd = dynamic_cast<QAction*>(sender());
204   //Do nothing on uncheck
205   if (aCmd->isCheckable() && !aCmd->isChecked()) {
206     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
207     if (myWorkshop->canStopOperation(anOperation))
208       myWorkshop->abortOperation(anOperation);
209     else {
210       aCmd->setChecked(true);
211     }
212   }
213   else {
214     launchOperation(aCmd->data().toString());
215     emit operationLaunched();
216   }
217 }
218
219 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
220 {
221   std::string aFeatureId = theFeature->getKind();
222   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
223     return;
224
225   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
226                                                          (createOperation(aFeatureId));
227   if (aFOperation) {
228     aFOperation->setFeature(theFeature);
229     sendOperation(aFOperation);
230   }
231 }
232
233 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
234 {
235   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
236                                                      (myWorkshop->currentOperation());
237   return !aFOperation || !aFOperation->hasObject(theObject);
238 }
239
240 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation) 
241 {
242   emit resumed(theOperation);
243 }