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(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 aCompFea = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
78     if (aCompFea)
79       anOperation->setParentFeature(aCompFea);
80   }
81
82   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
83   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
84   aWdgReader.readAll();
85   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
86   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
87
88   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
89   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
90
91   return anOperation;
92 }
93
94 void ModuleBase_IModule::createFeatures()
95 {
96   registerValidators();
97   registerFilters();
98
99   Config_ModuleReader aXMLReader = Config_ModuleReader();
100   aXMLReader.readAll();
101   myFeaturesInFiles = aXMLReader.featuresInFiles();
102 }
103
104
105 void ModuleBase_IModule::actionCreated(QAction* theFeature)
106 {
107   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
108 }
109
110
111 void ModuleBase_IModule::onFeatureTriggered()
112 {
113   QAction* aCmd = dynamic_cast<QAction*>(sender());
114   //Do nothing on uncheck
115   if (aCmd->isCheckable() && !aCmd->isChecked())
116     return;
117   launchOperation(aCmd->data().toString());
118 }
119
120
121 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
122 {
123   std::string aFeatureId = theFeature->getKind();
124   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
125     return;
126
127   ModuleBase_Operation* anOperation = createOperation(aFeatureId);
128   anOperation->setFeature(theFeature);
129   sendOperation(anOperation);
130 }