Salome HOME
Bugfix: duplicated commit on "Return" and crash on "Return" with no active operations
[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 <PartSet_Listener.h>
7 #include <PartSet_TestOCC.h>
8 #include <PartSet_WidgetSketchLabel.h>
9 #include <PartSet_Validators.h>
10 #include <PartSet_Tools.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_OperationDescription.h>
14 #include <ModuleBase_WidgetFactory.h>
15 #include <ModuleBase_Operation.h>
16
17 #include <ModelAPI_Object.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Validator.h>
20 #include <ModelAPI_Data.h>
21
22 #include <GeomDataAPI_Point2D.h>
23
24 #include <XGUI_MainWindow.h>
25 #include <XGUI_Displayer.h>
26 #include <XGUI_Viewer.h>
27 #include <XGUI_Workshop.h>
28 #include <XGUI_OperationMgr.h>
29 #include <XGUI_SelectionMgr.h>
30 #include <XGUI_Selection.h>
31 #include <XGUI_ViewPort.h>
32 #include <XGUI_ActionsMgr.h>
33 #include <XGUI_ViewerProxy.h>
34 #include <XGUI_ContextMenuMgr.h>
35 #include <XGUI_PropertyPanel.h>
36 #include <XGUI_ModuleConnector.h>
37 #include <XGUI_Tools.h>
38
39 #include <SketchPlugin_Line.h>
40
41 #include <Config_PointerMessage.h>
42 #include <Config_ModuleReader.h>
43 #include <Config_WidgetReader.h>
44 #include <Events_Loop.h>
45 #include <Events_Message.h>
46 #include <Events_Error.h>
47
48 #include <GeomAPI_Shape.h>
49 #include <GeomAPI_AISObject.h>
50 #include <AIS_Shape.hxx>
51
52 #include <StdSelect_FaceFilter.hxx>
53 #include <StdSelect_TypeOfFace.hxx>
54
55 #include <QObject>
56 #include <QMouseEvent>
57 #include <QString>
58
59 #include <GeomAlgoAPI_FaceBuilder.h>
60 #include <GeomDataAPI_Dir.h>
61
62 #ifdef _DEBUG
63 #include <QDebug>
64 #endif
65
66 /*!Create and return new instance of XGUI_Module*/
67 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
68 {
69   return new PartSet_Module(theWshop);
70 }
71
72 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
73 {
74   myWorkshop = theWshop;
75   myListener = new PartSet_Listener(this);
76
77   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
78
79   connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
80
81   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)), this,
82           SLOT(onOperationStopped(ModuleBase_Operation*)));
83
84   XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
85   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
86           SLOT(onContextMenuCommand(const QString&, bool)));
87
88   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
89           SLOT(onMousePressed(QMouseEvent*)));
90   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
91           SLOT(onMouseReleased(QMouseEvent*)));
92   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
93           SLOT(onMouseMoved(QMouseEvent*)));
94   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
95           SLOT(onKeyRelease(QKeyEvent*)));
96   connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
97           SLOT(onMouseDoubleClick(QMouseEvent*)));
98 }
99
100 PartSet_Module::~PartSet_Module()
101 {
102 }
103
104 XGUI_Workshop* PartSet_Module::workshop() const
105 {
106   return myWorkshop;
107 }
108
109 void PartSet_Module::createFeatures()
110 {
111   //Registering of validators
112   SessionPtr aMgr = ModelAPI_Session::get();
113   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
114   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
115   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
116   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
117   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
118   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
119
120   Config_ModuleReader aXMLReader = Config_ModuleReader();
121   aXMLReader.readAll();
122   myFeaturesInFiles = aXMLReader.featuresInFiles();
123 }
124
125 void PartSet_Module::featureCreated(QAction* theFeature)
126 {
127   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
128 }
129
130 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
131 {
132   return myFeaturesInFiles[theFeatureId];
133 }
134
135 /*
136  *
137  */
138 void PartSet_Module::onFeatureTriggered()
139 {
140   //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
141   //                                   myWorkshop->viewer()->activeView());
142
143   //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
144   //                                       myWorkshop->viewer()->activeView());
145   QAction* aCmd = dynamic_cast<QAction*>(sender());
146   //Do nothing on uncheck
147   if (aCmd->isCheckable() && !aCmd->isChecked())
148     return;
149   launchOperation(aCmd->data().toString());
150 }
151
152 void PartSet_Module::launchOperation(const QString& theCmdId)
153 {
154   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
155   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
156   if (aPreviewOp) {
157     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
158     // Initialise operation with preliminary selection
159     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
160     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
161     aPreviewOp->initSelection(aSelected, aHighlighted);
162   }
163   sendOperation(anOperation);
164 }
165
166 void PartSet_Module::onOperationStarted()
167 {
168   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
169       ->operationMgr()->currentOperation());
170   if (aPreviewOp) {
171     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
172     connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
173             SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
174   }
175 }
176
177 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
178 {
179   if (!theOperation)
180     return;
181   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
182   if (aPreviewOp) {
183     XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
184     //disconnect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)),
185     //           this, SLOT(onStorePoint2D(ObjectPtr, const std::string&)));
186   } else {
187     // Activate results of current feature for selection
188     FeaturePtr aFeature = theOperation->feature();
189     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
190     std::list<ResultPtr> aResults = aFeature->results();
191     std::list<ResultPtr>::const_iterator aIt;
192     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
193       aDisplayer->activate(*aIt);
194     }
195   }
196 }
197
198 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
199 {
200   QList<ObjectPtr> aFeatures = myWorkshop->selector()->selection()->selectedObjects();
201   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
202     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
203     if (aFeature)
204       editFeature(aFeature);
205   }
206 }
207
208 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
209 {
210   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
211       ->operationMgr()->currentOperation());
212   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
213   if (aPreviewOp && (!aView.IsNull())) {
214     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
215     // Initialise operation with preliminary selection
216     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
217     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
218
219     aPreviewOp->mousePressed(theEvent, aView, aSelected, aHighlighted);
220   }
221 }
222
223 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
224 {
225   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
226       ->operationMgr()->currentOperation());
227   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
228   if (aPreviewOp && (!aView.IsNull())) {
229     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
230     // Initialise operation with preliminary selection
231     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
232     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
233
234     aPreviewOp->mouseReleased(theEvent, aView, aSelected, aHighlighted);
235   }
236 }
237
238 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
239 {
240   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
241       ->operationMgr()->currentOperation());
242   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
243   if (aPreviewOp && (!aView.IsNull()))
244     aPreviewOp->mouseMoved(theEvent, aView);
245 }
246
247 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
248 {
249   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
250   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
251   if (aPreviewOp) {
252     aPreviewOp->keyReleased(theEvent->key());
253   }
254 }
255
256 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
257 {
258   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
259       ->operationMgr()->currentOperation());
260   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
261   if (aPreviewOp && (!aView.IsNull())) {
262     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
263     // Initialise operation with preliminary selection
264     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
265     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
266     aPreviewOp->mouseDoubleClick(theEvent, aView, aSelected, aHighlighted);
267   }
268 }
269
270 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
271 {
272   //erasePlanes();
273   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
274   myWorkshop->actionsMgr()->update();
275
276   //PartSet_TestOCC::testSelection(myWorkshop);
277 }
278
279 void PartSet_Module::onFitAllView()
280 {
281   myWorkshop->viewer()->fitAll();
282 }
283
284 void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
285 {
286   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
287
288   std::string aKind = aFeature ? aFeature->getKind() : "";
289   ModuleBase_Operation* anOperation = createOperation(theName, aKind);
290   PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
291   if (aSketchOp) {
292     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
293     // Initialise operation with preliminary selection
294     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
295     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
296     aSketchOp->initFeature(aFeature);
297     aSketchOp->initSelection(aSelected, aHighlighted);
298   } else if (aFeature) {
299     anOperation->setEditingFeature(aFeature);
300     //Deactivate result of current feature in order to avoid its selection
301     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
302     std::list<ResultPtr> aResults = aFeature->results();
303     std::list<ResultPtr>::const_iterator aIt;
304     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
305       aDisplayer->deactivate(*aIt);
306     }
307   }
308   sendOperation(anOperation);
309   myWorkshop->actionsMgr()->updateCheckState();
310 }
311
312 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
313 {
314   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
315   aViewer->enableMultiselection(theEnabled);
316 }
317
318 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
319 {
320   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
321   if (!isStop) {
322     foreach(ObjectPtr aObject, theFeatures)
323     {
324       activateFeature(aObject, false);
325     }
326   }
327   aDisplayer->stopSelection(theFeatures, isStop, false);
328
329   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
330   aViewer->enableSelection(!isStop);
331
332   aDisplayer->updateViewer();
333 }
334
335 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
336 {
337   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
338   aDisplayer->setSelected(theFeatures, false);
339   aDisplayer->updateViewer();
340 }
341
342 void PartSet_Module::onCloseLocalContext()
343 {
344   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
345   aDisplayer->deactivateObjectsOutOfContext();
346   aDisplayer->closeLocalContexts();
347 }
348
349 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
350 {
351   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
352   ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
353   PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
354   if (aPrevOp) {
355     std::list<FeaturePtr> aList = aPrevOp->subFeatures();
356     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
357     std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
358     std::list<FeaturePtr>::iterator aSFIt;
359     for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
360       std::list<ResultPtr> aResults = (*aSFIt)->results();
361       std::list<ResultPtr>::iterator aIt;
362       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
363         if (isDisplay)
364           aDisplayer->activateInLocalContext((*aIt), aModes, false);
365         else
366           aDisplayer->erase((*aIt), false);
367       }
368       if (!isDisplay)
369         aDisplayer->erase((*aSFIt), false);
370     }
371   }
372   if (isDisplay)
373     ModelAPI_EventCreator::get()->sendUpdated(
374         theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
375   /*  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
376    // TODO visualizePreview(theFeature, isDisplay, false);
377    if (!isDisplay) {
378    ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
379    FeaturePtr aSketch;
380    PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
381    if (aPrevOp) {
382    std::list<FeaturePtr> aList = aPrevOp->subFeatures();
383    XGUI_Displayer* aDisplayer = myWorkshop->displayer();
384    std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
385
386    std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
387    aLast = aList.end();
388    //TODO for (; anIt != aLast; anIt++)
389    //  visualizePreview((*anIt), false, false);
390    //aDisplayer->updateViewer();
391    }
392    }
393
394    if (theMode == PartSet_OperationSketchBase::FM_Activation ||
395    theMode == PartSet_OperationSketchBase::FM_Deactivation)
396    activateFeature(theFeature, true);*/
397 }
398
399 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
400                                                       const std::string& theFeatureKind)
401 {
402   // create the operation
403   ModuleBase_Operation* anOperation = 0;
404   if (theCmdId == PartSet_OperationSketch::Type()) {
405     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
406   } else {
407     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
408     FeaturePtr aSketch;
409     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
410     if (aPrevOp) {
411       aSketch = aPrevOp->sketch();
412     }
413     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
414       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
415     } else if (theCmdId == PartSet_OperationFeatureEditMulti::Type()) {
416       anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
417     } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
418       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
419     }
420   }
421
422   if (!anOperation) {
423     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
424   }
425
426   // set operation description and list of widgets corresponding to the feature xml definition
427   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
428
429   std::string aPluginFileName = featureFile(aFeatureKind);
430   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
431   aWdgReader.readAll();
432   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
433   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
434
435   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
436   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
437   //                                                             myWorkshop->moduleConnector());
438   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
439   //qDeleteAll(aContent->children());
440   //aFactory.createWidget(aContent);
441
442   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
443   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
444
445   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
446
447   // connect the operation
448   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
449   if (aPreviewOp) {
450     connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
451             SLOT(onFeatureConstructed(ObjectPtr, int)));
452     connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
453             SLOT(onRestartOperation(std::string, ObjectPtr)));
454     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
455             SLOT(onMultiSelectionEnabled(bool)));
456
457     connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
458             SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
459     connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
460             SLOT(onSetSelection(const QList<ObjectPtr>&)));
461
462     connect(aPreviewOp, SIGNAL(closeLocalContext()), this, SLOT(onCloseLocalContext()));
463
464     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
465     if (aSketchOp) {
466       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
467               SLOT(onPlaneSelected(double, double, double)));
468       connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
469     }
470   }
471
472   return anOperation;
473 }
474
475 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
476 {
477   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
478   boost::shared_ptr<Config_PointerMessage> aMessage =
479       boost::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
480   aMessage->setPointer(theOperation);
481   Events_Loop::loop()->send(aMessage);
482 }
483
484 /*void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
485  const bool isUpdateViewer)
486  {
487  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
488  if (!anOperation)
489  return;
490
491  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
492  if (!aPreviewOp)
493  return;
494
495  ResultPtr aResult = theFeature->firstResult();
496  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
497  if (isDisplay) {
498  boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
499  boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
500  if (aSPFeature) {
501  PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
502  if (aSketchOp && !aSketchOp->hasSketchPlane())
503  showPlanes();
504  }
505  }
506  else
507  aDisplayer->erase(aResult, false);
508
509  if (isUpdateViewer)
510  aDisplayer->updateViewer();
511  }*/
512
513 void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
514 {
515   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
516   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
517   if (aPreviewOp) {
518     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
519     std::list<int> aModes = aPreviewOp->getSelectionModes(theFeature);
520     aDisplayer->activateInLocalContext(theFeature, aModes, isUpdateViewer);
521
522     // If this is a Sketcher then activate objects (planar faces) outside of context
523     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
524     if (aSketchOp) {
525       Handle(StdSelect_FaceFilter) aFilter = new StdSelect_FaceFilter(StdSelect_Plane);
526       aDisplayer->activateObjectsOutOfContext(aModes, aFilter);
527     }
528   }
529 }
530
531 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
532 {
533   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
534   if (!anOperation)
535     return;
536
537   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
538   if (!aPreviewOp)
539     return;
540
541   FeaturePtr aFeature = aPreviewOp->feature();
542   if (!aFeature || aFeature->getKind() != theCmdId)
543     return;
544
545   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
546   // Hide result of sketch
547   std::list<ResultPtr> aResults = aFeature->results();
548   std::list<ResultPtr>::const_iterator aIt;
549   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
550     aDisplayer->erase(*aIt, false);
551
552   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
553   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
554
555   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
556   for (; anIt != aLast; anIt++) {
557     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
558         SketchPlugin_Feature>(*anIt);
559     if (!aSPFeature)
560       continue;
561     std::list<ResultPtr> aResults = aSPFeature->results();
562     std::list<ResultPtr>::const_iterator aRIt;
563     for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
564       aDisplayer->display((*aRIt), false);
565       aDisplayer->activateInLocalContext((*aRIt), aModes, false);
566     }
567     aDisplayer->display(aSPFeature, false);
568     aDisplayer->activateInLocalContext(aSPFeature, aModes, false);
569   }
570   aDisplayer->updateViewer();
571 }
572
573 void PartSet_Module::editFeature(FeaturePtr theFeature)
574 {
575   if (!theFeature)
576     return;
577
578 //  if (theFeature->getKind() == SKETCH_KIND) {
579   //FeaturePtr aFeature = theFeature;
580   //if (XGUI_Tools::isModelObject(aFeature)) {
581   //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
582   //  aFeature = aObject->featureRef();
583   //}
584
585   //if (aFeature) {
586   onRestartOperation(theFeature->getKind(), theFeature);
587   updateCurrentPreview(theFeature->getKind());
588   //}
589 //  }
590 }
591
592 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
593 {
594   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
595
596   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop
597       ->operationMgr()->currentOperation());
598   if (!aPreviewOp)
599     return;
600
601   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
602       aFeature->data()->attribute(theAttribute));
603
604   PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
605                                 aPoint->y());
606 }
607
608 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
609  {
610  XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
611  XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
612
613  ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
614  if (!aOperation)
615  return !aActMgr->isNested(theCmdId);
616
617  PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
618  if (aSketchEdtOp) {
619  QStringList aConstraintList;
620  aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
621  <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
622  return aConstraintList.contains(theCmdId);
623  }
624  QStringList aList = aActMgr->nestedCommands(aOperation->id());
625  return aList.contains(theCmdId);
626  }*/
627
628 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
629                                             Config_WidgetAPI* theWidgetApi,
630                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
631 {
632   if (theType == "sketch-start-label") {
633     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
634     aWgt->setOperationsMgr(myWorkshop->operationMgr());
635     theModelWidgets.append(aWgt);
636     return aWgt->getControl();
637   } else
638     return 0;
639 }