Salome HOME
Issue #1660: Ability to change the deflection coefficient
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index 61bb0e22ab94f3784176c5e592497314c0787504..0aa4a019e8eabbaac6bdfaf9fd44e80dc7e327ff 100755 (executable)
@@ -6,6 +6,7 @@
 #include "XGUI_ActionsMgr.h"
 #include "XGUI_MenuMgr.h"
 #include "XGUI_ColorDialog.h"
+#include "XGUI_DeflectionDialog.h"
 #include "XGUI_ContextMenuMgr.h"
 #include "XGUI_Displayer.h"
 #include "XGUI_ErrorDialog.h"
@@ -36,6 +37,7 @@
 
 #include <ModelAPI_AttributeDocRef.h>
 #include <ModelAPI_AttributeIntArray.h>
+#include <ModelAPI_AttributeDouble.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Events.h>
 #include <ModelAPI_Feature.h>
@@ -52,7 +54,7 @@
 //#include <PartSetPlugin_Part.h>
 
 #include <Events_Loop.h>
-#include <Events_Error.h>
+#include <Events_InfoMessage.h>
 #include <Events_LongOp.h>
 
 #include <ModuleBase_FilterFactory.h>
@@ -70,6 +72,7 @@
 #include <ModuleBase_PagedContainer.h>
 #include <ModuleBase_WidgetValidated.h>
 #include <ModuleBase_ModelWidget.h>
+#include <ModuleBase_ResultPrs.h>
 
 #include <Config_Common.h>
 #include <Config_FeatureMessage.h>
@@ -146,6 +149,7 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
   QString aPath = Config_XMLReader::pluginConfigFile().c_str();
   QDir aDir(aPath);
 
+  // Load translations
   QStringList aFilters;
   aFilters << "*_en.ts";
   QStringList aTsFiles = aDir.entryList(aFilters, QDir::Files);
@@ -175,8 +179,6 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
 
   myErrorMgr = new XGUI_ErrorMgr(this, aWorkshop);
 
-  connect(myOperationMgr, SIGNAL(operationStarted(ModuleBase_Operation*)), 
-          SLOT(onOperationStarted(ModuleBase_Operation*)));
   connect(myOperationMgr, SIGNAL(operationResumed(ModuleBase_Operation*)),
           SLOT(onOperationResumed(ModuleBase_Operation*)));
   connect(myOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
@@ -197,7 +199,7 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
   //Config_PropManager::registerProp("Visualization", "object_default_color", "Object color",
   //                                 Config_Prop::Color, "225,225,225");
 
-  Config_PropManager::registerProp("Visualization", "result_body_color", "Body color",
+  Config_PropManager::registerProp("Visualization", "result_body_color", "Result color",
                                    Config_Prop::Color, ModelAPI_ResultBody::DEFAULT_COLOR());
   Config_PropManager::registerProp("Visualization", "result_group_color", "Group color",
                                    Config_Prop::Color, ModelAPI_ResultGroup::DEFAULT_COLOR());
@@ -205,18 +207,31 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
                                    Config_Prop::Color, ModelAPI_ResultConstruction::DEFAULT_COLOR());
   Config_PropManager::registerProp("Visualization", "result_part_color", "Part color",
                                    Config_Prop::Color, ModelAPI_ResultPart::DEFAULT_COLOR());
-  
+
+  Config_PropManager::registerProp("Visualization", "result_deflection", "Result deflection",
+                                   Config_Prop::Double, "0.001");
+
   if (ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "face-selection", true))
     myViewerSelMode.append(TopAbs_FACE);
   if (ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "edge-selection", true))
     myViewerSelMode.append(TopAbs_EDGE);
   if (ModuleBase_Preferences::resourceMgr()->booleanValue("Viewer", "vertex-selection", true))
     myViewerSelMode.append(TopAbs_VERTEX);
+  //IMP: an attempt to use result selection with other selection modes
+  myViewerSelMode.append(ModuleBase_ResultPrs::Sel_Result);//TopAbs_VERTEX);
+  myViewerSelMode.append(TopAbs_COMPSOLID);
 }
 
 //******************************************************
 XGUI_Workshop::~XGUI_Workshop(void)
 {
+#ifdef _DEBUG
+#ifdef MISSED_TRANSLATION
+  // Save Missed translations
+  Config_Translator::saveMissedTranslations();
+#endif
+#endif
+
   delete myDisplayer;
   delete myDataModelXMLReader;
 }
@@ -476,7 +491,7 @@ bool XGUI_Workshop::isFeatureOfNested(const FeaturePtr& theFeature)
   return aHasNested;
 }
 
-void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
+void XGUI_Workshop::fillPropertyPanel(ModuleBase_Operation* theOperation)
 {
   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
   if (!aFOperation)
@@ -498,10 +513,9 @@ void XGUI_Workshop::setPropertyPanel(ModuleBase_Operation* theOperation)
   std::string aFeatureKind = aFeature->getKind();
   foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
     if (!aWidget->attributeID().empty() && !aFeature->attribute(aWidget->attributeID()).get()) {
-      std::string anErrorMsg = "The feature '" + aFeatureKind + "' has no attribute '"
-          + aWidget->attributeID() + "' used by widget '"
-          + aWidget->metaObject()->className() + "'.";
-      Events_Error::send(anErrorMsg);
+      std::string anErrorMsg = "The feature '%1' has no attribute '%2' used by widget '%3'.";
+      Events_InfoMessage("XGUI_Workshop", anErrorMsg)
+        .arg(aFeatureKind).arg(aWidget->attributeID()).arg(aWidget->metaObject()->className()).send();
       myPropertyPanel->cleanContent();
       return;
     }
@@ -572,20 +586,13 @@ void XGUI_Workshop::connectToPropertyPanel(const bool isToConnect)
   }
 }
 
-//******************************************************
-void XGUI_Workshop::onOperationStarted(ModuleBase_Operation* theOperation)
-{
-  setGrantedFeatures(theOperation);
-  myModule->operationStarted(theOperation);
-}
-
 //******************************************************
 void XGUI_Workshop::onOperationResumed(ModuleBase_Operation* theOperation)
 {
   setGrantedFeatures(theOperation);
 
   if (theOperation->getDescription()->hasXmlRepresentation()) {  //!< No need for property panel
-    setPropertyPanel(theOperation);
+    fillPropertyPanel(theOperation);
     connectToPropertyPanel(true);
   }
   updateCommandStatus();
@@ -620,7 +627,8 @@ void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
   if (aFeature.get()) { // feature may be not created (plugin load fail)
     if (myDisplayer->isVisible(aFeature) && !myDisplayer->isActive(aFeature))
       anObjects.append(aFeature);
-    std::list<ResultPtr> aResults = aFeature->results();
+    std::list<ResultPtr> aResults;
+    ModelAPI_Tools::allResults(aFeature, aResults);
     std::list<ResultPtr>::const_iterator aIt;
     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
       ResultPtr anObject = *aIt;
@@ -667,10 +675,7 @@ void XGUI_Workshop::setGrantedFeatures(ModuleBase_Operation* theOperation)
   aFOperation->setGrantedOperationIds(aGrantedIds);
 }
 
-
-/*
- * Saves document with given name.
- */
+//******************************************************
 void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>& theFileNames)
 {
   QApplication::restoreOverrideCursor();
@@ -679,11 +684,24 @@ void XGUI_Workshop::saveDocument(const QString& theName, std::list<std::string>&
   QApplication::restoreOverrideCursor();
 }
 
+//******************************************************
 bool XGUI_Workshop::abortAllOperations()
 {
   return myOperationMgr->abortAllOperations();
 }
 
+//******************************************************
+void XGUI_Workshop::operationStarted(ModuleBase_Operation* theOperation)
+{
+  setGrantedFeatures(theOperation);
+  if (!theOperation->getDescription()->hasXmlRepresentation()) {  //!< No need for property panel
+    updateCommandStatus();
+  }
+  else {
+    myModule->operationStarted(theOperation);
+  }
+}
+
 //******************************************************
 void XGUI_Workshop::onOpen()
 {
@@ -706,16 +724,26 @@ void XGUI_Workshop::onOpen()
   }
 
   //show file dialog, check if readable and open
-  myCurrentDir = QFileDialog::getExistingDirectory(desktop(), tr("Select directory"));
+  QString aDirectory = QFileDialog::getExistingDirectory(desktop(), tr("Select directory"));
+  openDirectory(aDirectory);
+}
+
+//******************************************************
+void XGUI_Workshop::openDirectory(const QString& theDirectory)
+{
+  myCurrentDir = theDirectory;
   if (myCurrentDir.isEmpty())
     return;
+
   QFileInfo aFileInfo(myCurrentDir);
   if (!aFileInfo.exists() || !aFileInfo.isReadable()) {
     QMessageBox::critical(desktop(), tr("Warning"), tr("Unable to open the file."));
     myCurrentDir = "";
     return;
   }
+
   QApplication::setOverrideCursor(Qt::WaitCursor);
+  SessionPtr aSession = ModelAPI_Session::get();
   aSession->closeAll();
   aSession->load(myCurrentDir.toLatin1().constData());
   myObjectBrowser->rebuildDataTree();
@@ -1012,7 +1040,7 @@ ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
 
   if (!err.isEmpty()) {
     if (desktop()) {
-      Events_Error::send(err.toStdString());
+      Events_InfoMessage("XGUI_Workshop", err.toStdString()).send();
     } else {
       qWarning(qPrintable(err));
     }
@@ -1246,7 +1274,10 @@ void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
   } else if (theId == "SELECT_FACE_CMD") {
     setViewerSelectionMode(TopAbs_FACE);
   } else if (theId == "SELECT_RESULT_CMD") {
-    setViewerSelectionMode(-1);
+    //setViewerSelectionMode(-1);
+    //IMP: an attempt to use result selection with other selection modes
+    setViewerSelectionMode(ModuleBase_ResultPrs::Sel_Result);
+    setViewerSelectionMode(TopAbs_COMPSOLID);
   } else if (theId == "SHOW_RESULTS_CMD") {
     highlightResults(aObjects);
   } else if (theId == "SHOW_FEATURE_CMD") {
@@ -1291,9 +1322,6 @@ void XGUI_Workshop::deleteObjects()
   QObjectPtrList anObjects = mySelector->selection()->selectedObjects();
   if (!abortAllOperations())
     return;
-  // check whether the object can be deleted. There should not be parts which are not loaded
-  if (!XGUI_Tools::canRemoveOrRename(desktop(), anObjects))
-    return;
 
   bool hasResult = false;
   bool hasFeature = false;
@@ -1492,20 +1520,21 @@ void XGUI_Workshop::moveObjects()
   // moving and negative consequences connected with processing of already moved items
   mySelector->clearSelection();
   // check whether the object can be moved. There should not be parts which are not loaded
-  if (!XGUI_Tools::canRemoveOrRename(desktop(), anObjects))
+  std::set<FeaturePtr> aFeatures;
+  ModuleBase_Tools::convertToFeatures(anObjects, aFeatures);
+  if (!XGUI_Tools::canRemoveOrRename(desktop(), aFeatures))
     return;
 
   DocumentPtr anActiveDocument = aMgr->activeDocument();
   FeaturePtr aCurrentFeature = anActiveDocument->currentFeature(true);
-  foreach (ObjectPtr aObject, anObjects) {
-    if (!myModule->canApplyAction(aObject, anActionId))
+  std::set<FeaturePtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
+  for (; anIt != aLast; anIt++) {
+    FeaturePtr aFeature = *anIt;
+    if (!aFeature.get() || !myModule->canApplyAction(aFeature, anActionId))
       continue;
 
-    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObject);
-    if (aFeature.get()) {
-      anActiveDocument->moveFeature(aFeature, aCurrentFeature);
-      aCurrentFeature = anActiveDocument->currentFeature(true);
-    }
+    anActiveDocument->moveFeature(aFeature, aCurrentFeature);
+    aCurrentFeature = anActiveDocument->currentFeature(true);
   }
   aMgr->finishOperation();
 }
@@ -1547,7 +1576,9 @@ std::list<FeaturePtr> allFeatures(const DocumentPtr& theDocument)
     // The order of appending features of the part and the part itself is important
 
     // Append features from a part feature
-    foreach (const ResultPtr& aResult, aFeature->results()) {
+    std::list<ResultPtr> aResults;
+    ModelAPI_Tools::allResults(aFeature, aResults);
+    foreach (const ResultPtr& aResult, aResults) {
       ResultPartPtr aResultPart =
           std::dynamic_pointer_cast<ModelAPI_ResultPart>(aResult);
       if (aResultPart.get() && aResultPart->partDoc().get()) {
@@ -1748,6 +1779,92 @@ void XGUI_Workshop::changeColor(const QObjectPtrList& theObjects)
   updateCommandStatus();
 }
 
+//**************************************************************
+bool XGUI_Workshop::canChangeDeflection() const
+{
+  QObjectPtrList aObjects = mySelector->selection()->selectedObjects();
+
+  std::set<std::string> aTypes;
+  aTypes.insert(ModelAPI_ResultGroup::group());
+  aTypes.insert(ModelAPI_ResultConstruction::group());
+  aTypes.insert(ModelAPI_ResultBody::group());
+  aTypes.insert(ModelAPI_ResultPart::group());
+
+  return hasResults(aObjects, aTypes);
+}
+
+void setDeflection(ResultPtr theResult, const double theDeflection)
+{
+  if (!theResult.get())
+    return;
+
+  AttributeDoublePtr aDeflectionAttr = theResult->data()->real(ModelAPI_Result::DEFLECTION_ID());
+  if (aDeflectionAttr.get() != NULL)
+    aDeflectionAttr->setValue(theDeflection);
+}
+
+
+//**************************************************************
+void XGUI_Workshop::changeDeflection(const QObjectPtrList& theObjects)
+{
+  AttributeDoublePtr aDoubleAttr;
+  // 1. find the current color of the object. This is a color of AIS presentation
+  // The objects are iterated until a first valid color is found 
+  double aDeflection = -1;
+  foreach(ObjectPtr anObject, theObjects) {
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
+    if (aResult.get()) {
+      aDeflection = XGUI_CustomPrs::getResultDeflection(aResult);
+    }
+    else {
+      // TODO: remove the obtaining a color from the AIS object
+      // this does not happen never because:
+      // 1. The color can be changed only on results
+      // 2. The result can be not visualized in the viewer(e.g. Origin Construction)
+      AISObjectPtr anAISObj = myDisplayer->getAISObject(anObject);
+      if (anAISObj.get()) {
+        aDeflection = anAISObj->getDeflection();
+      }
+    }
+    if (aDeflection > 0)
+      break;
+  }
+  if (aDeflection < 0)
+    return;
+
+  if (!abortAllOperations())
+  return; 
+  // 2. show the dialog to change the value
+  XGUI_DeflectionDialog* aDlg = new XGUI_DeflectionDialog(desktop());
+  aDlg->setDeflection(aDeflection);
+  aDlg->move(QCursor::pos());
+  bool isDone = aDlg->exec() == QDialog::Accepted;
+  if (!isDone)
+    return;
+
+  // 3. abort the previous operation and start a new one
+  SessionPtr aMgr = ModelAPI_Session::get();
+  QString aDescription = contextMenuMgr()->action("DEFLECTION_CMD")->text();
+  aMgr->startOperation(aDescription.toStdString());
+
+  // 4. set the value to all results
+  aDeflection = aDlg->getDeflection();
+  foreach(ObjectPtr anObj, theObjects) {
+    ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
+    if (aResult.get() != NULL) {
+      ResultCompSolidPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
+      if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
+        for(int i = 0; i < aCompsolidResult->numberOfSubs(); i++) {
+          setDeflection(aCompsolidResult->subResult(i), aDeflection);
+        }
+      }
+      setDeflection(aResult, aDeflection);
+    }
+  }
+  aMgr->finishOperation();
+  updateCommandStatus();
+}
+
 //**************************************************************
 #define SET_DISPLAY_GROUP(aGroupName, aDisplay) \
 for (int i = 0; i < aDoc->size(aGroupName); i++) { \
@@ -1952,7 +2069,8 @@ void XGUI_Workshop::highlightResults(const QObjectPtrList& theObjects)
   foreach(ObjectPtr aObj, theObjects) {
     aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
     if (aFeature.get()) {
-      aResList = aFeature->results();
+      std::list<ResultPtr> aResults;
+      ModelAPI_Tools::allResults(aFeature, aResults);
       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aIt;
       for(aIt = aResList.cbegin(); aIt != aResList.cend(); aIt++) {
         aHasHidden |= (*aIt)->isConcealed();