Salome HOME
Copyright update 2021
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
1 // Copyright (C) 2014-2021  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_WidgetSketchCreator.h"
21 #include "PartSet_Module.h"
22 #include "PartSet_WidgetSketchLabel.h"
23 #include "PartSet_PreviewPlanes.h"
24
25 #include <Config_Keywords.h>
26
27 #include <XGUI_ModuleConnector.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_Displayer.h>
30 #include <XGUI_SelectionMgr.h>
31 #include <XGUI_OperationMgr.h>
32 #include <XGUI_PropertyPanel.h>
33 #include <XGUI_Tools.h>
34 #include <XGUI_ViewerProxy.h>
35
36 #include <GeomAPI_Face.h>
37
38 #include <Events_InfoMessage.h>
39
40 #include <ModelAPI_Session.h>
41 #include <ModelAPI_ResultBody.h>
42 #include <ModelAPI_AttributeSelection.h>
43 #include <ModelAPI_AttributeSelectionList.h>
44 #include <ModelAPI_Validator.h>
45 #include <ModelAPI_Events.h>
46 #include <ModelAPI_ResultConstruction.h>
47
48 #include <SketchPlugin_SketchEntity.h>
49 #include <FeaturesPlugin_CompositeBoolean.h>
50
51 #include <ModuleBase_Tools.h>
52 #include <ModuleBase_Operation.h>
53 #include <ModuleBase_IPropertyPanel.h>
54 #include <ModuleBase_OperationFeature.h>
55 #include <ModuleBase_ViewerPrs.h>
56 #include <ModuleBase_ChoiceCtrl.h>
57 #include <ModuleBase_IWorkshop.h>
58 #include <ModuleBase_ISelectionActivate.h>
59
60 #include <Config_WidgetAPI.h>
61
62 #include <Events_Loop.h>
63
64 #include <QLabel>
65 #include <QLineEdit>
66 #include <QDoubleValidator>
67 //#include <QFormLayout>
68 #include <QVBoxLayout>
69 #include <QMessageBox>
70 #include <QMainWindow>
71
72 #define DEBUG_UNDO_INVALID_SKETCH
73
74
75
76 QStringList getIconsList(const QStringList& theNames)
77 {
78   QStringList aIcons;
79   foreach(QString aName, theNames) {
80     QString aUName = aName.toUpper();
81     if ((aUName == "VERTICES") || (aUName == "VERTEX"))
82       aIcons << ":pictures/vertex32.png";
83     else if ((aUName == "EDGES") || (aUName == "EDGE"))
84       aIcons << ":pictures/edge32.png";
85     else if ((aUName == "FACES") || (aUName == "FACE"))
86       aIcons << ":pictures/face32.png";
87   }
88   return aIcons;
89 }
90
91
92
93 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
94                                                          PartSet_Module* theModule,
95                                                          const Config_WidgetAPI* theData)
96 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
97   myModule(theModule), myIsCustomAttribute(false)
98 {
99   myAttributeListID = theData->getProperty("attribute_list_id");
100
101   //QFormLayout* aLayout = new QFormLayout(this);
102   QVBoxLayout* aLayout = new QVBoxLayout(this);
103   ModuleBase_Tools::zeroMargins(aLayout);
104
105   ModuleBase_Tools::adjustMargins(aLayout);
106
107   QString aLabelText = translate(theData->widgetLabel());
108   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
109
110   // Size of the View control
111   mySizeOfViewWidget = new QWidget(this);
112   QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
113   aSizeLayout->addWidget(new QLabel(tr("Size of the view"), mySizeOfViewWidget));
114   mySizeOfView = new QLineEdit(mySizeOfViewWidget);
115
116   QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
117   aValidator->setLocale(ModuleBase_Tools::doubleLocale());
118   aValidator->setNotation(QDoubleValidator::StandardNotation);
119   mySizeOfView->setValidator(aValidator);
120   aSizeLayout->addWidget(mySizeOfView);
121
122   myLabel = new QLabel(aLabelText, this);
123   myLabel->setWordWrap(true);
124
125   aLayout->addWidget(mySizeOfViewWidget);
126   aLayout->addWidget(myLabel);
127
128   std::string aTypes = theData->getProperty("shape_types");
129   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
130   myIsUseChoice = theData->getBooleanAttribute("use_choice", false);
131
132   QStringList aIconsList;
133   std::string aIcons = theData->getProperty("type_icons");
134   if (aIcons.size() > 0)
135     aIconsList = QString(aIcons.c_str()).split(' ', QString::SkipEmptyParts);
136
137   if (aIconsList.size() != myShapeTypes.size())
138     aIconsList = getIconsList(myShapeTypes);
139
140   myTypeCtrl = new ModuleBase_ChoiceCtrl(this, myShapeTypes, aIconsList);
141   myTypeCtrl->setLabel(tr("Type"));
142   if (!myShapeTypes.empty()) {
143     std::string aDefType = theData->getProperty("default_type");
144     if (aDefType.size() > 0) {
145       bool aOk = false;
146       int aId = QString(aDefType.c_str()).toInt(&aOk);
147       if (aOk) {
148         myTypeCtrl->setValue(aId);
149         myDefMode = myShapeTypes.at(aId).toStdString();
150       }
151     }
152     if (myDefMode.size() == 0) {
153       myTypeCtrl->setValue(0);
154       myDefMode = myShapeTypes.first().toStdString();
155     }
156   }
157   aLayout->addWidget(myTypeCtrl);
158   // There is no sense to parameterize list of types while we can not parameterize selection mode
159   // if the xml definition contains one type, the controls to select a type should not be shown
160   if (myShapeTypes.size() <= 1 || !myIsUseChoice) {
161     myTypeCtrl->setVisible(false);
162   }
163   connect(myTypeCtrl, SIGNAL(valueChanged(int)), this, SLOT(onSelectionTypeChanged()));
164
165   aLayout->addStretch(1);
166   myPreviewPlanes = new PartSet_PreviewPlanes();
167 }
168
169 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
170 {
171   // we need to deactivate here in order to hide preview planes if the selection mode is
172   // active
173   deactivate();
174 }
175
176 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
177 {
178   QList<QWidget*> aControls;
179   aControls.append(myLabel);
180   return aControls;
181 }
182
183 bool PartSet_WidgetSketchCreator::restoreValueCustom()
184 {
185   return true;
186 }
187
188 bool PartSet_WidgetSketchCreator::storeValueCustom()
189 {
190   return true;
191 }
192
193 AttributePtr PartSet_WidgetSketchCreator::attribute() const
194 {
195   AttributePtr anAttribute;
196   if (myIsCustomAttribute)
197     anAttribute = myFeature->attribute(myAttributeListID);
198   else
199     anAttribute = ModuleBase_WidgetSelector::attribute();
200
201   return anAttribute;
202 }
203
204 //********************************************************************
205 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
206 {
207   SessionPtr aMgr = ModelAPI_Session::get();
208   bool aIsOp = aMgr->isOperation();
209   if (!aIsOp) {
210     const static std::string aNestedOpID("Parameters modification");
211     aMgr->startOperation(aNestedOpID, true);
212   }
213 }
214
215 //********************************************************************
216 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
217 {
218   bool aValid = false;
219   if (myIsCustomAttribute) {
220     // check only suiting of the value to custom attribute (myAttributeListID)
221     // do not cash of validation to avoid using states, stored for XML attribute
222     // there is an alternative is to call clearValidatedCash() in setSelection()
223     aValid = isValidSelectionForAttribute(theValue, attribute());
224   }
225   else { /// if the validated attribute is already custom
226     if (getValidState(theValue, aValid)) {
227       return aValid;
228     }
229     aValid = isValidSelectionCustom(theValue);
230     if (!aValid)
231       // check selection to create new sketh (XML current attribute)
232       aValid = isValidSelectionForAttribute(theValue, attribute());
233     if (!aValid) {
234       // check selection to fill list attribute (myAttributeListID)
235       bool isCustomAttribute = myIsCustomAttribute;
236       myIsCustomAttribute = true;
237       aValid = isValidSelectionForAttribute(theValue, attribute());
238       myIsCustomAttribute = isCustomAttribute;
239     }
240   }
241   storeValidState(theValue, aValid);
242   return aValid;
243 }
244
245 //********************************************************************
246 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
247 {
248   return PartSet_WidgetSketchLabel::canFillSketch(theValue);
249 }
250
251 void PartSet_WidgetSketchCreator::activateSelectionControl()
252 {
253   // reset previously set size of view needed on restart extrusion after Sketch
254   if (myModule->sketchMgr()->previewSketchPlane()->isUseSizeOfView())
255     myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(0, false);
256
257   // we need to call activate here as the widget has no focus accepted controls
258   // if these controls are added here, activate will happens automatically after focusIn()
259   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
260   XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
261   aPanel->activateWidget(this, false);
262 }
263
264 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
265 {
266   // hide current widget, activate the next widget
267   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
268   XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
269   const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
270   foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
271     if (theSelectionControl) { // hide other controls
272       if (aWidget != this)
273         aWidget->setVisible(false);
274     }
275     else { // hide current control
276       if (aWidget == this)
277         aWidget->setVisible(false);
278       else {
279         aWidget->setVisible(true);
280         if (aWidget->attributeID() == myAttributeListID)
281           setEnabledModelWidget(aWidget, !hasSubObjects());
282       }
283     }
284   }
285
286   if (theSelectionControl) {
287     bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
288     bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
289     if (!aBodyIsVisualized && !aSketchIsVisualized) {
290       // We have to select a plane before any operation
291       myPreviewPlanes->showPreviewPlanes(myWorkshop);
292       mySizeOfViewWidget->setVisible(true);
293     }
294     else {
295       mySizeOfViewWidget->setVisible(false);
296     }
297
298   } else {
299     bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
300     myPreviewPlanes->erasePreviewPlanes(myWorkshop);
301     if (aHidePreview)
302       aWorkshop->viewer()->update();
303   }
304 }
305
306 QIntList PartSet_WidgetSketchCreator::shapeTypes() const
307 {
308   QIntList aShapeTypes;
309   if (myShapeTypes.length() > 1 && myIsUseChoice) {
310     QStringList aTypes = myTypeCtrl->textValue().split("|", QString::SkipEmptyParts);
311     foreach(QString aType, aTypes) {
312       aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
313     }
314   }
315   else {
316     foreach(QString aType, myShapeTypes) {
317       QStringList aSubTypes = aType.split("|", QString::SkipEmptyParts);
318       foreach(QString asubType, aSubTypes) {
319         aShapeTypes.append(ModuleBase_Tools::shapeType(asubType));
320       }
321     }
322   }
323   return aShapeTypes;
324 }
325
326 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
327 {
328   ModuleBase_ModelWidget::setEditingMode(isEditing);
329   if (isEditing) {
330     setVisibleSelectionControl(false);
331
332     ModuleBase_ModelWidget* anAttributeListWidget = 0;
333     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
334     XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
335     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
336     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
337       if (aWidget->attributeID() == myAttributeListID) {
338         anAttributeListWidget = aWidget;
339         break;
340       }
341     }
342     if (anAttributeListWidget)
343       setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
344   }
345 }
346
347 bool PartSet_WidgetSketchCreator::isSelectionMode() const
348 {
349   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
350   bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
351
352   return !aHasValueInList;
353 }
354
355 bool PartSet_WidgetSketchCreator::hasSubObjects() const
356 {
357   bool aHasSubObjects = false;
358   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
359   if (aComposite.get())
360     aHasSubObjects = aComposite->numberOfSubs() > 0;
361   return aHasSubObjects;
362 }
363
364 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
365                                                const bool theToValidate)
366 {
367   bool aDone = false;
368   if (!startSketchOperation(theValues)) {
369     myIsCustomAttribute = true;
370     QList<ModuleBase_ViewerPrsPtr>::const_iterator
371       anIt = theValues.begin(), aLast = theValues.end();
372     bool aProcessed = false;
373     for (; anIt != aLast; anIt++) {
374       ModuleBase_ViewerPrsPtr aValue = *anIt;
375       if (!theToValidate || isValidInFilters(aValue))
376         aProcessed = setSelectionCustom(aValue) || aProcessed;
377     }
378     myIsCustomAttribute = false;
379     aDone = aProcessed;
380     if (aProcessed) {
381       emit valuesChanged();
382       updateObject(myFeature);
383       setVisibleSelectionControl(false);
384       // manually deactivation because the widget was
385       // not activated as has no focus acceptin controls
386       deactivate();
387       emit focusOutWidget(this);
388     }
389   }
390   return aDone;
391 }
392
393 //********************************************************************
394 bool PartSet_WidgetSketchCreator::processSelection()
395 {
396   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
397   bool isDone = setSelection(aSelected, true/*false*/);
398
399   return isDone;
400 }
401
402 //********************************************************************
403 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
404 {
405 }
406
407 bool PartSet_WidgetSketchCreator::startSketchOperation(
408                               const QList<ModuleBase_ViewerPrsPtr>& theValues)
409 {
410   bool aSketchStarted = false;
411
412   if (theValues.size() != 1)
413     return aSketchStarted;
414
415   ModuleBase_ViewerPrsPtr aValue = theValues.front();
416   if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
417     return aSketchStarted;
418
419   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
420   /// sketch should not started by object(face) selected as global. If Local face is selected,
421   /// sketch is started
422   if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
423     ResultConstructionPtr aConstruction =
424       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
425     if (!aConstruction.get() || !aConstruction->isInfinite())
426       return aSketchStarted;
427   }
428   aSketchStarted = true;
429   // Set View size if a plane is selected
430   if (myPreviewPlanes->isPreviewDisplayed() &&
431       myPreviewPlanes->isPreviewShape(aValue->shape())) {
432     // set default plane size
433     bool isSetSizeOfView = false;
434     double aSizeOfView = 0;
435     QString aSizeOfViewStr = mySizeOfView->text();
436     if (!aSizeOfViewStr.isEmpty()) {
437       aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
438       if (isSetSizeOfView && aSizeOfView <= 0) {
439         isSetSizeOfView = false;
440       }
441     }
442     if (isSetSizeOfView)
443       myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
444   }
445   // manually deactivation because the widget was not activated as has no focus acceptin controls
446   deactivate();
447   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
448
449   // start edit operation for the sketch
450   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
451                                                             (myModule->createOperation("Sketch"));
452   QList<ModuleBase_ViewerPrsPtr> aValues;
453   aValues.push_back(aValue);
454   aFOperation->setPreselection(aValues);
455
456   myWorkshop->processLaunchOperation(aFOperation);
457
458   return aSketchStarted;
459 }
460
461 bool PartSet_WidgetSketchCreator::focusTo()
462 {
463   // this method is called only in creation mode. In Edition mode this widget is hidden
464   if (isSelectionMode() && !hasSubObjects()) {
465     setVisibleSelectionControl(true);
466     activateSelectionControl();
467     openExtrusionTransaction();
468     return true;
469   }
470   else
471     connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
472             SLOT(onResumed(ModuleBase_Operation*)));
473
474   return true;
475 }
476
477 void PartSet_WidgetSketchCreator::deactivate()
478 {
479   ModuleBase_WidgetSelector::deactivate();
480
481   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
482   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
483   if (aHidePreview)
484     XGUI_Tools::workshop(myWorkshop)->viewer()->update();
485
486 }
487
488 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
489 {
490   SessionPtr aMgr = ModelAPI_Session::get();
491   bool aIsOp = aMgr->isOperation();
492   if (aIsOp) {
493     // in current implementation, all transactions are closed when resume happens
494     // so, this is a wrong case, which is not checked.
495     // To provide it, make correction in later rows about abort/undo transactions
496     return;
497   }
498   // Set visible only selection control
499   setVisibleSelectionControl(true);
500
501   // Validate the created sketch. If it is valid, it is set into the composite feature selection
502   // list, otherwise it is removed
503   CompositeFeaturePtr aCompFeature =
504     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
505   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
506   if (aCompFeature->numberOfSubs() > 0) {
507     // set the sub feature to attribute selection list and check whether sketch is valid
508     const static std::string aNestedOpID("Set Sketch result into Selection list");
509     aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
510     setSketchObjectToList(aCompFeature, anAttrList);
511     aMgr->finishOperation();
512
513     if (!validateSelectionList()) {
514 #ifdef DEBUG_UNDO_INVALID_SKETCH
515       aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
516       aMgr->undo(); /// Sketch creation
517 #else
518       aMgr->startOperation("Delete invalid Sketch feature", false);
519
520       // delete invalid sketch
521       CompositeFeaturePtr aSketchFeature =
522               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
523       QObjectPtrList anObjects;
524       anObjects.append(aSketchFeature);
525
526       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
527       aWorkshop->deleteFeatures(anObjects);
528
529       aMgr->finishOperation();
530 #endif
531     }
532   }
533   openExtrusionTransaction();
534
535   if (aCompFeature->numberOfSubs() == 0) {
536     // call activateWidget() of the parent to connect to the viewer seleciton
537     activateSelectionControl();
538   }
539   else {
540     // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
541     /// the attribute selection list will be filled by result of this sketch.
542     setVisibleSelectionControl(false);
543
544     // Update value in attribute selection list
545     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
546     XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
547     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
548     ModuleBase_ModelWidget* aListWidget = 0;
549     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
550       if (aWidget->attributeID() == myAttributeListID) {
551         aListWidget = aWidget;
552         break;
553       }
554     }
555     if (aListWidget) {
556       aListWidget->restoreValue();
557       aPropertyPanel->activateNextWidget(aListWidget);
558     }
559
560     // Hide sketcher result
561     CompositeFeaturePtr aSketchFeature =
562       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
563     std::list<ResultPtr> aResults = aSketchFeature->results();
564     std::list<ResultPtr>::const_iterator aIt;
565     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
566       (*aIt)->setDisplayed(false);
567     }
568     aSketchFeature->setDisplayed(false);
569     static Events_Loop* aLoop = Events_Loop::loop();
570     aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
571
572     // Add Selected body were created the sketcher to list of selected objects
573     std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
574     AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
575     if (aSelList.get()) {
576       DataPtr aData = aSketchFeature->data();
577       AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
578                                     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
579       ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
580       if (aRes.get()) {
581         ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
582         AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
583         std::string aValidatorID;
584         Events_InfoMessage anError;
585         aSelList->append(aRes, GeomShapePtr());
586         if (aFactory->validate(anAttribute, aValidatorID, anError))
587           updateObject(aCompFeature);
588         else
589           aSelList->clear();
590       }
591     }
592   }
593   restoreValue();
594 }
595
596 bool PartSet_WidgetSketchCreator::validateSelectionList() const
597 {
598   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
599
600   SessionPtr aMgr = ModelAPI_Session::get();
601   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
602   std::string aValidatorID;
603   Events_InfoMessage anError;
604   bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
605   if (!isValidPComposite) {
606     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
607     // TODO(spo): translate
608     QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
609                   tr("Sketch is invalid and will be deleted.\nError: %1")
610                   .arg(anError.messageString().c_str()),
611                   QMessageBox::Ok);
612   }
613   return isValidPComposite;
614 }
615
616 void PartSet_WidgetSketchCreator::setSketchObjectToList(
617                             const CompositeFeaturePtr& theCompositeFeature,
618                             const AttributePtr& theAttribute)
619 {
620   if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
621     return;
622
623   AttributeSelectionListPtr aBaseObjectsSelectionList =
624                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
625   if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
626     return;
627   }
628
629   FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
630   if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
631     return;
632   }
633
634   ResultPtr aSketchRes = aSketchFeature->results().front();
635   ResultConstructionPtr aConstruction =
636     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
637   if(!aConstruction.get()) {
638     return;
639   }
640
641   if(aBaseObjectsSelectionList->size() == 0) {
642     aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
643   }
644 }
645
646 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
647                                                         const bool theEnabled)
648 {
649   QList<QWidget*> aMyControls = theModelWidget->getControls();
650   foreach(QWidget*  eachControl, aMyControls) {
651     eachControl->setEnabled(theEnabled);
652   }
653 }
654
655 void PartSet_WidgetSketchCreator::onSelectionTypeChanged()
656 {
657   // Clear current selection in order to avoid updating of object browser with obsolete indexes
658   // which can appear because of results deletetion after changing a type of selection
659   QString aSelectionType = myTypeCtrl->textValue();
660   QList<ModuleBase_ViewerPrsPtr> aEmptyList;
661   myWorkshop->setSelected(aEmptyList);
662
663   updateSelectionModesAndFilters(true);
664   myWorkshop->selectionActivate()->updateSelectionModes();
665
666   if (!myFeature)
667     return;
668
669   if (aSelectionType != "Faces") {
670     setVisibleSelectionControl(false);
671     myWorkshop->propertyPanel()->activateNextWidget();
672   }
673
674   /// store the selected type
675   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
676   anAttrList->setSelectionType(aSelectionType.toStdString());
677   anAttrList->clear();
678
679   // update object is necessary to flush update signal. It leads to objects references map update
680   // and the operation presentation will not contain deleted items visualized as parameters of
681   // the feature.
682   updateObject(myFeature);
683   myWorkshop->propertyPanel()->activeWidget()->restoreValue();
684   myWorkshop->setSelected(getAttributeSelection());
685   // may be the feature's result is not displayed, but attributes should be
686   // hope that something is redisplayed by object updated
687   myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments, true);
688 }