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