Salome HOME
7584e6bf0ac963b8789a56335938e566797dfa0c
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1
2 #include "ModuleBase_IModule.h"
3 #include "ModuleBase_IViewer.h"
4 #include "ModuleBase_ViewerPrs.h"
5 #include "ModuleBase_Operation.h"
6 #include "ModuleBase_ISelection.h"
7 #include "ModuleBase_OperationDescription.h"
8
9 #include <Events_Loop.h>
10
11 #include <ModelAPI_Events.h>
12 #include <ModelAPI_CompositeFeature.h>
13
14 #include <Config_PointerMessage.h>
15 #include <Config_WidgetReader.h>
16 #include <Config_ModuleReader.h>
17
18 #include <QAction>
19
20 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
21   : QObject(theParent), myWorkshop(theParent) 
22 {
23   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
24
25
26   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
27   //        SLOT(onMousePressed(QMouseEvent*)));
28   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
29   //        SLOT(onMouseReleased(QMouseEvent*)));
30   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
31   //        SLOT(onMouseMoved(QMouseEvent*)));
32   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
33   //        SLOT(onKeyRelease(QKeyEvent*)));
34   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
35   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
36 }
37
38
39 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
40 {
41   if (!myWorkshop->canStartOperation(theCmdId))
42     return;
43
44   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
45   ModuleBase_ISelection* aSelection = myWorkshop->selection();
46   // Initialise operation with preliminary selection
47   anOperation->initSelection(aSelection, myWorkshop->viewer());
48   sendOperation(anOperation);
49 }
50
51
52 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
53 {
54   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
55   std::shared_ptr<Config_PointerMessage> aMessage =
56       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
57   aMessage->setPointer(theOperation);
58   Events_Loop::loop()->send(aMessage);
59 }
60
61 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
62 {
63   return new ModuleBase_Operation(theFeatureId.c_str(), this);
64 }
65
66 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
67 {
68   ModuleBase_Operation* anOperation = getNewOperation(theFeatureId);
69
70   // If the operation is launched as sub-operation of another then we have to initialise
71   // parent feature
72   ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
73   if (aCurOperation) {
74     FeaturePtr aFeature = aCurOperation->feature();
75     CompositeFeaturePtr aCompFea = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
76     if (aCompFea)
77       anOperation->setParentFeature(aCompFea);
78   }
79
80   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
81   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
82   aWdgReader.readAll();
83   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
84   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
85
86   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
87   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
88
89   return anOperation;
90 }
91
92 void ModuleBase_IModule::createFeatures()
93 {
94   registerValidators();
95   registerFilters();
96
97   Config_ModuleReader aXMLReader = Config_ModuleReader();
98   aXMLReader.readAll();
99   myFeaturesInFiles = aXMLReader.featuresInFiles();
100 }
101
102
103 void ModuleBase_IModule::actionCreated(QAction* theFeature)
104 {
105   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
106 }
107
108
109 void ModuleBase_IModule::onFeatureTriggered()
110 {
111   QAction* aCmd = dynamic_cast<QAction*>(sender());
112   //Do nothing on uncheck
113   if (aCmd->isCheckable() && !aCmd->isChecked())
114     return;
115   launchOperation(aCmd->data().toString());
116 }
117
118
119 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
120 {
121   std::string aFeatureId = theFeature->getKind();
122   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
123     return;
124
125   ModuleBase_Operation* anOperation = createOperation(aFeatureId);
126   anOperation->setFeature(theFeature);
127   sendOperation(anOperation);
128 }