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 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
69 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
72 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
74 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
75 (getNewOperation(theFeatureId));
76 // If the operation is launched as sub-operation of another then we have to initialise
78 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
79 (myWorkshop->currentOperation());
81 FeaturePtr aFeature = aCurOperation->feature();
82 CompositeFeaturePtr aCompFeature =
83 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
85 aFOperation->setParentFeature(aCompFeature);
89 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
90 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
92 std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
93 std::string aDescription = aWdgReader.featureDescription(theFeatureId);
95 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
96 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
101 void ModuleBase_IModule::createFeatures()
103 registerValidators();
105 registerProperties();
107 Config_ModuleReader aXMLReader = Config_ModuleReader();
108 aXMLReader.readAll();
109 myFeaturesInFiles = aXMLReader.featuresInFiles();
113 void ModuleBase_IModule::actionCreated(QAction* theFeature)
115 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
118 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
123 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
128 bool ModuleBase_IModule::canUndo() const
130 SessionPtr aMgr = ModelAPI_Session::get();
131 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
134 bool ModuleBase_IModule::canRedo() const
136 SessionPtr aMgr = ModelAPI_Session::get();
137 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
140 bool ModuleBase_IModule::canCommitOperation() const
145 void ModuleBase_IModule::onFeatureTriggered()
147 QAction* aCmd = dynamic_cast<QAction*>(sender());
148 //Do nothing on uncheck
149 if (aCmd->isCheckable() && !aCmd->isChecked()) {
150 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
151 if (myWorkshop->canStopOperation(anOperation))
152 myWorkshop->abortOperation(anOperation);
154 aCmd->setChecked(true);
158 launchOperation(aCmd->data().toString());
159 emit operationLaunched();
163 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
165 std::string aFeatureId = theFeature->getKind();
166 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
169 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
170 (createOperation(aFeatureId));
172 aFOperation->setFeature(theFeature);
173 sendOperation(aFOperation);
177 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
179 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
180 (myWorkshop->currentOperation());
181 return !aFOperation || !aFOperation->hasObject(theObject);
184 void ModuleBase_IModule::onOperationResumed(ModuleBase_Operation* theOperation)
186 emit operationResumed(theOperation);