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