Salome HOME
#1857 In the Sketcher, ability to zoom the view from a given size
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchCreator.cpp
index 131106e74a6b9c1c51c4a339cd83117a6b6f6b35..6397b3caefd7f54a00169ecd1e292b882f1209d1 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <GeomAPI_Face.h>
 
+#include <Events_InfoMessage.h>
+
 #include <ModelAPI_Session.h>
 #include <ModelAPI_ResultBody.h>
 #include <ModelAPI_AttributeSelection.h>
@@ -45,6 +47,7 @@
 
 #include <QLabel>
 #include <QLineEdit>
+#include <QDoubleValidator>
 //#include <QFormLayout>
 #include <QVBoxLayout>
 #include <QMessageBox>
@@ -52,7 +55,7 @@
 
 #define DEBUG_UNDO_INVALID_SKETCH
 
-PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent, 
+PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
                                                          PartSet_Module* theModule,
                                                          const Config_WidgetAPI* theData)
 : ModuleBase_WidgetSelector(theParent, theModule->workshop(), theData),
@@ -69,8 +72,22 @@ PartSet_WidgetSketchCreator::PartSet_WidgetSketchCreator(QWidget* theParent,
   QString aLabelText = QString::fromStdString(theData->widgetLabel());
   QString aLabelIcon = QString::fromStdString(theData->widgetIcon());
 
+  // Size of the View control
+  mySizeOfViewWidget = new QWidget(this);
+  QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
+  aSizeLayout->addWidget(new QLabel("Size of the view", mySizeOfViewWidget));
+  mySizeOfView = new QLineEdit(mySizeOfViewWidget);
+
+  QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
+  aValidator->setLocale(ModuleBase_Tools::doubleLocale());
+  aValidator->setNotation(QDoubleValidator::StandardNotation);
+  mySizeOfView->setValidator(aValidator);
+  aSizeLayout->addWidget(mySizeOfView);
+
   myLabel = new QLabel(aLabelText, this);
   myLabel->setWordWrap(true);
+
+  aLayout->addWidget(mySizeOfViewWidget);
   aLayout->addWidget(myLabel);
   aLayout->addStretch(1);
 
@@ -99,7 +116,7 @@ bool PartSet_WidgetSketchCreator::restoreValueCustom()
   return true;
 }
 
-bool PartSet_WidgetSketchCreator::storeValueCustom() const
+bool PartSet_WidgetSketchCreator::storeValueCustom()
 {
   return true;
 }
@@ -195,10 +212,16 @@ void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelec
 
   if (theSelectionControl) {
     bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
-    if (!aBodyIsVisualized) {
+    bool aSketchIsVisualized = myPreviewPlanes->hasVisualizedSketch(myWorkshop);
+    if (!aBodyIsVisualized && !aSketchIsVisualized) {
       // We have to select a plane before any operation
       myPreviewPlanes->showPreviewPlanes(myWorkshop);
+      mySizeOfViewWidget->setVisible(true);
+    }
+    else {
+      mySizeOfViewWidget->setVisible(false);
     }
+
   } else {
     bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
     myPreviewPlanes->erasePreviewPlanes(myWorkshop);
@@ -207,7 +230,7 @@ void PartSet_WidgetSketchCreator::setVisibleSelectionControl(const bool theSelec
   }
 }
 
-QIntList PartSet_WidgetSketchCreator::getShapeTypes() const
+QIntList PartSet_WidgetSketchCreator::shapeTypes() const
 {
   QIntList aShapeTypes;
   foreach(QString aType, myShapeTypes) {
@@ -262,7 +285,8 @@ bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& t
   bool aDone = false;
   if (!startSketchOperation(theValues)) {
     myIsCustomAttribute = true;
-    QList<ModuleBase_ViewerPrsPtr>::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_ViewerPrsPtr aValue = *anIt;
@@ -275,7 +299,8 @@ bool PartSet_WidgetSketchCreator::setSelection(QList<ModuleBase_ViewerPrsPtr>& t
       emit valuesChanged();
       updateObject(myFeature);
       setVisibleSelectionControl(false);
-      // manually deactivation because the widget was not activated as has no focus acceptin controls
+      // manually deactivation because the widget was
+      // not activated as has no focus acceptin controls
       deactivate();
       emit focusOutWidget(this);
     }
@@ -295,7 +320,8 @@ void PartSet_WidgetSketchCreator::updateOnSelectionChanged(const bool theDone)
 {
 }
 
-bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_ViewerPrsPtr>& theValues)
+bool PartSet_WidgetSketchCreator::startSketchOperation(
+                              const QList<ModuleBase_ViewerPrsPtr>& theValues)
 {
   bool aSketchStarted = false;
 
@@ -309,18 +335,38 @@ bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_Vi
   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aValue->object());
   /// sketch should not started by object(face) selected as global. If Local face is selected,
   /// sketch is started
-  if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape()))
-    return aSketchStarted;
-
+  if (aResult.get() && aValue->shape().get() && aResult->shape()->isEqual(aValue->shape())) {
+    ResultConstructionPtr aConstruction =
+      std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aResult);
+    if (!aConstruction.get() || !aConstruction->isInfinite())
+      return aSketchStarted;
+  }
   aSketchStarted = true;
-
+  // Set View size if a plane is selected
+  if (myPreviewPlanes->isPreviewDisplayed() &&
+      myPreviewPlanes->isPreviewShape(aValue->shape())) {
+    QString aSizeOfViewStr = mySizeOfView->text();
+    if (!aSizeOfViewStr.isEmpty()) {
+      bool isOk;
+      double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
+      if (isOk && aSizeOfView > 0) {
+        Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
+        if (!aView3d.IsNull()) {
+          Bnd_Box aBndBox;
+          double aHalfSize = aSizeOfView/2.0;
+          aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
+          aView3d->FitAll(aBndBox, 0.01, false);
+        }
+      }
+    }
+  }
   // manually deactivation because the widget was not activated as has no focus acceptin controls
   deactivate();
   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
 
   // Launch Sketch operation
-  CompositeFeaturePtr aCompFeature = 
+  CompositeFeaturePtr aCompFeature =
     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
 
   // start edit operation for the sketch
@@ -330,7 +376,7 @@ bool PartSet_WidgetSketchCreator::startSketchOperation(const QList<ModuleBase_Vi
   aValues.push_back(aValue);
   aFOperation->setPreselection(aValues);
 
-  myModule->sendOperation(aFOperation);
+  myWorkshop->processLaunchOperation(aFOperation);
 
   return aSketchStarted;
 }
@@ -345,7 +391,8 @@ bool PartSet_WidgetSketchCreator::focusTo()
     return true;
   }
   else
-    connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)), SLOT(onResumed(ModuleBase_Operation*)));
+    connect(myModule, SIGNAL(resumed(ModuleBase_Operation*)),
+            SLOT(onResumed(ModuleBase_Operation*)));
 
   return true;
 }
@@ -376,7 +423,7 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
 
   // Validate the created sketch. If it is valid, it is set into the composite feature selection
   // list, otherwise it is removed
-  CompositeFeaturePtr aCompFeature = 
+  CompositeFeaturePtr aCompFeature =
     std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
   AttributeSelectionListPtr anAttrList = myFeature->data()->selectionList(myAttributeListID);
   if (aCompFeature->numberOfSubs() > 0) {
@@ -395,13 +442,13 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
       aMgr->startOperation("Delete invalid Sketch feature", false);
 
       // delete invalid sketch
-      CompositeFeaturePtr aSketchFeature = 
+      CompositeFeaturePtr aSketchFeature =
               std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
       QObjectPtrList anObjects;
       anObjects.append(aSketchFeature);
 
       XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
-      aWorkshop->deleteFeatures(anObjects, std::set<FeaturePtr>());
+      aWorkshop->deleteFeatures(anObjects);
 
       aMgr->finishOperation();
 #endif
@@ -420,15 +467,22 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
 
     // 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();
+    XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
+    const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
+    ModuleBase_ModelWidget* aListWidget = 0;
     foreach(ModuleBase_ModelWidget* aWidget, aWidgets) {
-      if (aWidget->attributeID() == myAttributeListID)
-        aWidget->restoreValue();
+      if (aWidget->attributeID() == myAttributeListID) {
+        aListWidget = aWidget;
+        break;
+      }
+    }
+    if (aListWidget) {
+      aListWidget->restoreValue();
+      aPropertyPanel->activateNextWidget(aListWidget);
     }
 
     // Hide sketcher result
-    CompositeFeaturePtr aSketchFeature = 
+    CompositeFeaturePtr aSketchFeature =
       std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aCompFeature->subFeature(0));
     std::list<ResultPtr> aResults = aSketchFeature->results();
     std::list<ResultPtr>::const_iterator aIt;
@@ -451,7 +505,8 @@ void PartSet_WidgetSketchCreator::onResumed(ModuleBase_Operation* theOp)
         SessionPtr aMgr = ModelAPI_Session::get();
         ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
         AttributePtr anAttribute = myFeature->attribute(anObjectsAttribute);
-        std::string aValidatorID, anError;
+        std::string aValidatorID;
+        Events_InfoMessage anError;
         aSelList->append(aRes, GeomShapePtr());
         if (aFactory->validate(anAttribute, aValidatorID, anError))
           updateObject(aCompFeature);
@@ -469,19 +524,23 @@ bool PartSet_WidgetSketchCreator::validateSelectionList() const
 
   SessionPtr aMgr = ModelAPI_Session::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-  std::string aValidatorID, anError;
+  std::string aValidatorID;
+  Events_InfoMessage anError;
   bool isValidPComposite = aFactory->validate(anAttrList, aValidatorID, anError);
   if (!isValidPComposite) {
     XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myModule->workshop());
+    // TODO(spo): translate
     QMessageBox::question(aWorkshop->desktop(), tr("Apply current feature"),
-                  tr("Sketch is invalid and will be deleted.\nError: %1").arg(anError.c_str()),
+                  tr("Sketch is invalid and will be deleted.\nError: %1")
+                  .arg(anError.messageString().c_str()),
                   QMessageBox::Ok);
   }
   return isValidPComposite;
 }
 
-void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePtr& theCompositeFeature,
-                                                        const AttributePtr& theAttribute)
+void PartSet_WidgetSketchCreator::setSketchObjectToList(
+                            const CompositeFeaturePtr& theCompositeFeature,
+                            const AttributePtr& theAttribute)
 {
   if (!theCompositeFeature.get() || theCompositeFeature->numberOfSubs() != 1)
     return;
@@ -498,7 +557,8 @@ void PartSet_WidgetSketchCreator::setSketchObjectToList(const CompositeFeaturePt
   }
 
   ResultPtr aSketchRes = aSketchFeature->results().front();
-  ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
+  ResultConstructionPtr aConstruction =
+    std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSketchRes);
   if(!aConstruction.get()) {
     return;
   }