]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchCreator.cpp
Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr: remove include of this class from...
[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() const
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     if (!aBodyIsVisualized) {
199       // We have to select a plane before any operation
200       myPreviewPlanes->showPreviewPlanes(myWorkshop);
201     }
202   } else {
203     bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
204     myPreviewPlanes->erasePreviewPlanes(myWorkshop);
205     if (aHidePreview)
206       aWorkshop->viewer()->update();
207   }
208 }
209
210 QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
211 {
212   QIntList aShapeTypes;
213   foreach(QString aType, myShapeTypes) {
214     aShapeTypes.append(ModuleBase_Tools::shapeType(aType));
215   }
216   return aShapeTypes;
217 }
218
219 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
220 {
221   ModuleBase_ModelWidget::setEditingMode(isEditing);
222   if (isEditing) {
223     setVisibleSelectionControl(false);
224
225     ModuleBase_ModelWidget* anAttributeListWidget = 0;
226     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
227     XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
228     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
229     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
230       if (aWidget->attributeID() == myAttributeListID) {
231         anAttributeListWidget = aWidget;
232         break;
233       }
234     }
235     if (anAttributeListWidget)
236       setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
237   }
238 }
239
240 bool PartSet_WidgetSketchCreator::isSelectionMode() const
241 {
242   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
243   bool aHasValueInList = anAttrList.get() && anAttrList->size() > 0;
244
245   return !aHasValueInList;
246 }
247
248 bool PartSet_WidgetSketchCreator::hasSubObjects() const
249 {
250   bool aHasSubObjects = false;
251
252   bool aCanSetFocus = true;
253   CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
254   if (aComposite.get())
255     aHasSubObjects = aComposite->numberOfSubs() > 0;
256   return aHasSubObjects;
257 }
258
259 bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
260                                                const bool theToValidate)
261 {
262   bool aDone = false;
263   if (!startSketchOperation(theValues)) {
264     myIsCustomAttribute = true;
265     QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
266     bool aProcessed = false;
267     for (; anIt != aLast; anIt++) {
268       ModuleBase_ViewerPrsPtr aValue = *anIt;
269       if (!theToValidate || isValidInFilters(aValue))
270         aProcessed = setSelectionCustom(aValue) || aProcessed;
271     }
272     myIsCustomAttribute = false;
273     aDone = aProcessed;
274     if (aProcessed) {
275       emit valuesChanged();
276       updateObject(myFeature);
277       setVisibleSelectionControl(false);
278       // manually deactivation because the widget was not activated as has no focus acceptin controls
279       deactivate();
280       emit focusOutWidget(this);
281     }
282   }
283   return aDone;
284 }
285
286 //********************************************************************
287 void PartSet_WidgetSketchCreator::onSelectionChanged()
288 {
289   QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
290   bool isDone = setSelection(aSelected, true/*false*/);
291 }
292
293 //********************************************************************
294 void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
295 {
296 }
297
298 bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrsPtr>& theValues)
299 {
300   bool aSketchStarted = false;
301
302   if (theValues.size() != 1)
303     return aSketchStarted;
304
305   ModuleBase_ViewerPrsPtr aValue = theValues.front();
306   if (!PartSet_WidgetSketchLabel::canFillSketch(aValue))
307     return aSketchStarted;
308
309   aSketchStarted = true;
310
311   // manually deactivation because the widget was not activated as has no focus acceptin controls
312   deactivate();
313   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
314   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
315
316   // Launch Sketch operation
317   CompositeFeaturePtr aCompFeature = 
318     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
319
320   // start edit operation for the sketch
321   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
322                                                             (myModule->createOperation("Sketch"));
323   QList<ModuleBase_ViewerPrsPtr> aValues;
324   aValues.push_back(aValue);
325   aFOperation->setPreselection(aValues);
326
327   myModule->sendOperation(aFOperation);
328
329   return aSketchStarted;
330 }
331
332 bool PartSet_WidgetSketchCreator::focusTo()
333 {
334   // this method is called only in creation mode. In Edition mode this widget is hidden
335   if (isSelectionMode() && !hasSubObjects()) {
336     setVisibleSelectionControl(true);
337     activateSelectionControl();
338     openExtrusionTransaction();
339     return true;
340   }
341   else
342     connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
343
344   return true;
345 }
346
347 void PartSet_WidgetSketchCreator::deactivate()
348 {
349   ModuleBase_WidgetSelector::deactivate();
350
351   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
352   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
353   if (aHidePreview)
354     XGUI_Tools::workshop(myWorkshop)->viewer()->update();
355
356 }
357
358 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
359 {
360   SessionPtr aMgr = ModelAPI_Session::get();
361   bool aIsOp = aMgr->isOperation();
362   if (aIsOp) {
363     // in current implementation, all transactions are closed when resume happens
364     // so, this is a wrong case, which is not checked.
365     // To provide it, make correction in later rows about abort/undo transactions
366     return;
367   }
368   // Set visible only selection control
369   setVisibleSelectionControl(true);
370
371   // Validate the created sketch. If it is valid, it is set into the composite feature selection
372   // list, otherwise it is removed
373   CompositeFeaturePtr aCompFeature = 
374     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
375   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
376   if (aCompFeature->numberOfSubs() > 0) {
377     // set the sub feature to attribute selection list and check whether sketch is valid
378     SessionPtr aMgr = ModelAPI_Session::get();
379     const static std::string aNestedOpID("Set Sketch result into Selection list");
380     aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
381     setSketchObjectToList(aCompFeature, anAttrList);
382     aMgr->finishOperation();
383
384     if (!validateSelectionList()) {
385 #ifdef DEBUG_UNDO_INVALID_SKETCH
386       aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
387       aMgr->undo(); /// Sketch creation
388 #else
389       aMgr->startOperation("Delete invalid Sketch feature", false);
390
391       // delete invalid sketch
392       CompositeFeaturePtr aSketchFeature = 
393               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
394       QObjectPtrList anObjects;
395       anObjects.append(aSketchFeature);
396
397       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
398       aWorkshop->deleteFeatures(anObjects, std::set<FeaturePtr>());
399
400       aMgr->finishOperation();
401 #endif
402     }
403   }
404   openExtrusionTransaction();
405
406   if (aCompFeature->numberOfSubs() == 0) {
407     // call activateWidget() of the parent to connect to the viewer seleciton
408     activateSelectionControl();
409   }
410   else {
411     // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
412     /// the attribute selection list will be filled by result of this sketch.
413     setVisibleSelectionControl(false);
414
415     // Update value in attribute selection list
416     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
417     XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
418     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
419     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
420       if (aWidget->attributeID() == myAttributeListID)
421         aWidget->restoreValue();
422     }
423
424     // Hide sketcher result
425     CompositeFeaturePtr aSketchFeature = 
426       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
427     std::list<ResultPtr> aResults = aSketchFeature->results();
428     std::list<ResultPtr>::const_iterator aIt;
429     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
430       (*aIt)->setDisplayed(false);
431     }
432     aSketchFeature->setDisplayed(false);
433     static Events_Loop* aLoop = Events_Loop::loop();
434     aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
435
436     // Add Selected body were created the sketcher to list of selected objects
437     std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
438     AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
439     if (aSelList.get()) {
440       DataPtr aData = aSketchFeature->data();
441       AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
442                                     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
443       ResultPtr aRes = aSelAttr.get() ? aSelAttr->context() : ResultPtr();
444       if (aRes.get()) {
445         SessionPtr aMgr = ModelAPI_Session::get();
446         ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
447         AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
448         std::string aValidatorID, anError;
449         aSelList->append(aRes, GeomShapePtr());
450         if (aFactory->validate(anAttribute, aValidatorID, anError))
451           updateObject(aCompFeature);
452         else
453           aSelList->clear();
454       }
455     }
456   }
457   restoreValue();
458 }
459
460 bool PartSet_WidgetSketchCreator::validateSelectionList() const
461 {
462   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
463
464   SessionPtr aMgr = ModelAPI_Session::get();
465   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
466   std::string aValidatorID, anError;
467   bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
468   if (!isValidPComposite) {
469     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
470     QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
471                   tr("Sketch is invalid and will be deleted.\nError: %1").arg(anError.c_str()),
472                   QMessageBox::Ok);
473   }
474   return isValidPComposite;
475 }
476
477 void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePtr& theCompositeFeature,
478                                                         const AttributePtr& theAttribute)
479 {
480   if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
481     return;
482
483   AttributeSelectionListPtr aBaseObjectsSelectionList =
484                      std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
485   if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
486     return;
487   }
488
489   FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
490   if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
491     return;
492   }
493
494   ResultPtr aSketchRes = aSketchFeature->results().front();
495   ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
496   if(!aConstruction.get()) {
497     return;
498   }
499
500   if(aBaseObjectsSelectionList->size() == 0) {
501     aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
502   }
503 }
504
505 void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
506                                                         const bool theEnabled)
507 {
508   QList<QWidget*> aMyControls = theModelWidget->getControls();
509   foreach(QWidget*  eachControl, aMyControls) {
510     eachControl->setEnabled(theEnabled);
511   }
512 }