Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 #include <ModelAPI_Session.h>
22
23 #include <GeomDataAPI_Point2D.h>
24 #include <GeomDataAPI_Point.h>
25 #include <GeomDataAPI_Dir.h>
26
27 #include <XGUI_MainWindow.h>
28 #include <XGUI_Displayer.h>
29 #include <XGUI_Viewer.h>
30 #include <XGUI_Workshop.h>
31 #include <XGUI_OperationMgr.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 #include <SketchPlugin_Sketch.h>
42
43 #include <Config_PointerMessage.h>
44 #include <Config_ModuleReader.h>
45 #include <Config_WidgetReader.h>
46 #include <Events_Loop.h>
47 //#include <Events_Message.h>
48 //#include <Events_Error.h>
49
50 #include <GeomAPI_Shape.h>
51 #include <GeomAPI_AISObject.h>
52 #include <AIS_Shape.hxx>
53 #include <AIS_DimensionSelectionMode.hxx>
54
55 #include <StdSelect_TypeOfFace.hxx>
56
57 #include <QObject>
58 #include <QMouseEvent>
59 #include <QString>
60
61 #include <GeomAlgoAPI_FaceBuilder.h>
62 #include <GeomDataAPI_Dir.h>
63
64 #ifdef _DEBUG
65 #include <QDebug>
66 #endif
67
68 /*!Create and return new instance of XGUI_Module*/
69 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
70 {
71   return new PartSet_Module(theWshop);
72 }
73
74 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
75   : ModuleBase_IModule(theWshop)
76 {
77   //myWorkshop = theWshop;
78   myListener = new PartSet_Listener(this);
79
80   connect(myWorkshop, SIGNAL(operationStarted(ModuleBase_Operation*)), 
81     this, SLOT(onOperationStarted(ModuleBase_Operation*)));
82
83   connect(myWorkshop, SIGNAL(operationStopped(ModuleBase_Operation*)), this,
84           SLOT(onOperationStopped(ModuleBase_Operation*)));
85
86   XGUI_Workshop* aXWshop = xWorkshop();
87   XGUI_ContextMenuMgr* aContextMenuMgr = aXWshop->contextMenuMgr();
88   connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
89           SLOT(onContextMenuCommand(const QString&, bool)));
90
91 }
92
93 PartSet_Module::~PartSet_Module()
94 {
95 }
96
97 void PartSet_Module::createFeatures()
98 {
99   //Registering of validators
100   SessionPtr aMgr = ModelAPI_Session::get();
101   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
102   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
103   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
104   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
105   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
106   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
107
108   Config_ModuleReader aXMLReader = Config_ModuleReader();
109   aXMLReader.readAll();
110   myFeaturesInFiles = aXMLReader.featuresInFiles();
111 }
112
113 void PartSet_Module::featureCreated(QAction* theFeature)
114 {
115   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
116 }
117
118 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
119 {
120   return myFeaturesInFiles[theFeatureId];
121 }
122
123 /*
124  *
125  */
126 void PartSet_Module::onFeatureTriggered()
127 {
128   QAction* aCmd = dynamic_cast<QAction*>(sender());
129   //Do nothing on uncheck
130   if (aCmd->isCheckable() && !aCmd->isChecked())
131     return;
132   launchOperation(aCmd->data().toString());
133 }
134
135
136 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
137 {
138   XGUI_Workshop* aXWshp = xWorkshop();
139   XGUI_Displayer* aDisplayer = aXWshp->displayer();
140   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
141   if (aPreviewOp) {
142     XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
143     connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
144             SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
145
146     //aDisplayer->deactivateObjectsOutOfContext();
147     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
148     if (aSketchOp) {
149       if (aSketchOp->isEditOperation()) {
150         setSketchingMode(getSketchPlane(aSketchOp->feature()));
151       } else {
152         aDisplayer->openLocalContext();
153         aDisplayer->activateObjectsOutOfContext(QIntList());
154         myPlaneFilter = new StdSelect_FaceFilter(StdSelect_Plane);
155         aDisplayer->addSelectionFilter(myPlaneFilter);
156         QIntList aModes = sketchSelectionModes(aPreviewOp->feature());
157         aDisplayer->setSelectionModes(aModes);
158       } 
159     }
160   }
161 }
162
163 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
164 {
165   if (!theOperation)
166     return;
167   XGUI_Workshop* aXWshp = xWorkshop();
168   XGUI_Displayer* aDisplayer = aXWshp->displayer();
169   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
170   if (aPreviewOp) {
171     XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
172
173     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
174     if (aSketchOp) {
175       aDisplayer->closeLocalContexts();
176     } else {
177       PartSet_OperationFeatureCreate* aCreationOp = 
178         dynamic_cast<PartSet_OperationFeatureCreate*>(aPreviewOp);
179       if (aCreationOp) {
180         // Activate just created object for selection
181         FeaturePtr aFeature = aCreationOp->feature();
182         QIntList aModes = sketchSelectionModes(aFeature);
183         const std::list<ResultPtr>& aResults = aFeature->results();
184         std::list<ResultPtr>::const_iterator anIt, aLast = aResults.end();
185         for (anIt = aResults.begin(); anIt != aLast; anIt++) {
186           aDisplayer->activate(*anIt, aModes);
187         }
188         aDisplayer->activate(aFeature, aModes);
189         aDisplayer->clearSelected();
190       }
191     }
192   }// else {
193     // Activate results of current feature for selection
194     //FeaturePtr aFeature = theOperation->feature();
195     //XGUI_Displayer* aDisplayer = aXWshp->displayer();
196     //std::list<ResultPtr> aResults = aFeature->results();
197     //std::list<ResultPtr>::const_iterator aIt;
198     //for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
199     //  aDisplayer->activate(*aIt);
200     //}    
201   //}
202 }
203
204 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
205 {
206   QList<ObjectPtr> aFeatures = workshop()->selection()->selectedObjects();
207   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
208     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
209     if (aFeature)
210       editFeature(aFeature);
211   }
212 }
213
214 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
215 {
216   XGUI_Workshop* aXWshp = xWorkshop();
217   PartSet_OperationSketchBase* aPreviewOp = 
218     dynamic_cast<PartSet_OperationSketchBase*>(workshop()->currentOperation());
219   if (aPreviewOp) {
220     ModuleBase_ISelection* aSelection = workshop()->selection();
221     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer(), aSelection);
222   }
223 }
224
225 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
226 {
227   PartSet_OperationSketchBase* aPreviewOp = 
228     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
229   if (aPreviewOp) {
230     ModuleBase_ISelection* aSelection = workshop()->selection();
231     // Initialise operation with preliminary selection
232     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer(), aSelection);
233   }
234 }
235
236 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
237 {
238   PartSet_OperationSketchBase* aPreviewOp = 
239     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
240   if (aPreviewOp)
241     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer());
242 }
243
244 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
245 {
246   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
247   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
248   if (aPreviewOp) {
249     aPreviewOp->keyReleased(theEvent->key());
250   }
251 }
252
253 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
254 {
255   PartSet_OperationSketchBase* aPreviewOp = 
256     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
257   Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
258   if (aPreviewOp && (!aView.IsNull())) {
259     ModuleBase_ISelection* aSelection = workshop()->selection();
260     // Initialise operation with preliminary selection
261     aPreviewOp->mouseDoubleClick(theEvent, aView, aSelection);
262   }
263 }
264
265 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
266 {
267   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
268   xWorkshop()->actionsMgr()->update();
269   // Set working plane
270   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
271   FeaturePtr aSketch = anOperation->feature();
272   setSketchingMode(getSketchPlane(aSketch));
273 }
274
275 void PartSet_Module::onFitAllView()
276 {
277   myWorkshop->viewer()->fitAll();
278 }
279
280 void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
281 {
282   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
283
284   std::string aKind = aFeature ? aFeature->getKind() : "";
285   ModuleBase_Operation* anOperation = createOperation(theName, aKind);
286
287   PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
288   if (aSketchOp) {
289     PartSet_OperationFeatureCreate* aCreateOp = dynamic_cast<PartSet_OperationFeatureCreate*>(anOperation);
290     if (aCreateOp)
291       aCreateOp->initFeature(aFeature);
292     else {
293       anOperation->setFeature(aFeature);
294     }
295     ModuleBase_ISelection* aSelection = workshop()->selection();
296     // Initialise operation with preliminary selection
297     aSketchOp->initSelection(aSelection, myWorkshop->viewer());
298   } else if (aFeature) { // In case of edit operation: set the previously created feature to the operation
299     anOperation->setFeature(aFeature);
300     ////Deactivate result of current feature in order to avoid its selection
301     XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
302     std::list<ResultPtr> aResults = aFeature->results();
303     std::list<ResultPtr>::const_iterator aIt;
304     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
305       aDisplayer->deactivate(*aIt);
306     }
307   }
308   sendOperation(anOperation);
309   xWorkshop()->actionsMgr()->updateCheckState();
310 }
311
312 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
313 {
314   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
315   aViewer->enableMultiselection(theEnabled);
316 }
317
318 void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
319 {
320   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
321   //if (!isStop) {
322   //  foreach(ObjectPtr aObject, theFeatures) {
323   //    activateFeature(aObject);
324   //  }
325   //}
326   aDisplayer->stopSelection(theFeatures, isStop, false);
327
328   ModuleBase_IViewer* aViewer = myWorkshop->viewer();
329   aViewer->enableSelection(!isStop);
330
331   aDisplayer->updateViewer();
332 }
333
334 void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
335 {
336   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
337   aDisplayer->setSelected(theFeatures, false);
338   aDisplayer->updateViewer();
339 }
340
341 void PartSet_Module::setSketchingMode(const gp_Pln& thePln)
342 {
343   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
344   if (!myPlaneFilter.IsNull()) {
345     aDisplayer->removeSelectionFilter(myPlaneFilter);
346     myPlaneFilter.Nullify();
347   }
348   QIntList aModes;
349   // Clear standard selection modes
350   aDisplayer->setSelectionModes(aModes);
351   aDisplayer->openLocalContext();
352
353   // Set filter
354   mySketchFilter = new ModuleBase_ShapeInPlaneFilter(thePln);
355   aDisplayer->addSelectionFilter(mySketchFilter);
356
357   // Get default selection modes
358   aModes = sketchSelectionModes(ObjectPtr());
359   aDisplayer->activateObjectsOutOfContext(aModes);
360 }
361
362 void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
363 {
364   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
365   ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
366   PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
367   if (aPrevOp) {
368     std::list<FeaturePtr> aList = aPrevOp->subFeatures();
369     XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
370     QIntList aModes = sketchSelectionModes(aPrevOp->feature());
371     std::list<FeaturePtr>::iterator aSFIt;
372     for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
373       std::list<ResultPtr> aResults = (*aSFIt)->results();
374       std::list<ResultPtr>::iterator aIt;
375       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
376         if (!isDisplay)
377           aDisplayer->erase((*aIt), false);
378       }
379       if (!isDisplay)
380         aDisplayer->erase((*aSFIt), false);
381     }
382     //aDisplayer->deactivateObjectsOutOfContext();
383   }
384   if (isDisplay)
385     ModelAPI_EventCreator::get()->sendUpdated(
386         theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
387 }
388
389 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
390                                                       const std::string& theFeatureKind)
391 {
392   // create the operation
393   ModuleBase_Operation* anOperation = 0;
394   if (theCmdId == PartSet_OperationSketch::Type()) {
395     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
396   } else {
397     ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
398     CompositeFeaturePtr aSketch;
399     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
400     if (aPrevOp) {
401       aSketch = aPrevOp->sketch();
402     }
403     if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
404       anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
405     } else if (theCmdId == PartSet_OperationFeatureEditMulti::Type()) {
406       anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
407     } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
408       anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
409     }
410   }
411
412   if (!anOperation) {
413     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
414   }
415
416   // set operation description and list of widgets corresponding to the feature xml definition
417   std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
418
419   std::string aPluginFileName = featureFile(aFeatureKind);
420   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
421   aWdgReader.readAll();
422   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
423   std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
424
425   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
426   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
427
428   // connect the operation
429   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
430   if (aPreviewOp) {
431     connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
432             SLOT(onFeatureConstructed(ObjectPtr, int)));
433     connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
434             SLOT(onRestartOperation(std::string, ObjectPtr)));
435     // If manage multi selection the it will be impossible to select more then one
436     // object under operation Edit
437 //    connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
438 //            SLOT(onMultiSelectionEnabled(bool)));
439
440     connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
441             SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
442     connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
443             SLOT(onSetSelection(const QList<ObjectPtr>&)));
444
445     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
446     if (aSketchOp) {
447       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
448               SLOT(onPlaneSelected(double, double, double)));
449       connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
450     }
451   }
452
453   return anOperation;
454 }
455
456
457 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
458 {
459   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
460   if (!anOperation)
461     return;
462
463   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
464   if (!aPreviewOp)
465     return;
466
467   FeaturePtr aFeature = aPreviewOp->feature();
468   if (!aFeature || aFeature->getKind() != theCmdId)
469     return;
470
471   XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
472   // Hide result of sketch
473   std::list<ResultPtr> aResults = aFeature->results();
474   std::list<ResultPtr>::const_iterator aIt;
475   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
476     aDisplayer->erase(*aIt, false);
477
478   std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
479
480   std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
481   for (; anIt != aLast; anIt++) {
482     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = boost::dynamic_pointer_cast<
483         SketchPlugin_Feature>(*anIt);
484     if (!aSPFeature)
485       continue;
486     std::list<ResultPtr> aResults = aSPFeature->results();
487     std::list<ResultPtr>::const_iterator aRIt;
488     for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
489       aDisplayer->display((*aRIt), false);
490       aDisplayer->activate((*aRIt), sketchSelectionModes((*aRIt)));
491     }
492     aDisplayer->display(aSPFeature, false);
493     aDisplayer->activate(aSPFeature, sketchSelectionModes(aSPFeature));
494   }
495   aDisplayer->updateViewer();
496 }
497
498 void PartSet_Module::editFeature(FeaturePtr theFeature)
499 {
500   if (!theFeature)
501     return;
502
503 //  if (theFeature->getKind() == SKETCH_KIND) {
504   //FeaturePtr aFeature = theFeature;
505   //if (XGUI_Tools::isModelObject(aFeature)) {
506   //  ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
507   //  aFeature = aObject->featureRef();
508   //}
509
510   //if (aFeature) {
511   onRestartOperation(theFeature->getKind(), theFeature);
512   updateCurrentPreview(theFeature->getKind());
513   //}
514 //  }
515 }
516
517 void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
518 {
519   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
520
521   PartSet_OperationSketchBase* aPreviewOp = 
522     dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
523   if (!aPreviewOp)
524     return;
525
526   boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
527       aFeature->data()->attribute(theAttribute));
528
529   PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
530                                 aPoint->y());
531 }
532
533 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
534                                             Config_WidgetAPI* theWidgetApi,
535                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
536 {
537   if (theType == "sketch-start-label") {
538     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, "");
539     aWgt->setOperationsMgr(xWorkshop()->operationMgr());
540     theModelWidgets.append(aWgt);
541     return aWgt->getControl();
542   } else
543     return 0;
544 }
545
546
547 XGUI_Workshop* PartSet_Module::xWorkshop() const
548 {
549   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
550   if (aConnector) {
551     return aConnector->workshop();
552   }
553   return 0;
554 }
555
556
557 QIntList PartSet_Module::sketchSelectionModes(ObjectPtr theFeature)
558 {
559   QIntList aModes;
560   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
561   if (aFeature) {
562     if (aFeature->getKind() == SketchPlugin_Sketch::ID()) {
563       aModes.append(TopAbs_FACE);
564       return aModes;
565     } else if (PartSet_Tools::isConstraintFeature(aFeature->getKind())) {
566       aModes.append(AIS_DSM_Text);
567       aModes.append(AIS_DSM_Line);
568       return aModes;
569     }
570   } 
571   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
572   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
573   return aModes;
574 }
575
576
577 gp_Pln PartSet_Module::getSketchPlane(FeaturePtr theSketch) const
578 {
579   DataPtr aData = theSketch->data();
580   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
581       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
582   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
583       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
584   gp_Pnt aOrig(anOrigin->x(), anOrigin->y(), anOrigin->z());
585   gp_Dir aDir(aNorm->x(), aNorm->y(), aNorm->z());
586   return gp_Pln(aOrig, aDir);
587 }
588
589
590 void PartSet_Module::onSelectionChanged()
591 {
592   ModuleBase_ISelection* aSelect = myWorkshop->selection();
593   QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
594   // We need to stop edit operation if selection is cleared
595   if (aSelected.size() == 0) {
596     PartSet_OperationFeatureEdit* anEditOp = 
597       dynamic_cast<PartSet_OperationFeatureEdit*>(myWorkshop->currentOperation());
598     if (!anEditOp)
599       return;
600     anEditOp->commit();
601   } else {
602     PartSet_OperationSketchBase* aSketchOp = 
603       dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
604     if (aSketchOp) {
605       aSketchOp->selectionChanged(aSelect);
606     }
607   }
608 }