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