Salome HOME
Provide pop-up menu for steps
[modules/shaper.git] / src / ModuleBase / ModuleBase_Tools.cpp
old mode 100755 (executable)
new mode 100644 (file)
index dc31878..3d47d3c
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:        ModuleBase_Tools.cpp
-// Created:     11 July 2014
-// Author:      Vitaly Smetannikov
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "ModuleBase_Tools.h"
 
 #include <ModuleBase_WidgetFactory.h>
 #include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_IModule.h>
+#include <ModuleBase_IViewer.h>
 #include <ModuleBase_IconFactory.h>
 #include <ModuleBase_ResultPrs.h>
+#include <ModuleBase_ViewerPrs.h>
 
 #include <ModelAPI_Attribute.h>
 #include <ModelAPI_AttributeRefAttr.h>
 #include <ModelAPI_AttributeRefAttrList.h>
 #include <ModelAPI_ResultPart.h>
 #include <ModelAPI_ResultConstruction.h>
+#include <ModelAPI_AttributeString.h>
+#include <ModelAPI_Expression.h>
+#include <ModelAPI_ResultField.h>
 #include <Events_Loop.h>
 
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Result.h>
-#include <ModelAPI_ResultCompSolid.h>
 #include <ModelAPI_ResultParameter.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_Session.h>
 #include <ModelAPI_Events.h>
+#include <ModelAPI_Folder.h>
 
 #include <ModelGeomAlgo_Point2D.h>
 
+#include <StdSelect_BRepOwner.hxx>
 #include <TopoDS_Iterator.hxx>
+#include <AIS_InteractiveContext.hxx>
+#include <Prs3d_LineAspect.hxx>
+#include <Prs3d_PlaneAspect.hxx>
 
 #include <GeomDataAPI_Point2D.h>
 #include <Events_InfoMessage.h>
@@ -118,7 +140,10 @@ void zeroMargins(QLayout* theLayout)
 
 void activateWindow(QWidget* theWidget, const QString& theInfo)
 {
-  theWidget->activateWindow();
+  if (theWidget) {
+    theWidget->activateWindow();
+    theWidget->raise();
+  }
 
 #ifdef DEBUG_ACTIVATE_WINDOW
   qDebug(QString("activateWindow: %1").arg(theInfo).toStdString().c_str());
@@ -127,8 +152,10 @@ void activateWindow(QWidget* theWidget, const QString& theInfo)
 
 void setFocus(QWidget* theWidget, const QString& theInfo)
 {
+  activateWindow(theWidget);
   theWidget->setFocus();
-
+  // rectangle of focus is not visible on tool button widgets
+  theWidget->repaint();
 #ifdef DEBUG_SET_FOCUS
   qDebug(QString("setFocus: %1").arg(theInfo).toStdString().c_str());
 #endif
@@ -155,28 +182,32 @@ QPixmap composite(const QString& theAdditionalIcon, const QString& theIcon)
 {
   QImage anIcon = ModuleBase_IconFactory::loadImage(theIcon);
   QImage anAditional(theAdditionalIcon);
+  return composite(anAditional, anIcon);
+}
 
-  if (anIcon.isNull())
+QPixmap composite(const QImage& theAdditionalIcon, QImage& theIcon)
+{
+  if (theIcon.isNull())
     return QPixmap();
 
-  int anAddWidth = anAditional.width();
-  int anAddHeight = anAditional.height();
+  int anAddWidth = theAdditionalIcon.width();
+  int anAddHeight = theAdditionalIcon.height();
 
-  int aWidth = anIcon.width();
-  int aHeight = anIcon.height();
+  int aWidth = theIcon.width();
+  int aHeight = theIcon.height();
 
-  int aStartWidthPos = aWidth - anAddWidth - 1;
-  int aStartHeightPos = aHeight - anAddHeight - 1;
+  int aStartWidthPos = aWidth - anAddWidth;
+  int aStartHeightPos = aHeight - anAddHeight;
 
   for (int i = 0; i < anAddWidth && i + aStartWidthPos < aWidth; i++)
   {
     for (int j = 0; j < anAddHeight && j + aStartHeightPos < aHeight; j++)
     {
-      if (qAlpha(anAditional.pixel(i, j)) > 0)
-        anIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, anAditional.pixel(i, j));
+      if (qAlpha(theAdditionalIcon.pixel(i, j)) > 0)
+        theIcon.setPixel(i + aStartWidthPos, j + aStartHeightPos, theAdditionalIcon.pixel(i, j));
     }
   }
-  return QPixmap::fromImage(anIcon);
+  return QPixmap::fromImage(theIcon);
 }
 
 QPixmap lighter(const QString& theIcon, const int theLighterValue)
@@ -267,6 +298,14 @@ QAction* createAction(const QIcon& theIcon, const QString& theText,
 }
 
 #ifdef _DEBUG
+QString objectName(const ObjectPtr& theObj)
+{
+  if (!theObj.get())
+    return "";
+
+  return theObj->data()->name().c_str();
+}
+
 QString objectInfo(const ObjectPtr& theObj, const bool isUseAttributesInfo)
 {
   QString aFeatureStr = "feature";
@@ -345,23 +384,35 @@ int shapeType(const QString& theType)
 }
 
 void checkObjects(const QObjectPtrList& theObjects, bool& hasResult, bool& hasFeature,
-                  bool& hasParameter, bool& hasCompositeOwner)
+                  bool& hasParameter, bool& hasCompositeOwner, bool& hasResultInHistory,
+                  bool& hasFolder)
 {
   hasResult = false;
   hasFeature = false;
   hasParameter = false;
   hasCompositeOwner = false;
+  hasResultInHistory = false;
+  hasFolder = false;
   foreach(ObjectPtr aObj, theObjects) {
     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(aObj);
+    FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aObj);
     ResultParameterPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aResult);
+    FieldStepPtr aStep = std::dynamic_pointer_cast<ModelAPI_ResultField::ModelAPI_FieldStep>(aObj);
 
-    hasResult |= (aResult.get() != NULL);
+    hasResult |= ((aResult.get() != NULL) || (aStep.get() != NULL));
     hasFeature |= (aFeature.get() != NULL);
+    hasFolder |= (aFolder.get() != NULL);
     hasParameter |= (aConstruction.get() != NULL);
     if (hasFeature)
       hasCompositeOwner |= (ModelAPI_Tools::compositeOwner(aFeature) != NULL);
-    if (hasFeature && hasResult  && hasParameter && hasCompositeOwner)
+
+    if (!hasResultInHistory && aResult.get()) {
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
+      hasResultInHistory = aFeature.get() && aFeature->isInHistory();
+    }
+
+    if (hasFeature && hasResult  && hasParameter && hasCompositeOwner && hasFeature)
       break;
   }
 }
@@ -477,6 +528,18 @@ TopAbs_ShapeEnum getCompoundSubType(const TopoDS_Shape& theShape)
   return aShapeType;
 }
 
+TopoDS_Shape getSelectedShape(const std::shared_ptr<ModuleBase_ViewerPrs>& thePrs)
+{
+  if (thePrs->shape().get())
+    return thePrs->shape()->impl<TopoDS_Shape>();
+
+  Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(thePrs->owner());
+  if (!anOwner.IsNull())
+    return anOwner->Shape();
+
+  return TopoDS_Shape();
+}
+
 void getParameters(QStringList& theParameters)
 {
   theParameters.clear();
@@ -621,18 +684,16 @@ bool setObject(const AttributePtr& theAttribute, const ObjectPtr& theObject,
   } else if (aType == ModelAPI_AttributeSelection::typeId()) {
     AttributeSelectionPtr aSelectAttr =
                              std::dynamic_pointer_cast<ModelAPI_AttributeSelection>(theAttribute);
-    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
     if (aSelectAttr.get() != NULL) {
-      aSelectAttr->setValue(aResult, theShape, theTemporarily);
+      aSelectAttr->setValue(theObject, theShape, theTemporarily);
     }
   }
   if (aType == ModelAPI_AttributeSelectionList::typeId()) {
     AttributeSelectionListPtr aSelectionListAttr =
                          std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(theAttribute);
-    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
     if (!theCheckIfAttributeHasObject ||
-        !aSelectionListAttr->isInList(aResult, theShape, theTemporarily))
-      aSelectionListAttr->append(aResult, theShape, theTemporarily);
+      !aSelectionListAttr->isInList(theObject, theShape, theTemporarily))
+      aSelectionListAttr->append(theObject, theShape, theTemporarily);
   }
   else if (aType == ModelAPI_AttributeRefList::typeId()) {
     AttributeRefListPtr aRefListAttr =
@@ -694,7 +755,12 @@ void flushUpdated(ObjectPtr theObject)
 {
   blockUpdateViewer(true);
 
+  // Fix the problem of not previewed results of constraints applied. Flush Create/Delete
+  // (for the sketch result) to start processing of the sketch in the solver.
+  // TODO: these flushes should be moved in a separate method provided by Model
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
 
   blockUpdateViewer(false);
 }
@@ -736,7 +802,7 @@ QString wrapTextByWords(const QString& theValue, QWidget* theWidget,
   int n = aWords.count();
   QString aLine;
   for (int i = 0; i < n; i++) {
-    QString aLineExt = aLine + " " + aWords[i];
+    QString aLineExt =  i == 0 ? aWords[i] : aLine + " " + aWords[i];
     qreal anWidthNonExt = tfm.boundingRect(aLine).width();
     qreal anWidthExt = tfm.boundingRect(aLineExt).width();
     qreal aDeltaNonExt = fabs(anWidthNonExt-aGoldWidth);
@@ -928,6 +994,8 @@ bool askToDelete(const std::set<FeaturePtr> theFeatures,
   aLast = theReferencesToDelete.end();
   for (; anIt != aLast; anIt++) {
     FeaturePtr aFeature = *anIt;
+    if (aFeature->getKind() == "RemoveResults")
+      continue; // skip the remove results feature mentioning: result will be removed anyway
     if (isFeatureOfResult(aFeature, ModelAPI_ResultPart::group()))
       aPartFeatureNames.append(aFeature->name().c_str());
     else
@@ -961,7 +1029,8 @@ bool askToDelete(const std::set<FeaturePtr> theFeatures,
                      .arg(anOtherFeatureNames.join(aSep));
   }
   if (!aParamFeatureNames.empty()) {
-    const char* aMsg = "Parameters are used in the following features: %1.\nThese features will "
+    const char* aMsg = "Parameters are used directly and through a sequence "
+                       "of dependencies in the following features: %1.\nThese features will "
                        "be deleted.\nOr parameters could be replaced by their values.\n";
     aText += QString(QObject::tr(aMsg))
                      .arg(aParamFeatureNames.join(aSep));
@@ -1000,10 +1069,26 @@ void convertToFeatures(const QObjectPtrList& theObjects, std::set<FeaturePtr>& t
     if (!aFeature.get() && anObject->groupName() == ModelAPI_ResultParameter::group()) {
       aFeature = ModelAPI_Feature::feature(anObject);
     }
-    theFeatures.insert(aFeature);
+    if (aFeature.get())
+      theFeatures.insert(aFeature);
+  }
+}
+
+//**************************************************************
+void convertToFolders(const QObjectPtrList& theObjects,
+                                         std::set<FolderPtr>& theFolders)
+{
+  QObjectPtrList::const_iterator anIt = theObjects.begin(), aLast = theObjects.end();
+  for(; anIt != aLast; anIt++) {
+    ObjectPtr anObject = *anIt;
+    FolderPtr aFeature = std::dynamic_pointer_cast<ModelAPI_Folder>(anObject);
+    if (aFeature.get())
+      theFolders.insert(aFeature);
   }
 }
 
+
+//**************************************************************
 QString translate(const Events_InfoMessage& theMessage)
 {
   QString aMessage;
@@ -1063,7 +1148,15 @@ void setPointBallHighlighting(AIS_Shape* theAIS)
   }
 
   Handle(Graphic3d_AspectMarker3d) anAspect;
-  Handle(Prs3d_Drawer) aDrawer = theAIS->HilightAttributes();
+  Handle(Prs3d_Drawer) aDrawer = theAIS->DynamicHilightAttributes();
+  if (aDrawer.IsNull()) {
+    if (ModuleBase_IViewer::DefaultHighlightDrawer.IsNull())
+      return;
+    aDrawer = new Prs3d_Drawer(*ModuleBase_IViewer::DefaultHighlightDrawer);
+    if (!aDrawer->HasOwnPointAspect()) {
+      aDrawer->SetPointAspect(new Prs3d_PointAspect(Aspect_TOM_BALL, Quantity_NOC_BLACK, 2.0));
+    }
+  }
   if(aDrawer->HasOwnPointAspect()) {
     Handle(Prs3d_PointAspect) aPntAspect = aDrawer->PointAspect();
     if(aPixMap->IsEmpty()) {
@@ -1079,8 +1172,131 @@ void setPointBallHighlighting(AIS_Shape* theAIS)
       aPntAspect->SetAspect(anAspect);
     }
     aDrawer->SetPointAspect(aPntAspect);
-    theAIS->SetHilightAttributes(aDrawer);
+         theAIS->SetDynamicHilightAttributes(aDrawer);
+  }
+}
+
+FeaturePtr createParameter(const QString& theText)
+{
+  FeaturePtr aParameter;
+  QStringList aList = theText.split("=");
+  if (aList.count() != 2) {
+    return aParameter;
+  }
+  QString aParamName = aList.at(0).trimmed();
+
+  if (isNameExist(aParamName, FeaturePtr())) {
+    return aParameter;
+  }
+
+  if (!ModelAPI_Expression::isVariable(aParamName.toStdString())) {
+    return aParameter;
+  }
+
+  QString aExpression = aList.at(1).trimmed();
+  if (aExpression.isEmpty()) {
+    return aParameter;
+  }
+
+  SessionPtr aMgr = ModelAPI_Session::get();
+  std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
+
+  aParameter = aDoc->addFeature("Parameter");
+  if (aParameter.get()) {
+    AttributeStringPtr aNameAttr = aParameter->string("variable");
+    aNameAttr->setValue(aParamName.toStdString());
+
+    AttributeStringPtr aExprAttr = aParameter->string("expression");
+    aExprAttr->setValue(aExpression.toStdString());
+    aParameter->execute();
+
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+  }
+  return aParameter;
+}
+
+void editParameter(FeaturePtr theParam, const QString& theText)
+{
+  QStringList aList = theText.split("=");
+  QString aParamName = aList.at(0).trimmed();
+
+  QString aExpression = aList.at(1).trimmed();
+  if (aExpression.isEmpty()) {
+    return;
+  }
+
+  if (isNameExist(aParamName, theParam)) {
+    return;
+  }
+  AttributeStringPtr aNameAttr = theParam->string("variable");
+  aNameAttr->setValue(aParamName.toStdString());
+
+  AttributeStringPtr aExprAttr = theParam->string("expression");
+  aExprAttr->setValue(aExpression.toStdString());
+  theParam->execute();
+
+  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
+}
+
+bool isNameExist(const QString& theName, FeaturePtr theIgnoreParameter)
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
+  FeaturePtr aParamFeature;
+  int aNbFeatures = aDoc->numInternalFeatures();
+  std::string aName = theName.toStdString();
+  for (int i = 0; i < aNbFeatures; i++) {
+    aParamFeature = aDoc->internalFeature(i);
+    if (aParamFeature && aParamFeature->getKind() == "Parameter") {
+      if ((theIgnoreParameter != aParamFeature) && (aParamFeature->name() == aName))
+        return true;
+    }
+  }
+  return false;
+}
+
+FeaturePtr findParameter(const QString& theName)
+{
+  SessionPtr aMgr = ModelAPI_Session::get();
+  std::shared_ptr<ModelAPI_Document> aDoc = aMgr->activeDocument();
+  FeaturePtr aParamFeature;
+  int aNbFeatures = aDoc->numInternalFeatures();
+  std::string aName = theName.toStdString();
+  for (int i = 0; i < aNbFeatures; i++) {
+    aParamFeature = aDoc->internalFeature(i);
+    if (aParamFeature && aParamFeature->getKind() == "Parameter") {
+      if (aParamFeature->name() == aName)
+        return aParamFeature;
+    }
+  }
+  return FeaturePtr();
+}
+
+
+//********************************************************************
+std::string generateName(const AttributePtr& theAttribute,
+  ModuleBase_IWorkshop* theWorkshop)
+{
+  std::string aName;
+  if (theAttribute.get() != NULL) {
+    ModuleBase_Operation* anOperation = theWorkshop->currentOperation();
+
+    FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
+    if (aFeature.get()) {
+      std::string aXmlCfg, aDescription;
+      theWorkshop->module()->getXMLRepresentation(aFeature->getKind(), aXmlCfg, aDescription);
+
+      ModuleBase_WidgetFactory aFactory(aXmlCfg, theWorkshop);
+      std::string anAttributeTitle;
+      aFactory.getAttributeTitle(theAttribute->id(), anAttributeTitle);
+
+      std::stringstream aStreamName;
+      aStreamName << theAttribute->owner()->data()->name() << "/" << anAttributeTitle.c_str();
+      aName = aStreamName.str();
+    }
   }
+  return aName;
 }
 
 } // namespace ModuleBase_Tools