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