Salome HOME
7d5751707a40a97f613130f259a244ecddf58538
[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_OperationEditLine.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   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
230   if (aPreviewOp)
231   {
232     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
233       // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
234     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected(TopAbs_VERTEX);
235     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted(TopAbs_VERTEX);
236     aPreviewOp->init(theFeature, aSelected, aHighlighted);
237   } else {
238     anOperation->setEditingFeature(theFeature);
239   }
240   sendOperation(anOperation);
241   myWorkshop->actionsMgr()->updateCheckState();
242 }
243
244 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
245 {
246   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
247   aViewer->enableMultiselection(theEnabled);
248 }
249
250 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
251 {
252   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
253   if (!isStop) {
254     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
255     FeaturePtr aFeature;
256     for (; anIt != aLast; anIt++) {
257       activateFeature((*anIt).feature(), false);
258     }
259   }
260   aDisplayer->stopSelection(theFeatures, isStop, false);
261
262   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
263   aViewer->enableSelection(!isStop);
264
265   aDisplayer->updateViewer();
266 }
267
268 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
269 {
270   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
271   aDisplayer->setSelected(theFeatures, false);
272   aDisplayer->updateViewer();
273 }
274
275 void PartSet_Module::onCloseLocalContext()
276 {
277   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
278   aDisplayer->closeLocalContexts();
279 }
280
281 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
282 {
283   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
284   visualizePreview(theFeature, isDisplay, false);
285   if (!isDisplay) {
286     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
287     FeaturePtr aSketch;
288     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
289     if (aPrevOp) {
290       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
291       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
292       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
293
294       std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
295                                                              anIt = aList.begin(), aLast = aList.end();
296       for (; anIt != aLast; anIt++) {
297         FeaturePtr aFeature = (*anIt).first;
298         visualizePreview(aFeature, false, false);
299       }
300       aDisplayer->updateViewer();
301     }
302   }
303
304   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
305       theMode == PartSet_OperationSketchBase::FM_Deactivation)
306     activateFeature(theFeature, true);
307 }
308
309 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
310 {
311   // get operation xml description
312   std::string aStdCmdId = theCmdId;
313   if (aStdCmdId == PartSet_OperationEditLine::Type())
314     aStdCmdId = SKETCH_LINE_KIND;
315   std::string aPluginFileName = featureFile(aStdCmdId);
316   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
317   aWdgReader.readAll();
318   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
319   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
320
321   // create the operation
322   ModuleBase_Operation* anOperation = 0;
323   if (theCmdId == PartSet_OperationSketch::Type()) {
324     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
325   }
326   else {
327     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
328     FeaturePtr aSketch;
329     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
330     if (aPrevOp)
331       aSketch = aPrevOp->sketch();
332     if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
333       anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
334     else if (theCmdId == PartSet_OperationEditLine::Type())
335       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
336     else if (theCmdId == PartSet_OperationConstraint::Type())
337       anOperation = new PartSet_OperationConstraint(theCmdId.c_str(), this, aSketch);
338   }
339
340   if (!anOperation) {
341     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
342   }
343   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
344   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
345
346   // connect the operation
347   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
348   if (aPreviewOp) {
349     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
350             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
351     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
352             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
353     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
354             this, SLOT(onMultiSelectionEnabled(bool)));
355
356     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
357             this, SLOT(onMultiSelectionEnabled(bool)));
358     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
359             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
360     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
361             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
362
363      connect(aPreviewOp, SIGNAL(closeLocalContext()),
364              this, SLOT(onCloseLocalContext()));
365
366     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
367     if (aSketchOp) {
368       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
369               this, SLOT(onPlaneSelected(double, double, double)));
370       connect(aSketchOp, SIGNAL(fitAllView()),
371               this, SLOT(onFitAllView()));
372     }
373   }
374
375   return anOperation;
376 }
377
378 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
379 {
380   //TODO(sbh): Implement static method to extract event id [SEID]
381   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
382   Config_PointerMessage aMessage(aModuleEvent, this);
383   aMessage.setPointer(theOperation);
384   Events_Loop::loop()->send(aMessage);
385 }
386
387 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
388                                       const bool isUpdateViewer)
389 {
390   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
391   if (!anOperation)
392     return;
393
394   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
395   if (!aPreviewOp)
396     return;
397
398   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
399   if (isDisplay) {
400     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
401     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
402                            theFeature, aPreviewOp->sketch(),
403                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
404                            aDisplayer->getAISObject(theFeature));
405
406     int aSelectionMode = -1;
407     if (theFeature->getKind() == SKETCH_CONSTRAINT_LENGTH_KIND) {
408       aSelectionMode = AIS_DSM_Text;
409     }
410     aDisplayer->redisplay(theFeature, anAIS, aSelectionMode, false);
411   }
412   else
413     aDisplayer->erase(theFeature, false);
414
415   if (isUpdateViewer)
416     aDisplayer->updateViewer();
417 }
418
419 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
420 {
421   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
422   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
423   if (aPreviewOp) {
424     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
425     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
426                                        isUpdateViewer);
427   }
428 }
429
430 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
431 {
432   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
433   if (!anOperation)
434     return;
435
436   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
437   if (!aPreviewOp)
438     return;
439
440   FeaturePtr aFeature = aPreviewOp->feature();
441   if (!aFeature || aFeature->getKind() != theCmdId)
442     return;
443
444   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
445   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
446   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
447
448   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
449                                                          anIt = aList.begin(), aLast = aList.end();
450   for (; anIt != aLast; anIt++) {
451     FeaturePtr aFeature = (*anIt).first;
452     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
453     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
454                            aFeature, aPreviewOp->sketch(),
455                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
456                            aDisplayer->getAISObject(aFeature));
457     if (!anAIS.IsNull())
458       aDisplayer->redisplay(aFeature, anAIS, -1, false);
459     aDisplayer->activateInLocalContext(aFeature, aModes, false);
460   }
461   aDisplayer->updateViewer();
462 }
463
464 void PartSet_Module::editFeature(FeaturePtr theFeature)
465 {
466   if (!theFeature)
467     return;
468
469 //  if (theFeature->getKind() == SKETCH_KIND) {
470     FeaturePtr aFeature = theFeature;
471     if (XGUI_Tools::isModelObject(aFeature)) {
472       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
473       aFeature = aObject->featureRef();
474     }
475
476     if (aFeature) {
477       onLaunchOperation(aFeature->getKind(), aFeature);
478       updateCurrentPreview(aFeature->getKind());
479     }
480 //  }
481 }