Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / XGUI / XGUI_Workshop.cpp
index 1a5d25095749729ae9f06d69327ec457fb867a26..ee311ff8f6bc943b96cf651331f1a684b8968511 100644 (file)
@@ -1,4 +1,4 @@
-#include "XGUI_Module.h"
+#include "ModuleBase_IModule.h"
 #include "XGUI_Constants.h"
 #include "XGUI_Command.h"
 #include "XGUI_MainMenu.h"
@@ -20,6 +20,7 @@
 #include "XGUI_ViewerProxy.h"
 #include "XGUI_PropertyPanel.h"
 #include "XGUI_ContextMenuMgr.h"
+#include "XGUI_ModuleConnector.h"
 
 #include <Model_Events.h>
 #include <ModelAPI_PluginManager.h>
@@ -28,6 +29,8 @@
 #include <ModelAPI_AttributeDocRef.h>
 #include <ModelAPI_Object.h>
 
+#include <PartSetPlugin_Part.h>
+
 #include <Events_Loop.h>
 #include <Events_Error.h>
 #include <ModuleBase_Operation.h>
@@ -82,6 +85,7 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
   myDisplayer = new XGUI_Displayer(this);
 
   mySelector = new XGUI_SelectionMgr(this);
+  connect(mySelector, SIGNAL(selectionChanged()), this, SLOT(updateModuleCommands()));
 
   myOperationMgr = new XGUI_OperationMgr(this);
   myActionsMgr = new XGUI_ActionsMgr(this);
@@ -91,6 +95,8 @@ XGUI_Workshop::XGUI_Workshop(XGUI_SalomeConnector* theConnector)
           this, SLOT(onContextMenuCommand(const QString&, bool)));
 
   myViewerProxy = new XGUI_ViewerProxy(this);
+  
+  myModuleConnector = new XGUI_ModuleConnector(this);
 
   connect(myOperationMgr, SIGNAL(operationStarted()), SLOT(onOperationStarted()));
   connect(myOperationMgr, SIGNAL(operationResumed()), SLOT(onOperationStarted()));
@@ -225,11 +231,14 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage)
     bool aHasPart = false;
     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
       FeaturePtr aFeature = (*aIt);
-      if (aFeature->getKind() == "Part") {
+      if (aFeature->getKind() == PARTSET_PART_KIND) {
         aHasPart = true;
-        break;
+        //break;
+      } else {
+        myDisplayer->display(aFeature, false);
       }
     }
+    myDisplayer->updateViewer();
     if (aHasPart) {
       //The created part will be created in Object Browser later and we have to activate it
       // only when it is created everywere
@@ -240,31 +249,46 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage)
   //Update property panel on corresponding message. If there is no current operation (no
   //property panel), or received message has different feature to the current - do nothing.
   static Events_ID aFeatureUpdatedId = Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED);
-  if (theMessage->eventID() == aFeatureUpdatedId && myOperationMgr->hasOperation())
-  {
+  if (theMessage->eventID() == aFeatureUpdatedId) {
     const Model_FeatureUpdatedMessage* anUpdateMsg =
         dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
-    std::set<FeaturePtr> aFeatures = anUpdateMsg->features();
 
-    FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
+    std::set<FeaturePtr> aFeatures = anUpdateMsg->features();
+    if (myOperationMgr->hasOperation())
+    {
+      FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
+      std::set<FeaturePtr>::const_iterator aIt;
+      for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
+        FeaturePtr aNewFeature = (*aIt);
+        if(aNewFeature == aCurrentFeature) {
+          myPropertyPanel->updateContentWidget(aCurrentFeature);
+          break;
+        } 
+      }
+    }
+    // Redisplay feature if it is modified
     std::set<FeaturePtr>::const_iterator aIt;
     for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
-      FeaturePtr aNewFeature = (*aIt);
-      if(aNewFeature == aCurrentFeature) {
-        myPropertyPanel->updateContentWidget(aCurrentFeature);
-        break;
-      } 
+      FeaturePtr aFeature = (*aIt);
+      if (aFeature->getKind() != PARTSET_PART_KIND) {
+        if (myDisplayer->isVisible(aFeature))
+          myDisplayer->redisplay(aFeature, false);
+        else 
+          myDisplayer->display(aFeature, false);
+      }
     }
+    myDisplayer->updateViewer();
   }
   //An operation passed by message. Start it, process and commit.
   const Config_PointerMessage* aPartSetMsg = dynamic_cast<const Config_PointerMessage*>(theMessage);
   if (aPartSetMsg) {
+    myPropertyPanel->cleanContent();
     ModuleBase_Operation* anOperation =
         (ModuleBase_Operation*)(aPartSetMsg->pointer());
 
     if (myOperationMgr->startOperation(anOperation)) {
       myPropertyPanel->updateContentWidget(anOperation->feature());
-      if (anOperation->getDescription()->xmlRepresentation().isEmpty()) {
+      if (!anOperation->getDescription()->hasXmlRepresentation()) {
         anOperation->commit();
         updateCommandStatus();
       }
@@ -275,9 +299,6 @@ void XGUI_Workshop::processEvent(const Events_Message* theMessage)
   const Events_Error* anAppError = dynamic_cast<const Events_Error*>(theMessage);
   if (anAppError) {
     emit errorOccurred(QString::fromLatin1(anAppError->description()));
-    myErrorDlg->show();
-    myErrorDlg->raise();
-    myErrorDlg->activateWindow();
   }
 
 }
@@ -287,16 +308,31 @@ void XGUI_Workshop::onOperationStarted()
 {
   ModuleBase_Operation* aOperation = myOperationMgr->currentOperation();
 
-  if(!aOperation->getDescription()->xmlRepresentation().isEmpty()) { //!< No need for property panel
+  if(aOperation->getDescription()->hasXmlRepresentation()) { //!< No need for property panel
     connectWithOperation(aOperation);
 
     showPropertyPanel();
-
-    ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aOperation);
+    QString aXmlRepr = aOperation->getDescription()->xmlRepresentation();
+    ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(), myModuleConnector);
     QWidget* aContent = myPropertyPanel->contentWidget();
     qDeleteAll(aContent->children());
     aFactory.createWidget(aContent);
-    myPropertyPanel->setModelWidgets(aFactory.getModelWidgets());
+    
+    QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
+    QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
+    ModuleBase_ModelWidget* aWidget;
+    for (; anIt != aLast; anIt++) {
+      aWidget = *anIt;
+      //QObject::connect(aWidget, SIGNAL(valuesChanged()),  aOperation, SLOT(storeCustomValue()));
+      QObject::connect(aWidget, SIGNAL(valuesChanged()),
+                       this, SLOT(onWidgetValuesChanged()));
+      // Init default values
+      if (!aOperation->isEditOperation() && aWidget->hasDefaultValue()) {
+        aWidget->storeValue(aOperation->feature());
+      }
+    }
+
+    myPropertyPanel->setModelWidgets(aWidgets);
     myPropertyPanel->setWindowTitle(aOperation->getDescription()->description());
   }
   updateCommandStatus();
@@ -308,6 +344,7 @@ void XGUI_Workshop::onOperationStopped(ModuleBase_Operation* theOperation)
   //!< No need for property panel
   updateCommandStatus();
   hidePropertyPanel();
+  //myPropertyPanel->cleanContent();
 }
 
 /*
@@ -432,6 +469,7 @@ void XGUI_Workshop::onNew()
     aWnd->showMaximized();
     updateCommandStatus();
   }
+  myContextMenuMgr->connectViewer();
   QApplication::restoreOverrideCursor();
 }
 
@@ -468,8 +506,9 @@ void XGUI_Workshop::onOpen()
   }
   QApplication::setOverrideCursor(Qt::WaitCursor);
   aDoc->load(myCurrentDir.toLatin1().constData());
-  QApplication::restoreOverrideCursor();
   updateCommandStatus();
+  myObjectBrowser->rebuildDataTree();
+  QApplication::restoreOverrideCursor();
 }
 
 //******************************************************
@@ -537,7 +576,7 @@ void XGUI_Workshop::onRedo()
 }
 
 //******************************************************
-XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
+ModuleBase_IModule* XGUI_Workshop::loadModule(const QString& theModule)
 {
   QString libName =
       QString::fromStdString(library(theModule.toStdString()));
@@ -585,7 +624,7 @@ XGUI_Module* XGUI_Workshop::loadModule(const QString& theModule)
   }
 #endif
 
-  XGUI_Module* aModule = crtInst ? crtInst(this) : 0;
+  ModuleBase_IModule* aModule = crtInst ? crtInst(this) : 0;
 
   if (!err.isEmpty()) {
     if (mainWindow()) {
@@ -651,6 +690,26 @@ void XGUI_Workshop::updateCommandStatus()
   myActionsMgr->update();
 }
 
+//******************************************************
+void XGUI_Workshop::updateModuleCommands()
+{
+  QList<QAction*> aCommands;
+  if (isSalomeMode()) { // update commands in SALOME mode
+    aCommands = salomeConnector()->commandList();
+  } else {
+    XGUI_MainMenu* aMenuBar = myMainWindow->menuObject();
+    foreach (XGUI_Workbench* aWb, aMenuBar->workbenches()) {
+      if (aWb != aMenuBar->generalPage()) {
+        foreach(XGUI_Command* aCmd, aWb->features())
+          aCommands.append(aCmd);
+      }
+    }
+  }
+  foreach(QAction* aCmd, aCommands) {
+    aCmd->setEnabled(myPartSetModule->isFeatureEnabled(aCmd->data().toString()));
+  }
+}
+
 //******************************************************
 QDockWidget* XGUI_Workshop::createObjectBrowser(QWidget* theParent)
 {
@@ -690,6 +749,11 @@ void XGUI_Workshop::createDockWidgets()
 
   connect(myPropertyPanel, SIGNAL(keyReleased(const std::string&, QKeyEvent*)),
           myOperationMgr, SLOT(onKeyReleased(const std::string&, QKeyEvent*)));
+
+  connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
+          myOperationMgr, SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
+  connect(myOperationMgr, SIGNAL(activateNextWidget(ModuleBase_ModelWidget*)),
+          myPropertyPanel, SLOT(onActivateNextWidget(ModuleBase_ModelWidget*)));
 }
 
 //******************************************************
@@ -740,7 +804,7 @@ void XGUI_Workshop::changeCurrentDocument(FeaturePtr thePart)
   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
   if (thePart) {
     DocumentPtr aFeaDoc;
-    if (thePart->data()) {
+    if (!XGUI_Tools::isModelObject(thePart)) {
       aFeaDoc = thePart->data()->docRef("PartDocument")->value();
     } else {
       ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(thePart);
@@ -776,6 +840,30 @@ void XGUI_Workshop::onContextMenuCommand(const QString& theId, bool isChecked)
     activatePart(FeaturePtr());
   else if (theId == "DELETE_CMD")
     deleteFeatures(aFeatures);
+  else if (theId == "SHOW_CMD")
+    showFeatures(aFeatures, true);
+  else if (theId == "HIDE_CMD")
+    showFeatures(aFeatures, false);
+}
+
+//**************************************************************
+void XGUI_Workshop::onWidgetValuesChanged()
+{
+  ModuleBase_Operation* anOperation = myOperationMgr->currentOperation();
+  FeaturePtr aFeature = anOperation->feature();
+
+  ModuleBase_ModelWidget* aSenderWidget = dynamic_cast<ModuleBase_ModelWidget*>(sender());
+  //if (aCustom)
+  //  aCustom->storeValue(aFeature);
+
+  const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
+  QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
+  for (; anIt != aLast; anIt++) {
+    ModuleBase_ModelWidget* aCustom = *anIt;
+    if (aCustom && (/*!aCustom->isInitialized(aFeature) ||*/ aCustom == aSenderWidget)) {
+      aCustom->storeValue(aFeature);
+    }
+  }
 }
 
 //**************************************************************
@@ -805,9 +893,9 @@ void XGUI_Workshop::deleteFeatures(QFeatureList theList)
     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
     aMgr->rootDocument()->startOperation();
     foreach (FeaturePtr aFeature, theList) {
-      if (aFeature->getKind() == "Part") {
+      if (aFeature->getKind() == PARTSET_PART_KIND) {
         DocumentPtr aDoc;
-        if (aFeature->data()) {
+        if (!XGUI_Tools::isModelObject(aFeature)) {
           aDoc = aFeature->data()->docRef("PartDocument")->value();
         } else {
           ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
@@ -818,13 +906,31 @@ void XGUI_Workshop::deleteFeatures(QFeatureList theList)
           aDoc->close();
         }
       } else {
-        if (!aFeature->data()) {
+        if (XGUI_Tools::isModelObject(aFeature)) {
           ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
           aFeature = aObject->featureRef();
         }
       }
+      if (myDisplayer->isVisible(aFeature))
+        myDisplayer->erase(aFeature, false);
       aFeature->document()->removeFeature(aFeature);
     }
+    myDisplayer->updateViewer();
     aMgr->rootDocument()->finishOperation();
   }
 }
+
+//**************************************************************
+void XGUI_Workshop::showFeatures(QFeatureList theList, bool isVisible)
+{
+  if (isVisible) {
+    foreach (FeaturePtr aFeature, theList) {
+      myDisplayer->display(aFeature, false);
+    }
+  } else {
+    foreach (FeaturePtr aFeature, theList) {
+      myDisplayer->erase(aFeature, false);
+    }
+  }
+  myDisplayer->updateViewer();
+}