]> 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_OperationEditConstraint.h>
5 #include <PartSet_OperationCreateConstraint.h>
6 #include <PartSet_OperationFeatureCreate.h>
7 #include <PartSet_OperationFeatureEditMulti.h>
8 #include <PartSet_OperationFeatureEdit.h>
9 #include <ModuleBase_Operation.h>
10 #include <ModuleBase_OperationDescription.h>
11 #include <ModuleBase_WidgetFactory.h>
12 #include <PartSet_Listener.h>
13 #include <PartSet_TestOCC.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();
257     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
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 QFeatureList& theFeatures, const bool isStop)
273 {
274   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
275   if (!isStop) {
276     QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
277     for (; anIt != aLast; anIt++) {
278       activateFeature((*anIt), 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 QFeatureList& 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_OperationFeatureCreate::canProcessKind(theCmdId))
345       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
346         else if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
347       anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
348         else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
349                 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
350     else if (theCmdId == PartSet_OperationFeatureEdit::Type())
351       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
352     else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
353       anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
354     else if (theCmdId == PartSet_OperationEditConstraint::Type())
355       anOperation = new PartSet_OperationEditConstraint(theCmdId.c_str(), theFeatureKind, this, aSketch);
356   }
357
358   if (!anOperation) {
359     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
360   }
361
362   // set operation description and list of widgets corresponding to the feature xml definition
363   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
364
365   std::string aPluginFileName = featureFile(aFeatureKind);
366   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
367   aWdgReader.readAll();
368   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
369   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
370
371   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
372   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
373   //                                                             myWorkshop->moduleConnector());
374   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
375   //qDeleteAll(aContent->children());
376   //aFactory.createWidget(aContent);
377
378   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
379   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
380
381   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
382
383   // connect the operation
384   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
385   if (aPreviewOp) {
386     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
387             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
388     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
389             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
390     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
391             this, SLOT(onMultiSelectionEnabled(bool)));
392
393     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
394             this, SLOT(onMultiSelectionEnabled(bool)));
395     connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
396             this, SLOT(onStopSelection(const QFeatureList&, const bool)));
397     connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
398             this, SLOT(onSetSelection(const QFeatureList&)));
399
400      connect(aPreviewOp, SIGNAL(closeLocalContext()),
401              this, SLOT(onCloseLocalContext()));
402
403     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
404     if (aSketchOp) {
405       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
406               this, SLOT(onPlaneSelected(double, double, double)));
407       connect(aSketchOp, SIGNAL(fitAllView()),
408               this, SLOT(onFitAllView()));
409     }
410   }
411
412   return anOperation;
413 }
414
415 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
416 {
417   //TODO(sbh): Implement static method to extract event id [SEID]
418   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
419   Config_PointerMessage aMessage(aModuleEvent, this);
420   aMessage.setPointer(theOperation);
421   Events_Loop::loop()->send(aMessage);
422 }
423
424 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
425                                       const bool isUpdateViewer)
426 {
427   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
428   if (!anOperation)
429     return;
430
431   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
432   if (!aPreviewOp)
433     return;
434
435   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
436   if (isDisplay) {
437     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
438       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
439     if (aSPFeature)
440     {
441       Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(theFeature));
442       aDisplayer->redisplay(theFeature, anAIS, false);
443     }
444   }
445   else
446     aDisplayer->erase(theFeature, false);
447
448   if (isUpdateViewer)
449     aDisplayer->updateViewer();
450 }
451
452 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
453 {
454   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
455   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
456   if (aPreviewOp) {
457     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
458     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
459                                        isUpdateViewer);
460   }
461 }
462
463 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
464 {
465   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
466   if (!anOperation)
467     return;
468
469   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
470   if (!aPreviewOp)
471     return;
472
473   FeaturePtr aFeature = aPreviewOp->feature();
474   if (!aFeature || aFeature->getKind() != theCmdId)
475     return;
476
477   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
478   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
479   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
480
481   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
482                                                          anIt = aList.begin(), aLast = aList.end();
483   for (; anIt != aLast; anIt++) {
484     FeaturePtr aFeature = (*anIt).first;
485     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
486       boost::dynamic_pointer_cast<SketchPlugin_Feature>((*anIt).first);
487     if (!aSPFeature)
488       continue;
489
490     Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(aFeature));
491     if (!anAIS.IsNull())
492       aDisplayer->redisplay(aFeature, anAIS, false);
493     aDisplayer->activateInLocalContext(aFeature, aModes, false);
494   }
495   aDisplayer->updateViewer();
496 }
497
498 void PartSet_Module::editFeature(FeaturePtr theFeature)
499 {
500   if (!theFeature)
501     return;
502
503 //  if (theFeature->getKind() == SKETCH_KIND) {
504     FeaturePtr aFeature = theFeature;
505     if (XGUI_Tools::isModelObject(aFeature)) {
506       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
507       aFeature = aObject->featureRef();
508     }
509
510     if (aFeature) {
511       onLaunchOperation(aFeature->getKind(), aFeature);
512       updateCurrentPreview(aFeature->getKind());
513     }
514 //  }
515 }