]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[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 #include <SketchPlugin_Point.h>
30
31 #include <Config_PointerMessage.h>
32 #include <Config_ModuleReader.h>
33 #include <Config_WidgetReader.h>
34 #include <Events_Loop.h>
35 #include <Events_Message.h>
36 #include <Events_Error.h>
37
38 #include <GeomAPI_Shape.h>
39
40 #include <AIS_ListOfInteractive.hxx>
41 #include <AIS_DimensionSelectionMode.hxx>
42
43 #include <QObject>
44 #include <QMouseEvent>
45 #include <QString>
46
47 #ifdef _DEBUG
48 #include <QDebug>
49 #endif
50
51 /*!Create and return new instance of XGUI_Module*/
52 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
53 {
54   return new PartSet_Module(theWshop);
55 }
56
57 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
58 {
59   myWorkshop = theWshop;
60   myListener = new PartSet_Listener(this);
61
62   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
63
64   connect(anOperationMgr, SIGNAL(operationStarted()),
65           this, SLOT(onOperationStarted()));
66
67   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
68           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
69
70   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
71   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), 
72           this, SLOT(onContextMenuCommand(const QString&, bool)));
73
74   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
75           this, SLOT(onMousePressed(QMouseEvent*)));
76   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
77           this, SLOT(onMouseReleased(QMouseEvent*)));
78   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
79           this, SLOT(onMouseMoved(QMouseEvent*)));
80   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
81           this, SLOT(onKeyRelease(QKeyEvent*)));
82 }
83
84 PartSet_Module::~PartSet_Module()
85 {
86 }
87
88 XGUI_Workshop* PartSet_Module::workshop() const
89 {
90   return myWorkshop;
91 }
92
93 void PartSet_Module::createFeatures()
94 {
95   Config_ModuleReader aXMLReader = Config_ModuleReader();
96   aXMLReader.readAll();
97   myFeaturesInFiles = aXMLReader.featuresInFiles();
98 }
99
100 void PartSet_Module::featureCreated(QAction* theFeature)
101 {
102   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
103 }
104
105 QStringList PartSet_Module::nestedFeatures(QString)
106 {
107   return QStringList();
108 }
109
110 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
111 {
112   return myFeaturesInFiles[theFeatureId];
113 }
114
115 /*
116  *
117  */
118 void PartSet_Module::onFeatureTriggered()
119 {
120   //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
121   //                                   myWorkshop->viewer()->activeView());
122
123   //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
124   //                                       myWorkshop->viewer()->activeView());
125   QAction* aCmd = dynamic_cast<QAction*>(sender());
126   //Do nothing on uncheck
127   if(aCmd->isCheckable() && !aCmd->isChecked())
128     return;
129   launchOperation(aCmd->data().toString());
130 }
131   
132 void PartSet_Module::launchOperation(const QString& theCmdId)
133 {
134   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
135   sendOperation(anOperation);
136 }
137
138 void PartSet_Module::onOperationStarted()
139 {
140   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
141                                        myWorkshop->operationMgr()->currentOperation());
142   if (aPreviewOp) {
143     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
144     connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
145             aPropPanel, SLOT(onFocusActivated(const std::string&)));
146   }
147 }
148
149 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
150 {
151   if (!theOperation)
152     return;
153   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
154   if (aPreviewOp) {
155     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
156     disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
157                aPropPanel, SLOT(onFocusActivated(const std::string&)));
158   }
159 }
160
161 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
162 {
163   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
164   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
165     editFeature(aFeatures.first());
166   }
167 }
168
169 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
170 {
171   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
172                                        myWorkshop->operationMgr()->currentOperation());
173   if (aPreviewOp)
174   {
175     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
176     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
177     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
178
179     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
180   }
181 }
182
183 void PartSet_Module::onMouseReleased(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->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
194   }
195 }
196
197 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
198 {
199   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
200                                        myWorkshop->operationMgr()->currentOperation());
201   if (aPreviewOp)
202     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
203 }
204
205 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
206 {
207   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
208   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
209   if (aPreviewOp) {
210     aPreviewOp->keyReleased(theEvent->key());
211   }
212 }
213
214 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
215 {
216   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
217   myWorkshop->actionsMgr()->update();
218
219   //PartSet_TestOCC::testSelection(myWorkshop);
220 }
221
222 void PartSet_Module::onFitAllView()
223 {
224   myWorkshop->viewer()->fitAll();
225 }
226
227 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
228 {
229   ModuleBase_Operation* anOperation = createOperation(theName.c_str());
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 {
312   // get operation xml description
313   std::string aStdCmdId = theCmdId;
314   if (aStdCmdId == PartSet_OperationEditLine::Type())
315     aStdCmdId = SKETCH_LINE_KIND;
316   std::string aPluginFileName = featureFile(aStdCmdId);
317   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
318   aWdgReader.readAll();
319   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
320   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
321
322   // create the operation
323   ModuleBase_Operation* anOperation = 0;
324   if (theCmdId == PartSet_OperationSketch::Type()) {
325     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
326   }
327   else {
328     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
329     FeaturePtr aSketch;
330     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
331     if (aPrevOp)
332       aSketch = aPrevOp->sketch();
333     if (theCmdId == SKETCH_LINE_KIND || theCmdId == SKETCH_POINT_KIND)
334       anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
335     else if (theCmdId == PartSet_OperationEditLine::Type())
336       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
337     else if (theCmdId == PartSet_OperationConstraint::Type())
338       anOperation = new PartSet_OperationConstraint(theCmdId.c_str(), this, aSketch);
339   }
340
341   if (!anOperation) {
342     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
343   }
344   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
345   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
346
347   // connect the operation
348   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
349   if (aPreviewOp) {
350     connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
351             this, SLOT(onFeatureConstructed(FeaturePtr, int)));
352     connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
353             this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
354     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
355             this, SLOT(onMultiSelectionEnabled(bool)));
356
357     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
358             this, SLOT(onMultiSelectionEnabled(bool)));
359     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
360             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
361     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
362             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
363
364      connect(aPreviewOp, SIGNAL(closeLocalContext()),
365              this, SLOT(onCloseLocalContext()));
366
367     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
368     if (aSketchOp) {
369       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
370               this, SLOT(onPlaneSelected(double, double, double)));
371       connect(aSketchOp, SIGNAL(fitAllView()),
372               this, SLOT(onFitAllView()));
373     }
374   }
375
376   return anOperation;
377 }
378
379 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
380 {
381   //TODO(sbh): Implement static method to extract event id [SEID]
382   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
383   Config_PointerMessage aMessage(aModuleEvent, this);
384   aMessage.setPointer(theOperation);
385   Events_Loop::loop()->send(aMessage);
386 }
387
388 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
389                                       const bool isUpdateViewer)
390 {
391   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
392   if (!anOperation)
393     return;
394
395   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
396   if (!aPreviewOp)
397     return;
398
399   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
400   if (isDisplay) {
401     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
402     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
403                            theFeature, aPreviewOp->sketch(),
404                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
405                            aDisplayer->getAISObject(theFeature));
406
407     int aSelectionMode = -1;
408     if (theFeature->getKind() == SKETCH_CONSTRAINT_LENGTH_KIND) {
409       aSelectionMode = AIS_DSM_Text;
410     }
411     aDisplayer->redisplay(theFeature, anAIS, aSelectionMode, false);
412   }
413   else
414     aDisplayer->erase(theFeature, false);
415
416   if (isUpdateViewer)
417     aDisplayer->updateViewer();
418 }
419
420 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
421 {
422   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
423   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
424   if (aPreviewOp) {
425     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
426     aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
427                                        isUpdateViewer);
428   }
429 }
430
431 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
432 {
433   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
434   if (!anOperation)
435     return;
436
437   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
438   if (!aPreviewOp)
439     return;
440
441   FeaturePtr aFeature = aPreviewOp->feature();
442   if (!aFeature || aFeature->getKind() != theCmdId)
443     return;
444
445   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
446   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
447   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
448
449   std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
450                                                          anIt = aList.begin(), aLast = aList.end();
451   for (; anIt != aLast; anIt++) {
452     FeaturePtr aFeature = (*anIt).first;
453     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
454     Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
455                            aFeature, aPreviewOp->sketch(),
456                            aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(),
457                            aDisplayer->getAISObject(aFeature));
458     if (!anAIS.IsNull())
459       aDisplayer->redisplay(aFeature, anAIS, -1, false);
460     aDisplayer->activateInLocalContext(aFeature, aModes, false);
461   }
462   aDisplayer->updateViewer();
463 }
464
465 void PartSet_Module::editFeature(FeaturePtr theFeature)
466 {
467   if (!theFeature)
468     return;
469
470 //  if (theFeature->getKind() == SKETCH_KIND) {
471     FeaturePtr aFeature = theFeature;
472     if (XGUI_Tools::isModelObject(aFeature)) {
473       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
474       aFeature = aObject->featureRef();
475     }
476
477     if (aFeature) {
478       onLaunchOperation(aFeature->getKind(), aFeature);
479       updateCurrentPreview(aFeature->getKind());
480     }
481 //  }
482 }