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