1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
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"
12 #include <Events_Loop.h>
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_CompositeFeature.h>
16 #include <ModelAPI_Session.h>
18 #include <Config_PointerMessage.h>
19 #include <Config_WidgetReader.h>
20 #include <Config_ModuleReader.h>
24 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
25 : QObject(theParent), myWorkshop(theParent)
27 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
30 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
31 // SLOT(onMousePressed(QMouseEvent*)));
32 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
33 // SLOT(onMouseReleased(QMouseEvent*)));
34 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
35 // SLOT(onMouseMoved(QMouseEvent*)));
36 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
37 // SLOT(onKeyRelease(QKeyEvent*)));
38 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
39 // SLOT(onMouseDoubleClick(QMouseEvent*)));
42 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
44 if (!myWorkshop->canStartOperation(theCmdId))
47 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
48 (createOperation(theCmdId.toStdString()));
50 ModuleBase_ISelection* aSelection = myWorkshop->selection();
51 // Initialise operation with preliminary selection
52 aFOperation->initSelection(aSelection, myWorkshop->viewer());
53 sendOperation(aFOperation);
58 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
60 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
61 std::shared_ptr<Config_PointerMessage> aMessage =
62 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
63 aMessage->setPointer(theOperation);
64 Events_Loop::loop()->send(aMessage);
67 const char* toString(ModelAPI_ExecState theExecState)
69 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
70 switch (theExecState) {
71 TO_STRING(ModelAPI_StateDone)
72 TO_STRING(ModelAPI_StateMustBeUpdated)
73 TO_STRING(ModelAPI_StateExecFailed)
74 TO_STRING(ModelAPI_StateInvalidArgument)
75 TO_STRING(ModelAPI_StateNothing)
76 default: return "Unknown ExecState.";
81 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature, const bool isCheckGUI)
84 if (!theFeature.get() || !theFeature->data()->isValid() || theFeature->isAction())
87 // to be removed later, this error should be got from the feature
88 if (theFeature->data()->execState() == ModelAPI_StateDone ||
89 theFeature->data()->execState() == ModelAPI_StateMustBeUpdated)
92 // set error indication
93 anError = QString::fromStdString(theFeature->error());
94 if (anError.isEmpty()) {
95 bool isDone = ( theFeature->data()->execState() == ModelAPI_StateDone
96 || theFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
98 anError = toString(theFeature->data()->execState());
104 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
105 QStringList& theIds) const
109 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
111 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
114 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
119 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
121 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
122 (getNewOperation(theFeatureId));
123 // If the operation is launched as sub-operation of another then we have to initialize
125 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
126 (myWorkshop->currentOperation());
128 FeaturePtr aFeature = aCurOperation->feature();
129 CompositeFeaturePtr aCompFeature =
130 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
132 aFOperation->setParentFeature(aCompFeature);
136 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
137 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
138 aWdgReader.readAll();
139 std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
140 std::string aDescription = aWdgReader.featureDescription(theFeatureId);
142 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
143 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
148 void ModuleBase_IModule::createFeatures()
150 registerValidators();
152 registerProperties();
154 Config_ModuleReader aXMLReader = Config_ModuleReader();
155 aXMLReader.readAll();
156 myFeaturesInFiles = aXMLReader.featuresInFiles();
160 void ModuleBase_IModule::actionCreated(QAction* theFeature)
162 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
165 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
170 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
175 bool ModuleBase_IModule::canUndo() const
177 SessionPtr aMgr = ModelAPI_Session::get();
178 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
181 bool ModuleBase_IModule::canRedo() const
183 SessionPtr aMgr = ModelAPI_Session::get();
184 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
187 void ModuleBase_IModule::onFeatureTriggered()
189 QAction* aCmd = dynamic_cast<QAction*>(sender());
190 //Do nothing on uncheck
191 if (aCmd->isCheckable() && !aCmd->isChecked()) {
192 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
193 if (myWorkshop->canStopOperation(anOperation))
194 myWorkshop->abortOperation(anOperation);
196 aCmd->setChecked(true);
200 launchOperation(aCmd->data().toString());
201 emit operationLaunched();
205 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
207 std::string aFeatureId = theFeature->getKind();
208 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
211 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
212 (createOperation(aFeatureId));
214 aFOperation->setFeature(theFeature);
215 sendOperation(aFOperation);
219 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
221 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
222 (myWorkshop->currentOperation());
223 return !aFOperation || !aFOperation->hasObject(theObject);
226 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
228 emit resumed(theOperation);