Salome HOME
ModuleBase_ViewerPrs is wrapped into shared_ptr: remove include of this class from...
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
index cc3f93362a60c5d7d613b55cf1a26c5dc5061e26..ba9f9f124ee448081ff3794d84bc114669215bd9 100644 (file)
@@ -37,6 +37,8 @@
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_IPropertyPanel.h>
 #include <ModuleBase_OperationFeature.h>
+#include <ModuleBase_ViewerPrs.h>
+
 #include <Config_WidgetAPI.h>
 
 #include <Events_Loop.h>
@@ -48,7 +50,7 @@
 #include <QMessageBox>
 #include <QMainWindow>
 
-//#define DEBUG_UNDO_INVALID_SKETCH
+#define DEBUG_UNDO_INVALID_SKETCH
 
 PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent, 
                                                          PartSet_Module* theModule,
@@ -71,19 +73,6 @@ PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
   myLabel->setWordWrap(true);
   aLayout->addWidget(myLabel);
   aLayout->addStretch(1);
-  /*if (!aLabelIcon.isEmpty())
-    myLabel->setPixmap(QPixmap(aLabelIcon));
-
-
-  QString aToolTip = QString::fromStdString(theData->widgetTooltip());
-  myTextLine = new QLineEdit(this);
-  myTextLine->setReadOnly(true);
-  myTextLine->setToolTip(aToolTip);
-  myTextLine->installEventFilter(this);
-
-  myLabel->setToolTip(aToolTip);
-
-  aLayout->addRow(myLabel, myTextLine);*/
 
   std::string aTypes = theData->getProperty("shape_types");
   myShapeTypes = QString(aTypes.c_str()).split(' ', QString::SkipEmptyParts);
@@ -93,6 +82,9 @@ PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
 
 PartSet_WidgetSketchCreator::~PartSet_WidgetSketchCreator()
 {
+  // we need to deactivate here in order to hide preview planes if the selection mode is
+  // active
+  deactivate();
 }
 
 QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
@@ -104,12 +96,6 @@ QList<QWidget*> PartSet_WidgetSketchCreator::getControls() const
 
 bool PartSet_WidgetSketchCreator::restoreValueCustom()
 {
-  /*CompositeFeaturePtr aCompFeature = 
-    std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
-  if (aCompFeature->numberOfSubs() > 0) {
-    FeaturePtr aSubFeature = aCompFeature->subFeature(0);
-    myTextLine->setText(QString::fromStdString(aSubFeature->data()->name()));
-  }*/
   return true;
 }
 
@@ -141,7 +127,7 @@ void PartSet_WidgetSketchCreator::openExtrusionTransaction()
 }
 
 //********************************************************************
-bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrs& theValue)
+bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrsPtr& theValue)
 {
   bool aValid = false;
   if (myIsCustomAttribute) {
@@ -171,15 +157,13 @@ bool PartSet_WidgetSketchCreator::isValidSelection(const ModuleBase_ViewerPrs& t
 }
 
 //********************************************************************
-bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrs& theValue)
+bool PartSet_WidgetSketchCreator::isValidSelectionCustom(const ModuleBase_ViewerPrsPtr& theValue)
 {
   return PartSet_WidgetSketchLabel::canFillSketch(theValue);
 }
 
 void PartSet_WidgetSketchCreator::activateSelectionControl()
 {
-  setVisibleSelectionControl(true);
-
   // we need to call activate here as the widget has no focus accepted controls
   // if these controls are added here, activate will happens automatically after focusIn()
   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
@@ -201,8 +185,11 @@ void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelec
     else { // hide current control
       if (aWidget == this)
         aWidget->setVisible(false);
-      else
+      else {
         aWidget->setVisible(true);
+        if (aWidget->attributeID() == myAttributeListID)
+          setEnabledModelWidget(aWidget, !hasSubObjects());
+      }
     }
   }
 
@@ -232,8 +219,22 @@ QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
 void PartSet_WidgetSketchCreator::setEditingMode(bool isEditing)
 {
   ModuleBase_ModelWidget::setEditingMode(isEditing);
-  if (isEditing)
+  if (isEditing) {
     setVisibleSelectionControl(false);
+
+    ModuleBase_ModelWidget* anAttributeListWidget = 0;
+    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) {
+      if (aWidget->attributeID() == myAttributeListID) {
+        anAttributeListWidget = aWidget;
+        break;
+      }
+    }
+    if (anAttributeListWidget)
+      setEnabledModelWidget(anAttributeListWidget, !hasSubObjects());
+  }
 }
 
 bool PartSet_WidgetSketchCreator::isSelectionMode() const
@@ -244,16 +245,27 @@ bool PartSet_WidgetSketchCreator::isSelectionMode() const
   return !aHasValueInList;
 }
 
-bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
+bool PartSet_WidgetSketchCreator::hasSubObjects() const
+{
+  bool aHasSubObjects = false;
+
+  bool aCanSetFocus = true;
+  CompositeFeaturePtr aComposite = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
+  if (aComposite.get())
+    aHasSubObjects = aComposite->numberOfSubs() > 0;
+  return aHasSubObjects;
+}
+
+bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
                                                const bool theToValidate)
 {
   bool aDone = false;
   if (!startSketchOperation(theValues)) {
     myIsCustomAttribute = true;
-    QList<ModuleBase_ViewerPrs>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
+    QList<ModuleBase_ViewerPrsPtr>::const_iterator anIt = theValues.begin(), aLast = theValues.end();
     bool aProcessed = false;
     for (; anIt != aLast; anIt++) {
-      ModuleBase_ViewerPrs aValue = *anIt;
+      ModuleBase_ViewerPrsPtr aValue = *anIt;
       if (!theToValidate || isValidInFilters(aValue))
         aProcessed = setSelectionCustom(aValue) || aProcessed;
     }
@@ -274,7 +286,7 @@ bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrs>& theV
 //********************************************************************
 void PartSet_WidgetSketchCreator::onSelectionChanged()
 {
-  QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
+  QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
   bool isDone = setSelection(aSelected, true/*false*/);
 }
 
@@ -283,14 +295,14 @@ void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
 {
 }
 
-bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrs>& theValues)
+bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrsPtr>& theValues)
 {
   bool aSketchStarted = false;
 
   if (theValues.size() != 1)
     return aSketchStarted;
 
-  ModuleBase_ViewerPrs aValue = theValues.front();
+  ModuleBase_ViewerPrsPtr aValue = theValues.front();
   if (!PartSet_WidgetSketchLabel::canFillSketch(aValue))
     return aSketchStarted;
 
@@ -305,23 +317,13 @@ bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_Vi
   CompositeFeaturePtr aCompFeature = 
     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
 
-  /// add sketch feature without current feature change.
-  /// it is important to do not change the current feature in order to
-  /// after sketch edition, the extrusion cut feature becomes current
-  SessionPtr aMgr = ModelAPI_Session::get();
-  DocumentPtr aDoc = aMgr->activeDocument();
-  FeaturePtr aPreviousCurrentFeature = aDoc->currentFeature(false);
-  FeaturePtr aSketch = aCompFeature->addFeature("Sketch");
-
-  PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(aSketch, aValue);
-
-  aDoc->setCurrentFeature(aPreviousCurrentFeature, false);
-
   // start edit operation for the sketch
   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
                                                             (myModule->createOperation("Sketch"));
-  if (aFOperation)
-    aFOperation->setFeature(aSketch);
+  QList<ModuleBase_ViewerPrsPtr> aValues;
+  aValues.push_back(aValue);
+  aFOperation->setPreselection(aValues);
+
   myModule->sendOperation(aFOperation);
 
   return aSketchStarted;
@@ -330,16 +332,16 @@ bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_Vi
 bool PartSet_WidgetSketchCreator::focusTo()
 {
   // 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) {
+  if (isSelectionMode() && !hasSubObjects()) {
+    setVisibleSelectionControl(true);
     activateSelectionControl();
     openExtrusionTransaction();
     return true;
   }
   else
     connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
-  return false;
+
+  return true;
 }
 
 void PartSet_WidgetSketchCreator::deactivate()
@@ -401,7 +403,11 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
   }
   openExtrusionTransaction();
 
-  if (aCompFeature->numberOfSubs() > 0) {
+  if (aCompFeature->numberOfSubs() == 0) {
+    // call activateWidget() of the parent to connect to the viewer seleciton
+    activateSelectionControl();
+  }
+  else {
     // 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);
@@ -495,3 +501,12 @@ void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePt
     aBaseObjectsSelectionList->append(aSketchRes, GeomShapePtr());
   }
 }
+
+void PartSet_WidgetSketchCreator::setEnabledModelWidget(ModuleBase_ModelWidget* theModelWidget,
+                                                        const bool theEnabled)
+{
+  QList<QWidget*> aMyControls = theModelWidget->getControls();
+  foreach(QWidget*  eachControl, aMyControls) {
+    eachControl->setEnabled(theEnabled);
+  }
+}