Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[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 #include <GeomAPI_AISObject.h>
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 PartSet_Module::~PartSet_Module()
89 {
90 }
91
92 XGUI_Workshop* PartSet_Module::workshop() const
93 {
94   return myWorkshop;
95 }
96
97 void PartSet_Module::createFeatures()
98 {
99   Config_ModuleReader aXMLReader = Config_ModuleReader();
100   aXMLReader.readAll();
101   myFeaturesInFiles = aXMLReader.featuresInFiles();
102 }
103
104 void PartSet_Module::featureCreated(QAction* theFeature)
105 {
106   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
107 }
108
109 QStringList PartSet_Module::nestedFeatures(QString)
110 {
111   return QStringList();
112 }
113
114 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
115 {
116   return myFeaturesInFiles[theFeatureId];
117 }
118
119 /*
120  *
121  */
122 void PartSet_Module::onFeatureTriggered()
123 {
124   //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
125   //                                   myWorkshop->viewer()->activeView());
126
127   //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
128   //                                       myWorkshop->viewer()->activeView());
129   QAction* aCmd = dynamic_cast<QAction*>(sender());
130   //Do nothing on uncheck
131   if(aCmd->isCheckable() && !aCmd->isChecked())
132     return;
133   launchOperation(aCmd->data().toString());
134 }
135   
136 void PartSet_Module::launchOperation(const QString& theCmdId)
137 {
138   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
139   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
140   if (aPreviewOp) {
141     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
142     // Initialise operation with preliminary selection
143     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
144     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
145     aPreviewOp->initSelection(aSelected, aHighlighted);
146   } 
147   sendOperation(anOperation);
148 }
149
150 void PartSet_Module::onOperationStarted()
151 {
152   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
153                                        myWorkshop->operationMgr()->currentOperation());
154   if (aPreviewOp) {
155     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
156     connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
157       this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
158   }
159 }
160
161 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
162 {
163   if (!theOperation)
164     return;
165   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
166   if (aPreviewOp) {
167     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
168     //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
169     //           this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
170   }
171 }
172
173 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
174 {
175   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
176   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
177     editFeature(aFeatures.first());
178   }
179 }
180
181 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
182 {
183   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
184                                        myWorkshop->operationMgr()->currentOperation());
185   if (aPreviewOp)
186   {
187     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
188     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
189     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
190
191     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
192   }
193 }
194
195 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
196 {
197   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
198                                        myWorkshop->operationMgr()->currentOperation());
199   if (aPreviewOp)
200   {
201     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
202     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
203     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
204
205     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
206   }
207 }
208
209 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
210 {
211   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
212                                        myWorkshop->operationMgr()->currentOperation());
213   if (aPreviewOp)
214     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
215 }
216
217 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
218 {
219   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
220   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
221   if (aPreviewOp) {
222     aPreviewOp->keyReleased(theEvent->key());
223   }
224 }
225
226 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
227 {
228   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
229                                        myWorkshop->operationMgr()->currentOperation());
230   if (aPreviewOp)
231   {
232     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
233     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
234     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
235     aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
236                                  aHighlighted);
237   }
238 }
239
240 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
241 {
242   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
243   myWorkshop->actionsMgr()->update();
244
245   //PartSet_TestOCC::testSelection(myWorkshop);
246 }
247
248 void PartSet_Module::onFitAllView()
249 {
250   myWorkshop->viewer()->fitAll();
251 }
252
253 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
254 {
255   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
256                                                       theFeature ? theFeature->getKind() : "");
257   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
258   if (aPreviewOp)
259   {
260     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
261     // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
262     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
263     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
264     aPreviewOp->initFeature(theFeature);
265     aPreviewOp->initSelection(aSelected, aHighlighted);
266   } else {
267     anOperation->setEditingFeature(theFeature);
268   }
269   sendOperation(anOperation);
270   myWorkshop->actionsMgr()->updateCheckState();
271 }
272
273 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
274 {
275   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
276   aViewer->enableMultiselection(theEnabled);
277 }
278
279 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
280 {
281   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
282   if (!isStop) {
283     QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
284     for (; anIt != aLast; anIt++) {
285       activateFeature((*anIt), false);
286     }
287   }
288   aDisplayer->stopSelection(theFeatures, isStop, false);
289
290   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
291   aViewer->enableSelection(!isStop);
292
293   aDisplayer->updateViewer();
294 }
295
296 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
297 {
298   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
299   aDisplayer->setSelected(theFeatures, false);
300   aDisplayer->updateViewer();
301 }
302
303 void PartSet_Module::onCloseLocalContext()
304 {
305   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
306   aDisplayer->closeLocalContexts();
307 }
308
309 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
310 {
311   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
312   visualizePreview(theFeature, isDisplay, false);
313   if (!isDisplay) {
314     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
315     FeaturePtr aSketch;
316     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
317     if (aPrevOp) {
318       std::list<FeaturePtr> aList = aPrevOp->subFeatures();
319       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
320       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
321
322       std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
323                                             aLast = aList.end();
324       for (; anIt != aLast; anIt++)
325         visualizePreview(*anIt, false, false);
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(stopSelection(const QFeatureList&, const bool)),
393             this, SLOT(onStopSelection(const QFeatureList&, const bool)));
394     connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
395             this, SLOT(onSetSelection(const QFeatureList&)));
396
397      connect(aPreviewOp, SIGNAL(closeLocalContext()),
398              this, SLOT(onCloseLocalContext()));
399
400     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
401     if (aSketchOp) {
402       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
403               this, SLOT(onPlaneSelected(double, double, double)));
404       connect(aSketchOp, SIGNAL(fitAllView()),
405               this, SLOT(onFitAllView()));
406     }
407   }
408
409   return anOperation;
410 }
411
412 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
413 {
414   //TODO(sbh): Implement static method to extract event id [SEID]
415   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
416   Config_PointerMessage aMessage(aModuleEvent, this);
417   aMessage.setPointer(theOperation);
418   Events_Loop::loop()->send(aMessage);
419 }
420
421 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
422                                       const bool isUpdateViewer)
423 {
424   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
425   if (!anOperation)
426     return;
427
428   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
429   if (!aPreviewOp)
430     return;
431
432   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
433   if (isDisplay) {
434     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
435       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
436     if (aSPFeature)
437     {
438       boost::shared_ptr<GeomAPI_AISObject> anAIS = 
439         aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
440       aDisplayer->redisplay(theFeature, anAIS, false);
441     }
442   }
443   else
444     aDisplayer->erase(theFeature, false);
445
446   if (isUpdateViewer)
447     aDisplayer->updateViewer();
448 }
449
450 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
451 {
452   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
453   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
454   if (aPreviewOp) {
455     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
456     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
457                                        isUpdateViewer);
458   }
459 }
460
461 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
462 {
463   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
464   if (!anOperation)
465     return;
466
467   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
468   if (!aPreviewOp)
469     return;
470
471   FeaturePtr aFeature = aPreviewOp->feature();
472   if (!aFeature || aFeature->getKind() != theCmdId)
473     return;
474
475   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
476   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
477   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
478
479   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), 
480                                         aLast = aList.end();
481   for (; anIt != aLast; anIt++) {
482     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
483       boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
484     if (!aSPFeature)
485       continue;
486     visualizePreview(*anIt, true, false);
487     aDisplayer->activateInLocalContext(*anIt, aModes, false);
488   }
489   aDisplayer->updateViewer();
490 }
491
492 void PartSet_Module::editFeature(FeaturePtr theFeature)
493 {
494   if (!theFeature)
495     return;
496
497 //  if (theFeature->getKind() == SKETCH_KIND) {
498     FeaturePtr aFeature = theFeature;
499     if (XGUI_Tools::isModelObject(aFeature)) {
500       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
501       aFeature = aObject->featureRef();
502     }
503
504     if (aFeature) {
505       onLaunchOperation(aFeature->getKind(), aFeature);
506       updateCurrentPreview(aFeature->getKind());
507     }
508 //  }
509 }
510
511 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
512 {
513   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
514                                        myWorkshop->operationMgr()->currentOperation());
515   if (!aPreviewOp)
516     return;
517
518   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
519         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
520
521   PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
522                                 aPoint->x(), aPoint->y());
523 }