1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include "ModelAPI_IReentrant.h"
4 #include "ModelAPI_EventReentrantMessage.h"
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"
20 #include <Events_Loop.h>
21 #include <Events_Message.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_CompositeFeature.h>
25 #include <ModelAPI_Session.h>
26 #include "ModelAPI_Tools.h"
28 #include <Config_PointerMessage.h>
29 #include <Config_WidgetReader.h>
30 #include <Config_ModuleReader.h>
33 #include <QMainWindow>
36 #include <QDialogButtonBox>
37 #include <QPushButton>
39 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
40 : QObject(theParent), myWorkshop(theParent)
42 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
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*)));
57 void ModuleBase_IModule::launchModal(const QString& theCmdId)
60 if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
63 std::string aXmlCfg, aDescription;
64 getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
66 SessionPtr aMgr = ModelAPI_Session::get();
67 aMgr->startOperation(theCmdId.toStdString());
69 ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
70 if (aDlg.exec() == QDialog::Accepted)
71 aMgr->finishOperation();
73 aMgr->abortOperation();
74 myWorkshop->updateCommandStatus();
78 void ModuleBase_IModule::launchOperation(const QString& theCmdId,
79 const bool& isStartAfterCommitOnly)
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);
89 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
90 (myWorkshop->currentOperation());
91 QString aCurOperationKind = aCurOperation ? aCurOperation->getDescription()->operationId() : "";
94 if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
97 /// reentrant operation(Sketch Line) should not be started if operation is aborted
98 if (isStartAfterCommitOnly && !isCommitted)
101 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
102 (createOperation(theCmdId.toStdString()));
104 std::shared_ptr<Events_Message> aMessage = reentrantMessage();
105 if (aMessage.get()) {
106 setReentrantPreSelection(aMessage);
108 else if (canUsePreselection(aCurOperationKind, theCmdId)) {
109 // restore of previous opeation is absent or new launched operation has the same kind
110 aFOperation->initSelection(aPreSelected);
112 workshop()->processLaunchOperation(aFOperation);
115 FeaturePtr aFeature = aFOperation->feature();
116 ModelReentrantPtr aReentrantFeature =
117 std::dynamic_pointer_cast<ModelAPI_IReentrant>(aFeature);
118 if (aReentrantFeature.get()) {
119 if (aMessage.get()) {
120 ModuleBase_IPropertyPanel* aPanel = workshop()->propertyPanel();
121 std::string aPrevAttribute = aReentrantFeature->processEvent(aMessage);
122 if (!aPrevAttribute.empty()) {
123 workshop()->errorMgr()->updateActions(aFeature);
124 ModuleBase_ModelWidget* aPrevWidget = aPanel->modelWidget(aPrevAttribute);
125 aPanel->activateNextWidget(aPrevWidget);
133 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
135 return Handle(AIS_InteractiveObject)();
138 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
143 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
145 // Error already translated.
146 std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
147 return QString::fromUtf8(aMsg.c_str());
150 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
151 QStringList& theIds) const
155 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
157 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
160 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
161 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
162 const bool theUpdateViewer)
167 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
169 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
170 (getNewOperation(theFeatureId));
171 // If the operation is launched as sub-operation of another then we have to initialize
173 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
174 (myWorkshop->currentOperation());
176 FeaturePtr aFeature = aCurOperation->feature();
177 CompositeFeaturePtr aCompFeature =
178 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
180 aFOperation->setParentFeature(aCompFeature);
184 std::string aXmlCfg, aDescription;
185 getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
186 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
187 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
192 void ModuleBase_IModule::createFeatures()
194 registerValidators();
195 registerProperties();
197 Config_ModuleReader aXMLReader = Config_ModuleReader();
198 aXMLReader.readAll();
199 myFeaturesInFiles = aXMLReader.featuresInFiles();
203 void ModuleBase_IModule::actionCreated(QAction* theFeature)
205 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
208 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
213 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
218 bool ModuleBase_IModule::canUsePreselection(const QString& thePreviousOperationKind,
219 const QString& theStartedOperationKind)
221 // no previous operation
222 if (thePreviousOperationKind.isEmpty())
225 if (thePreviousOperationKind.endsWith(ModuleBase_OperationFeature::EditSuffix()))
228 // reentrant operation
229 if (thePreviousOperationKind == theStartedOperationKind)
235 bool ModuleBase_IModule::canUndo() const
237 SessionPtr aMgr = ModelAPI_Session::get();
238 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
241 bool ModuleBase_IModule::canRedo() const
243 SessionPtr aMgr = ModelAPI_Session::get();
244 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
247 void ModuleBase_IModule::onFeatureTriggered()
249 QAction* aCmd = dynamic_cast<QAction*>(sender());
250 //Do nothing on uncheck
251 if (aCmd->isCheckable() && !aCmd->isChecked()) {
252 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
253 if (myWorkshop->canStopOperation(anOperation)) {
255 myWorkshop->stopOperation(anOperation, isCommitted);
258 aCmd->setChecked(true);
262 QString aCmdId = aCmd->data().toString();
263 std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
264 if (aInfo.get() && aInfo->isModal()) {
267 launchOperation(aCmdId, false);
268 emit operationLaunched();
273 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
275 std::string aFeatureId = theFeature->getKind();
277 if (!myWorkshop->canStartOperation(aFeatureId.c_str(), isCommitted))
280 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
281 (createOperation(aFeatureId));
283 aFOperation->setFeature(theFeature);
284 workshop()->processLaunchOperation(aFOperation);
288 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
290 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
291 (myWorkshop->currentOperation());
292 return !aFOperation || !aFOperation->hasObject(theObject);
295 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
297 emit resumed(theOperation);
300 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
301 std::string& theXmlCfg, std::string& theDescription)
303 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
304 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
305 aWdgReader.readAll();
307 theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
308 theDescription = aWdgReader.featureDescription(theFeatureId);