]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
17998703c234ef06343493c59074509beaaf792f
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationSketchLine.h>
4 #include <PartSet_OperationEditLine.h>
5 #include <ModuleBase_Operation.h>
6 #include <ModuleBase_OperationDescription.h>
7 #include <PartSet_Listener.h>
8
9 #include <ModuleBase_Operation.h>
10
11 #include <XGUI_MainWindow.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_Viewer.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_OperationMgr.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <XGUI_ViewPort.h>
18 #include <XGUI_ActionsMgr.h>
19 #include <XGUI_ViewerProxy.h>
20
21 #include <Config_PointerMessage.h>
22 #include <Config_ModuleReader.h>
23 #include <Config_WidgetReader.h>
24 #include <Events_Loop.h>
25 #include <Events_Message.h>
26 #include <Events_Error.h>
27
28 #include <GeomAPI_Shape.h>
29
30 #include <AIS_ListOfInteractive.hxx>
31
32 #include <QObject>
33 #include <QMouseEvent>
34 #include <QString>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40
41 /*!Create and return new instance of XGUI_Module*/
42 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
43 {
44   return new PartSet_Module(theWshop);
45 }
46
47 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
48 {
49   myWorkshop = theWshop;
50   myListener = new PartSet_Listener(this);
51
52   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
53
54   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
55           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
56
57   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
58           this, SLOT(onMousePressed(QMouseEvent*)));
59   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
60           this, SLOT(onMouseReleased(QMouseEvent*)));
61   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
62           this, SLOT(onMouseMoved(QMouseEvent*)));
63   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
64           this, SLOT(onKeyRelease(QKeyEvent*)));
65 }
66
67 PartSet_Module::~PartSet_Module()
68 {
69 }
70
71 XGUI_Workshop* PartSet_Module::workshop() const
72 {
73   return myWorkshop;
74 }
75
76 void PartSet_Module::createFeatures()
77 {
78   Config_ModuleReader aXMLReader = Config_ModuleReader();
79   aXMLReader.readAll();
80   myFeaturesInFiles = aXMLReader.featuresInFiles();
81 }
82
83 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
84 {
85   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
86 }
87
88 QStringList PartSet_Module::nestedFeatures(QString)
89 {
90   return QStringList();
91 }
92
93 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
94 {
95   return myFeaturesInFiles[theFeatureId];
96 }
97
98 /*
99  *
100  */
101 void PartSet_Module::onFeatureTriggered()
102 {
103   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
104   //Do nothing on uncheck
105   if(aCmd->isCheckable() && !aCmd->isChecked())
106     return;
107   launchOperation(aCmd->id());
108 }
109   
110 void PartSet_Module::launchOperation(const QString& theCmdId)
111 {
112   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
113   sendOperation(anOperation);
114 }
115
116 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
117 {
118   if (!theOperation)
119     return;
120   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
121   if (aPreviewOp) {
122   }
123 }
124
125 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
126 {
127   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
128                                        myWorkshop->operationMgr()->currentOperation());
129   if (aPreviewOp)
130   {
131     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView());
132   }
133 }
134
135 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
136 {
137   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
138                                        myWorkshop->operationMgr()->currentOperation());
139   if (aPreviewOp)
140   {
141     XGUI_SelectionMgr* aSelector = myWorkshop->selector();
142     std::list<XGUI_ViewerPrs> aPresentations;
143     if (aSelector) {
144       NCollection_List<TopoDS_Shape> aList;
145       aSelector->selectedShapes(aList);
146       aPresentations = myWorkshop->displayer()->GetViewerPrs(aList);
147     }
148     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
149   }
150 }
151
152 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
153 {
154   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
155                                        myWorkshop->operationMgr()->currentOperation());
156   if (aPreviewOp)
157   {
158     XGUI_SelectionMgr* aSelector = myWorkshop->selector();
159     std::list<XGUI_ViewerPrs> aPresentations;
160     if (aSelector) {
161       NCollection_List<TopoDS_Shape> aList;
162       aSelector->selectedShapes(aList);
163       aPresentations = myWorkshop->displayer()->GetViewerPrs(aList);
164     }
165     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
166   }
167 }
168
169 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
170 {
171   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
172   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
173   if (aPreviewOp) {
174     aPreviewOp->keyReleased(theEvent->key());
175   }
176 }
177
178 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
179 {
180   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
181
182   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
183   if (anOperation) {
184     PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
185     if (aPreviewOp) {
186       visualizePreview(aPreviewOp->feature(), false);
187     }
188   }
189
190   myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
191 }
192
193 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
194 {
195   ModuleBase_Operation* anOperation = createOperation(theName.c_str());
196   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
197   if (aPreviewOp)
198   {
199     aPreviewOp->init(theFeature);
200   }
201   myWorkshop->actionsMgr()->setActionChecked(anOperation->getDescription()->operationId(), true);
202   sendOperation(anOperation);
203 }
204
205 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
206 {
207   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
208   aViewer->enableMultiselection(theEnabled);
209 }
210
211 void PartSet_Module::onSelectionEnabled(bool theEnabled)
212 {
213   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
214   aViewer->enableSelection(theEnabled);
215 }
216
217 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
218                                           int theMode)
219 {
220   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Abort;
221   visualizePreview(theFeature, isDisplay);
222 }
223
224 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
225 {
226   // get operation xml description
227   std::string aStdCmdId = theCmdId;
228   if (aStdCmdId == PartSet_OperationEditLine::Type())
229     aStdCmdId = PartSet_OperationSketchLine::Type();
230   std::string aPluginFileName = featureFile(aStdCmdId);
231   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
232   aWdgReader.readAll();
233   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
234   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
235
236   // create the operation
237   ModuleBase_Operation* anOperation;
238   if (theCmdId == PartSet_OperationSketch::Type()) {
239     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
240   }
241   else if(theCmdId == PartSet_OperationSketchLine::Type() ||
242           theCmdId == PartSet_OperationEditLine::Type()) {
243     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
244     boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
245     if (aCurOperation)
246       aSketchFeature = aCurOperation->feature();
247     if (theCmdId == PartSet_OperationSketchLine::Type())
248       anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketchFeature);
249     else
250       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketchFeature);
251   }
252   else {
253     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
254   }
255   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
256   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
257
258   // connect the operation
259   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
260   if (aPreviewOp) {
261     connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
262             this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
263     connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
264             this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
265     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
266             this, SLOT(onMultiSelectionEnabled(bool)));
267     connect(aPreviewOp, SIGNAL(selectionEnabled(bool)),
268             this, SLOT(onSelectionEnabled(bool)));
269
270     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
271     if (aSketchOp) {
272       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
273               this, SLOT(onPlaneSelected(double, double, double)));
274     }
275   }
276
277   return anOperation;
278 }
279
280 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
281 {
282   //TODO(sbh): Implement static method to extract event id [SEID]
283   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
284   Config_PointerMessage aMessage(aModuleEvent, this);
285   aMessage.setPointer(theOperation);
286   Events_Loop::loop()->send(aMessage);
287 }
288
289 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay)
290 {
291   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
292   if (!anOperation)
293     return;
294
295   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
296   if (!aPreviewOp)
297     return;
298
299   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
300   if (isDisplay) {
301     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
302     aDisplayer->RedisplayInLocalContext(theFeature,
303                                         aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
304                                         aPreviewOp->getSelectionModes(theFeature));
305   }
306   else {
307     //aDisplayer->CloseLocalContexts(false);
308     aDisplayer->Erase(anOperation->feature());
309   }
310 }
311
312 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
313 {
314   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
315   if (!anOperation)
316     return;
317
318   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
319   if (!aPreviewOp)
320     return;
321
322   boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
323   if (!aFeature || aFeature->getKind() != theCmdId)
324     return;
325
326   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
327                                                                      aList = aPreviewOp->preview();
328   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
329   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
330
331   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
332                                                          anIt = aList.begin(), aLast = aList.end();
333   for (; anIt != aLast; anIt++) {
334     boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
335     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
336     aDisplayer->RedisplayInLocalContext(aFeature,
337                                         aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
338                                         aModes, false);
339   }
340   aDisplayer->UpdateViewer();
341 }
342