Salome HOME
Task #3005 : To be able to create a group on a whole result
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModelAPI_IReentrant.h"
21 #include "ModelAPI_EventReentrantMessage.h"
22
23 #include "ModuleBase_IModule.h"
24 #include "ModuleBase_IViewer.h"
25 #include "ModuleBase_ViewerPrs.h"
26 #include "ModuleBase_Operation.h"
27 #include "ModuleBase_IPropertyPanel.h"
28 #include "ModuleBase_ISelection.h"
29 #include "ModuleBase_OperationDescription.h"
30 #include "ModuleBase_OperationFeature.h"
31 #include "ModuleBase_ModelWidget.h"
32 #include "ModuleBase_WidgetFactory.h"
33 #include "ModuleBase_PageWidget.h"
34 #include "ModuleBase_Dialog.h"
35 #include "ModuleBase_IErrorMgr.h"
36
37 #include <Events_Loop.h>
38 #include <Events_Message.h>
39
40 #include <ModelAPI_Events.h>
41 #include <ModelAPI_CompositeFeature.h>
42 #include <ModelAPI_Session.h>
43 #include "ModelAPI_Tools.h"
44
45 #include <Config_PointerMessage.h>
46 #include <Config_WidgetReader.h>
47 #include <Config_ModuleReader.h>
48
49 #include <QAction>
50 #include <QMainWindow>
51 #include <QDialog>
52 #include <QLayout>
53 #include <QDialogButtonBox>
54 #include <QPushButton>
55
56 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
57   : QObject(theParent), myWorkshop(theParent)
58 {
59   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
60
61
62   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
63   //        SLOT(onMousePressed(QMouseEvent*)));
64   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
65   //        SLOT(onMouseReleased(QMouseEvent*)));
66   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
67   //        SLOT(onMouseMoved(QMouseEvent*)));
68   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
69   //        SLOT(onKeyRelease(QKeyEvent*)));
70   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
71   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
72 }
73
74 ModuleBase_IModule::~ModuleBase_IModule()
75 {
76   std::map<ModuleBase_SelectionFilterType, Handle(SelectMgr_Filter)>::const_iterator aFiltersIt =
77     mySelectionFilters.begin();
78   for (; aFiltersIt != mySelectionFilters.end(); aFiltersIt++) {
79     Handle(SelectMgr_Filter) aFilter = aFiltersIt->second;
80     if (!aFilter.IsNull())
81       aFilter.Nullify();
82   }
83 }
84
85
86 void ModuleBase_IModule::launchModal(const QString& theCmdId)
87 {
88   bool isCommitted;
89   if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
90     return;
91
92   std::string aXmlCfg, aDescription;
93   getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
94
95   SessionPtr aMgr = ModelAPI_Session::get();
96   aMgr->startOperation(theCmdId.toStdString());
97
98   ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
99   if (aDlg.exec() == QDialog::Accepted)
100     aMgr->finishOperation();
101   else
102     aMgr->abortOperation();
103   myWorkshop->updateCommandStatus();
104 }
105
106
107 void ModuleBase_IModule::launchOperation(const QString& theCmdId,
108                                          const bool& isStartAfterCommitOnly)
109 {
110   /// selection should be obtained from workshop before ask if the operation can be started as
111   /// the canStartOperation method performs commit/abort of previous operation.
112   /// Sometimes commit/abort may cause selection clear(Sketch operation) as a result
113   /// it will be lost and is not used for preselection.
114   ModuleBase_ISelection* aSelection = myWorkshop->selection();
115   QList<ModuleBase_ViewerPrsPtr> aPreSelected =
116     aSelection->getSelected(ModuleBase_ISelection::AllControls);
117
118   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
119                                                          (myWorkshop->currentOperation());
120   QString aCurOperationKind = aCurOperation ? aCurOperation->getDescription()->operationId() : "";
121
122   bool isCommitted;
123   if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
124     return;
125
126   /// reentrant operation(Sketch Line) should not be started if operation is aborted
127   if (isStartAfterCommitOnly && !isCommitted)
128     return;
129
130   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
131                                              (createOperation(theCmdId.toStdString()));
132   if (aFOperation) {
133     std::shared_ptr<Events_Message> aMessage = reentrantMessage();
134     if (aMessage.get()) {
135       setReentrantPreSelection(aMessage);
136     }
137     else if (canUsePreselection(aCurOperationKind, theCmdId)) {
138       // restore of previous opeation is absent or new launched operation has the same kind
139       aFOperation->initSelection(aPreSelected);
140     }
141     workshop()->processLaunchOperation(aFOperation);
142
143     if (aFOperation) {
144       FeaturePtr aFeature = aFOperation->feature();
145       ModelReentrantPtr aReentrantFeature =
146                                       std::dynamic_pointer_cast<ModelAPI_IReentrant>(aFeature);
147       if (aReentrantFeature.get()) {
148         if (aMessage.get()) {
149           ModuleBase_IPropertyPanel* aPanel = workshop()->propertyPanel();
150           std::string aPrevAttribute = aReentrantFeature->processEvent(aMessage);
151           if (!aPrevAttribute.empty()) {
152             workshop()->errorMgr()->updateActions(aFeature);
153             ModuleBase_ModelWidget* aPrevWidget = aPanel->modelWidget(aPrevAttribute);
154             aPanel->activateNextWidget(aPrevWidget);
155           }
156         }
157       }
158     }
159   }
160 }
161
162 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ObjectPtr& theResult)
163 {
164   return Handle(AIS_InteractiveObject)();
165 }
166
167 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
168 {
169   return true;
170 }
171
172 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
173 {
174   // Error already translated.
175   std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
176   return QString::fromUtf8(aMsg.c_str());
177 }
178
179 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
180                                              QStringList& theIds) const
181 {
182 }
183
184 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
185 {
186   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
187 }
188
189 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
190                               const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
191                               const bool theUpdateViewer)
192 {
193   return false;
194 }
195
196 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
197 {
198   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
199                                                           (getNewOperation(theFeatureId));
200   // If the operation is launched as sub-operation of another then we have to initialize
201   // parent feature
202   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
203                                                          (myWorkshop->currentOperation());
204   if (aCurOperation) {
205     FeaturePtr aFeature = aCurOperation->feature();
206     CompositeFeaturePtr aCompFeature =
207         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
208     if (aCompFeature) {
209       aFOperation->setParentFeature(aCompFeature);
210     }
211   }
212
213   std::string aXmlCfg, aDescription;
214   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
215   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
216   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
217
218   return aFOperation;
219 }
220
221 void ModuleBase_IModule::createFeatures()
222 {
223   registerValidators();
224   registerProperties();
225
226   Config_ModuleReader aXMLReader = Config_ModuleReader();
227   aXMLReader.readAll();
228   myFeaturesInFiles = aXMLReader.featuresInFiles();
229 }
230
231
232 void ModuleBase_IModule::actionCreated(QAction* theFeature)
233 {
234   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
235 }
236
237 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
238 {
239   return true;
240 }
241
242 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
243 {
244   return true;
245 }
246
247 bool ModuleBase_IModule::canUsePreselection(const QString& thePreviousOperationKind,
248                                             const QString& theStartedOperationKind)
249 {
250   // no previous operation
251   if (thePreviousOperationKind.isEmpty())
252     return true;
253   // edit operation
254   if (thePreviousOperationKind.endsWith(ModuleBase_OperationFeature::EditSuffix()))
255     return true;
256
257   // reentrant operation
258   if (thePreviousOperationKind == theStartedOperationKind)
259     return true;
260
261   return false;
262 }
263
264 bool ModuleBase_IModule::canUndo() const
265 {
266   SessionPtr aMgr = ModelAPI_Session::get();
267   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
268 }
269
270 bool ModuleBase_IModule::canRedo() const
271 {
272   SessionPtr aMgr = ModelAPI_Session::get();
273   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
274 }
275
276 void ModuleBase_IModule::onFeatureTriggered()
277 {
278   QAction* aCmd = dynamic_cast<QAction*>(sender());
279   //Do nothing on uncheck
280   if (aCmd->isCheckable() && !aCmd->isChecked()) {
281     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
282     if (myWorkshop->canStopOperation(anOperation)) {
283       bool isCommitted;
284       myWorkshop->stopOperation(anOperation, isCommitted);
285     }
286     else {
287       aCmd->setChecked(true);
288     }
289   }
290   else {
291     QString aCmdId = aCmd->data().toString();
292     std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
293     if (aInfo.get() && aInfo->isModal()) {
294       launchModal(aCmdId);
295     } else {
296       launchOperation(aCmdId, false);
297     }
298   }
299 }
300
301 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
302 {
303   std::string aFeatureId = theFeature->getKind();
304   bool isCommitted;
305   if (!myWorkshop->canStartOperation(aFeatureId.c_str(), isCommitted))
306     return;
307
308   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
309                                                          (createOperation(aFeatureId));
310   if (aFOperation) {
311     aFOperation->setFeature(theFeature);
312     workshop()->processLaunchOperation(aFOperation);
313   }
314 }
315
316 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
317 {
318   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
319                                                      (myWorkshop->currentOperation());
320   return !aFOperation || !aFOperation->hasObject(theObject);
321 }
322
323 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
324 {
325   emit resumed(theOperation);
326 }
327
328 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
329                                               std::string& theXmlCfg, std::string& theDescription)
330 {
331   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
332   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
333   aWdgReader.readAll();
334
335   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
336   theDescription = aWdgReader.featureDescription(theFeatureId);
337 }
338
339
340 //******************************************************
341 QIntList ModuleBase_IModule::selectionFilters()
342 {
343   QIntList aTypes;
344
345   std::map<ModuleBase_SelectionFilterType, Handle(SelectMgr_Filter)>::const_iterator aFiltersIt =
346     mySelectionFilters.begin();
347   for (; aFiltersIt != mySelectionFilters.end(); aFiltersIt++)
348     aTypes.append(aFiltersIt->first);
349
350   return aTypes;
351 }
352
353 //******************************************************
354 void ModuleBase_IModule::registerSelectionFilter(const ModuleBase_SelectionFilterType theFilterType,
355   const Handle(SelectMgr_Filter)& theFilter)
356 {
357   mySelectionFilters[theFilterType] = theFilter;
358 }
359
360 //******************************************************
361 Handle(SelectMgr_Filter) ModuleBase_IModule::selectionFilter(const int theType)
362 {
363   ModuleBase_SelectionFilterType aType = (ModuleBase_SelectionFilterType)theType;
364
365   if (mySelectionFilters.find(aType) != mySelectionFilters.end())
366     return mySelectionFilters[aType];
367   else
368     return Handle(SelectMgr_Filter)();
369 }