Salome HOME
Merge branch 'python_parametric_api' of https://git.salome-platform.org/git/modules...
[modules/shaper.git] / src / XGUI / XGUI_WorkshopListener.cpp
index 2e22c49eda0c6e4b103b2dbe5b41d85c9cd4425f..8b658c9975d5fa5734464b6af085aaa48f73e736 100755 (executable)
@@ -3,6 +3,7 @@
 #include "XGUI_WorkshopListener.h"
 #include "XGUI_Workshop.h"
 #include "XGUI_Displayer.h"
+#include "XGUI_ErrorMgr.h"
 #include "XGUI_OperationMgr.h"
 #include "XGUI_SalomeConnector.h"
 #include "XGUI_ActionsMgr.h"
 #include "XGUI_ModuleConnector.h"
 #include "XGUI_QtEvents.h"
 
+#ifndef HAVE_SALOME
 #include <AppElements_Workbench.h>
 #include <AppElements_Command.h>
 #include <AppElements_MainMenu.h>
 #include <AppElements_MainWindow.h>
 #include <AppElements_MenuGroupPanel.h>
 #include <AppElements_Button.h>
+#endif
 
 #include <ModuleBase_IModule.h>
 
@@ -37,6 +40,7 @@
 
 #include <ModuleBase_Operation.h>
 #include <ModuleBase_OperationDescription.h>
+#include <ModuleBase_OperationFeature.h>
 #include <ModuleBase_Tools.h>
 #include <ModuleBase_IViewer.h>
 #include <ModuleBase_FilterFactory.h>
@@ -44,6 +48,7 @@
 #include <Config_FeatureMessage.h>
 #include <Config_PointerMessage.h>
 #include <Config_SelectionFilterMessage.h>
+#include <Config_Keywords.h>
 
 #include <QApplication>
 #include <QMainWindow>
 
 //#define DEBUG_FEATURE_CREATED
 //#define DEBUG_FEATURE_REDISPLAY
+//#define DEBUG_FEATURE_UPDATED
 //#define DEBUG_RESULT_COMPSOLID
 
+#ifdef DEBUG_FEATURE_REDISPLAY
+const std::string DebugFeatureKind = "Extrusion";
+#endif
+
 XGUI_WorkshopListener::XGUI_WorkshopListener(ModuleBase_IWorkshop* theWorkshop)
   : myWorkshop(theWorkshop),
     myUpdatePrefs(false)
 {
+  XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
+  //connect(anOperationMgr, SIGNAL(nestedStateChanged(const std::string&, const bool)),
+  //        this, SLOT(onNestedStateChanged(const std::string&, const bool)));
 }
 
 //******************************************************
@@ -84,6 +97,7 @@ void XGUI_WorkshopListener::initializeEventListening()
   aLoop->registerListener(this, Events_LongOp::eventID());
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_PLUGIN_LOADED));
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_SELFILTER_LOADED));
+  aLoop->registerListener(this, Events_Loop::eventByName(EVENT_OBJECT_ERROR_CHANGED));
 
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED));
   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED));
@@ -153,7 +167,9 @@ void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>&
     XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
 
     if (anOperationMgr->startOperation(anOperation)) {
-      workshop()->propertyPanel()->updateContentWidget(anOperation->feature());
+      ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(anOperation);
+      if (aFOperation)
+        workshop()->propertyPanel()->updateContentWidget(aFOperation->feature());
       if (!anOperation->getDescription()->hasXmlRepresentation()) {
         if (anOperation->commit())
           workshop()->updateCommandStatus();
@@ -177,7 +193,25 @@ void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>&
     // the viewer's update context is unblocked, the viewer's update works
     XGUI_Displayer* aDisplayer = workshop()->displayer();
     aDisplayer->enableUpdateViewer(true);
-    aDisplayer->updateViewer();
+  } else if (theMessage->eventID() == Events_Loop::eventByName(EVENT_OBJECT_ERROR_CHANGED)) {
+    std::shared_ptr<ModelAPI_ObjectUpdatedMessage> aUpdMsg =
+        std::dynamic_pointer_cast<ModelAPI_ObjectUpdatedMessage>(theMessage);
+    std::set<ObjectPtr> aObjects = aUpdMsg->objects();
+
+    ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                              (workshop()->operationMgr()->currentOperation());
+    bool aFeatureChanged = false;
+    if(aFOperation ) {
+      FeaturePtr aFeature = aFOperation->feature();
+      if (aFeature.get()) {
+        std::set<ObjectPtr>::const_iterator aIt;
+        for (aIt = aObjects.begin(); aIt != aObjects.end() && !aFeatureChanged; ++aIt) {
+          aFeatureChanged = ModelAPI_Feature::feature(*aIt) == aFeature;
+        }
+      }
+      if (aFeatureChanged)
+        workshop()->operationMgr()->onValidateOperation();
+    }
   } else {
     //Show error dialog if error message received.
     std::shared_ptr<Events_Error> anAppError = std::dynamic_pointer_cast<Events_Error>(theMessage);
@@ -186,31 +220,47 @@ void XGUI_WorkshopListener::processEvent(const std::shared_ptr<Events_Message>&
     }
     return;
   }
-  if (!workshop()->isSalomeMode()) {
+#ifndef HAVE_SALOME
     SessionPtr aMgr = ModelAPI_Session::get();
     AppElements_MainWindow* aMainWindow = workshop()->mainWindow();
     if (aMgr->isModified() != aMainWindow->isModifiedState())
       aMainWindow->setModifiedState(aMgr->isModified());
-  }
+#endif
 }
 
 //******************************************************
-void XGUI_WorkshopListener::onFeatureUpdatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
+void XGUI_WorkshopListener::onFeatureUpdatedMsg(
+                                     const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
 {
+#ifdef DEBUG_FEATURE_UPDATED
+  std::set<ObjectPtr> aObjects = theMsg->objects();
+  std::set<ObjectPtr>::const_iterator aIt;
+  QStringList anInfo;
+  for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
+    anInfo.append(ModuleBase_Tools::objectInfo((*aIt)));
+  }
+  QString anInfoStr = anInfo.join(";\t");
+  qDebug(QString("onFeatureUpdatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
+#endif
   std::set<ObjectPtr> aFeatures = theMsg->objects();
   XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
   if (anOperationMgr->hasOperation()) {
-    FeaturePtr aCurrentFeature = anOperationMgr->currentOperation()->feature();
-    std::set<ObjectPtr>::const_iterator aIt;
-    for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
-      ObjectPtr aNewFeature = (*aIt);
-      if (aNewFeature == aCurrentFeature) {
-        workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
-        break;
+    ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                      (anOperationMgr->currentOperation());
+    if (aFOperation) {
+      FeaturePtr aCurrentFeature = aFOperation->feature();
+      std::set<ObjectPtr>::const_iterator aIt;
+      for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
+        ObjectPtr aNewFeature = (*aIt);
+        if (aNewFeature == aCurrentFeature) {
+          workshop()->propertyPanel()->updateContentWidget(aCurrentFeature);
+          break;
+        }
       }
     }
   }
-  anOperationMgr->onValidateOperation();
+  //anOperationMgr->onValidateOperation();
+
   //if (myObjectBrowser)
   //  myObjectBrowser->processEvent(theMsg);
 }
@@ -232,6 +282,9 @@ void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI
 
   XGUI_Workshop* aWorkshop = workshop();
   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
+  bool aFirstVisualizedBody = false;
+
+  bool aRedisplayed = false;
   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
     ObjectPtr aObj = (*aIt);
 
@@ -253,8 +306,19 @@ void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI
         qDebug("COMPSOLID sub-object");
     }
 #endif
+    #ifdef DEBUG_FEATURE_REDISPLAY
+      QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
+      FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
+      if (aFeature.get()) {
+        std::string aKind = aFeature->getKind();
+        if (aKind == DebugFeatureKind) {
+          qDebug(QString("visible=%1, hide=%2 : display= %2").arg(aDisplayer->isVisible(aObj))
+                                            .arg(aHide).arg(anObjInfo).toStdString().c_str());
+        }
+      }
+    #endif
     if (aHide) {
-      aDisplayer->erase(aObj, false);
+      aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
       #ifdef DEBUG_FEATURE_REDISPLAY
         // Redisplay the visible object or the object of the current operation
         bool isVisibleObject = aDisplayer->isVisible(aObj);
@@ -269,12 +333,6 @@ void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI
       #ifdef DEBUG_FEATURE_REDISPLAY
         QString anObjInfo = ModuleBase_Tools::objectInfo((aObj));
         //qDebug(QString("visible=%1 : display= %2").arg(isVisibleObject).arg(anObjInfo).toStdString().c_str());
-        /*FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aObj);
-        if (aFeature.get()) {
-          std::string aKind = aFeature->getKind();
-          if (aKind == "SketchMultiRotation")
-            bool aValue = true;
-        }*/
       #endif
 
       if (isVisibleObject)  { // redisplay visible object
@@ -283,22 +341,30 @@ void XGUI_WorkshopListener::onFeatureRedisplayMsg(const std::shared_ptr<ModelAPI
         // of redisplay is called. This modification is made in order to have the line is updated
         // by creation of a horizontal constraint on the line by preselection
         if (ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(aObj))) {
-          aDisplayer->erase(aObj, false);
+          aRedisplayed = aDisplayer->erase(aObj, false) || aRedisplayed;
         }
         else {
-          aDisplayer->redisplay(aObj, false);
+          aRedisplayed = aDisplayer->redisplay(aObj, false) || aRedisplayed;
           // Deactivate object of current operation from selection
           aWorkshop->deactivateActiveObject(aObj, false);
         }
       } else { // display object if the current operation has it
-        if (displayObject(aObj)) {
+        if (displayObject(aObj, aFirstVisualizedBody)) {
+          aRedisplayed = true;
           // Deactivate object of current operation from selection
           aWorkshop->deactivateActiveObject(aObj, false);
         }
       }
     }
   }
-  aDisplayer->updateViewer();
+  if (aRedisplayed) {
+    customizeCurrentObject();
+    //VSV FitAll updated viewer by it self
+    if (aFirstVisualizedBody)
+      myWorkshop->viewer()->fitAll();
+    else 
+      aDisplayer->updateViewer();
+  }
 }
 //******************************************************
 void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_ObjectUpdatedMessage>& theMsg)
@@ -314,8 +380,10 @@ void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_O
   qDebug(QString("onFeatureCreatedMsg: %1, %2").arg(aObjects.size()).arg(anInfoStr).toStdString().c_str());
 #endif
 
+  bool aFirstVisualizedBody = false;
+
   //bool aHasPart = false;
-  bool isDisplayed = false;
+  bool aDisplayed = false;
   for (aIt = aObjects.begin(); aIt != aObjects.end(); ++aIt) {
     ObjectPtr anObject = *aIt;
 
@@ -341,20 +409,51 @@ void XGUI_WorkshopListener::onFeatureCreatedMsg(const std::shared_ptr<ModelAPI_O
       // with list of displayed objects
       if (myWorkshop->module()->canDisplayObject(anObject)) {
         anObject->setDisplayed(true);
-        isDisplayed = displayObject(*aIt);
+        aDisplayed = displayObject(*aIt, aFirstVisualizedBody);
       } else 
         anObject->setDisplayed(false);
     }
   }
+
   //if (myObjectBrowser)
   //  myObjectBrowser->processEvent(theMsg);
-  if (isDisplayed)
-    workshop()->displayer()->updateViewer();
+  if (aDisplayed) {
+    customizeCurrentObject();
+    //VSV FitAll updated viewer by it self
+    if (aFirstVisualizedBody)
+      myWorkshop->viewer()->fitAll();
+    else
+      workshop()->displayer()->updateViewer();
+  }
   //if (aHasPart) { // TODO: Avoid activate last part on loading of document
   //  activateLastPart();
   //}
 }
 
+/*void XGUI_WorkshopListener::onNestedStateChanged(const std::string& theFeatureId, const bool theState)
+{
+  XGUI_Workshop* aWorkshop = workshop();
+
+  //one button is used for all features, which can have nested actions, so it is obtained from
+  // the action manager
+  //bool aActionToBeUpdated = aWorkshop->isFeatureOfNested(theFeatureId);
+  if (aWorkshop->isSalomeMode()) {
+    XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
+    XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
+    if (aSalomeConnector->isFeatureOfNested(anActionsMgr->action(theFeatureId.c_str())))
+      aActionToBeUpdated = true;
+  } else {
+    AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
+    AppElements_Command* aCommand = aMenuBar->feature(theFeatureId.c_str());
+    if (aCommand && aCommand->button()->additionalButtonWidget())
+      aActionToBeUpdated = true;
+  }
+  if (aActionToBeUpdated) {
+    QAction* anAcceptAllAction = aWorkshop->actionsMgr()->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
+    anAcceptAllAction->setEnabled(theState);
+  }
+}*/
+
 bool XGUI_WorkshopListener::event(QEvent * theEvent)
 {
   PostponeMessageQtEvent* aPostponedEv = dynamic_cast<PostponeMessageQtEvent*>(theEvent);
@@ -389,69 +488,69 @@ void XGUI_WorkshopListener::addFeature(const std::shared_ptr<Config_FeatureMessa
     QString aNestedActions = QString::fromStdString(theMessage->actionsWhenNested());
     XGUI_OperationMgr* anOperationMgr = aWorkshop->operationMgr();
     XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
-    if (aNestedActions.contains("accept")) {
+    if (aNestedActions.contains(FEATURE_WHEN_NESTED_ACCEPT)) {
       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AcceptAll, NULL);
       connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(commitAllOperations()));
       aNestedActList << anAction;
     }
-    if (aNestedActions.contains("abort")) {
+    if (aNestedActions.contains(FEATURE_WHEN_NESTED_ABORT)) {
       QAction* anAction = anActionsMgr->operationStateAction(XGUI_ActionsMgr::AbortAll, NULL);
       connect(anAction, SIGNAL(triggered()), anOperationMgr, SLOT(abortAllOperations()));
       aNestedActList << anAction;
     }
   }
 
-  if (aWorkshop->isSalomeMode()) {
-    XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
-    QAction* aAction;
-    if (isColumnButton) {
-      aAction = aSalomeConnector->addNestedFeature(aWchName, aFeatureInfo, aNestedActList);
-    } else {
-      //Issue #650: in the SALOME mode the tooltip should be same as text
-      aFeatureInfo.toolTip = aFeatureInfo.text;
-      aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
-    }
-    aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
-    aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
-
-    aWorkshop->actionsMgr()->addCommand(aAction);
-    aWorkshop->module()->actionCreated(aAction);
+#ifdef HAVE_SALOME
+  XGUI_SalomeConnector* aSalomeConnector = aWorkshop->salomeConnector();
+  QAction* aAction;
+  if (isColumnButton) {
+    aAction = aSalomeConnector->addFeatureOfNested(aWchName, aFeatureInfo, aNestedActList);
   } else {
-    //Find or create Workbench
-    AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
-    AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
-    if (!aPage) {
-      aPage = aWorkshop->addWorkbench(aWchName);
-    }
-    //Find or create Group
-    QString aGroupName = QString::fromStdString(theMessage->groupId());
-    AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
-    if (!aGroup) {
-      aGroup = aPage->addGroup(aGroupName);
-    }
-    // Check if hotkey sequence is already defined:
-    XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
-    QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
-    if(aHotKey != aFeatureInfo.shortcut) {
-      aFeatureInfo.shortcut = aHotKey;
-    }
-    // Create feature...
-    AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
-                                                       aDocKind,
-                                                       aNestedFeatures);
-    // Enrich created button with accept/abort buttons if necessary
-    AppElements_Button* aButton = aCommand->button();
-    if (aButton->isColumnButton()) {
-      aButton->setAdditionalButtons(aNestedActList);
-    }
-    aWorkshop->actionsMgr()->addCommand(aCommand);
-    aWorkshop->module()->actionCreated(aCommand);
+    //Issue #650: in the SALOME mode the tooltip should be same as text
+    aFeatureInfo.toolTip = aFeatureInfo.text;
+    aAction = aSalomeConnector->addFeature(aWchName, aFeatureInfo);
+  }
+  aSalomeConnector->setNestedActions(aFeatureInfo.id, aNestedFeatures);
+  aSalomeConnector->setDocumentKind(aFeatureInfo.id, aDocKind);
+
+  aWorkshop->actionsMgr()->addCommand(aAction);
+  aWorkshop->module()->actionCreated(aAction);
+#else 
+  //Find or create Workbench
+  AppElements_MainMenu* aMenuBar = aWorkshop->mainWindow()->menuObject();
+  AppElements_Workbench* aPage = aMenuBar->findWorkbench(aWchName);
+  if (!aPage) {
+    aPage = aWorkshop->addWorkbench(aWchName);
   }
+  //Find or create Group
+  QString aGroupName = QString::fromStdString(theMessage->groupId());
+  AppElements_MenuGroupPanel* aGroup = aPage->findGroup(aGroupName);
+  if (!aGroup) {
+    aGroup = aPage->addGroup(aGroupName);
+  }
+  // Check if hotkey sequence is already defined:
+  XGUI_ActionsMgr* anActionsMgr = aWorkshop->actionsMgr();
+  QKeySequence aHotKey = anActionsMgr->registerShortcut(aFeatureInfo.shortcut);
+  if(aHotKey != aFeatureInfo.shortcut) {
+    aFeatureInfo.shortcut = aHotKey;
+  }
+  // Create feature...
+  AppElements_Command* aCommand = aGroup->addFeature(aFeatureInfo,
+                                                      aDocKind,
+                                                      aNestedFeatures);
+  // Enrich created button with accept/abort buttons if necessary
+  AppElements_Button* aButton = aCommand->button();
+  if (aButton->isColumnButton()) {
+    aButton->setAdditionalButtons(aNestedActList);
+  }
+  aWorkshop->actionsMgr()->addCommand(aCommand);
+  aWorkshop->module()->actionCreated(aCommand);
+#endif
 }
 
 
 //**************************************************************
-bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
+bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj, bool& theFirstVisualizedBody)
 {
 #ifdef DEBUG_RESULT_COMPSOLID
   ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
@@ -463,23 +562,40 @@ bool XGUI_WorkshopListener::displayObject(ObjectPtr theObj)
   }
 #endif
 
+  bool aDisplayed = false;
   XGUI_Workshop* aWorkshop = workshop();
   // do not display the object if it has sub objects. They should be displayed separately.
   if (!aWorkshop->module()->canDisplayObject(theObj) ||
       ModelAPI_Tools::hasSubResults(std::dynamic_pointer_cast<ModelAPI_Result>(theObj)))
-    return false;
+    return aDisplayed;
 
   XGUI_Displayer* aDisplayer = aWorkshop->displayer();
   ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(theObj);
   if (aBody.get() != NULL) {
     int aNb = aDisplayer->objectsCount();
-    aDisplayer->display(theObj, false);
+    aDisplayed = aDisplayer->display(theObj, false);
     if (aNb == 0)
-      myWorkshop->viewer()->fitAll();
+      theFirstVisualizedBody = true;
   } else 
-    aDisplayer->display(theObj, false);
+    aDisplayed = aDisplayer->display(theObj, false);
+
+  return aDisplayed;
+}
 
-  return true;
+bool XGUI_WorkshopListener::customizeCurrentObject()
+{
+  bool aCustomized = false;
+  XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
+  if (anOperationMgr->hasOperation()) {
+    ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
+                                                      (anOperationMgr->currentOperation());
+    if (aFOperation) {
+      FeaturePtr aCurrentFeature = aFOperation->feature();
+      if (aCurrentFeature.get())
+        aCustomized = myWorkshop->module()->customizeObject(aCurrentFeature, false);
+    }
+  }
+  return aCustomized;
 }
 
 XGUI_Workshop* XGUI_WorkshopListener::workshop() const