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