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"
11 #include <Events_Loop.h>
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_CompositeFeature.h>
15 #include <ModelAPI_Session.h>
17 #include <Config_PointerMessage.h>
18 #include <Config_WidgetReader.h>
19 #include <Config_ModuleReader.h>
23 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
24 : QObject(theParent), myWorkshop(theParent)
26 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
29 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
30 // SLOT(onMousePressed(QMouseEvent*)));
31 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
32 // SLOT(onMouseReleased(QMouseEvent*)));
33 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
34 // SLOT(onMouseMoved(QMouseEvent*)));
35 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
36 // SLOT(onKeyRelease(QKeyEvent*)));
37 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
38 // SLOT(onMouseDoubleClick(QMouseEvent*)));
41 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
43 if (!myWorkshop->canStartOperation(theCmdId))
46 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
47 ModuleBase_ISelection* aSelection = myWorkshop->selection();
48 // Initialise operation with preliminary selection
49 anOperation->initSelection(aSelection, myWorkshop->viewer());
50 sendOperation(anOperation);
54 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
56 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
57 std::shared_ptr<Config_PointerMessage> aMessage =
58 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
59 aMessage->setPointer(theOperation);
60 Events_Loop::loop()->send(aMessage);
63 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
65 return new ModuleBase_Operation(theFeatureId.c_str(), this);
68 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
70 ModuleBase_Operation* anOperation = getNewOperation(theFeatureId);
72 // If the operation is launched as sub-operation of another then we have to initialise
74 ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
76 FeaturePtr aFeature = aCurOperation->feature();
77 CompositeFeaturePtr aCompFeature =
78 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
80 anOperation->setParentFeature(aCompFeature);
84 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
85 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
87 std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
88 std::string aDescription = aWdgReader.featureDescription(theFeatureId);
90 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
91 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
96 void ModuleBase_IModule::createFeatures()
100 registerProperties();
102 Config_ModuleReader aXMLReader = Config_ModuleReader();
103 aXMLReader.readAll();
104 myFeaturesInFiles = aXMLReader.featuresInFiles();
108 void ModuleBase_IModule::actionCreated(QAction* theFeature)
110 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
113 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
118 bool ModuleBase_IModule::canUndo() const
120 SessionPtr aMgr = ModelAPI_Session::get();
121 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
124 bool ModuleBase_IModule::canRedo() const
126 SessionPtr aMgr = ModelAPI_Session::get();
127 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
130 bool ModuleBase_IModule::canCommitOperation() const
135 void ModuleBase_IModule::onFeatureTriggered()
137 QAction* aCmd = dynamic_cast<QAction*>(sender());
138 //Do nothing on uncheck
139 if (aCmd->isCheckable() && !aCmd->isChecked()) {
140 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
141 if (myWorkshop->canStopOperation())
142 myWorkshop->abortOperation(anOperation);
144 aCmd->setChecked(true);
148 launchOperation(aCmd->data().toString());
149 emit operationLaunched();
153 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
155 std::string aFeatureId = theFeature->getKind();
156 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
159 ModuleBase_Operation* anOperation = createOperation(aFeatureId);
160 anOperation->setFeature(theFeature);
161 sendOperation(anOperation);
164 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
166 ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
167 return !aOperation || !aOperation->hasObject(theObject);
170 void ModuleBase_IModule::onOperationResumed(ModuleBase_Operation* theOperation)
172 emit operationResumed(theOperation);