Salome HOME
fd2686df308b3bc672761701127e048f974182b7
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
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
11 #include <Events_Loop.h>
12
13 #include <ModelAPI_Events.h>
14 #include <ModelAPI_CompositeFeature.h>
15
16 #include <Config_PointerMessage.h>
17 #include <Config_WidgetReader.h>
18 #include <Config_ModuleReader.h>
19
20 #include <QAction>
21
22 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
23   : QObject(theParent), myWorkshop(theParent) 
24 {
25   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
26
27
28   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
29   //        SLOT(onMousePressed(QMouseEvent*)));
30   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
31   //        SLOT(onMouseReleased(QMouseEvent*)));
32   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
33   //        SLOT(onMouseMoved(QMouseEvent*)));
34   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
35   //        SLOT(onKeyRelease(QKeyEvent*)));
36   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
37   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
38 }
39
40
41 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
42 {
43   if (!myWorkshop->canStartOperation(theCmdId))
44     return;
45
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);
51 }
52
53
54 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
55 {
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);
61 }
62
63 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
64 {
65   return new ModuleBase_Operation(theFeatureId.c_str(), this);
66 }
67
68 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
69 {
70   ModuleBase_Operation* anOperation = getNewOperation(theFeatureId);
71
72   // If the operation is launched as sub-operation of another then we have to initialise
73   // parent feature
74   ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
75   if (aCurOperation) {
76     FeaturePtr aFeature = aCurOperation->feature();
77     CompositeFeaturePtr aCompFeature =
78         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
79     if (aCompFeature) {
80       anOperation->setParentFeature(aCompFeature);
81     }
82   }
83
84   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
85   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
86   aWdgReader.readAll();
87   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
88   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
89
90   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
91   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
92
93   return anOperation;
94 }
95
96 void ModuleBase_IModule::createFeatures()
97 {
98   registerValidators();
99   registerFilters();
100
101   Config_ModuleReader aXMLReader = Config_ModuleReader();
102   aXMLReader.readAll();
103   myFeaturesInFiles = aXMLReader.featuresInFiles();
104 }
105
106
107 void ModuleBase_IModule::actionCreated(QAction* theFeature)
108 {
109   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
110 }
111
112 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
113 {
114   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
115   return anOperation && anOperation->hasObject(theObject);
116 }
117
118 void ModuleBase_IModule::onFeatureTriggered()
119 {
120   QAction* aCmd = dynamic_cast<QAction*>(sender());
121   //Do nothing on uncheck
122   if (aCmd->isCheckable() && !aCmd->isChecked())
123     return;
124   launchOperation(aCmd->data().toString());
125 }
126
127
128 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
129 {
130   std::string aFeatureId = theFeature->getKind();
131   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
132     return;
133
134   ModuleBase_Operation* anOperation = createOperation(aFeatureId);
135   anOperation->setFeature(theFeature);
136   sendOperation(anOperation);
137 }