Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 <PartSet_OperationFeatureCreate.h>
8 #include <ModuleBase_Operation.h>
9 #include <ModuleBase_OperationDescription.h>
10 #include <ModuleBase_WidgetFactory.h>
11 #include <PartSet_Listener.h>
12 #include <PartSet_TestOCC.h>
13 #include <PartSet_Presentation.h>
14
15 #include <ModuleBase_Operation.h>
16 #include <ModelAPI_Object.h>
17
18 #include <XGUI_MainWindow.h>
19 #include <XGUI_Displayer.h>
20 #include <XGUI_Viewer.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_OperationMgr.h>
23 #include <XGUI_SelectionMgr.h>
24 #include <XGUI_ViewPort.h>
25 #include <XGUI_ActionsMgr.h>
26 #include <XGUI_ViewerProxy.h>
27 #include <XGUI_ContextMenuMgr.h>
28 #include <XGUI_PropertyPanel.h>
29 #include <XGUI_ModuleConnector.h>
30 #include <XGUI_Tools.h>
31
32 #include <SketchPlugin_Line.h>
33
34 #include <Config_PointerMessage.h>
35 #include <Config_ModuleReader.h>
36 #include <Config_WidgetReader.h>
37 #include <Events_Loop.h>
38 #include <Events_Message.h>
39 #include <Events_Error.h>
40
41 #include <GeomAPI_Shape.h>
42
43 #include <AIS_ListOfInteractive.hxx>
44 //#include <AIS_DimensionSelectionMode.hxx>
45
46 #include <QObject>
47 #include <QMouseEvent>
48 #include <QString>
49
50 #ifdef _DEBUG
51 #include <QDebug>
52 #endif
53
54 /*!Create and return new instance of XGUI_Module*/
55 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
56 {
57   return new PartSet_Module(theWshop);
58 }
59
60 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
61 {
62   myWorkshop = theWshop;
63   myListener = new PartSet_Listener(this);
64
65   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
66
67   connect(anOperationMgr, SIGNAL(operationStarted()),
68           this, SLOT(onOperationStarted()));
69
70   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
71           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
72
73   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
74   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
75           this, SLOT(onContextMenuCommand(const QString&, bool)));
76
77   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
78           this, SLOT(onMousePressed(QMouseEvent*)));
79   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
80           this, SLOT(onMouseReleased(QMouseEvent*)));
81   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
82           this, SLOT(onMouseMoved(QMouseEvent*)));
83   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
84           this, SLOT(onKeyRelease(QKeyEvent*)));
85   connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
86           this, SLOT(onMouseDoubleClick(QMouseEvent*)));
87
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 }
154
155 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
156 {
157   if (!theOperation)
158     return;
159   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
160   if (aPreviewOp) {
161     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
162     disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
163                aPropPanel, SLOT(onFocusActivated(const std::string&)));
164   }
165 }
166
167 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
168 {
169   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
170   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
171     editFeature(aFeatures.first());
172   }
173 }
174
175 void PartSet_Module::onMousePressed(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->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
186   }
187 }
188
189 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
190 {
191   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
192                                        myWorkshop->operationMgr()->currentOperation());
193   if (aPreviewOp)
194   {
195     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
196     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
197     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
198
199     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
200   }
201 }
202
203 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
204 {
205   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
206                                        myWorkshop->operationMgr()->currentOperation());
207   if (aPreviewOp)
208     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
209 }
210
211 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
212 {
213   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
214   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
215   if (aPreviewOp) {
216     aPreviewOp->keyReleased(theEvent->key());
217   }
218 }
219
220 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
221 {
222   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
223                                        myWorkshop->operationMgr()->currentOperation());
224   if (aPreviewOp)
225   {
226     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
227     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
228     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
229     aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
230                                  aHighlighted);
231   }
232 }
233
234 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
235 {
236   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
237   myWorkshop->actionsMgr()->update();
238
239   //PartSet_TestOCC::testSelection(myWorkshop);
240 }
241
242 void PartSet_Module::onFitAllView()
243 {
244   myWorkshop->viewer()->fitAll();
245 }
246
247 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
248 {
249   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
250                                                       theFeature ? theFeature->getKind() : "");
251   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
252   if (aPreviewOp)
253   {
254     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
255     // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
256     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
257     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
258     aPreviewOp->init(theFeature, aSelected, aHighlighted);
259   } else {
260     anOperation->setEditingFeature(theFeature);
261   }
262   sendOperation(anOperation);
263   myWorkshop->actionsMgr()->updateCheckState();
264 }
265
266 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
267 {
268   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
269   aViewer->enableMultiselection(theEnabled);
270 }
271
272 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
273 {
274   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
275   if (!isStop) {
276     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
277     FeaturePtr aFeature;
278     for (; anIt != aLast; anIt++) {
279       activateFeature((*anIt).feature(), false);
280     }
281   }
282   aDisplayer->stopSelection(theFeatures, isStop, false);
283
284   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
285   aViewer->enableSelection(!isStop);
286
287   aDisplayer->updateViewer();
288 }
289
290 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
291 {
292   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
293   aDisplayer->setSelected(theFeatures, false);
294   aDisplayer->updateViewer();
295 }
296
297 void PartSet_Module::onCloseLocalContext()
298 {
299   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
300   aDisplayer->closeLocalContexts();
301 }
302
303 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
304 {
305   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
306   visualizePreview(theFeature, isDisplay, false);
307   if (!isDisplay) {
308     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
309     FeaturePtr aSketch;
310     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
311     if (aPrevOp) {
312       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
313       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
314       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
315
316       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
317                                                              anIt = aList.begin(), aLast = aList.end();
318       for (; anIt != aLast; anIt++) {
319         FeaturePtr aFeature = (*anIt).first;
320         visualizePreview(aFeature, false, false);
321       }
322       aDisplayer->updateViewer();
323     }
324   }
325
326   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
327       theMode == PartSet_OperationSketchBase::FM_Deactivation)
328     activateFeature(theFeature, true);
329 }
330
331 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
332                                                       const std::string& theFeatureKind)
333 {
334   // create the operation
335   ModuleBase_Operation* anOperation = 0;
336   if (theCmdId == PartSet_OperationSketch::Type()) {
337     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
338   }
339   else {
340     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
341     FeaturePtr aSketch;
342     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
343     if (aPrevOp)
344       aSketch = aPrevOp->sketch();
345     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
346       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
347     else if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
348       anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
349     else if (theCmdId == PartSet_OperationEditFeature::Type())
350       anOperation = new PartSet_OperationEditFeature(theCmdId.c_str(), this, aSketch);
351     else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
352       anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
353     else if (theCmdId == PartSet_OperationEditConstraint::Type())
354       anOperation = new PartSet_OperationEditConstraint(theCmdId.c_str(), theFeatureKind, 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 std::list<XGUI_ViewerPrs>&, const bool)),
395             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
396     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
397             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
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<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
437     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
438                            theFeature, aPreviewOp->sketch(),
439                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
440                            aDisplayer->getAISObject(theFeature));
441
442     aDisplayer->redisplay(theFeature, anAIS, false);
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<GeomAPI_Shape> aPreview = (*anIt).second;
485     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
486                            aFeature, aPreviewOp->sketch(),
487                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
488                            aDisplayer->getAISObject(aFeature));
489     if (!anAIS.IsNull())
490       aDisplayer->redisplay(aFeature, anAIS, false);
491     aDisplayer->activateInLocalContext(aFeature, aModes, false);
492   }
493   aDisplayer->updateViewer();
494 }
495
496 void PartSet_Module::editFeature(FeaturePtr theFeature)
497 {
498   if (!theFeature)
499     return;
500
501 //  if (theFeature->getKind() == SKETCH_KIND) {
502     FeaturePtr aFeature = theFeature;
503     if (XGUI_Tools::isModelObject(aFeature)) {
504       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
505       aFeature = aObject->featureRef();
506     }
507
508     if (aFeature) {
509       onLaunchOperation(aFeature->getKind(), aFeature);
510       updateCurrentPreview(aFeature->getKind());
511     }
512 //  }
513 }