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"
11 #include "ModuleBase_ModelWidget.h"
13 #include <Events_Loop.h>
15 #include <ModelAPI_Validator.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_CompositeFeature.h>
18 #include <ModelAPI_Session.h>
19 #include "ModelAPI_Tools.h"
21 #include <Config_PointerMessage.h>
22 #include <Config_WidgetReader.h>
23 #include <Config_ModuleReader.h>
27 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
28 : QObject(theParent), myWorkshop(theParent)
30 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
33 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
34 // SLOT(onMousePressed(QMouseEvent*)));
35 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
36 // SLOT(onMouseReleased(QMouseEvent*)));
37 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
38 // SLOT(onMouseMoved(QMouseEvent*)));
39 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
40 // SLOT(onKeyRelease(QKeyEvent*)));
41 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
42 // SLOT(onMouseDoubleClick(QMouseEvent*)));
45 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
47 if (!myWorkshop->canStartOperation(theCmdId))
50 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
51 (createOperation(theCmdId.toStdString()));
53 ModuleBase_ISelection* aSelection = myWorkshop->selection();
54 // Initialise operation with preliminary selection
55 aFOperation->initSelection(aSelection, myWorkshop->viewer());
56 sendOperation(aFOperation);
61 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
63 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
64 std::shared_ptr<Config_PointerMessage> aMessage =
65 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
66 aMessage->setPointer(theOperation);
67 Events_Loop::loop()->send(aMessage);
70 const char* toString(ModelAPI_ExecState theExecState)
72 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
73 switch (theExecState) {
74 TO_STRING(ModelAPI_StateDone)
75 TO_STRING(ModelAPI_StateMustBeUpdated)
76 TO_STRING(ModelAPI_StateExecFailed)
77 TO_STRING(ModelAPI_StateInvalidArgument)
78 TO_STRING(ModelAPI_StateNothing)
79 default: return "Unknown ExecState.";
84 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
86 return ModelAPI_Tools::getFeatureError(theFeature).c_str();
89 QString ModuleBase_IModule::getWidgetError(ModuleBase_ModelWidget* theWidget)
93 if (!theWidget || !theWidget->feature().get())
96 std::string anAttributeID = theWidget->attributeID();
97 AttributePtr anAttribute = theWidget->feature()->attribute(anAttributeID);
98 if (!anAttribute.get())
101 std::string aValidatorID;
102 std::string anErrorMsg;
104 static ModelAPI_ValidatorsFactory* aValidators = ModelAPI_Session::get()->validators();
105 if (!aValidators->validate(anAttribute, aValidatorID, anErrorMsg)) {
106 if (anErrorMsg.empty())
107 anErrorMsg = "unknown error.";
108 anErrorMsg = anAttributeID + " - " + aValidatorID + ": " + anErrorMsg;
111 anError = QString::fromStdString(anErrorMsg);
112 if (anError.isEmpty())
113 anError = theWidget->getValueStateError();
118 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
119 QStringList& theIds) const
123 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
125 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
128 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
133 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
135 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
136 (getNewOperation(theFeatureId));
137 // If the operation is launched as sub-operation of another then we have to initialize
139 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
140 (myWorkshop->currentOperation());
142 FeaturePtr aFeature = aCurOperation->feature();
143 CompositeFeaturePtr aCompFeature =
144 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
146 aFOperation->setParentFeature(aCompFeature);
150 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
151 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
152 aWdgReader.readAll();
153 std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
154 std::string aDescription = aWdgReader.featureDescription(theFeatureId);
156 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
157 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
162 void ModuleBase_IModule::createFeatures()
164 registerValidators();
166 registerProperties();
168 Config_ModuleReader aXMLReader = Config_ModuleReader();
169 aXMLReader.readAll();
170 myFeaturesInFiles = aXMLReader.featuresInFiles();
174 void ModuleBase_IModule::actionCreated(QAction* theFeature)
176 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
179 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
184 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
189 bool ModuleBase_IModule::canUndo() const
191 SessionPtr aMgr = ModelAPI_Session::get();
192 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
195 bool ModuleBase_IModule::canRedo() const
197 SessionPtr aMgr = ModelAPI_Session::get();
198 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
201 void ModuleBase_IModule::onFeatureTriggered()
203 QAction* aCmd = dynamic_cast<QAction*>(sender());
204 //Do nothing on uncheck
205 if (aCmd->isCheckable() && !aCmd->isChecked()) {
206 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
207 if (myWorkshop->canStopOperation(anOperation))
208 myWorkshop->abortOperation(anOperation);
210 aCmd->setChecked(true);
214 launchOperation(aCmd->data().toString());
215 emit operationLaunched();
219 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
221 std::string aFeatureId = theFeature->getKind();
222 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
225 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
226 (createOperation(aFeatureId));
228 aFOperation->setFeature(theFeature);
229 sendOperation(aFOperation);
233 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
235 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
236 (myWorkshop->currentOperation());
237 return !aFOperation || !aFOperation->hasObject(theObject);
240 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
242 emit resumed(theOperation);