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