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*)));
42 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
44 if (!myWorkshop->canStartOperation(theCmdId))
47 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
48 ModuleBase_ISelection* aSelection = myWorkshop->selection();
49 // Initialise operation with preliminary selection
50 anOperation->initSelection(aSelection, myWorkshop->viewer());
51 sendOperation(anOperation);
55 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
57 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
58 std::shared_ptr<Config_PointerMessage> aMessage =
59 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
60 aMessage->setPointer(theOperation);
61 Events_Loop::loop()->send(aMessage);
64 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
66 return new ModuleBase_Operation(theFeatureId.c_str(), this);
69 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
71 ModuleBase_Operation* anOperation = getNewOperation(theFeatureId);
73 // If the operation is launched as sub-operation of another then we have to initialise
75 ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
77 FeaturePtr aFeature = aCurOperation->feature();
78 CompositeFeaturePtr aCompFeature =
79 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
81 anOperation->setParentFeature(aCompFeature);
85 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
86 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
88 std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
89 std::string aDescription = aWdgReader.featureDescription(theFeatureId);
91 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
92 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
97 void ModuleBase_IModule::createFeatures()
101 registerProperties();
103 Config_ModuleReader aXMLReader = Config_ModuleReader();
104 aXMLReader.readAll();
105 myFeaturesInFiles = aXMLReader.featuresInFiles();
109 void ModuleBase_IModule::actionCreated(QAction* theFeature)
111 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
114 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
116 ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
117 return anOperation && anOperation->hasObject(theObject);
120 bool ModuleBase_IModule::canUndo() const
122 SessionPtr aMgr = ModelAPI_Session::get();
123 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
126 bool ModuleBase_IModule::canRedo() const
128 SessionPtr aMgr = ModelAPI_Session::get();
129 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
132 void ModuleBase_IModule::onFeatureTriggered()
134 QAction* aCmd = dynamic_cast<QAction*>(sender());
135 //Do nothing on uncheck
136 if (aCmd->isCheckable() && !aCmd->isChecked())
138 launchOperation(aCmd->data().toString());
142 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
144 std::string aFeatureId = theFeature->getKind();
145 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
148 ModuleBase_Operation* anOperation = createOperation(aFeatureId);
149 anOperation->setFeature(theFeature);
150 sendOperation(anOperation);