Salome HOME
Merge branch 'master' of newgeom:newgeom
[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_OperationConstraint.h>
6 #include <ModuleBase_Operation.h>
7 #include <ModuleBase_OperationDescription.h>
8 #include <PartSet_Listener.h>
9 #include <PartSet_TestOCC.h>
10 #include <PartSet_Presentation.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModelAPI_Object.h>
14
15 #include <XGUI_MainWindow.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_Viewer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_OperationMgr.h>
20 #include <XGUI_SelectionMgr.h>
21 #include <XGUI_ViewPort.h>
22 #include <XGUI_ActionsMgr.h>
23 #include <XGUI_ViewerProxy.h>
24 #include <XGUI_ContextMenuMgr.h>
25 #include <XGUI_PropertyPanel.h>
26 #include <XGUI_Tools.h>
27
28 #include <SketchPlugin_Line.h>
29
30 #include <Config_PointerMessage.h>
31 #include <Config_ModuleReader.h>
32 #include <Config_WidgetReader.h>
33 #include <Events_Loop.h>
34 #include <Events_Message.h>
35 #include <Events_Error.h>
36
37 #include <GeomAPI_Shape.h>
38
39 #include <AIS_ListOfInteractive.hxx>
40 #include <AIS_DimensionSelectionMode.hxx>
41
42 #include <QObject>
43 #include <QMouseEvent>
44 #include <QString>
45
46 #ifdef _DEBUG
47 #include <QDebug>
48 #endif
49
50 /*!Create and return new instance of XGUI_Module*/
51 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
52 {
53   return new PartSet_Module(theWshop);
54 }
55
56 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
57 {
58   myWorkshop = theWshop;
59   myListener = new PartSet_Listener(this);
60
61   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
62
63   connect(anOperationMgr, SIGNAL(operationStarted()),
64           this, SLOT(onOperationStarted()));
65
66   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
67           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
68
69   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
70   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
71           this, SLOT(onContextMenuCommand(const QString&, bool)));
72
73   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
74           this, SLOT(onMousePressed(QMouseEvent*)));
75   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
76           this, SLOT(onMouseReleased(QMouseEvent*)));
77   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
78           this, SLOT(onMouseMoved(QMouseEvent*)));
79   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
80           this, SLOT(onKeyRelease(QKeyEvent*)));
81 }
82
83 PartSet_Module::~PartSet_Module()
84 {
85 }
86
87 XGUI_Workshop* PartSet_Module::workshop() const
88 {
89   return myWorkshop;
90 }
91
92 void PartSet_Module::createFeatures()
93 {
94   Config_ModuleReader aXMLReader = Config_ModuleReader();
95   aXMLReader.readAll();
96   myFeaturesInFiles = aXMLReader.featuresInFiles();
97 }
98
99 void PartSet_Module::featureCreated(QAction* theFeature)
100 {
101   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
102 }
103
104 QStringList PartSet_Module::nestedFeatures(QString)
105 {
106   return QStringList();
107 }
108
109 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
110 {
111   return myFeaturesInFiles[theFeatureId];
112 }
113
114 /*
115  *
116  */
117 void PartSet_Module::onFeatureTriggered()
118 {
119   //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
120   //                                   myWorkshop->viewer()->activeView());
121
122   //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
123   //                                       myWorkshop->viewer()->activeView());
124   QAction* aCmd = dynamic_cast<QAction*>(sender());
125   //Do nothing on uncheck
126   if(aCmd->isCheckable() && !aCmd->isChecked())
127     return;
128   launchOperation(aCmd->data().toString());
129 }
130   
131 void PartSet_Module::launchOperation(const QString& theCmdId)
132 {
133   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
134   sendOperation(anOperation);
135 }
136
137 void PartSet_Module::onOperationStarted()
138 {
139   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
140                                        myWorkshop->operationMgr()->currentOperation());
141   if (aPreviewOp) {
142     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
143     connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
144             aPropPanel, SLOT(onFocusActivated(const std::string&)));
145   }
146 }
147
148 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
149 {
150   if (!theOperation)
151     return;
152   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
153   if (aPreviewOp) {
154     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
155     disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
156                aPropPanel, SLOT(onFocusActivated(const std::string&)));
157   }
158 }
159
160 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
161 {
162   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
163   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
164     editFeature(aFeatures.first());
165   }
166 }
167
168 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
169 {
170   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
171                                        myWorkshop->operationMgr()->currentOperation());
172   if (aPreviewOp)
173   {
174     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
175     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
176     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
177
178     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
179   }
180 }
181
182 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
183 {
184   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
185                                        myWorkshop->operationMgr()->currentOperation());
186   if (aPreviewOp)
187   {
188     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
189     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
190     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
191
192     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
193   }
194 }
195
196 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
197 {
198   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
199                                        myWorkshop->operationMgr()->currentOperation());
200   if (aPreviewOp)
201     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
202 }
203
204 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
205 {
206   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
207   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
208   if (aPreviewOp) {
209     aPreviewOp->keyReleased(theEvent->key());
210   }
211 }
212
213 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
214 {
215   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
216   myWorkshop->actionsMgr()->update();
217
218   //PartSet_TestOCC::testSelection(myWorkshop);
219 }
220
221 void PartSet_Module::onFitAllView()
222 {
223   myWorkshop->viewer()->fitAll();
224 }
225
226 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
227 {
228   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
229                                                       theFeature ? theFeature->getKind() : "");
230   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
231   if (aPreviewOp)
232   {
233     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
234       // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
235     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
236     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
237     aPreviewOp->init(theFeature, aSelected, aHighlighted);
238   } else {
239     anOperation->setEditingFeature(theFeature);
240   }
241   sendOperation(anOperation);
242   myWorkshop->actionsMgr()->updateCheckState();
243 }
244
245 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
246 {
247   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
248   aViewer->enableMultiselection(theEnabled);
249 }
250
251 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
252 {
253   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
254   if (!isStop) {
255     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
256     FeaturePtr aFeature;
257     for (; anIt != aLast; anIt++) {
258       activateFeature((*anIt).feature(), false);
259     }
260   }
261   aDisplayer->stopSelection(theFeatures, isStop, false);
262
263   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
264   aViewer->enableSelection(!isStop);
265
266   aDisplayer->updateViewer();
267 }
268
269 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
270 {
271   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
272   aDisplayer->setSelected(theFeatures, false);
273   aDisplayer->updateViewer();
274 }
275
276 void PartSet_Module::onCloseLocalContext()
277 {
278   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
279   aDisplayer->closeLocalContexts();
280 }
281
282 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
283 {
284   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
285   visualizePreview(theFeature, isDisplay, false);
286   if (!isDisplay) {
287     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
288     FeaturePtr aSketch;
289     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
290     if (aPrevOp) {
291       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
292       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
293       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
294
295       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
296                                                              anIt = aList.begin(), aLast = aList.end();
297       for (; anIt != aLast; anIt++) {
298         FeaturePtr aFeature = (*anIt).first;
299         visualizePreview(aFeature, false, false);
300       }
301       aDisplayer->updateViewer();
302     }
303   }
304
305   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
306       theMode == PartSet_OperationSketchBase::FM_Deactivation)
307     activateFeature(theFeature, true);
308 }
309
310 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
311                                                       const std::string& theFeatureKind)
312 {
313   // create the operation
314   ModuleBase_Operation* anOperation = 0;
315   if (theCmdId == PartSet_OperationSketch::Type()) {
316     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
317   }
318   else {
319     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
320     FeaturePtr aSketch;
321     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
322     if (aPrevOp)
323       aSketch = aPrevOp->sketch();
324     if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
325       anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
326     else if (theCmdId == PartSet_OperationEditFeature::Type())
327       anOperation = new PartSet_OperationEditFeature(theCmdId.c_str(), this, aSketch);
328     else if (theCmdId == PartSet_OperationConstraint::Type())
329       anOperation = new PartSet_OperationConstraint(theCmdId.c_str(), this, aSketch);
330   }
331
332   if (!anOperation) {
333     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
334   }
335
336   // set operation xml description
337   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
338
339   std::string aPluginFileName = featureFile(aFeatureKind);
340   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
341   aWdgReader.readAll();
342   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
343   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
344
345   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
346   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
347
348   // connect the operation
349   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
350   if (aPreviewOp) {
351     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
352             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
353     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
354             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
355     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
356             this, SLOT(onMultiSelectionEnabled(bool)));
357
358     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
359             this, SLOT(onMultiSelectionEnabled(bool)));
360     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
361             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
362     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
363             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
364
365      connect(aPreviewOp, SIGNAL(closeLocalContext()),
366              this, SLOT(onCloseLocalContext()));
367
368     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
369     if (aSketchOp) {
370       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
371               this, SLOT(onPlaneSelected(double, double, double)));
372       connect(aSketchOp, SIGNAL(fitAllView()),
373               this, SLOT(onFitAllView()));
374     }
375   }
376
377   return anOperation;
378 }
379
380 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
381 {
382   //TODO(sbh): Implement static method to extract event id [SEID]
383   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
384   Config_PointerMessage aMessage(aModuleEvent, this);
385   aMessage.setPointer(theOperation);
386   Events_Loop::loop()->send(aMessage);
387 }
388
389 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
390                                       const bool isUpdateViewer)
391 {
392   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
393   if (!anOperation)
394     return;
395
396   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
397   if (!aPreviewOp)
398     return;
399
400   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
401   if (isDisplay) {
402     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
403     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
404                            theFeature, aPreviewOp->sketch(),
405                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
406                            aDisplayer->getAISObject(theFeature));
407
408     int aSelectionMode = -1;
409     if (theFeature->getKind() == SKETCH_CONSTRAINT_LENGTH_KIND) {
410       aSelectionMode = AIS_DSM_Text;
411     }
412     aDisplayer->redisplay(theFeature, anAIS, aSelectionMode, false);
413   }
414   else
415     aDisplayer->erase(theFeature, false);
416
417   if (isUpdateViewer)
418     aDisplayer->updateViewer();
419 }
420
421 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
422 {
423   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
424   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
425   if (aPreviewOp) {
426     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
427     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
428                                        isUpdateViewer);
429   }
430 }
431
432 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
433 {
434   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
435   if (!anOperation)
436     return;
437
438   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
439   if (!aPreviewOp)
440     return;
441
442   FeaturePtr aFeature = aPreviewOp->feature();
443   if (!aFeature || aFeature->getKind() != theCmdId)
444     return;
445
446   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
447   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
448   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
449
450   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
451                                                          anIt = aList.begin(), aLast = aList.end();
452   for (; anIt != aLast; anIt++) {
453     FeaturePtr aFeature = (*anIt).first;
454     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
455     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
456                            aFeature, aPreviewOp->sketch(),
457                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
458                            aDisplayer->getAISObject(aFeature));
459     if (!anAIS.IsNull())
460       aDisplayer->redisplay(aFeature, anAIS, -1, false);
461     aDisplayer->activateInLocalContext(aFeature, aModes, false);
462   }
463   aDisplayer->updateViewer();
464 }
465
466 void PartSet_Module::editFeature(FeaturePtr theFeature)
467 {
468   if (!theFeature)
469     return;
470
471 //  if (theFeature->getKind() == SKETCH_KIND) {
472     FeaturePtr aFeature = theFeature;
473     if (XGUI_Tools::isModelObject(aFeature)) {
474       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
475       aFeature = aObject->featureRef();
476     }
477
478     if (aFeature) {
479       onLaunchOperation(aFeature->getKind(), aFeature);
480       updateCurrentPreview(aFeature->getKind());
481     }
482 //  }
483 }