Salome HOME
619fe0bbbb321b97324f08c80886af2bd385c8dc
[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(operationStarted(ModuleBase_Operation*)), 
26           SLOT(onOperationStarted(ModuleBase_Operation*)));
27
28   connect(myWorkshop, SIGNAL(operationStopped(ModuleBase_Operation*)), 
29           SLOT(onOperationStopped(ModuleBase_Operation*)));
30
31   connect(myWorkshop, SIGNAL(operationResumed(ModuleBase_Operation*)), 
32           SLOT(onOperationResumed(ModuleBase_Operation*)));
33
34   connect(myWorkshop, SIGNAL(operationComitted(ModuleBase_Operation*)), 
35           SLOT(onOperationComitted(ModuleBase_Operation*)));
36
37   connect(myWorkshop, SIGNAL(operationAborted(ModuleBase_Operation*)), 
38           SLOT(onOperationAborted(ModuleBase_Operation*)));
39
40   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
41
42
43   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
44   //        SLOT(onMousePressed(QMouseEvent*)));
45   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
46   //        SLOT(onMouseReleased(QMouseEvent*)));
47   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
48   //        SLOT(onMouseMoved(QMouseEvent*)));
49   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
50   //        SLOT(onKeyRelease(QKeyEvent*)));
51   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
52   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
53 }
54
55
56 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
57 {
58   if (!myWorkshop->canStartOperation(theCmdId))
59     return;
60
61   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
62   ModuleBase_ISelection* aSelection = myWorkshop->selection();
63   // Initialise operation with preliminary selection
64   anOperation->initSelection(aSelection, myWorkshop->viewer());
65   sendOperation(anOperation);
66 }
67
68
69 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
70 {
71   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
72   std::shared_ptr<Config_PointerMessage> aMessage =
73       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
74   aMessage->setPointer(theOperation);
75   Events_Loop::loop()->send(aMessage);
76 }
77
78 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
79 {
80   return new ModuleBase_Operation(theFeatureId.c_str(), this);
81 }
82
83 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
84 {
85   ModuleBase_Operation* anOperation = getNewOperation(theFeatureId);
86
87   // If the operation is launched as sub-operation of another then we have to initialise
88   // parent feature
89   ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
90   if (aCurOperation) {
91     FeaturePtr aFeature = aCurOperation->feature();
92     CompositeFeaturePtr aCompFea = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
93     if (aCompFea)
94       anOperation->setParentFeature(aCompFea);
95   }
96
97   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
98   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
99   aWdgReader.readAll();
100   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
101   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
102
103   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
104   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
105
106   return anOperation;
107 }
108
109 void ModuleBase_IModule::createFeatures()
110 {
111   registerValidators();
112   registerFilters();
113
114   Config_ModuleReader aXMLReader = Config_ModuleReader();
115   aXMLReader.readAll();
116   myFeaturesInFiles = aXMLReader.featuresInFiles();
117 }
118
119
120 void ModuleBase_IModule::actionCreated(QAction* theFeature)
121 {
122   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
123 }
124
125
126 void ModuleBase_IModule::onFeatureTriggered()
127 {
128   QAction* aCmd = dynamic_cast<QAction*>(sender());
129   //Do nothing on uncheck
130   if (aCmd->isCheckable() && !aCmd->isChecked())
131     return;
132   launchOperation(aCmd->data().toString());
133 }
134
135
136 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
137 {
138   std::string aFeatureId = theFeature->getKind();
139   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
140     return;
141
142   ModuleBase_Operation* anOperation = createOperation(aFeatureId);
143   anOperation->setFeature(theFeature);
144   sendOperation(anOperation);
145 }