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