Salome HOME
Move generation of AIS presentation into SketchPlugin
[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
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_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_OperationEditFeature::Type())
349       anOperation = new PartSet_OperationEditFeature(theCmdId.c_str(), this, aSketch);
350     else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
351       anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
352     else if (theCmdId == PartSet_OperationEditConstraint::Type())
353       anOperation = new PartSet_OperationEditConstraint(theCmdId.c_str(), theFeatureKind, this, aSketch);
354   }
355
356   if (!anOperation) {
357     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
358   }
359
360   // set operation description and list of widgets corresponding to the feature xml definition
361   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
362
363   std::string aPluginFileName = featureFile(aFeatureKind);
364   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
365   aWdgReader.readAll();
366   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
367   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
368
369   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
370   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
371   //                                                             myWorkshop->moduleConnector());
372   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
373   //qDeleteAll(aContent->children());
374   //aFactory.createWidget(aContent);
375
376   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
377   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
378
379   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
380
381   // connect the operation
382   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
383   if (aPreviewOp) {
384     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
385             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
386     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
387             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
388     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
389             this, SLOT(onMultiSelectionEnabled(bool)));
390
391     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
392             this, SLOT(onMultiSelectionEnabled(bool)));
393     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
394             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
395     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
396             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
397
398      connect(aPreviewOp, SIGNAL(closeLocalContext()),
399              this, SLOT(onCloseLocalContext()));
400
401     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
402     if (aSketchOp) {
403       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
404               this, SLOT(onPlaneSelected(double, double, double)));
405       connect(aSketchOp, SIGNAL(fitAllView()),
406               this, SLOT(onFitAllView()));
407     }
408   }
409
410   return anOperation;
411 }
412
413 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
414 {
415   //TODO(sbh): Implement static method to extract event id [SEID]
416   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
417   Config_PointerMessage aMessage(aModuleEvent, this);
418   aMessage.setPointer(theOperation);
419   Events_Loop::loop()->send(aMessage);
420 }
421
422 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
423                                       const bool isUpdateViewer)
424 {
425   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
426   if (!anOperation)
427     return;
428
429   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
430   if (!aPreviewOp)
431     return;
432
433   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
434   if (isDisplay) {
435     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
436       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
437     if (aSPFeature)
438     {
439       Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(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::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
476   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
477   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
478
479   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
480                                                          anIt = aList.begin(), aLast = aList.end();
481   for (; anIt != aLast; anIt++) {
482     FeaturePtr aFeature = (*anIt).first;
483     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
484       boost::dynamic_pointer_cast<SketchPlugin_Feature>((*anIt).first);
485     if (!aSPFeature)
486       continue;
487
488     Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(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 }