Salome HOME
Issue #1489 Multi-rotation problem if there is a reference to copied objects
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetSketchCreator.cpp
4 // Created:     08 June 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_WidgetSketchCreator.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetSketchLabel.h"
10 #include "PartSet_PreviewPlanes.h"
11
12 #include <Config_Keywords.h>
13
14 #include <XGUI_ModuleConnector.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_Displayer.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <XGUI_OperationMgr.h>
19 #include <XGUI_PropertyPanel.h>
20 #include <XGUI_Tools.h>
21 #include <XGUI_ViewerProxy.h>
22
23 #include <GeomAPI_Face.h>
24
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_AttributeSelection.h>
28 #include <ModelAPI_AttributeSelectionList.h>
29 #include <ModelAPI_Validator.h>
30 #include <ModelAPI_Events.h>
31 #include <ModelAPI_ResultConstruction.h>
32
33 #include <SketchPlugin_SketchEntity.h>
34 #include <FeaturesPlugin_CompositeBoolean.h>
35
36 #include <ModuleBase_Tools.h>
37 #include <ModuleBase_Operation.h>
38 #include <ModuleBase_IPropertyPanel.h>
39 #include <ModuleBase_OperationFeature.h>
40 #include <ModuleBase_ViewerPrs.h>
41
42 #include <Config_WidgetAPI.h>
43
44 #include <Events_Loop.h>
45
46 #include <QLabel>
47 #include <QLineEdit>
48 //#include <QFormLayout>
49 #include <QVBoxLayout>
50 #include <QMessageBox>
51 #include <QMainWindow>
52
53 #define DEBUG_UNDO_INVALID_SKETCH
54
55 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent, 
56                                                          PartSet_Module* theModule,
57                                                          const Config_WidgetAPI* theData)
58 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
59   myModule(theModule), myIsCustomAttribute(false)
60 {
61   myAttributeListID = theData->getProperty("attribute_list_id");
62
63   //QFormLayout* aLayout = new QFormLayout(this);
64   QVBoxLayout* aLayout = new QVBoxLayout(this);
65   ModuleBase_Tools::zeroMargins(aLayout);
66
67   ModuleBase_Tools::adjustMargins(aLayout);
68
69   QString aLabelText = QString::fromStdString(theData->widgetLabel());
70   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
71
72   myLabel = new QLabel(aLabelText, this);
73   myLabel->setWordWrap(true);
74   aLayout->addWidget(myLabel);
75   aLayout->addStretch(1);
76
77   std::string aTypes = theData->getProperty("shape_types");
78   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
79
80   myPreviewPlanes = new PartSet_PreviewPlanes();
81 }
82
83 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
84 {
85   // we need to deactivate here in order to hide preview planes if the selection mode is
86   // active
87   deactivate();
88 }
89
90 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
91 {
92   QList<QWidget*> aControls;
93   aControls.append(myLabel);
94   return aControls;
95 }
96
97 bool PartSet_WidgetSketchCreator::restoreValueCustom()
98 {
99   return true;
100 }
101
102 bool PartSet_WidgetSketchCreator::storeValueCustom()
103 {
104   return true;
105 }
106
107 AttributePtr PartSet_WidgetSketchCreator::attribute() const
108 {
109   AttributePtr anAttribute;
110   if (myIsCustomAttribute)
111     anAttribute = myFeature->attribute(myAttributeListID);
112   else
113     anAttribute = ModuleBase_WidgetSelector::attribute();
114
115   return anAttribute;
116 }
117
118 //********************************************************************
119 void PartSet_WidgetSketchCreator::openExtrusionTransaction()
120 {
121   SessionPtr aMgr = ModelAPI_Session::get();
122   bool aIsOp = aMgr->isOperation();
123   if (!aIsOp) {
124     const static std::string aNestedOpID("Parameters modification");
125     aMgr->startOperation(aNestedOpID, true);
126   }
127 }
128
129 //********************************************************************
130 bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
131 {
132   bool aValid = false;
133   if (myIsCustomAttribute) {
134     // check only suiting of the value to custom attribute (myAttributeListID)
135     // do not cash of validation to avoid using states, stored for XML attribute
136     // there is an alternative is to call clearValidatedCash() in setSelection()
137     aValid = isValidSelectionForAttribute(theValue, attribute());
138   }
139   else { /// if the validated attribute is already custom
140     if (getValidState(theValue, aValid)) {
141       return aValid;
142     }
143     aValid = isValidSelectionCustom(theValue);
144     if (!aValid)
145       // check selection to create new sketh (XML current attribute)
146       aValid = isValidSelectionForAttribute(theValue, attribute());
147     if (!aValid) {
148       // check selection to fill list attribute (myAttributeListID)
149       bool isCustomAttribute = myIsCustomAttribute;
150       myIsCustomAttribute = true;
151       aValid = isValidSelectionForAttribute(theValue, attribute());
152       myIsCustomAttribute = isCustomAttribute;
153     }
154   }
155   storeValidState(theValue, aValid);
156   return aValid;
157 }
158
159 //********************************************************************
160 bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
161 {
162   return PartSet_WidgetSketchLabel::canFillSketch(theValue);
163 }
164
165 void PartSet_WidgetSketchCreator::activateSelectionControl()
166 {
167   // we need to call activate here as the widget has no focus accepted controls
168   // if these controls are added here, activate will happens automatically after focusIn()
169   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
170   XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
171   aPanel->activateWidget(this, false);
172 }
173
174 void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelectionControl)
175 {
176   // hide current widget, activate the next widget
177   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
178   XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
179   const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
180   foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
181     if (theSelectionControl) { // hide other controls
182       if (aWidget != this)
183         aWidget->setVisible(false);
184     }
185     else { // hide current control
186       if (aWidget == this)
187         aWidget->setVisible(false);
188       else {
189         aWidget->setVisible(true);
190         if (aWidget->attributeID() == myAttributeListID)
191           setEnabledModelWidget(aWidget, !hasSubObjects());
192       }
193     }
194   }
195
196   if (theSelectionControl) {
197     bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
198     bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
199     if (!aBodyIsVisualized && !aSketchIsVisualized) {
200       // We have to select a plane before any operation
201       myPreviewPlanes->showPreviewPlanes(myWorkshop);
202     }
203   } else {
204     bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
205     myPreviewPlanes->erasePreviewPlanes(myWorkshop);
206     if (aHidePreview)
207       aWorkshop->viewer()->update();
208   }
209 }
210
211 QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
212 {
213   QIntList aShapeTypes;
214   foreach(QString aType, myShapeTypes) {
215     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
216   }
217   return aShapeTypes;
218 }
219
220 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
221 {
222   ModuleBase_ModelWidget::setEditingMode(isEditing);
223   if (isEditing) {
224     setVisibleSelectionControl(false);
225
226     ModuleBase_ModelWidget* anAttributeListWidget = 0;
227     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
228     XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
229     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
230     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
231       if (aWidget->attributeID() == myAttributeListID) {
232         anAttributeListWidget = aWidget;
233         break;
234       }
235     }
236     if (anAttributeListWidget)
237       setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
238   }
239 }
240
241 bool PartSet_WidgetSketchCreator::isSelectionMode() const
242 {
243   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
244   bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
245
246   return !aHasValueInList;
247 }
248
249 bool PartSet_WidgetSketchCreator::hasSubObjects() const
250 {
251   bool aHasSubObjects = false;
252
253   bool aCanSetFocus = true;
254   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
255   if (aComposite.get())
256     aHasSubObjects = aComposite->numberOfSubs() > 0;
257   return aHasSubObjects;
258 }
259
260 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
261                                                const bool theToValidate)
262 {
263   bool aDone = false;
264   if (!startSketchOperation(theValues)) {
265     myIsCustomAttribute = true;
266     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
267     bool aProcessed = false;
268     for (; anIt != aLast; anIt++) {
269       ModuleBase_ViewerPrsPtr aValue = *anIt;
270       if (!theToValidate || isValidInFilters(aValue))
271         aProcessed = setSelectionCustom(aValue) || aProcessed;
272     }
273     myIsCustomAttribute = false;
274     aDone = aProcessed;
275     if (aProcessed) {
276       emit valuesChanged();
277       updateObject(myFeature);
278       setVisibleSelectionControl(false);
279       // manually deactivation because the widget was not activated as has no focus acceptin controls
280       deactivate();
281       emit focusOutWidget(this);
282     }
283   }
284   return aDone;
285 }
286
287 //********************************************************************
288 void PartSet_WidgetSketchCreator::onSelectionChanged()
289 {
290   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
291   bool isDone = setSelection(aSelected, true/*false*/);
292 }
293
294 //********************************************************************
295 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
296 {
297 }
298
299 bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrsPtr>& theValues)
300 {
301   bool aSketchStarted = false;
302
303   if (theValues.size() != 1)
304     return aSketchStarted;
305
306   ModuleBase_ViewerPrsPtr aValue = theValues.front();
307   if (!aValue.get() || !PartSet_WidgetSketchLabel::canFillSketch(aValue))
308     return aSketchStarted;
309
310   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
311   /// sketch should not started by object(face) selected as global. If Local face is selected,
312   /// sketch is started
313   if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
314     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
315     if (!aConstruction.get() || !aConstruction->isInfinite())
316       return aSketchStarted;
317   }
318   aSketchStarted = true;
319
320   // manually deactivation because the widget was not activated as has no focus acceptin controls
321   deactivate();
322   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
323   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
324
325   // Launch Sketch operation
326   CompositeFeaturePtr aCompFeature = 
327     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
328
329   // start edit operation for the sketch
330   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
331                                                             (myModule->createOperation("Sketch"));
332   QList<ModuleBase_ViewerPrsPtr> aValues;
333   aValues.push_back(aValue);
334   aFOperation->setPreselection(aValues);
335
336   myModule->sendOperation(aFOperation);
337
338   return aSketchStarted;
339 }
340
341 bool PartSet_WidgetSketchCreator::focusTo()
342 {
343   // this method is called only in creation mode. In Edition mode this widget is hidden
344   if (isSelectionMode() && !hasSubObjects()) {
345     setVisibleSelectionControl(true);
346     activateSelectionControl();
347     openExtrusionTransaction();
348     return true;
349   }
350   else
351     connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
352
353   return true;
354 }
355
356 void PartSet_WidgetSketchCreator::deactivate()
357 {
358   ModuleBase_WidgetSelector::deactivate();
359
360   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
361   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
362   if (aHidePreview)
363     XGUI_Tools::workshop(myWorkshop)->viewer()->update();
364
365 }
366
367 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
368 {
369   SessionPtr aMgr = ModelAPI_Session::get();
370   bool aIsOp = aMgr->isOperation();
371   if (aIsOp) {
372     // in current implementation, all transactions are closed when resume happens
373     // so, this is a wrong case, which is not checked.
374     // To provide it, make correction in later rows about abort/undo transactions
375     return;
376   }
377   // Set visible only selection control
378   setVisibleSelectionControl(true);
379
380   // Validate the created sketch. If it is valid, it is set into the composite feature selection
381   // list, otherwise it is removed
382   CompositeFeaturePtr aCompFeature = 
383     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
384   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
385   if (aCompFeature->numberOfSubs() > 0) {
386     // set the sub feature to attribute selection list and check whether sketch is valid
387     SessionPtr aMgr = ModelAPI_Session::get();
388     const static std::string aNestedOpID("Set Sketch result into Selection list");
389     aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
390     setSketchObjectToList(aCompFeature, anAttrList);
391     aMgr->finishOperation();
392
393     if (!validateSelectionList()) {
394 #ifdef DEBUG_UNDO_INVALID_SKETCH
395       aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
396       aMgr->undo(); /// Sketch creation
397 #else
398       aMgr->startOperation("Delete invalid Sketch feature", false);
399
400       // delete invalid sketch
401       CompositeFeaturePtr aSketchFeature = 
402               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
403       QObjectPtrList anObjects;
404       anObjects.append(aSketchFeature);
405
406       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
407       aWorkshop->deleteFeatures(anObjects);
408
409       aMgr->finishOperation();
410 #endif
411     }
412   }
413   openExtrusionTransaction();
414
415   if (aCompFeature->numberOfSubs() == 0) {
416     // call activateWidget() of the parent to connect to the viewer seleciton
417     activateSelectionControl();
418   }
419   else {
420     // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
421     /// the attribute selection list will be filled by result of this sketch.
422     setVisibleSelectionControl(false);
423
424     // Update value in attribute selection list
425     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
426     XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
427     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
428     ModuleBase_ModelWidget* aListWidget = 0;
429     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
430       if (aWidget->attributeID() == myAttributeListID) {
431         aListWidget = aWidget;
432         break;
433       }
434     }
435     if (aListWidget) {
436       aListWidget->restoreValue();
437       aPropertyPanel->activateNextWidget(aListWidget);
438     }
439
440     // Hide sketcher result
441     CompositeFeaturePtr aSketchFeature = 
442       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
443     std::list<ResultPtr> aResults = aSketchFeature->results();
444     std::list<ResultPtr>::const_iterator aIt;
445     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
446       (*aIt)->setDisplayed(false);
447     }
448     aSketchFeature->setDisplayed(false);
449     static Events_Loop* aLoop = Events_Loop::loop();
450     aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
451
452     // Add Selected body were created the sketcher to list of selected objects
453     std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
454     AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
455     if (aSelList.get()) {
456       DataPtr aData = aSketchFeature->data();
457       AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
458                                     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
459       ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
460       if (aRes.get()) {
461         SessionPtr aMgr = ModelAPI_Session::get();
462         ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
463         AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
464         std::string aValidatorID, anError;
465         aSelList->append(aRes, GeomShapePtr());
466         if (aFactory->validate(anAttribute, aValidatorID, anError))
467           updateObject(aCompFeature);
468         else
469           aSelList->clear();
470       }
471     }
472   }
473   restoreValue();
474 }
475
476 bool PartSet_WidgetSketchCreator::validateSelectionList() const
477 {
478   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
479
480   SessionPtr aMgr = ModelAPI_Session::get();
481   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
482   std::string aValidatorID, anError;
483   bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
484   if (!isValidPComposite) {
485     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
486     QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
487                   tr("Sketch is invalid and will be deleted.\nError: %1").arg(anError.c_str()),
488                   QMessageBox::Ok);
489   }
490   return isValidPComposite;
491 }
492
493 void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePtr& theCompositeFeature,
494                                                         const AttributePtr& theAttribute)
495 {
496   if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
497     return;
498
499   AttributeSelectionListPtr aBaseObjectsSelectionList =
500                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
501   if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
502     return;
503   }
504
505   FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
506   if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
507     return;
508   }
509
510   ResultPtr aSketchRes = aSketchFeature->results().front();
511   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
512   if(!aConstruction.get()) {
513     return;
514   }
515
516   if(aBaseObjectsSelectionList->size() == 0) {
517     aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
518   }
519 }
520
521 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
522                                                         const bool theEnabled)
523 {
524   QList<QWidget*> aMyControls = theModelWidget->getControls();
525   foreach(QWidget*  eachControl, aMyControls) {
526     eachControl->setEnabled(theEnabled);
527   }
528 }