Salome HOME
1266a0cb1e503eba2ada7e35c8823c4d6c2ddd74
[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   sendOperation(anOperation);
142 }
143
144 void PartSet_Module::onOperationStarted()
145 {
146   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
147                                        myWorkshop->operationMgr()->currentOperation());
148   if (aPreviewOp) {
149     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
150     connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
151             aPropPanel, SLOT(onFocusActivated(const std::string&)));
152
153     connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
154       this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
155   }
156 }
157
158 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
159 {
160   if (!theOperation)
161     return;
162   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
163   if (aPreviewOp) {
164     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
165     disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
166                aPropPanel, SLOT(onFocusActivated(const std::string&)));
167     //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
168     //           this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
169   }
170 }
171
172 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
173 {
174   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
175   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
176     editFeature(aFeatures.first());
177   }
178 }
179
180 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
181 {
182   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
183                                        myWorkshop->operationMgr()->currentOperation());
184   if (aPreviewOp)
185   {
186     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
187     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
188     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
189
190     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
191   }
192 }
193
194 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
195 {
196   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
197                                        myWorkshop->operationMgr()->currentOperation());
198   if (aPreviewOp)
199   {
200     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
201     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
202     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
203
204     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
205   }
206 }
207
208 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
209 {
210   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
211                                        myWorkshop->operationMgr()->currentOperation());
212   if (aPreviewOp)
213     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
214 }
215
216 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
217 {
218   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
219   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
220   if (aPreviewOp) {
221     aPreviewOp->keyReleased(theEvent->key());
222   }
223 }
224
225 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
226 {
227   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
228                                        myWorkshop->operationMgr()->currentOperation());
229   if (aPreviewOp)
230   {
231     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
232     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
233     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
234     aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
235                                  aHighlighted);
236   }
237 }
238
239 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
240 {
241   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
242   myWorkshop->actionsMgr()->update();
243
244   //PartSet_TestOCC::testSelection(myWorkshop);
245 }
246
247 void PartSet_Module::onFitAllView()
248 {
249   myWorkshop->viewer()->fitAll();
250 }
251
252 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
253 {
254   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
255                                                       theFeature ? theFeature->getKind() : "");
256   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
257   if (aPreviewOp)
258   {
259     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
260     // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
261     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
262     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
263     aPreviewOp->init(theFeature, aSelected, aHighlighted);
264   } else {
265     anOperation->setEditingFeature(theFeature);
266   }
267   sendOperation(anOperation);
268   myWorkshop->actionsMgr()->updateCheckState();
269 }
270
271 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
272 {
273   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
274   aViewer->enableMultiselection(theEnabled);
275 }
276
277 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
278 {
279   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
280   if (!isStop) {
281     QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
282     for (; anIt != aLast; anIt++) {
283       activateFeature((*anIt), false);
284     }
285   }
286   aDisplayer->stopSelection(theFeatures, isStop, false);
287
288   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
289   aViewer->enableSelection(!isStop);
290
291   aDisplayer->updateViewer();
292 }
293
294 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
295 {
296   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
297   aDisplayer->setSelected(theFeatures, false);
298   aDisplayer->updateViewer();
299 }
300
301 void PartSet_Module::onCloseLocalContext()
302 {
303   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
304   aDisplayer->closeLocalContexts();
305 }
306
307 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
308 {
309   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
310   visualizePreview(theFeature, isDisplay, false);
311   if (!isDisplay) {
312     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
313     FeaturePtr aSketch;
314     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
315     if (aPrevOp) {
316       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
317       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
318       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
319
320       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
321                                                              anIt = aList.begin(), aLast = aList.end();
322       for (; anIt != aLast; anIt++) {
323         FeaturePtr aFeature = (*anIt).first;
324         visualizePreview(aFeature, false, false);
325       }
326       aDisplayer->updateViewer();
327     }
328   }
329
330   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
331       theMode == PartSet_OperationSketchBase::FM_Deactivation)
332     activateFeature(theFeature, true);
333 }
334
335 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
336                                                       const std::string& theFeatureKind)
337 {
338   // create the operation
339   ModuleBase_Operation* anOperation = 0;
340   if (theCmdId == PartSet_OperationSketch::Type()) {
341     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
342   }
343   else {
344     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
345     FeaturePtr aSketch;
346     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
347     if (aPrevOp)
348       aSketch = aPrevOp->sketch();
349     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
350       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
351     else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
352                 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
353     else if (theCmdId == PartSet_OperationFeatureEdit::Type())
354       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
355   }
356
357   if (!anOperation) {
358     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
359   }
360
361   // set operation description and list of widgets corresponding to the feature xml definition
362   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
363
364   std::string aPluginFileName = featureFile(aFeatureKind);
365   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
366   aWdgReader.readAll();
367   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
368   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
369
370   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
371   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
372   //                                                             myWorkshop->moduleConnector());
373   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
374   //qDeleteAll(aContent->children());
375   //aFactory.createWidget(aContent);
376
377   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
378   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
379
380   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
381
382   // connect the operation
383   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
384   if (aPreviewOp) {
385     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
386             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
387     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
388             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
389     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
390             this, SLOT(onMultiSelectionEnabled(bool)));
391
392     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
393             this, SLOT(onMultiSelectionEnabled(bool)));
394     connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
395             this, SLOT(onStopSelection(const QFeatureList&, const bool)));
396     connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
397             this, SLOT(onSetSelection(const QFeatureList&)));
398
399      connect(aPreviewOp, SIGNAL(closeLocalContext()),
400              this, SLOT(onCloseLocalContext()));
401
402     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
403     if (aSketchOp) {
404       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
405               this, SLOT(onPlaneSelected(double, double, double)));
406       connect(aSketchOp, SIGNAL(fitAllView()),
407               this, SLOT(onFitAllView()));
408     }
409   }
410
411   return anOperation;
412 }
413
414 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
415 {
416   //TODO(sbh): Implement static method to extract event id [SEID]
417   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
418   Config_PointerMessage aMessage(aModuleEvent, this);
419   aMessage.setPointer(theOperation);
420   Events_Loop::loop()->send(aMessage);
421 }
422
423 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
424                                       const bool isUpdateViewer)
425 {
426   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
427   if (!anOperation)
428     return;
429
430   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
431   if (!aPreviewOp)
432     return;
433
434   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
435   if (isDisplay) {
436     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
437       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
438     if (aSPFeature)
439     {
440       Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(theFeature));
441       aDisplayer->redisplay(theFeature, anAIS, false);
442     }
443   }
444   else
445     aDisplayer->erase(theFeature, false);
446
447   if (isUpdateViewer)
448     aDisplayer->updateViewer();
449 }
450
451 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
452 {
453   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
454   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
455   if (aPreviewOp) {
456     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
457     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
458                                        isUpdateViewer);
459   }
460 }
461
462 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
463 {
464   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
465   if (!anOperation)
466     return;
467
468   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
469   if (!aPreviewOp)
470     return;
471
472   FeaturePtr aFeature = aPreviewOp->feature();
473   if (!aFeature || aFeature->getKind() != theCmdId)
474     return;
475
476   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
477   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
478   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
479
480   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
481                                                          anIt = aList.begin(), aLast = aList.end();
482   for (; anIt != aLast; anIt++) {
483     FeaturePtr aFeature = (*anIt).first;
484     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
485       boost::dynamic_pointer_cast<SketchPlugin_Feature>((*anIt).first);
486     if (!aSPFeature)
487       continue;
488
489     Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(aFeature));
490     if (!anAIS.IsNull())
491       aDisplayer->redisplay(aFeature, anAIS, false);
492     aDisplayer->activateInLocalContext(aFeature, aModes, false);
493   }
494   aDisplayer->updateViewer();
495 }
496
497 void PartSet_Module::editFeature(FeaturePtr theFeature)
498 {
499   if (!theFeature)
500     return;
501
502 //  if (theFeature->getKind() == SKETCH_KIND) {
503     FeaturePtr aFeature = theFeature;
504     if (XGUI_Tools::isModelObject(aFeature)) {
505       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
506       aFeature = aObject->featureRef();
507     }
508
509     if (aFeature) {
510       onLaunchOperation(aFeature->getKind(), aFeature);
511       updateCurrentPreview(aFeature->getKind());
512     }
513 //  }
514 }
515
516 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
517 {
518   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
519                                        myWorkshop->operationMgr()->currentOperation());
520   if (!aPreviewOp)
521     return;
522
523   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
524         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
525
526   PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
527                                 aPoint->x(), aPoint->y());
528 }