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