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