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