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