Salome HOME
Issues #2027, #2024, #2063, #2067: reentrant message to fill new operation by result...
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "ModelAPI_IReentrant.h"
4 #include "ModelAPI_EventReentrantMessage.h"
5
6 #include "ModuleBase_IModule.h"
7 #include "ModuleBase_IViewer.h"
8 #include "ModuleBase_ViewerPrs.h"
9 #include "ModuleBase_Operation.h"
10 #include "ModuleBase_IPropertyPanel.h"
11 #include "ModuleBase_ISelection.h"
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_OperationFeature.h"
14 #include "ModuleBase_ModelWidget.h"
15 #include "ModuleBase_WidgetFactory.h"
16 #include "ModuleBase_PageWidget.h"
17 #include "ModuleBase_Dialog.h"
18 #include "ModuleBase_IErrorMgr.h"
19
20 #include <Events_Loop.h>
21 #include <Events_Message.h>
22
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <ModelAPI_Session.h>
26 #include "ModelAPI_Tools.h"
27
28 #include <Config_PointerMessage.h>
29 #include <Config_WidgetReader.h>
30 #include <Config_ModuleReader.h>
31
32 #include <QAction>
33 #include <QMainWindow>
34 #include <QDialog>
35 #include <QLayout>
36 #include <QDialogButtonBox>
37 #include <QPushButton>
38
39 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
40   : QObject(theParent), myWorkshop(theParent)
41 {
42   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
43
44
45   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
46   //        SLOT(onMousePressed(QMouseEvent*)));
47   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
48   //        SLOT(onMouseReleased(QMouseEvent*)));
49   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
50   //        SLOT(onMouseMoved(QMouseEvent*)));
51   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
52   //        SLOT(onKeyRelease(QKeyEvent*)));
53   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
54   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
55 }
56
57 void ModuleBase_IModule::launchModal(const QString& theCmdId)
58 {
59   if (!myWorkshop->canStartOperation(theCmdId))
60     return;
61
62   std::string aXmlCfg, aDescription;
63   getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
64
65   SessionPtr aMgr = ModelAPI_Session::get();
66   aMgr->startOperation(theCmdId.toStdString());
67
68   ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
69   if (aDlg.exec() == QDialog::Accepted)
70     aMgr->finishOperation();
71   else
72     aMgr->abortOperation();
73   myWorkshop->updateCommandStatus();
74 }
75
76
77 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
78 {
79   /// selection should be obtained from workshop before ask if the operation can be started as
80   /// the canStartOperation method performs commit/abort of previous operation.
81   /// Sometimes commit/abort may cause selection clear(Sketch operation) as a result
82   /// it will be lost and is not used for preselection.
83   ModuleBase_ISelection* aSelection = myWorkshop->selection();
84   QList<ModuleBase_ViewerPrsPtr> aPreSelected =
85     aSelection->getSelected(ModuleBase_ISelection::AllControls);
86
87   if (!myWorkshop->canStartOperation(theCmdId))
88     return;
89
90   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
91                                              (createOperation(theCmdId.toStdString()));
92   if (aFOperation) {
93     std::shared_ptr<Events_Message> aMessage = reentrantMessage();
94     if (aMessage.get()) {
95       setReentrantPreSelection(aMessage);
96     }
97     else
98       aFOperation->initSelection(aPreSelected);
99
100     workshop()->processLaunchOperation(aFOperation);
101
102     if (aFOperation) {
103       FeaturePtr aFeature = aFOperation->feature();
104       ModelReentrantPtr aReentrantFeature =
105                                       std::dynamic_pointer_cast<ModelAPI_IReentrant>(aFeature);
106       if (aReentrantFeature.get()) {
107         if (aMessage.get()) {
108           ModuleBase_IPropertyPanel* aPanel = workshop()->propertyPanel();
109           std::string aPrevAttribute = aReentrantFeature->processEvent(aMessage);
110           workshop()->errorMgr()->updateActions(aFeature);
111
112           ModuleBase_ModelWidget* aPrevWidget = aPanel->modelWidget(aPrevAttribute);
113           aPanel->activateNextWidget(aPrevWidget);
114         }
115       }
116     }
117   }
118 }
119
120 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
121 {
122   return Handle(AIS_InteractiveObject)();
123 }
124
125 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
126 {
127   return true;
128 }
129
130 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
131 {
132   // Error already translated.
133   std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
134   return QString::fromUtf8(aMsg.c_str());
135 }
136
137 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
138                                              QStringList& theIds) const
139 {
140 }
141
142 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
143 {
144   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
145 }
146
147 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
148                               const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
149                               const bool theUpdateViewer)
150 {
151   return false;
152 }
153
154 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
155 {
156   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
157                                                           (getNewOperation(theFeatureId));
158   // If the operation is launched as sub-operation of another then we have to initialize
159   // parent feature
160   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
161                                                          (myWorkshop->currentOperation());
162   if (aCurOperation) {
163     FeaturePtr aFeature = aCurOperation->feature();
164     CompositeFeaturePtr aCompFeature =
165         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
166     if (aCompFeature) {
167       aFOperation->setParentFeature(aCompFeature);
168     }
169   }
170
171   std::string aXmlCfg, aDescription;
172   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
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   registerProperties();
183
184   Config_ModuleReader aXMLReader = Config_ModuleReader();
185   aXMLReader.readAll();
186   myFeaturesInFiles = aXMLReader.featuresInFiles();
187 }
188
189
190 void ModuleBase_IModule::actionCreated(QAction* theFeature)
191 {
192   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
193 }
194
195 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
196 {
197   return true;
198 }
199
200 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
201 {
202   return true;
203 }
204
205 bool ModuleBase_IModule::canUndo() const
206 {
207   SessionPtr aMgr = ModelAPI_Session::get();
208   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
209 }
210
211 bool ModuleBase_IModule::canRedo() const
212 {
213   SessionPtr aMgr = ModelAPI_Session::get();
214   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
215 }
216
217 void ModuleBase_IModule::onFeatureTriggered()
218 {
219   QAction* aCmd = dynamic_cast<QAction*>(sender());
220   //Do nothing on uncheck
221   if (aCmd->isCheckable() && !aCmd->isChecked()) {
222     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
223     if (myWorkshop->canStopOperation(anOperation))
224       myWorkshop->stopOperation(anOperation);
225     else {
226       aCmd->setChecked(true);
227     }
228   }
229   else {
230     QString aCmdId = aCmd->data().toString();
231     std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
232     if (aInfo.get() && aInfo->isModal()) {
233       launchModal(aCmdId);
234     } else {
235       launchOperation(aCmdId);
236       emit operationLaunched();
237     }
238   }
239 }
240
241 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
242 {
243   std::string aFeatureId = theFeature->getKind();
244   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
245     return;
246
247   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
248                                                          (createOperation(aFeatureId));
249   if (aFOperation) {
250     aFOperation->setFeature(theFeature);
251     workshop()->processLaunchOperation(aFOperation);
252   }
253 }
254
255 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
256 {
257   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
258                                                      (myWorkshop->currentOperation());
259   return !aFOperation || !aFOperation->hasObject(theObject);
260 }
261
262 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
263 {
264   emit resumed(theOperation);
265 }
266
267 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
268                                               std::string& theXmlCfg, std::string& theDescription)
269 {
270   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
271   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
272   aWdgReader.readAll();
273
274   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
275   theDescription = aWdgReader.featureDescription(theFeatureId);
276 }