]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
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 <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
376   anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
377
378   // connect the operation
379   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
380   if (aPreviewOp) {
381     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
382             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
383     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
384             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
385     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
386             this, SLOT(onMultiSelectionEnabled(bool)));
387
388     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
389             this, SLOT(onMultiSelectionEnabled(bool)));
390     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
391             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
392     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
393             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
394
395      connect(aPreviewOp, SIGNAL(closeLocalContext()),
396              this, SLOT(onCloseLocalContext()));
397
398     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
399     if (aSketchOp) {
400       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
401               this, SLOT(onPlaneSelected(double, double, double)));
402       connect(aSketchOp, SIGNAL(fitAllView()),
403               this, SLOT(onFitAllView()));
404     }
405   }
406
407   return anOperation;
408 }
409
410 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
411 {
412   //TODO(sbh): Implement static method to extract event id [SEID]
413   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
414   Config_PointerMessage aMessage(aModuleEvent, this);
415   aMessage.setPointer(theOperation);
416   Events_Loop::loop()->send(aMessage);
417 }
418
419 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
420                                       const bool isUpdateViewer)
421 {
422   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
423   if (!anOperation)
424     return;
425
426   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
427   if (!aPreviewOp)
428     return;
429
430   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
431   if (isDisplay) {
432     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
433     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
434                            theFeature, aPreviewOp->sketch(),
435                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
436                            aDisplayer->getAISObject(theFeature));
437
438     aDisplayer->redisplay(theFeature, anAIS, false);
439   }
440   else
441     aDisplayer->erase(theFeature, false);
442
443   if (isUpdateViewer)
444     aDisplayer->updateViewer();
445 }
446
447 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
448 {
449   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
450   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
451   if (aPreviewOp) {
452     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
453     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
454                                        isUpdateViewer);
455   }
456 }
457
458 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
459 {
460   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
461   if (!anOperation)
462     return;
463
464   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
465   if (!aPreviewOp)
466     return;
467
468   FeaturePtr aFeature = aPreviewOp->feature();
469   if (!aFeature || aFeature->getKind() != theCmdId)
470     return;
471
472   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
473   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
474   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
475
476   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
477                                                          anIt = aList.begin(), aLast = aList.end();
478   for (; anIt != aLast; anIt++) {
479     FeaturePtr aFeature = (*anIt).first;
480     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
481     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
482                            aFeature, aPreviewOp->sketch(),
483                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
484                            aDisplayer->getAISObject(aFeature));
485     if (!anAIS.IsNull())
486       aDisplayer->redisplay(aFeature, anAIS, false);
487     aDisplayer->activateInLocalContext(aFeature, 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 }