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