]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
374b1d503bb2607c30e30610eeaeb09e4395df78
[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 #include <PartSet_TestOCC.h>
9
10 #include <ModuleBase_Operation.h>
11
12 #include <XGUI_MainWindow.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_Viewer.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_OperationMgr.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <XGUI_ViewPort.h>
19 #include <XGUI_ActionsMgr.h>
20 #include <XGUI_ViewerProxy.h>
21 #include <XGUI_ContextMenuMgr.h>
22 #include <XGUI_PropertyPanel.h>
23
24 #include <Config_PointerMessage.h>
25 #include <Config_ModuleReader.h>
26 #include <Config_WidgetReader.h>
27 #include <Events_Loop.h>
28 #include <Events_Message.h>
29 #include <Events_Error.h>
30
31 #include <GeomAPI_Shape.h>
32
33 #include <AIS_ListOfInteractive.hxx>
34
35 #include <QObject>
36 #include <QMouseEvent>
37 #include <QString>
38
39 #ifdef _DEBUG
40 #include <QDebug>
41 #endif
42
43 /*!Create and return new instance of XGUI_Module*/
44 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
45 {
46   return new PartSet_Module(theWshop);
47 }
48
49 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
50 {
51   myWorkshop = theWshop;
52   myListener = new PartSet_Listener(this);
53
54   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
55
56   connect(anOperationMgr, SIGNAL(operationStarted()),
57           this, SLOT(onOperationStarted()));
58
59   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
60           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
61
62   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
63   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
64           this, SLOT(onContextMenuCommand(const QString&, bool)));
65
66   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
67           this, SLOT(onMousePressed(QMouseEvent*)));
68   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
69           this, SLOT(onMouseReleased(QMouseEvent*)));
70   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
71           this, SLOT(onMouseMoved(QMouseEvent*)));
72   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
73           this, SLOT(onKeyRelease(QKeyEvent*)));
74 }
75
76 PartSet_Module::~PartSet_Module()
77 {
78 }
79
80 XGUI_Workshop* PartSet_Module::workshop() const
81 {
82   return myWorkshop;
83 }
84
85 void PartSet_Module::createFeatures()
86 {
87   Config_ModuleReader aXMLReader = Config_ModuleReader();
88   aXMLReader.readAll();
89   myFeaturesInFiles = aXMLReader.featuresInFiles();
90 }
91
92 void PartSet_Module::featureCreated(QAction* theFeature)
93 {
94   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
95 }
96
97 QStringList PartSet_Module::nestedFeatures(QString)
98 {
99   return QStringList();
100 }
101
102 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
103 {
104   return myFeaturesInFiles[theFeatureId];
105 }
106
107 /*
108  *
109  */
110 void PartSet_Module::onFeatureTriggered()
111 {
112   //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
113   //                                   myWorkshop->viewer()->activeView());
114
115   //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
116   //                                       myWorkshop->viewer()->activeView());
117   QAction* aCmd = dynamic_cast<QAction*>(sender());
118   //Do nothing on uncheck
119   if(aCmd->isCheckable() && !aCmd->isChecked())
120     return;
121   launchOperation(aCmd->data().toString());
122 }
123   
124 void PartSet_Module::launchOperation(const QString& theCmdId)
125 {
126   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
127   sendOperation(anOperation);
128 }
129
130 void PartSet_Module::onOperationStarted()
131 {
132   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
133                                        myWorkshop->operationMgr()->currentOperation());
134   if (aPreviewOp) {
135     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
136     connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
137             aPropPanel, SLOT(onFocusActivated(const std::string&)));
138   }
139 }
140
141 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
142 {
143   if (!theOperation)
144     return;
145   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
146   if (aPreviewOp) {
147     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
148     disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
149                aPropPanel, SLOT(onFocusActivated(const std::string&)));
150   }
151 }
152
153 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
154 {
155   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
156   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
157     editFeature(aFeatures.first());
158   }
159 }
160
161 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
162 {
163   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
164                                        myWorkshop->operationMgr()->currentOperation());
165   if (aPreviewOp)
166   {
167     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
168     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
169     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
170
171     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
172   }
173 }
174
175 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
176 {
177   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
178                                        myWorkshop->operationMgr()->currentOperation());
179   if (aPreviewOp)
180   {
181     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
183     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
184
185     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
186   }
187 }
188
189 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
190 {
191   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
192                                        myWorkshop->operationMgr()->currentOperation());
193   if (aPreviewOp)
194     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
195 }
196
197 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
198 {
199   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
200   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
201   if (aPreviewOp) {
202     aPreviewOp->keyReleased(theEvent->key());
203   }
204 }
205
206 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
207 {
208   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
209   myWorkshop->actionsMgr()->update();
210
211   //PartSet_TestOCC::testSelection(myWorkshop);
212 }
213
214 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
215 {
216   ModuleBase_Operation* anOperation = createOperation(theName.c_str());
217   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
218   if (aPreviewOp)
219   {
220     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
221       // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
222     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
223     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
224     aPreviewOp->init(theFeature, aSelected, aHighlighted);
225   }
226   sendOperation(anOperation);
227   myWorkshop->actionsMgr()->updateCheckState();
228 }
229
230 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
231 {
232   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
233   aViewer->enableMultiselection(theEnabled);
234 }
235
236 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
237 {
238   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
239   if (!isStop) {
240     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
241     boost::shared_ptr<ModelAPI_Feature> aFeature;
242     for (; anIt != aLast; anIt++) {
243       activateFeature((*anIt).feature(), false);
244     }
245   }
246   aDisplayer->StopSelection(theFeatures, isStop, false);
247
248   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
249   aViewer->enableSelection(!isStop);
250
251   aDisplayer->UpdateViewer();
252 }
253
254 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
255 {
256   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
257   aDisplayer->SetSelected(theFeatures, false);
258   aDisplayer->UpdateViewer();
259 }
260
261 void PartSet_Module::onCloseLocalContext()
262 {
263   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
264   aDisplayer->CloseLocalContexts();
265 }
266
267 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
268                                           int theMode)
269 {
270   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
271   visualizePreview(theFeature, isDisplay, false);
272   if (!isDisplay) {
273     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
274     boost::shared_ptr<ModelAPI_Feature> aSketch;
275     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
276     if (aPrevOp) {
277       std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
278                                                                          aList = aPrevOp->subPreview();
279       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
280       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
281
282       std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
283                                                              anIt = aList.begin(), aLast = aList.end();
284       for (; anIt != aLast; anIt++) {
285         boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
286         visualizePreview(aFeature, false, false);
287       }
288       aDisplayer->UpdateViewer();
289     }
290   }
291
292   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
293       theMode == PartSet_OperationSketchBase::FM_Deactivation)
294     activateFeature(theFeature, true);
295 }
296
297 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
298 {
299   // get operation xml description
300   std::string aStdCmdId = theCmdId;
301   if (aStdCmdId == PartSet_OperationEditLine::Type())
302     aStdCmdId = PartSet_OperationSketchLine::Type();
303   std::string aPluginFileName = featureFile(aStdCmdId);
304   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
305   aWdgReader.readAll();
306   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
307   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
308
309   // create the operation
310   ModuleBase_Operation* anOperation;
311   if (theCmdId == PartSet_OperationSketch::Type()) {
312     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
313   }
314   else if(theCmdId == PartSet_OperationSketchLine::Type() ||
315           theCmdId == PartSet_OperationEditLine::Type()) {
316     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
317     boost::shared_ptr<ModelAPI_Feature> aSketch;
318     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
319     if (aPrevOp)
320       aSketch = aPrevOp->sketch();
321     if (theCmdId == PartSet_OperationSketchLine::Type())
322       anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
323     else
324       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
325   }
326   else {
327     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
328   }
329   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
330   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
331
332   // connect the operation
333   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
334   if (aPreviewOp) {
335     connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
336             this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
337     connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
338             this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
339     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
340             this, SLOT(onMultiSelectionEnabled(bool)));
341
342     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
343             this, SLOT(onMultiSelectionEnabled(bool)));
344     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
345             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
346     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
347             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
348
349      connect(aPreviewOp, SIGNAL(closeLocalContext()),
350              this, SLOT(onCloseLocalContext()));
351
352     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
353     if (aSketchOp) {
354       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
355               this, SLOT(onPlaneSelected(double, double, double)));
356     }
357   }
358
359   return anOperation;
360 }
361
362 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
363 {
364   //TODO(sbh): Implement static method to extract event id [SEID]
365   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
366   Config_PointerMessage aMessage(aModuleEvent, this);
367   aMessage.setPointer(theOperation);
368   Events_Loop::loop()->send(aMessage);
369 }
370
371 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
372                                       const bool isUpdateViewer)
373 {
374   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
375   if (!anOperation)
376     return;
377
378   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
379   if (!aPreviewOp)
380     return;
381
382   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
383   if (isDisplay) {
384     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
385     aDisplayer->Redisplay(theFeature,
386                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
387   }
388   else
389     aDisplayer->Erase(theFeature, false);
390
391   if (isUpdateViewer)
392     aDisplayer->UpdateViewer();
393 }
394
395 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
396                                      const bool isUpdateViewer)
397 {
398   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
399   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
400   if (aPreviewOp) {
401     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
402     aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
403                                        isUpdateViewer);
404   }
405 }
406
407 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
408 {
409   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
410   if (!anOperation)
411     return;
412
413   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
414   if (!aPreviewOp)
415     return;
416
417   boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
418   if (!aFeature || aFeature->getKind() != theCmdId)
419     return;
420
421   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
422                                                                      aList = aPreviewOp->subPreview();
423   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
424   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
425
426   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
427                                                          anIt = aList.begin(), aLast = aList.end();
428   for (; anIt != aLast; anIt++) {
429     boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
430     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
431     aDisplayer->Redisplay(aFeature,
432                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
433     aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
434   }
435   aDisplayer->UpdateViewer();
436 }
437
438 void PartSet_Module::editFeature(FeaturePtr theFeature)
439 {
440   if (!theFeature)
441     return;
442
443   if (theFeature->getKind() == "Sketch") {
444     onLaunchOperation(theFeature->getKind(), theFeature);
445     updateCurrentPreview(theFeature->getKind());
446   }
447 }