Salome HOME
Issue #3236: Provide icons for extrusions
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
1 // Copyright (C) 2014-2019  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     aShapeTypes.append(ModuleBase_Tools::shapeType(myTypeCtrl->textValue()));
311   }
312   else {
313     foreach(QString aType, myShapeTypes) {
314       aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
315     }
316   }
317   return aShapeTypes;
318 }
319
320 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
321 {
322   ModuleBase_ModelWidget::setEditingMode(isEditing);
323   if (isEditing) {
324     setVisibleSelectionControl(false);
325
326     ModuleBase_ModelWidget* anAttributeListWidget = 0;
327     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
328     XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
329     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
330     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
331       if (aWidget->attributeID() == myAttributeListID) {
332         anAttributeListWidget = aWidget;
333         break;
334       }
335     }
336     if (anAttributeListWidget)
337       setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
338   }
339 }
340
341 bool PartSet_WidgetSketchCreator::isSelectionMode() const
342 {
343   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
344   bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
345
346   return !aHasValueInList;
347 }
348
349 bool PartSet_WidgetSketchCreator::hasSubObjects() const
350 {
351   bool aHasSubObjects = false;
352
353   bool aCanSetFocus = true;
354   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
355   if (aComposite.get())
356     aHasSubObjects = aComposite->numberOfSubs() > 0;
357   return aHasSubObjects;
358 }
359
360 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
361                                                const bool theToValidate)
362 {
363   bool aDone = false;
364   if (!startSketchOperation(theValues)) {
365     myIsCustomAttribute = true;
366     QList<ModuleBase_ViewerPrsPtr>::const_iterator
367       anIt = theValues.begin(), aLast = theValues.end();
368     bool aProcessed = false;
369     for (; anIt != aLast; anIt++) {
370       ModuleBase_ViewerPrsPtr aValue = *anIt;
371       if (!theToValidate || isValidInFilters(aValue))
372         aProcessed = setSelectionCustom(aValue) || aProcessed;
373     }
374     myIsCustomAttribute = false;
375     aDone = aProcessed;
376     if (aProcessed) {
377       emit valuesChanged();
378       updateObject(myFeature);
379       setVisibleSelectionControl(false);
380       // manually deactivation because the widget was
381       // not activated as has no focus acceptin controls
382       deactivate();
383       emit focusOutWidget(this);
384     }
385   }
386   return aDone;
387 }
388
389 //********************************************************************
390 bool PartSet_WidgetSketchCreator::processSelection()
391 {
392   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
393   bool isDone = setSelection(aSelected, true/*false*/);
394
395   return isDone;
396 }
397
398 //********************************************************************
399 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
400 {
401 }
402
403 bool PartSet_WidgetSketchCreator::startSketchOperation(
404                               const QList<ModuleBase_ViewerPrsPtr>& theValues)
405 {
406   bool aSketchStarted = false;
407
408   if (theValues.size() != 1)
409     return aSketchStarted;
410
411   ModuleBase_ViewerPrsPtr aValue = theValues.front();
412   if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
413     return aSketchStarted;
414
415   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
416   /// sketch should not started by object(face) selected as global. If Local face is selected,
417   /// sketch is started
418   if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
419     ResultConstructionPtr aConstruction =
420       std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
421     if (!aConstruction.get() || !aConstruction->isInfinite())
422       return aSketchStarted;
423   }
424   aSketchStarted = true;
425   // Set View size if a plane is selected
426   if (myPreviewPlanes->isPreviewDisplayed() &&
427       myPreviewPlanes->isPreviewShape(aValue->shape())) {
428     // set default plane size
429     bool isSetSizeOfView = false;
430     double aSizeOfView = 0;
431     QString aSizeOfViewStr = mySizeOfView->text();
432     if (!aSizeOfViewStr.isEmpty()) {
433       aSizeOfView = aSizeOfViewStr.toDouble(&isSetSizeOfView);
434       if (isSetSizeOfView && aSizeOfView <= 0) {
435         isSetSizeOfView = false;
436       }
437     }
438     if (isSetSizeOfView)
439       myModule->sketchMgr()->previewSketchPlane()->setSizeOfView(aSizeOfView, true);
440   }
441   // manually deactivation because the widget was not activated as has no focus acceptin controls
442   deactivate();
443   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
444   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
445
446   // start edit operation for the sketch
447   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
448                                                             (myModule->createOperation("Sketch"));
449   QList<ModuleBase_ViewerPrsPtr> aValues;
450   aValues.push_back(aValue);
451   aFOperation->setPreselection(aValues);
452
453   myWorkshop->processLaunchOperation(aFOperation);
454
455   return aSketchStarted;
456 }
457
458 bool PartSet_WidgetSketchCreator::focusTo()
459 {
460   // this method is called only in creation mode. In Edition mode this widget is hidden
461   if (isSelectionMode() && !hasSubObjects()) {
462     setVisibleSelectionControl(true);
463     activateSelectionControl();
464     openExtrusionTransaction();
465     return true;
466   }
467   else
468     connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
469             SLOT(onResumed(ModuleBase_Operation*)));
470
471   return true;
472 }
473
474 void PartSet_WidgetSketchCreator::deactivate()
475 {
476   ModuleBase_WidgetSelector::deactivate();
477
478   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
479   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
480   if (aHidePreview)
481     XGUI_Tools::workshop(myWorkshop)->viewer()->update();
482
483 }
484
485 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
486 {
487   SessionPtr aMgr = ModelAPI_Session::get();
488   bool aIsOp = aMgr->isOperation();
489   if (aIsOp) {
490     // in current implementation, all transactions are closed when resume happens
491     // so, this is a wrong case, which is not checked.
492     // To provide it, make correction in later rows about abort/undo transactions
493     return;
494   }
495   // Set visible only selection control
496   setVisibleSelectionControl(true);
497
498   // Validate the created sketch. If it is valid, it is set into the composite feature selection
499   // list, otherwise it is removed
500   CompositeFeaturePtr aCompFeature =
501     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
502   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
503   if (aCompFeature->numberOfSubs() > 0) {
504     // set the sub feature to attribute selection list and check whether sketch is valid
505     SessionPtr aMgr = ModelAPI_Session::get();
506     const static std::string aNestedOpID("Set Sketch result into Selection list");
507     aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
508     setSketchObjectToList(aCompFeature, anAttrList);
509     aMgr->finishOperation();
510
511     if (!validateSelectionList()) {
512 #ifdef DEBUG_UNDO_INVALID_SKETCH
513       aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
514       aMgr->undo(); /// Sketch creation
515 #else
516       aMgr->startOperation("Delete invalid Sketch feature", false);
517
518       // delete invalid sketch
519       CompositeFeaturePtr aSketchFeature =
520               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
521       QObjectPtrList anObjects;
522       anObjects.append(aSketchFeature);
523
524       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
525       aWorkshop->deleteFeatures(anObjects);
526
527       aMgr->finishOperation();
528 #endif
529     }
530   }
531   openExtrusionTransaction();
532
533   if (aCompFeature->numberOfSubs() == 0) {
534     // call activateWidget() of the parent to connect to the viewer seleciton
535     activateSelectionControl();
536   }
537   else {
538     // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
539     /// the attribute selection list will be filled by result of this sketch.
540     setVisibleSelectionControl(false);
541
542     // Update value in attribute selection list
543     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
544     XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
545     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
546     ModuleBase_ModelWidget* aListWidget = 0;
547     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
548       if (aWidget->attributeID() == myAttributeListID) {
549         aListWidget = aWidget;
550         break;
551       }
552     }
553     if (aListWidget) {
554       aListWidget->restoreValue();
555       aPropertyPanel->activateNextWidget(aListWidget);
556     }
557
558     // Hide sketcher result
559     CompositeFeaturePtr aSketchFeature =
560       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
561     std::list<ResultPtr> aResults = aSketchFeature->results();
562     std::list<ResultPtr>::const_iterator aIt;
563     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
564       (*aIt)->setDisplayed(false);
565     }
566     aSketchFeature->setDisplayed(false);
567     static Events_Loop* aLoop = Events_Loop::loop();
568     aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
569
570     // Add Selected body were created the sketcher to list of selected objects
571     std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
572     AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
573     if (aSelList.get()) {
574       DataPtr aData = aSketchFeature->data();
575       AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
576                                     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
577       ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
578       if (aRes.get()) {
579         SessionPtr aMgr = ModelAPI_Session::get();
580         ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
581         AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
582         std::string aValidatorID;
583         Events_InfoMessage anError;
584         aSelList->append(aRes, GeomShapePtr());
585         if (aFactory->validate(anAttribute, aValidatorID, anError))
586           updateObject(aCompFeature);
587         else
588           aSelList->clear();
589       }
590     }
591   }
592   restoreValue();
593 }
594
595 bool PartSet_WidgetSketchCreator::validateSelectionList() const
596 {
597   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
598
599   SessionPtr aMgr = ModelAPI_Session::get();
600   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
601   std::string aValidatorID;
602   Events_InfoMessage anError;
603   bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
604   if (!isValidPComposite) {
605     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
606     // TODO(spo): translate
607     QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
608                   tr("Sketch is invalid and will be deleted.\nError: %1")
609                   .arg(anError.messageString().c_str()),
610                   QMessageBox::Ok);
611   }
612   return isValidPComposite;
613 }
614
615 void PartSet_WidgetSketchCreator::setSketchObjectToList(
616                             const CompositeFeaturePtr& theCompositeFeature,
617                             const AttributePtr& theAttribute)
618 {
619   if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
620     return;
621
622   AttributeSelectionListPtr aBaseObjectsSelectionList =
623                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
624   if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
625     return;
626   }
627
628   FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
629   if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
630     return;
631   }
632
633   ResultPtr aSketchRes = aSketchFeature->results().front();
634   ResultConstructionPtr aConstruction =
635     std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
636   if(!aConstruction.get()) {
637     return;
638   }
639
640   if(aBaseObjectsSelectionList->size() == 0) {
641     aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
642   }
643 }
644
645 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
646                                                         const bool theEnabled)
647 {
648   QList<QWidget*> aMyControls = theModelWidget->getControls();
649   foreach(QWidget*  eachControl, aMyControls) {
650     eachControl->setEnabled(theEnabled);
651   }
652 }
653
654 void PartSet_WidgetSketchCreator::onSelectionTypeChanged()
655 {
656   // Clear current selection in order to avoid updating of object browser with obsolete indexes
657   // which can appear because of results deletetion after changing a type of selection
658   QString aSelectionType = myTypeCtrl->textValue();
659   QList<ModuleBase_ViewerPrsPtr> aEmptyList;
660   myWorkshop->setSelected(aEmptyList);
661
662   updateSelectionModesAndFilters(true);
663   myWorkshop->selectionActivate()->updateSelectionModes();
664
665   if (!myFeature)
666     return;
667
668   if (aSelectionType != "Faces") {
669     setVisibleSelectionControl(false);
670     myWorkshop->propertyPanel()->activateNextWidget();
671   }
672
673   /// store the selected type
674   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
675   anAttrList->setSelectionType(aSelectionType.toStdString());
676   anAttrList->clear();
677
678   // update object is necessary to flush update signal. It leads to objects references map update
679   // and the operation presentation will not contain deleted items visualized as parameters of
680   // the feature.
681   updateObject(myFeature);
682   myWorkshop->propertyPanel()->activeWidget()->restoreValue();
683   myWorkshop->setSelected(getAttributeSelection());
684   // may be the feature's result is not displayed, but attributes should be
685   // hope that something is redisplayed by object updated
686   myWorkshop->module()->customizeFeature(myFeature, ModuleBase_IModule::CustomizeArguments, true);
687 }