Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModelAPI_IReentrant.h"
22 #include "ModelAPI_EventReentrantMessage.h"
23
24 #include "ModuleBase_IModule.h"
25 #include "ModuleBase_IViewer.h"
26 #include "ModuleBase_ViewerPrs.h"
27 #include "ModuleBase_Operation.h"
28 #include "ModuleBase_IPropertyPanel.h"
29 #include "ModuleBase_ISelection.h"
30 #include "ModuleBase_OperationDescription.h"
31 #include "ModuleBase_OperationFeature.h"
32 #include "ModuleBase_ModelWidget.h"
33 #include "ModuleBase_WidgetFactory.h"
34 #include "ModuleBase_PageWidget.h"
35 #include "ModuleBase_Dialog.h"
36 #include "ModuleBase_IErrorMgr.h"
37
38 #include <Events_Loop.h>
39 #include <Events_Message.h>
40
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_CompositeFeature.h>
43 #include <ModelAPI_Session.h>
44 #include "ModelAPI_Tools.h"
45
46 #include <Config_PointerMessage.h>
47 #include <Config_WidgetReader.h>
48 #include <Config_ModuleReader.h>
49
50 #include <QAction>
51 #include <QMainWindow>
52 #include <QDialog>
53 #include <QLayout>
54 #include <QDialogButtonBox>
55 #include <QPushButton>
56
57 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
58   : QObject(theParent), myWorkshop(theParent)
59 {
60   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
61
62
63   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
64   //        SLOT(onMousePressed(QMouseEvent*)));
65   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
66   //        SLOT(onMouseReleased(QMouseEvent*)));
67   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
68   //        SLOT(onMouseMoved(QMouseEvent*)));
69   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
70   //        SLOT(onKeyRelease(QKeyEvent*)));
71   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
72   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
73 }
74
75 void ModuleBase_IModule::launchModal(const QString& theCmdId)
76 {
77   bool isCommitted;
78   if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
79     return;
80
81   std::string aXmlCfg, aDescription;
82   getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
83
84   SessionPtr aMgr = ModelAPI_Session::get();
85   aMgr->startOperation(theCmdId.toStdString());
86
87   ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
88   if (aDlg.exec() == QDialog::Accepted)
89     aMgr->finishOperation();
90   else
91     aMgr->abortOperation();
92   myWorkshop->updateCommandStatus();
93 }
94
95
96 void ModuleBase_IModule::launchOperation(const QString& theCmdId,
97                                          const bool& isStartAfterCommitOnly)
98 {
99   /// selection should be obtained from workshop before ask if the operation can be started as
100   /// the canStartOperation method performs commit/abort of previous operation.
101   /// Sometimes commit/abort may cause selection clear(Sketch operation) as a result
102   /// it will be lost and is not used for preselection.
103   ModuleBase_ISelection* aSelection = myWorkshop->selection();
104   QList<ModuleBase_ViewerPrsPtr> aPreSelected =
105     aSelection->getSelected(ModuleBase_ISelection::AllControls);
106
107   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
108                                                          (myWorkshop->currentOperation());
109   QString aCurOperationKind = aCurOperation ? aCurOperation->getDescription()->operationId() : "";
110
111   bool isCommitted;
112   if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
113     return;
114
115   /// reentrant operation(Sketch Line) should not be started if operation is aborted
116   if (isStartAfterCommitOnly && !isCommitted)
117     return;
118
119   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
120                                              (createOperation(theCmdId.toStdString()));
121   if (aFOperation) {
122     std::shared_ptr<Events_Message> aMessage = reentrantMessage();
123     if (aMessage.get()) {
124       setReentrantPreSelection(aMessage);
125     }
126     else if (canUsePreselection(aCurOperationKind, theCmdId)) {
127       // restore of previous opeation is absent or new launched operation has the same kind
128       aFOperation->initSelection(aPreSelected);
129     }
130     workshop()->processLaunchOperation(aFOperation);
131
132     if (aFOperation) {
133       FeaturePtr aFeature = aFOperation->feature();
134       ModelReentrantPtr aReentrantFeature =
135                                       std::dynamic_pointer_cast<ModelAPI_IReentrant>(aFeature);
136       if (aReentrantFeature.get()) {
137         if (aMessage.get()) {
138           ModuleBase_IPropertyPanel* aPanel = workshop()->propertyPanel();
139           std::string aPrevAttribute = aReentrantFeature->processEvent(aMessage);
140           if (!aPrevAttribute.empty()) {
141             workshop()->errorMgr()->updateActions(aFeature);
142             ModuleBase_ModelWidget* aPrevWidget = aPanel->modelWidget(aPrevAttribute);
143             aPanel->activateNextWidget(aPrevWidget);
144           }
145         }
146       }
147     }
148   }
149 }
150
151 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
152 {
153   return Handle(AIS_InteractiveObject)();
154 }
155
156 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
157 {
158   return true;
159 }
160
161 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
162 {
163   // Error already translated.
164   std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
165   return QString::fromUtf8(aMsg.c_str());
166 }
167
168 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
169                                              QStringList& theIds) const
170 {
171 }
172
173 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
174 {
175   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
176 }
177
178 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
179                               const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
180                               const bool theUpdateViewer)
181 {
182   return false;
183 }
184
185 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
186 {
187   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
188                                                           (getNewOperation(theFeatureId));
189   // If the operation is launched as sub-operation of another then we have to initialize
190   // parent feature
191   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
192                                                          (myWorkshop->currentOperation());
193   if (aCurOperation) {
194     FeaturePtr aFeature = aCurOperation->feature();
195     CompositeFeaturePtr aCompFeature =
196         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
197     if (aCompFeature) {
198       aFOperation->setParentFeature(aCompFeature);
199     }
200   }
201
202   std::string aXmlCfg, aDescription;
203   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
204   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
205   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
206
207   return aFOperation;
208 }
209
210 void ModuleBase_IModule::createFeatures()
211 {
212   registerValidators();
213   registerProperties();
214
215   Config_ModuleReader aXMLReader = Config_ModuleReader();
216   aXMLReader.readAll();
217   myFeaturesInFiles = aXMLReader.featuresInFiles();
218 }
219
220
221 void ModuleBase_IModule::actionCreated(QAction* theFeature)
222 {
223   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
224 }
225
226 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
227 {
228   return true;
229 }
230
231 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
232 {
233   return true;
234 }
235
236 bool ModuleBase_IModule::canUsePreselection(const QString& thePreviousOperationKind,
237                                             const QString& theStartedOperationKind)
238 {
239   // no previous operation
240   if (thePreviousOperationKind.isEmpty())
241     return true;
242   // edit operation
243   if (thePreviousOperationKind.endsWith(ModuleBase_OperationFeature::EditSuffix()))
244     return true;
245
246   // reentrant operation
247   if (thePreviousOperationKind == theStartedOperationKind)
248     return true;
249
250   return false;
251 }
252
253 bool ModuleBase_IModule::canUndo() const
254 {
255   SessionPtr aMgr = ModelAPI_Session::get();
256   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
257 }
258
259 bool ModuleBase_IModule::canRedo() const
260 {
261   SessionPtr aMgr = ModelAPI_Session::get();
262   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
263 }
264
265 void ModuleBase_IModule::onFeatureTriggered()
266 {
267   QAction* aCmd = dynamic_cast<QAction*>(sender());
268   //Do nothing on uncheck
269   if (aCmd->isCheckable() && !aCmd->isChecked()) {
270     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
271     if (myWorkshop->canStopOperation(anOperation)) {
272       bool isCommitted;
273       myWorkshop->stopOperation(anOperation, isCommitted);
274     }
275     else {
276       aCmd->setChecked(true);
277     }
278   }
279   else {
280     QString aCmdId = aCmd->data().toString();
281     std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
282     if (aInfo.get() && aInfo->isModal()) {
283       launchModal(aCmdId);
284     } else {
285       launchOperation(aCmdId, false);
286       emit operationLaunched();
287     }
288   }
289 }
290
291 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
292 {
293   std::string aFeatureId = theFeature->getKind();
294   bool isCommitted;
295   if (!myWorkshop->canStartOperation(aFeatureId.c_str(), isCommitted))
296     return;
297
298   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
299                                                          (createOperation(aFeatureId));
300   if (aFOperation) {
301     aFOperation->setFeature(theFeature);
302     workshop()->processLaunchOperation(aFOperation);
303   }
304 }
305
306 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
307 {
308   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
309                                                      (myWorkshop->currentOperation());
310   return !aFOperation || !aFOperation->hasObject(theObject);
311 }
312
313 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
314 {
315   emit resumed(theOperation);
316 }
317
318 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
319                                               std::string& theXmlCfg, std::string& theDescription)
320 {
321   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
322   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
323   aWdgReader.readAll();
324
325   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
326   theDescription = aWdgReader.featureDescription(theFeatureId);
327 }