Salome HOME
39f9abc799b7e20de9e6ba9aa09aba247b15d782
[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   bool isCommitted;
60   if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
61     return;
62
63   std::string aXmlCfg, aDescription;
64   getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
65
66   SessionPtr aMgr = ModelAPI_Session::get();
67   aMgr->startOperation(theCmdId.toStdString());
68
69   ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
70   if (aDlg.exec() == QDialog::Accepted)
71     aMgr->finishOperation();
72   else
73     aMgr->abortOperation();
74   myWorkshop->updateCommandStatus();
75 }
76
77
78 void ModuleBase_IModule::launchOperation(const QString& theCmdId,
79                                          const bool& isStartAfterCommitOnly)
80 {
81   /// selection should be obtained from workshop before ask if the operation can be started as
82   /// the canStartOperation method performs commit/abort of previous operation.
83   /// Sometimes commit/abort may cause selection clear(Sketch operation) as a result
84   /// it will be lost and is not used for preselection.
85   ModuleBase_ISelection* aSelection = myWorkshop->selection();
86   QList<ModuleBase_ViewerPrsPtr> aPreSelected =
87     aSelection->getSelected(ModuleBase_ISelection::AllControls);
88
89   bool isCommitted;
90   if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
91     return;
92
93   /// reentrant operation(Sketch Line) should not be started if operation is aborted
94   if (isStartAfterCommitOnly && !isCommitted)
95     return;
96
97   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
98                                              (createOperation(theCmdId.toStdString()));
99   if (aFOperation) {
100     std::shared_ptr<Events_Message> aMessage = reentrantMessage();
101     if (aMessage.get()) {
102       setReentrantPreSelection(aMessage);
103     }
104     else
105       aFOperation->initSelection(aPreSelected);
106
107     workshop()->processLaunchOperation(aFOperation);
108
109     if (aFOperation) {
110       FeaturePtr aFeature = aFOperation->feature();
111       ModelReentrantPtr aReentrantFeature =
112                                       std::dynamic_pointer_cast<ModelAPI_IReentrant>(aFeature);
113       if (aReentrantFeature.get()) {
114         if (aMessage.get()) {
115           ModuleBase_IPropertyPanel* aPanel = workshop()->propertyPanel();
116           std::string aPrevAttribute = aReentrantFeature->processEvent(aMessage);
117           workshop()->errorMgr()->updateActions(aFeature);
118
119           ModuleBase_ModelWidget* aPrevWidget = aPanel->modelWidget(aPrevAttribute);
120           aPanel->activateNextWidget(aPrevWidget);
121         }
122       }
123     }
124   }
125 }
126
127 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
128 {
129   return Handle(AIS_InteractiveObject)();
130 }
131
132 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
133 {
134   return true;
135 }
136
137 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
138 {
139   // Error already translated.
140   std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
141   return QString::fromUtf8(aMsg.c_str());
142 }
143
144 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
145                                              QStringList& theIds) const
146 {
147 }
148
149 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
150 {
151   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
152 }
153
154 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
155                               const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
156                               const bool theUpdateViewer)
157 {
158   return false;
159 }
160
161 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
162 {
163   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
164                                                           (getNewOperation(theFeatureId));
165   // If the operation is launched as sub-operation of another then we have to initialize
166   // parent feature
167   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
168                                                          (myWorkshop->currentOperation());
169   if (aCurOperation) {
170     FeaturePtr aFeature = aCurOperation->feature();
171     CompositeFeaturePtr aCompFeature =
172         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
173     if (aCompFeature) {
174       aFOperation->setParentFeature(aCompFeature);
175     }
176   }
177
178   std::string aXmlCfg, aDescription;
179   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
180   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
181   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
182
183   return aFOperation;
184 }
185
186 void ModuleBase_IModule::createFeatures()
187 {
188   registerValidators();
189   registerProperties();
190
191   Config_ModuleReader aXMLReader = Config_ModuleReader();
192   aXMLReader.readAll();
193   myFeaturesInFiles = aXMLReader.featuresInFiles();
194 }
195
196
197 void ModuleBase_IModule::actionCreated(QAction* theFeature)
198 {
199   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
200 }
201
202 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
203 {
204   return true;
205 }
206
207 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
208 {
209   return true;
210 }
211
212 bool ModuleBase_IModule::canUndo() const
213 {
214   SessionPtr aMgr = ModelAPI_Session::get();
215   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
216 }
217
218 bool ModuleBase_IModule::canRedo() const
219 {
220   SessionPtr aMgr = ModelAPI_Session::get();
221   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
222 }
223
224 void ModuleBase_IModule::onFeatureTriggered()
225 {
226   QAction* aCmd = dynamic_cast<QAction*>(sender());
227   //Do nothing on uncheck
228   if (aCmd->isCheckable() && !aCmd->isChecked()) {
229     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
230     if (myWorkshop->canStopOperation(anOperation)) {
231       bool isCommitted;
232       myWorkshop->stopOperation(anOperation, isCommitted);
233     }
234     else {
235       aCmd->setChecked(true);
236     }
237   }
238   else {
239     QString aCmdId = aCmd->data().toString();
240     std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
241     if (aInfo.get() && aInfo->isModal()) {
242       launchModal(aCmdId);
243     } else {
244       launchOperation(aCmdId, false);
245       emit operationLaunched();
246     }
247   }
248 }
249
250 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
251 {
252   std::string aFeatureId = theFeature->getKind();
253   bool isCommitted;
254   if (!myWorkshop->canStartOperation(aFeatureId.c_str(), isCommitted))
255     return;
256
257   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
258                                                          (createOperation(aFeatureId));
259   if (aFOperation) {
260     aFOperation->setFeature(theFeature);
261     workshop()->processLaunchOperation(aFOperation);
262   }
263 }
264
265 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
266 {
267   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
268                                                      (myWorkshop->currentOperation());
269   return !aFOperation || !aFOperation->hasObject(theObject);
270 }
271
272 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
273 {
274   emit resumed(theOperation);
275 }
276
277 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
278                                               std::string& theXmlCfg, std::string& theDescription)
279 {
280   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
281   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
282   aWdgReader.readAll();
283
284   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
285   theDescription = aWdgReader.featureDescription(theFeatureId);
286 }