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_Events.h>
16 #include <ModelAPI_CompositeFeature.h>
17 #include <ModelAPI_Session.h>
18 #include "ModelAPI_Tools.h"
20 #include <Config_PointerMessage.h>
21 #include <Config_WidgetReader.h>
22 #include <Config_ModuleReader.h>
26 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
27 : QObject(theParent), myWorkshop(theParent)
29 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
32 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
33 // SLOT(onMousePressed(QMouseEvent*)));
34 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
35 // SLOT(onMouseReleased(QMouseEvent*)));
36 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
37 // SLOT(onMouseMoved(QMouseEvent*)));
38 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
39 // SLOT(onKeyRelease(QKeyEvent*)));
40 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
41 // SLOT(onMouseDoubleClick(QMouseEvent*)));
44 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
46 if (!myWorkshop->canStartOperation(theCmdId))
49 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
50 (createOperation(theCmdId.toStdString()));
52 ModuleBase_ISelection* aSelection = myWorkshop->selection();
53 // Initialise operation with preliminary selection
54 aFOperation->initSelection(aSelection, myWorkshop->viewer());
55 sendOperation(aFOperation);
60 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
62 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
63 std::shared_ptr<Config_PointerMessage> aMessage =
64 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
65 aMessage->setPointer(theOperation);
66 Events_Loop::loop()->send(aMessage);
69 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
71 return ModelAPI_Tools::getFeatureError(theFeature).c_str();
74 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
75 QStringList& theIds) const
79 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
81 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
84 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
85 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
86 const bool theUpdateViewer)
91 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
93 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
94 (getNewOperation(theFeatureId));
95 // If the operation is launched as sub-operation of another then we have to initialize
97 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
98 (myWorkshop->currentOperation());
100 FeaturePtr aFeature = aCurOperation->feature();
101 CompositeFeaturePtr aCompFeature =
102 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
104 aFOperation->setParentFeature(aCompFeature);
108 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
109 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
110 aWdgReader.readAll();
111 std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
112 std::string aDescription = aWdgReader.featureDescription(theFeatureId);
114 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
115 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
120 void ModuleBase_IModule::createFeatures()
122 registerValidators();
124 registerProperties();
126 Config_ModuleReader aXMLReader = Config_ModuleReader();
127 aXMLReader.readAll();
128 myFeaturesInFiles = aXMLReader.featuresInFiles();
132 void ModuleBase_IModule::actionCreated(QAction* theFeature)
134 theFeature->setStatusTip(theFeature->text());
135 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
138 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
143 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
148 bool ModuleBase_IModule::canUndo() const
150 SessionPtr aMgr = ModelAPI_Session::get();
151 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
154 bool ModuleBase_IModule::canRedo() const
156 SessionPtr aMgr = ModelAPI_Session::get();
157 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
160 void ModuleBase_IModule::onFeatureTriggered()
162 QAction* aCmd = dynamic_cast<QAction*>(sender());
163 //Do nothing on uncheck
164 if (aCmd->isCheckable() && !aCmd->isChecked()) {
165 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
166 if (myWorkshop->canStopOperation(anOperation))
167 myWorkshop->abortOperation(anOperation);
169 aCmd->setChecked(true);
173 launchOperation(aCmd->data().toString());
174 emit operationLaunched();
178 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
180 std::string aFeatureId = theFeature->getKind();
181 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
184 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
185 (createOperation(aFeatureId));
187 aFOperation->setFeature(theFeature);
188 sendOperation(aFOperation);
192 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
194 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
195 (myWorkshop->currentOperation());
196 return !aFOperation || !aFOperation->hasObject(theObject);
199 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
201 emit resumed(theOperation);