]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
Some slight improvements and bugs fixing.
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationFeatureCreate.h>
4 #include <PartSet_OperationFeatureEditMulti.h>
5 #include <PartSet_OperationFeatureEdit.h>
6 #include <ModuleBase_Operation.h>
7 #include <ModuleBase_OperationDescription.h>
8 #include <ModuleBase_WidgetFactory.h>
9 #include <PartSet_Listener.h>
10 #include <PartSet_TestOCC.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModelAPI_Object.h>
14
15 #include <ModelAPI_Data.h>
16 #include <GeomDataAPI_Point2D.h>
17 #include <PartSet_Tools.h>
18
19 #include <XGUI_MainWindow.h>
20 #include <XGUI_Displayer.h>
21 #include <XGUI_Viewer.h>
22 #include <XGUI_Workshop.h>
23 #include <XGUI_OperationMgr.h>
24 #include <XGUI_SelectionMgr.h>
25 #include <XGUI_ViewPort.h>
26 #include <XGUI_ActionsMgr.h>
27 #include <XGUI_ViewerProxy.h>
28 #include <XGUI_ContextMenuMgr.h>
29 #include <XGUI_PropertyPanel.h>
30 #include <XGUI_ModuleConnector.h>
31 #include <XGUI_Tools.h>
32
33 #include <SketchPlugin_Line.h>
34
35 #include <Config_PointerMessage.h>
36 #include <Config_ModuleReader.h>
37 #include <Config_WidgetReader.h>
38 #include <Events_Loop.h>
39 #include <Events_Message.h>
40 #include <Events_Error.h>
41
42 #include <GeomAPI_Shape.h>
43
44 #include <AIS_ListOfInteractive.hxx>
45 //#include <AIS_DimensionSelectionMode.hxx>
46
47 #include <QObject>
48 #include <QMouseEvent>
49 #include <QString>
50
51 #ifdef _DEBUG
52 #include <QDebug>
53 #endif
54
55 /*!Create and return new instance of XGUI_Module*/
56 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
57 {
58   return new PartSet_Module(theWshop);
59 }
60
61 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
62 {
63   myWorkshop = theWshop;
64   myListener = new PartSet_Listener(this);
65
66   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
67
68   connect(anOperationMgr, SIGNAL(operationStarted()),
69           this, SLOT(onOperationStarted()));
70
71   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
72           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
73
74   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
75   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
76           this, SLOT(onContextMenuCommand(const QString&, bool)));
77
78   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
79           this, SLOT(onMousePressed(QMouseEvent*)));
80   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
81           this, SLOT(onMouseReleased(QMouseEvent*)));
82   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
83           this, SLOT(onMouseMoved(QMouseEvent*)));
84   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
85           this, SLOT(onKeyRelease(QKeyEvent*)));
86   connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
87           this, SLOT(onMouseDoubleClick(QMouseEvent*)));
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   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
142   if (aPreviewOp) {
143     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
144     // Initialise operation with preliminary selection
145     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
146     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
147     aPreviewOp->initSelection(aSelected, aHighlighted);
148   } 
149   sendOperation(anOperation);
150 }
151
152 void PartSet_Module::onOperationStarted()
153 {
154   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
155                                        myWorkshop->operationMgr()->currentOperation());
156   if (aPreviewOp) {
157     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
158     connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
159       this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
160   }
161 }
162
163 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
164 {
165   if (!theOperation)
166     return;
167   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
168   if (aPreviewOp) {
169     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
170     //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
171     //           this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
172   }
173 }
174
175 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
176 {
177   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
178   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
179     editFeature(aFeatures.first());
180   }
181 }
182
183 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
184 {
185   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
186                                        myWorkshop->operationMgr()->currentOperation());
187   if (aPreviewOp)
188   {
189     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
190     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
191     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
192
193     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
194   }
195 }
196
197 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
198 {
199   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
200                                        myWorkshop->operationMgr()->currentOperation());
201   if (aPreviewOp)
202   {
203     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
204     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
205     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
206
207     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
208   }
209 }
210
211 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
212 {
213   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
214                                        myWorkshop->operationMgr()->currentOperation());
215   if (aPreviewOp)
216     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
217 }
218
219 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
220 {
221   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
222   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
223   if (aPreviewOp) {
224     aPreviewOp->keyReleased(theEvent->key());
225   }
226 }
227
228 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
229 {
230   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
231                                        myWorkshop->operationMgr()->currentOperation());
232   if (aPreviewOp)
233   {
234     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
235     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
236     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
237     aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
238                                  aHighlighted);
239   }
240 }
241
242 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
243 {
244   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
245   myWorkshop->actionsMgr()->update();
246
247   //PartSet_TestOCC::testSelection(myWorkshop);
248 }
249
250 void PartSet_Module::onFitAllView()
251 {
252   myWorkshop->viewer()->fitAll();
253 }
254
255 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
256 {
257   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
258                                                       theFeature ? theFeature->getKind() : "");
259   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
260   if (aPreviewOp)
261   {
262     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
263     // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
264     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
265     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
266     aPreviewOp->initFeature(theFeature);
267     aPreviewOp->initSelection(aSelected, aHighlighted);
268   } else {
269     anOperation->setEditingFeature(theFeature);
270   }
271   sendOperation(anOperation);
272   myWorkshop->actionsMgr()->updateCheckState();
273 }
274
275 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
276 {
277   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
278   aViewer->enableMultiselection(theEnabled);
279 }
280
281 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
282 {
283   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
284   if (!isStop) {
285     QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
286     for (; anIt != aLast; anIt++) {
287       activateFeature((*anIt), false);
288     }
289   }
290   aDisplayer->stopSelection(theFeatures, isStop, false);
291
292   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
293   aViewer->enableSelection(!isStop);
294
295   aDisplayer->updateViewer();
296 }
297
298 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
299 {
300   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
301   aDisplayer->setSelected(theFeatures, false);
302   aDisplayer->updateViewer();
303 }
304
305 void PartSet_Module::onCloseLocalContext()
306 {
307   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
308   aDisplayer->closeLocalContexts();
309 }
310
311 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
312 {
313   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
314   visualizePreview(theFeature, isDisplay, false);
315   if (!isDisplay) {
316     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
317     FeaturePtr aSketch;
318     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
319     if (aPrevOp) {
320       std::list<FeaturePtr> aList = aPrevOp->subFeatures();
321       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
322       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
323
324       std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
325                                             aLast = aList.end();
326       for (; anIt != aLast; anIt++)
327         visualizePreview(*anIt, false, false);
328       aDisplayer->updateViewer();
329     }
330   }
331
332   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
333       theMode == PartSet_OperationSketchBase::FM_Deactivation)
334     activateFeature(theFeature, true);
335 }
336
337 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
338                                                       const std::string& theFeatureKind)
339 {
340   // create the operation
341   ModuleBase_Operation* anOperation = 0;
342   if (theCmdId == PartSet_OperationSketch::Type()) {
343     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
344   }
345   else {
346     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
347     FeaturePtr aSketch;
348     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
349     if (aPrevOp)
350       aSketch = aPrevOp->sketch();
351     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
352       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
353     else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
354                 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
355     else if (theCmdId == PartSet_OperationFeatureEdit::Type())
356       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
357   }
358
359   if (!anOperation) {
360     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
361   }
362
363   // set operation description and list of widgets corresponding to the feature xml definition
364   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
365
366   std::string aPluginFileName = featureFile(aFeatureKind);
367   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
368   aWdgReader.readAll();
369   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
370   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
371
372   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
373   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
374   //                                                             myWorkshop->moduleConnector());
375   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
376   //qDeleteAll(aContent->children());
377   //aFactory.createWidget(aContent);
378
379   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
380   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
381
382   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
383
384   // connect the operation
385   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
386   if (aPreviewOp) {
387     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
388             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
389     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
390             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
391     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
392             this, SLOT(onMultiSelectionEnabled(bool)));
393
394     connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
395             this, SLOT(onStopSelection(const QFeatureList&, const bool)));
396     connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
397             this, SLOT(onSetSelection(const QFeatureList&)));
398
399      connect(aPreviewOp, SIGNAL(closeLocalContext()),
400              this, SLOT(onCloseLocalContext()));
401
402     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
403     if (aSketchOp) {
404       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
405               this, SLOT(onPlaneSelected(double, double, double)));
406       connect(aSketchOp, SIGNAL(fitAllView()),
407               this, SLOT(onFitAllView()));
408     }
409   }
410
411   return anOperation;
412 }
413
414 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
415 {
416   //TODO(sbh): Implement static method to extract event id [SEID]
417   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
418   Config_PointerMessage aMessage(aModuleEvent, this);
419   aMessage.setPointer(theOperation);
420   Events_Loop::loop()->send(aMessage);
421 }
422
423 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
424                                       const bool isUpdateViewer)
425 {
426   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
427   if (!anOperation)
428     return;
429
430   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
431   if (!aPreviewOp)
432     return;
433
434   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
435   if (isDisplay) {
436     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
437       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
438     if (aSPFeature)
439     {
440       Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(theFeature));
441       aDisplayer->redisplay(theFeature, anAIS, false);
442     }
443   }
444   else
445     aDisplayer->erase(theFeature, false);
446
447   if (isUpdateViewer)
448     aDisplayer->updateViewer();
449 }
450
451 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
452 {
453   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
454   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
455   if (aPreviewOp) {
456     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
457     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
458                                        isUpdateViewer);
459   }
460 }
461
462 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
463 {
464   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
465   if (!anOperation)
466     return;
467
468   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
469   if (!aPreviewOp)
470     return;
471
472   FeaturePtr aFeature = aPreviewOp->feature();
473   if (!aFeature || aFeature->getKind() != theCmdId)
474     return;
475
476   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
477   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
478   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
479
480   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), 
481                                         aLast = aList.end();
482   for (; anIt != aLast; anIt++) {
483     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
484       boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
485     if (!aSPFeature)
486       continue;
487     visualizePreview(*anIt, true, false);
488     aDisplayer->activateInLocalContext(*anIt, aModes, false);
489   }
490   aDisplayer->updateViewer();
491 }
492
493 void PartSet_Module::editFeature(FeaturePtr theFeature)
494 {
495   if (!theFeature)
496     return;
497
498 //  if (theFeature->getKind() == SKETCH_KIND) {
499     FeaturePtr aFeature = theFeature;
500     if (XGUI_Tools::isModelObject(aFeature)) {
501       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
502       aFeature = aObject->featureRef();
503     }
504
505     if (aFeature) {
506       onLaunchOperation(aFeature->getKind(), aFeature);
507       updateCurrentPreview(aFeature->getKind());
508     }
509 //  }
510 }
511
512 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
513 {
514   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
515                                        myWorkshop->operationMgr()->currentOperation());
516   if (!aPreviewOp)
517     return;
518
519   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
520         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
521
522   PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
523                                 aPoint->x(), aPoint->y());
524 }
525
526 bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
527 {
528   qDebug("### isFeatureEnabled %s", qPrintable(theCmdId));
529   return true;
530 }