Salome HOME
8ceabc22eb944ac2ed90eddebe7661d874bec4a8
[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   }
192 }
193
194 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
195 {
196   QList<ObjectPtr> aFeatures = myWorkshop->selector()->selection()->selectedObjects();
197   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
198     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
199     if (aFeature)
200       editFeature(aFeature);
201   }
202 }
203
204 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
205 {
206   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
207                                        myWorkshop->operationMgr()->currentOperation());
208   if (aPreviewOp) {
209     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
210     // Initialise operation with preliminary selection
211     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
212     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
213
214     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
215   }
216 }
217
218 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
219 {
220   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
221                                        myWorkshop->operationMgr()->currentOperation());
222   if (aPreviewOp) {
223     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
224     // Initialise operation with preliminary selection
225     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
226     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
227
228     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
229   }
230 }
231
232 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
233 {
234   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
235                                        myWorkshop->operationMgr()->currentOperation());
236   if (aPreviewOp)
237     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
238 }
239
240 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
241 {
242   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
243   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
244   if (aPreviewOp) {
245     aPreviewOp->keyReleased(theEvent->key());
246   }
247 }
248
249 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
250 {
251   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
252                                        myWorkshop->operationMgr()->currentOperation());
253   if (aPreviewOp)
254   {
255     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
256     // Initialise operation with preliminary selection
257     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
258     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
259     aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
260                                  aHighlighted);
261   }
262 }
263
264 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
265 {
266   //erasePlanes();
267   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
268   myWorkshop->actionsMgr()->update();
269
270   //PartSet_TestOCC::testSelection(myWorkshop);
271 }
272
273 void PartSet_Module::onFitAllView()
274 {
275   myWorkshop->viewer()->fitAll();
276 }
277
278 void PartSet_Module::onLaunchOperation(std::string theName, ObjectPtr theObject)
279 {
280   FeaturePtr aFeature = ModuleBase_Tools::feature(theObject);
281   if (!aFeature) {
282     qDebug("Warning! Restart operation without feature!");
283     return;
284   }
285   ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
286                                                       aFeature ? aFeature->getKind() : "");
287   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
288   if (aPreviewOp) {
289     XGUI_Selection* aSelection = myWorkshop->selector()->selection();
290     // Initialise operation with preliminary selection
291     std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
292     std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
293     aPreviewOp->initFeature(aFeature);
294     aPreviewOp->initSelection(aSelected, aHighlighted);
295   } else {
296     anOperation->setEditingFeature(aFeature);
297   }
298   sendOperation(anOperation);
299   myWorkshop->actionsMgr()->updateCheckState();
300 }
301
302 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
303 {
304   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
305   aViewer->enableMultiselection(theEnabled);
306 }
307
308 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
309 {
310   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
311   if (!isStop) {
312     foreach(ObjectPtr aObject, theFeatures) {
313       activateFeature(aObject, false);
314     }
315   }
316   aDisplayer->stopSelection(theFeatures, isStop, false);
317
318   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
319   aViewer->enableSelection(!isStop);
320
321   aDisplayer->updateViewer();
322 }
323
324 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
325 {
326   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
327   aDisplayer->setSelected(theFeatures, false);
328   aDisplayer->updateViewer();
329 }
330
331 void PartSet_Module::onCloseLocalContext()
332 {
333   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
334   aDisplayer->closeLocalContexts();
335 }
336
337 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
338 {
339   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
340   ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
341   PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
342   if (aPrevOp) {
343     std::list<FeaturePtr> aList = aPrevOp->subFeatures();
344     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
345     std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
346     std::list<FeaturePtr>::iterator aSFIt; 
347     for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
348       std::list<ResultPtr> aResults = (*aSFIt)->results();
349       std::list<ResultPtr>::iterator aIt;
350       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
351         if (isDisplay)
352           aDisplayer->activateInLocalContext((*aIt), aModes, false);
353         else
354           aDisplayer->erase((*aIt), false);
355       }
356     }
357   }
358   if (isDisplay)
359     ModelAPI_EventCreator::get()->sendUpdated(theFeature, 
360         Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
361 /*  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
362   // TODO visualizePreview(theFeature, isDisplay, false);
363   if (!isDisplay) {
364     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
365     FeaturePtr aSketch;
366     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
367     if (aPrevOp) {
368       std::list<FeaturePtr> aList = aPrevOp->subFeatures();
369       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
370       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
371
372       std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
373                                             aLast = aList.end();
374       //TODO for (; anIt != aLast; anIt++)
375       //  visualizePreview((*anIt), false, false);
376       //aDisplayer->updateViewer();
377     }
378   }
379
380   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
381       theMode == PartSet_OperationSketchBase::FM_Deactivation)
382     activateFeature(theFeature, true);*/
383 }
384
385 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
386                                                       const std::string& theFeatureKind)
387 {
388   // create the operation
389   ModuleBase_Operation* anOperation = 0;
390   if (theCmdId == PartSet_OperationSketch::Type()) {
391     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
392   }
393   else {
394     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
395     FeaturePtr aSketch;
396     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
397     if (aPrevOp)
398       aSketch = aPrevOp->sketch();
399     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
400       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
401     else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
402                 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
403     else if (theCmdId == PartSet_OperationFeatureEdit::Type())
404       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
405   }
406
407   if (!anOperation) {
408     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
409   }
410
411   // set operation description and list of widgets corresponding to the feature xml definition
412   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
413
414   std::string aPluginFileName = featureFile(aFeatureKind);
415   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
416   aWdgReader.readAll();
417   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
418   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
419
420   //QString aXmlRepr = QString::fromStdString(aXmlCfg);
421   //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
422   //                                                             myWorkshop->moduleConnector());
423   //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
424   //qDeleteAll(aContent->children());
425   //aFactory.createWidget(aContent);
426
427   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
428   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
429
430   //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
431
432   // connect the operation
433   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
434   if (aPreviewOp) {
435     connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)),
436             this, SLOT(onFeatureConstructed(ObjectPtr, int)));
437     connect(aPreviewOp, SIGNAL(launchOperation(std::string, ObjectPtr)),
438             this, SLOT(onLaunchOperation(std::string, ObjectPtr)));
439     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
440             this, SLOT(onMultiSelectionEnabled(bool)));
441
442     connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)),
443             this, SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
444     connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)),
445             this, SLOT(onSetSelection(const QList<ObjectPtr>&)));
446
447      connect(aPreviewOp, SIGNAL(closeLocalContext()),
448              this, SLOT(onCloseLocalContext()));
449
450     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
451     if (aSketchOp) {
452       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
453               this, SLOT(onPlaneSelected(double, double, double)));
454       connect(aSketchOp, SIGNAL(fitAllView()),
455               this, SLOT(onFitAllView()));
456     }
457   }
458
459   return anOperation;
460 }
461
462 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
463 {
464   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
465   Config_PointerMessage aMessage(aModuleEvent, this);
466   aMessage.setPointer(theOperation);
467   Events_Loop::loop()->send(aMessage);
468 }
469
470
471 /*void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
472                                       const bool isUpdateViewer)
473 {
474   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
475   if (!anOperation)
476     return;
477
478   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
479   if (!aPreviewOp)
480     return;
481
482   ResultPtr aResult = theFeature->firstResult();
483   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
484   if (isDisplay) {
485     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
486       boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
487     if (aSPFeature) {
488       PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
489       if (aSketchOp && !aSketchOp->hasSketchPlane())
490         showPlanes();
491     }
492   }
493   else
494     aDisplayer->erase(aResult, false);
495
496   if (isUpdateViewer)
497     aDisplayer->updateViewer();
498 }*/
499
500 void PartSet_Module::activateFeature(ObjectPtr theFeature, const bool isUpdateViewer)
501 {
502   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
503   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
504   if (aPreviewOp) {
505     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
506     std::list<int> aModes = aPreviewOp->getSelectionModes(theFeature);
507     aDisplayer->activateInLocalContext(theFeature, aModes, isUpdateViewer);
508   }
509 }
510
511 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
512 {
513   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
514   if (!anOperation)
515     return;
516
517   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
518   if (!aPreviewOp)
519     return;
520
521   FeaturePtr aFeature = aPreviewOp->feature();
522   if (!aFeature || aFeature->getKind() != theCmdId)
523     return;
524
525   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
526   // Hide result of sketch
527   std::list<ResultPtr> aResults = aFeature->results();
528   std::list<ResultPtr>::const_iterator aIt;
529   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
530     aDisplayer->erase(*aIt, false);
531
532   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
533   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
534
535   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), 
536                                         aLast = aList.end();
537   for (; anIt != aLast; anIt++) {
538     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
539       boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
540     if (!aSPFeature)
541       continue;
542     std::list<ResultPtr> aResults = aSPFeature->results();
543     std::list<ResultPtr>::const_iterator aRIt;
544     for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
545       aDisplayer->display((*aRIt), false);
546       aDisplayer->activateInLocalContext((*aRIt), aModes, false);
547     }
548     aDisplayer->display(aSPFeature, false);
549   }
550   aDisplayer->updateViewer();
551 }
552
553 void PartSet_Module::editFeature(FeaturePtr theFeature)
554 {
555   if (!theFeature)
556     return;
557
558 //  if (theFeature->getKind() == SKETCH_KIND) {
559     //FeaturePtr aFeature = theFeature;
560     //if (XGUI_Tools::isModelObject(aFeature)) {
561     //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
562     //  aFeature = aObject->featureRef();
563     //}
564
565     //if (aFeature) {
566       onLaunchOperation(theFeature->getKind(), theFeature);
567       updateCurrentPreview(theFeature->getKind());
568     //}
569 //  }
570 }
571
572 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
573 {
574   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
575
576   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
577                                        myWorkshop->operationMgr()->currentOperation());
578   if (!aPreviewOp)
579     return;
580
581   boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
582         boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFeature->data()->attribute(theAttribute));
583
584   PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute,
585                                 aPoint->x(), aPoint->y());
586 }
587
588 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
589 {
590   XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
591   XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
592
593   ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
594   if (!aOperation)
595     return !aActMgr->isNested(theCmdId);
596
597   PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
598   if (aSketchEdtOp) {
599     QStringList aConstraintList;
600     aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
601       <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
602     return aConstraintList.contains(theCmdId);
603   }
604   QStringList aList = aActMgr->nestedCommands(aOperation->id());
605   return aList.contains(theCmdId);
606 }*/
607
608 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent, 
609                          Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
610 {
611   if (theType == "sketch-start-label") {
612     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
613     aWgt->setOperationsMgr(myWorkshop->operationMgr());
614     theModelWidgets.append(aWgt);
615     return aWgt->getControl();
616   } else
617     return 0;
618 }