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