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