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