Salome HOME
Issue #1343. Improvement of Extrusion and Revolution operations: filling extrusion...
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
index 24e0f5f504b6c40171fb39a8f9768de90517c7f7..cc3f93362a60c5d7d613b55cf1a26c5dc5061e26 100644 (file)
@@ -28,6 +28,7 @@
 #include <ModelAPI_AttributeSelectionList.h>
 #include <ModelAPI_Validator.h>
 #include <ModelAPI_Events.h>
+#include <ModelAPI_ResultConstruction.h>
 
 #include <SketchPlugin_SketchEntity.h>
 #include <FeaturesPlugin_CompositeBoolean.h>
 #include <QMessageBox>
 #include <QMainWindow>
 
+//#define DEBUG_UNDO_INVALID_SKETCH
+
 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent, 
                                                          PartSet_Module* theModule,
                                                          const Config_WidgetAPI* theData)
 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
-  myModule(theModule)
+  myModule(theModule), myIsCustomAttribute(false)
 {
   myAttributeListID = theData->getProperty("attribute_list_id");
 
@@ -67,6 +70,7 @@ PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
   myLabel = new QLabel(aLabelText, this);
   myLabel->setWordWrap(true);
   aLayout->addWidget(myLabel);
+  aLayout->addStretch(1);
   /*if (!aLabelIcon.isEmpty())
     myLabel->setPixmap(QPixmap(aLabelIcon));
 
@@ -114,6 +118,64 @@ bool PartSet_WidgetSketchCreator::storeValueCustom() const
   return true;
 }
 
+AttributePtr PartSet_WidgetSketchCreator::attribute() const
+{
+  AttributePtr anAttribute;
+  if (myIsCustomAttribute)
+    anAttribute = myFeature->attribute(myAttributeListID);
+  else
+    anAttribute = ModuleBase_WidgetSelector::attribute();
+
+  return anAttribute;
+}
+
+//********************************************************************
+void PartSet_WidgetSketchCreator::openExtrusionTransaction()
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  bool aIsOp = aMgr->isOperation();
+  if (!aIsOp) {
+    const static std::string aNestedOpID("Parameters modification");
+    aMgr->startOperation(aNestedOpID, true);
+  }
+}
+
+//********************************************************************
+bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrs& theValue)
+{
+  bool aValid = false;
+  if (myIsCustomAttribute) {
+    // check only suiting of the value to custom attribute (myAttributeListID)
+    // do not cash of validation to avoid using states, stored for XML attribute
+    // there is an alternative is to call clearValidatedCash() in setSelection()
+    aValid = isValidSelectionForAttribute(theValue, attribute());
+  }
+  else { /// if the validated attribute is already custom
+    if (getValidState(theValue, aValid)) {
+      return aValid;
+    }
+    aValid = isValidSelectionCustom(theValue);
+    if (!aValid)
+      // check selection to create new sketh (XML current attribute)
+      aValid = isValidSelectionForAttribute(theValue, attribute());
+    if (!aValid) {
+      // check selection to fill list attribute (myAttributeListID)
+      bool isCustomAttribute = myIsCustomAttribute;
+      myIsCustomAttribute = true;
+      aValid = isValidSelectionForAttribute(theValue, attribute());
+      myIsCustomAttribute = isCustomAttribute;
+    }
+  }
+  storeValidState(theValue, aValid);
+  return aValid;
+}
+
+//********************************************************************
+bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrs& theValue)
+{
+  return PartSet_WidgetSketchLabel::canFillSketch(theValue);
+}
+
 void PartSet_WidgetSketchCreator::activateSelectionControl()
 {
   setVisibleSelectionControl(true);
@@ -174,54 +236,6 @@ void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
     setVisibleSelectionControl(false);
 }
 
-bool PartSet_WidgetSketchCreator::canCommitCurrentSketch(ModuleBase_IWorkshop* theWorkshop)
-{
-  bool aCanCommit = true;
-  ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
-  XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(theWorkshop);
-  XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
-  // check if the operation is nested
-  if (anOperation && anOpMgr->operationsCount() > 1) {
-    ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
-    FeaturePtr aCurrentFeature = aFOperation ? aFOperation->feature() : FeaturePtr();
-
-    ModuleBase_Operation* aPOperation =  anOpMgr->previousOperation(anOperation);
-    ModuleBase_OperationFeature* aFPOperation = dynamic_cast<ModuleBase_OperationFeature*>(aPOperation);
-    FeaturePtr aParentFeature = aFPOperation ? aFPOperation->feature() : FeaturePtr();
-
-    CompositeFeaturePtr aCompositeFeature = 
-                             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCurrentFeature);
-    CompositeFeaturePtr aPCompositeFeature = 
-                             std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aParentFeature);
-    // check if both features are composite: extrusion and sketch
-    if (aCompositeFeature.get() && aPCompositeFeature.get()) {
-      aPCompositeFeature->execute(); // to fill attribute selection list
-      std::list<AttributePtr> aSelListAttributes = aParentFeature->data()->attributes(
-                                                        ModelAPI_AttributeSelectionList::typeId());
-      if (aSelListAttributes.size() == 1) {
-        AttributePtr aFirstAttribute = aSelListAttributes.front();
-
-        SessionPtr aMgr = ModelAPI_Session::get();
-        ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-        std::string aValidatorID, anError;
-        bool isValidPComposite = aFactory->validate(aFirstAttribute, aValidatorID, anError);
-        if (!isValidPComposite) {
-          int anAnswer = QMessageBox::question(
-              aWorkshop->desktop(), tr("Apply current feature"),
-                            tr("The current feature can not be used as an argument of the parent feature.\n\
-                               After apply it will be deleted. Would you like to continue?"),
-                            QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel);
-          if (anAnswer == QMessageBox::Ok)
-            aCanCommit = true;
-          else
-            aCanCommit = false;
-        }
-      }
-    }
-  }
-  return aCanCommit;
-}
-
 bool PartSet_WidgetSketchCreator::isSelectionMode() const
 {
   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
@@ -230,18 +244,21 @@ bool PartSet_WidgetSketchCreator::isSelectionMode() const
   return !aHasValueInList;
 }
 
-void PartSet_WidgetSketchCreator::onSelectionChanged()
+bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
+                                               const bool theToValidate)
 {
-  QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
-
-  if (!startSketchOperation(aSelected)) {
-    QList<ModuleBase_ViewerPrs>::const_iterator anIt = aSelected.begin(), aLast = aSelected.end();
+  bool aDone = false;
+  if (!startSketchOperation(theValues)) {
+    myIsCustomAttribute = true;
+    QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
     bool aProcessed = false;
     for (; anIt != aLast; anIt++) {
       ModuleBase_ViewerPrs aValue = *anIt;
-      if (isValidInFilters(aValue))
+      if (!theToValidate || isValidInFilters(aValue))
         aProcessed = setSelectionCustom(aValue) || aProcessed;
     }
+    myIsCustomAttribute = false;
+    aDone = aProcessed;
     if (aProcessed) {
       emit valuesChanged();
       updateObject(myFeature);
@@ -251,13 +268,19 @@ void PartSet_WidgetSketchCreator::onSelectionChanged()
       emit focusOutWidget(this);
     }
   }
+  return aDone;
 }
 
 //********************************************************************
-void PartSet_WidgetSketchCreator::setObject(ObjectPtr theSelectedObject,
-                                            GeomShapePtr theShape)
+void PartSet_WidgetSketchCreator::onSelectionChanged()
+{
+  QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
+  bool isDone = setSelection(aSelected, true/*false*/);
+}
+
+//********************************************************************
+void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
 {
-  // do nothing because all processing is in onSelectionChanged()
 }
 
 bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrs>& theValues)
@@ -306,23 +329,16 @@ bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_Vi
 
 bool PartSet_WidgetSketchCreator::focusTo()
 {
-  if (isSelectionMode()) {
+  // this method is called only in creation mode. In Edition mode this widget is hidden
+  CompositeFeaturePtr aCompFeature = 
+      std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
+  if (isSelectionMode() && aCompFeature->numberOfSubs() == 0) {
     activateSelectionControl();
+    openExtrusionTransaction();
     return true;
   }
-  else {
-    //setVisibleSelectionControl(false);
-
+  else
     connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
-    SessionPtr aMgr = ModelAPI_Session::get();
-    // Open transaction that is general for the previous nested one: it will be closed on nested commit
-    bool aIsOp = aMgr->isOperation();
-    if (!aIsOp) {
-      const static std::string aNestedOpID("Parameters modification");
-      aMgr->startOperation(aNestedOpID, true);
-    }
-    restoreValue();
-  }
   return false;
 }
 
@@ -339,42 +355,59 @@ void PartSet_WidgetSketchCreator::deactivate()
 
 void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
 {
-  XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
+  SessionPtr aMgr = ModelAPI_Session::get();
+  bool aIsOp = aMgr->isOperation();
+  if (aIsOp) {
+    // in current implementation, all transactions are closed when resume happens
+    // so, this is a wrong case, which is not checked.
+    // To provide it, make correction in later rows about abort/undo transactions
+    return;
+  }
+  // Set visible only selection control
+  setVisibleSelectionControl(true);
 
+  // Validate the created sketch. If it is valid, it is set into the composite feature selection
+  // list, otherwise it is removed
   CompositeFeaturePtr aCompFeature = 
     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
-  //CompositeFeaturePtr aSketchFeature = 
-  //  std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
-  if (aCompFeature->numberOfSubs() == 0) {
-    // do nothing, selection control should be hidden
-    setVisibleSelectionControl(false);
-  } else {
-    // check if the created sketch is invalid. Validate attribute selection list
-    // Shetch should be deleted if the attribute is invalid.
-    AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
-    
+  AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
+  if (aCompFeature->numberOfSubs() > 0) {
+    // set the sub feature to attribute selection list and check whether sketch is valid
     SessionPtr aMgr = ModelAPI_Session::get();
-    ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-    std::string aValidatorID, anError;
-    bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
-    /// if the sketch is not appropriate fro extrusion, it should be deleted and
-    /// the selection control should be activated again
-    if (!isValidPComposite) {
+    const static std::string aNestedOpID("Set Sketch result into Selection list");
+    aMgr->startOperation(aNestedOpID, false); // false to not attach to Extrusion operation
+    setSketchObjectToList(aCompFeature, anAttrList);
+    aMgr->finishOperation();
+
+    if (!validateSelectionList()) {
+#ifdef DEBUG_UNDO_INVALID_SKETCH
+      aMgr->undo(); // Extrusion modification parameters: setSketchObjectToList()
+      aMgr->undo(); /// Sketch creation
+#else
+      aMgr->startOperation("Delete invalid Sketch feature", false);
+
+      // delete invalid sketch
       CompositeFeaturePtr aSketchFeature = 
-               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
-
+              std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
       QObjectPtrList anObjects;
       anObjects.append(aSketchFeature);
-      std::set<FeaturePtr> anIgnoredFeatures;
-      aWorkshop->deleteFeatures(anObjects, anIgnoredFeatures);
 
-      // do nothing, selection control should be shown
-      activateSelectionControl();
-      return;
+      XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
+      aWorkshop->deleteFeatures(anObjects, std::set<FeaturePtr>());
+
+      aMgr->finishOperation();
+#endif
     }
-    // do nothing, selection control should be hidden
+  }
+  openExtrusionTransaction();
+
+  if (aCompFeature->numberOfSubs() > 0) {
+    // check if the created sketch is valid. If it is invalid, it will be deleted with warning else
+    /// the attribute selection list will be filled by result of this sketch.
     setVisibleSelectionControl(false);
+
     // Update value in attribute selection list
+    XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
     XGUI_PropertyPanel* aPanel = aWorkshop->propertyPanel();
     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
@@ -391,12 +424,11 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
       (*aIt)->setDisplayed(false);
     }
     aSketchFeature->setDisplayed(false);
-
-    // restore value in the selection control
-
+    static Events_Loop* aLoop = Events_Loop::loop();
+    aLoop->flush(aLoop->eventByName(EVENT_OBJECT_TO_REDISPLAY));
 
     // Add Selected body were created the sketcher to list of selected objects
-    std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::BOOLEAN_OBJECTS_ID();
+    std::string anObjectsAttribute = FeaturesPlugin_CompositeBoolean::OBJECTS_ID();
     AttributeSelectionListPtr aSelList = aCompFeature->data()->selectionList(anObjectsAttribute);
     if (aSelList.get()) {
       DataPtr aData = aSketchFeature->data();
@@ -416,4 +448,50 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
       }
     }
   }
+  restoreValue();
+}
+
+bool PartSet_WidgetSketchCreator::validateSelectionList() const
+{
+  AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
+
+  SessionPtr aMgr = ModelAPI_Session::get();
+  ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
+  std::string aValidatorID, anError;
+  bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
+  if (!isValidPComposite) {
+    XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
+    QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
+                  tr("Sketch is invalid and will be deleted.\nError: %1").arg(anError.c_str()),
+                  QMessageBox::Ok);
+  }
+  return isValidPComposite;
+}
+
+void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePtr& theCompositeFeature,
+                                                        const AttributePtr& theAttribute)
+{
+  if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
+    return;
+
+  AttributeSelectionListPtr aBaseObjectsSelectionList =
+                     std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
+  if(!aBaseObjectsSelectionList.get() || aBaseObjectsSelectionList->isInitialized()) {
+    return;
+  }
+
+  FeaturePtr aSketchFeature = theCompositeFeature->subFeature(0);
+  if(!aSketchFeature.get() || aSketchFeature->results().empty()) {
+    return;
+  }
+
+  ResultPtr aSketchRes = aSketchFeature->results().front();
+  ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
+  if(!aConstruction.get()) {
+    return;
+  }
+
+  if(aBaseObjectsSelectionList->size() == 0) {
+    aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
+  }
 }