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