Salome HOME
Synchronize displayed objects if Viewer was opened after displaying
[modules/shaper.git] / src / NewGeom / NewGeom_Module.cpp
index 6bb769fdafa20b1bfe402eca040e8418232c6361..75f33a64f9a32f8bb840b259b24d1f4e52427f3b 100644 (file)
@@ -1,20 +1,27 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 
 #include "NewGeom_Module.h"
 #include "NewGeom_DataModel.h"
 #include "NewGeom_OCCSelector.h"
+#include <NewGeom_NestedButton.h>
 
 #include <XGUI_Workshop.h>
 #include <XGUI_PropertyPanel.h>
 #include <XGUI_ContextMenuMgr.h>
-#include <XGUI_Preferences.h>
 #include <XGUI_ObjectsBrowser.h>
 #include <XGUI_OperationMgr.h>
+#include <XGUI_Displayer.h>
 
 #include <ModuleBase_Operation.h>
+#include <ModuleBase_Preferences.h>
+#include <ModuleBase_ActionInfo.h>
+#include <ModuleBase_IModule.h>
 
 #include <LightApp_Application.h>
 #include <LightApp_SelectionMgr.h>
 #include <LightApp_OCCSelector.h>
+#include <LightApp_Study.h>
 #include <OCCViewer_ViewModel.h>
 
 #include <SUIT_Selector.h>
 
 #include <QtxPopupMgr.h>
 #include <QtxActionMenuMgr.h>
+#include <QtxActionToolMgr.h>
 #include <QtxResourceMgr.h>
 
 #include <Config_PropManager.h>
+#include <Config_ModuleReader.h>
+
+#include <AIS_ListOfInteractive.hxx>
+#include <AIS_ListIteratorOfListOfInteractive.hxx>
 
 #include <QDockWidget>
 #include <QAction>
 #include <QTimer>
+#include <QMenu>
 
 
 extern "C" {
@@ -45,9 +58,15 @@ NewGeom_EXPORT char* getModuleVersion()
 }
 }
 
-class NewGeom_PrefMgr: public XGUI_IPrefMgr
+/** 
+* Class for preferences management
+*/
+class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
 {
 public:
+  /// Constructor
+  /// \param theMgr preferences manager of SALOME
+  /// \param theModName name of the module
   NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
 
   virtual int addPreference(const QString& theLbl, int pId, 
@@ -57,6 +76,14 @@ public:
     return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
   }
 
+  virtual void setItemProperty(const QString& thePropName,
+                               const QVariant& theValue,
+                               const int theId = -1)
+  {
+    myMgr->setItemProperty(thePropName, theValue, theId);
+  }
+
+
   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
 
 private:
@@ -70,13 +97,16 @@ private:
 //******************************************************
 NewGeom_Module::NewGeom_Module()
     : LightApp_Module("NewGeom"),
-      mySelector(0), myIsOpened(0)
+      mySelector(0), myIsOpened(0), myPopupMgr(0)
 {
   myWorkshop = new XGUI_Workshop(this);
+  connect(myWorkshop, SIGNAL(commandStatusUpdated()),
+          this, SLOT(onUpdateCommandStatus()));
+
   myProxyViewer = new NewGeom_SalomeViewer(this);
 
-  XGUI_Preferences::setResourceMgr(application()->resourceMgr());
-  XGUI_Preferences::loadCustomProps();
+  ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
+  ModuleBase_Preferences::loadCustomProps();
 }
 
 //******************************************************
@@ -88,11 +118,14 @@ NewGeom_Module::~NewGeom_Module()
 void NewGeom_Module::initialize(CAM_Application* theApp)
 {
   LightApp_Module::initialize(theApp);
+  inspectSalomeModules();
 
   myWorkshop->startApplication();
   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
   if (anApp)
+  {
     connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
+  }
 }
 
 //******************************************************
@@ -122,6 +155,7 @@ bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
       aViewAct->setEnabled(true);
       myWorkshop->objectBrowser()->setVisible(true);
       aObjDoc->setVisible(true);
+      desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
     }
 
     if (!mySelector) {
@@ -131,18 +165,22 @@ bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
         mySelector = createSelector(OCCViewManagers.first());
       }
     }
-    QtxPopupMgr* aMgr = popupMgr();  // Create popup manager
-    action(myEraseAll)->setEnabled(false);
+    // it should be pefromed after the selector creation in order to have AISContext 
+    myWorkshop->activateModule();
+    //action(myEraseAll)->setEnabled(false);
 
     if (myIsOpened) {
       myWorkshop->objectBrowser()->rebuildDataTree();
       myWorkshop->updateCommandStatus();
       myIsOpened = false;
-      QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
     }
+    else
+      myWorkshop->updateCommandStatus();
   }
   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
   myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
+  myIsEditEnabled = getApp()->isEditEnabled();
+  getApp()->setEditEnabled(false);
 
   // this following row is caused by #187 bug.
   // SALOME saves the dock widget positions before deactivateModule() and
@@ -152,15 +190,50 @@ bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
   // switch off in this module
   aResMgr->setValue("Study", "store_positions", false);
 
+  // Synchronize displayed objects
+  Handle(AIS_InteractiveContext) aContext;
+  if (mySelector && mySelector->viewer())
+    aContext = mySelector->viewer()->getAISContext();
+
+  if (!aContext.IsNull()) {
+    XGUI_Displayer* aDisp = myWorkshop->displayer();
+    QObjectPtrList aObjList = aDisp->displayedObjects();
+
+    AIS_ListOfInteractive aList;
+    aContext->DisplayedObjects(aList);
+    AIS_ListIteratorOfListOfInteractive aLIt;
+    Handle(AIS_InteractiveObject) anAISIO;
+    foreach (ObjectPtr aObj, aObjList) {
+      AISObjectPtr aPrs = aDisp->getAISObject(aObj);
+      Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
+      bool aFound = false;
+      for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
+        anAISIO = aLIt.Value();
+        if (anAISIO.Access() == aAIS.Access()) {
+          aFound = true;
+          break;
+        }
+      }
+      if (!aFound) {
+        aObj->setDisplayed(false);
+        //aDisp->erase(aObj, false);
+      }
+    }
+    Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
+  }
+  myProxyViewer->activateViewer(true);
   return isDone;
 }
 
 //******************************************************
 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
 {
+  myProxyViewer->activateViewer(false);
   setMenuShown(false);
   setToolShown(false);
 
+  myWorkshop->deactivateModule();
+
   QObject* aObj = myWorkshop->objectBrowser()->parent();
   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
   if (aObjDoc) {
@@ -176,9 +249,10 @@ bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
   // because the displayed objects should be removed from the viewer, but
   // the AIS context is obtained from the selector.
   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
-  if (anOperation)
+  while (anOperation) {
     anOperation->abort();
-
+    anOperation = myWorkshop->operationMgr()->currentOperation();
+  }
   // Delete selector because it has to be redefined on next activation
   if (mySelector) {
     myProxyViewer->setSelector(0);
@@ -190,6 +264,7 @@ bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
 
   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
+  getApp()->setEditEnabled(myIsEditEnabled);
 
   return LightApp_Module::deactivateModule(theStudy);
 }
@@ -197,20 +272,60 @@ bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
 //******************************************************
 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
 {
-  if ((!mySelector)) {
+  if (!mySelector) {
     mySelector = createSelector(theMgr);
+    myWorkshop->module()->activateSelectionFilters();
+    myWorkshop->synchronizeViewer();
   }
 }
 
+//******************************************************
+void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
+{
+  if (mySelector) {
+    if (theMgr->getType() == OCCViewer_Viewer::Type()) {
+      OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
+      if (mySelector->viewer() == aViewer) {
+        XGUI_Displayer* aDisp = myWorkshop->displayer();
+        QObjectPtrList aObjects = aDisp->displayedObjects();
+        foreach(ObjectPtr aObj, aObjects)
+          aObj->setDisplayed(false);
+        Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
+        myProxyViewer->setSelector(0);
+        delete mySelector;
+        mySelector = 0;
+
+        myWorkshop->module()->clearViewer();
+      }
+    }
+  }
+}
+
+//******************************************************
+QtxPopupMgr* NewGeom_Module::popupMgr()
+{
+  if (!myPopupMgr)
+    myPopupMgr = new QtxPopupMgr( 0, this );
+  return myPopupMgr;
+}
+
 //******************************************************
 void NewGeom_Module::onDefaultPreferences()
 {
-  XGUI_Preferences::resetConfig();
-  XGUI_Preferences::updateResourcesByConfig();
+  ModuleBase_Preferences::resetConfig();
+  ModuleBase_Preferences::updateResourcesByConfig();
 
   LightApp_Preferences* pref = preferences();
   if (pref)
     pref->retrieve();
+
+  myWorkshop->displayer()->redisplayObjects();
+}
+
+//******************************************************
+void NewGeom_Module::onUpdateCommandStatus()
+{
+  getApp()->updateActions();
 }
 
 //******************************************************
@@ -235,7 +350,24 @@ NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
 //******************************************************
 CAM_DataModel* NewGeom_Module::createDataModel()
 {
-  return new NewGeom_DataModel(this);
+  NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
+
+  // Calling addComponent() for persistent functionality work in the SalomeApp_Study
+  LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
+  aStudy->addComponent(aDataModel);
+
+  return aDataModel;
+}
+
+QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
+{
+  return addFeature(theWBName,
+                    theInfo.id,
+                    theInfo.text,
+                    theInfo.toolTip,
+                    theInfo.icon,
+                    theInfo.shortcut,
+                    theInfo.checkable);
 }
 
 //******************************************************
@@ -244,8 +376,21 @@ QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& the
                                     const QIcon& theIcon, const QKeySequence& theKeys,
                                     bool isCheckable)
 {
+  static QString aLastTool = "";
+  static int aNb = 0;
+  if (aLastTool.isEmpty())
+    aLastTool = theWBName;
+  else if (theWBName != aLastTool) {
+    aLastTool = theWBName;
+    if (aNb > 20) {
+      desktop()->addToolBarBreak();
+      aNb = 0;
+    }
+  }
+  aNb++;
+
   int aMenu = createMenu(theWBName, -1, -1, 50);
-  int aTool = createTool(theWBName);
+  int aTool = createTool(theWBName, theWBName);
 
   int aId = myActionsList.size();
   myActionsList.append(theId);
@@ -258,15 +403,51 @@ QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& the
   aAction->setData(theId);
   int aItemId = createMenu(aId, aMenu, -1, 10);
   int aToolId = createTool(aId, aTool);
+
   return aAction;
 }
 
+bool NewGeom_Module::isFeatureOfNested(const QAction* theAction)
+{
+  return dynamic_cast<const NewGeom_NestedButton*>(theAction);
+}
+
+QAction* NewGeom_Module::addFeatureOfNested(const QString& theWBName,
+                                            const ActionInfo& theInfo,
+                                            const QList<QAction*>& theNestedActions)
+{
+  int aMenu = createMenu(theWBName, -1, -1, 50);
+  int aTool = createTool(theWBName, theWBName);
+
+  int aId = myActionsList.size();
+  myActionsList.append(theInfo.id);
+  SUIT_Desktop* aDesk = application()->desktop();
+  NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
+  anAction->setData(theInfo.id);
+  anAction->setCheckable(theInfo.checkable);
+  anAction->setChecked(theInfo.checked);
+  anAction->setEnabled(theInfo.enabled);
+  anAction->setVisible(theInfo.visible);
+  anAction->setIcon(theInfo.icon);
+  anAction->setText(theInfo.text);
+  anAction->setToolTip(theInfo.toolTip);
+  anAction->setShortcut(theInfo.shortcut);
+  anAction->setFont(theInfo.font);
+
+  //int aItemId = createMenu(aId, aMenu, -1, 10);
+  int aToolId = createTool(anAction, aTool, aId);
+
+  return anAction;
+}
+
+
 //******************************************************
-QAction* NewGeom_Module::addEditCommand(const QString& theId, const QString& theTitle,
-                                        const QString& theTip, const QIcon& theIcon,
-                                        const QKeySequence& theKeys, bool isCheckable)
+QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
+                                           const QString& theTip, const QIcon& theIcon,
+                                           const QKeySequence& theKeys, bool isCheckable,
+                                           const char* theMenuSourceText, const int theMenuPosition)
 {
-  int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
+  int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
 
   int aId = myActionsList.size();
   myActionsList.append(theId);
@@ -277,23 +458,43 @@ QAction* NewGeom_Module::addEditCommand(const QString& theId, const QString& the
   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
                                   isCheckable);
   aAction->setData(theId);
-  createMenu(aId, aMenu, 10);
+  createMenu(aId, aMenu, theMenuPosition);
   return aAction;
 }
 
 //******************************************************
-void NewGeom_Module::addEditMenuSeparator()
+void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
 {
-  int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
-  createMenu(separator(), aMenu, -1, 10);
+  int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
+  createMenu(separator(), aMenu, -1, theMenuPosition);
+}
+
+bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
+{
+  if( !theAction )
+    return false;
+
+  SUIT_Desktop* aDesktop = application()->desktop();
+  if( !aDesktop )
+    return false;
+
+  QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
+  if( !aToolMgr )
+    return false;
+
+  aToolMgr->append( theAction, theToolBarTitle );
+  return true;
 }
 
 //******************************************************
 QList<QAction*> NewGeom_Module::commandList() const
 {
   QList<QAction*> aActions;
-  for (int i = 0; i < myActionsList.size(); i++)
-    aActions.append(action(i));
+  for (int i = 0; i < myActionsList.size(); i++) {
+    QAction* aCmd = action(i);
+    if (aCmd && myActionsList.contains(aCmd->data().toString()))
+      aActions.append(aCmd);
+  }
   return aActions;
 }
 
@@ -367,7 +568,8 @@ void NewGeom_Module::selectionChanged()
 //******************************************************
 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
 {
-  myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
+  myWorkshop->contextMenuMgr()->updateViewerMenu();
+  myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
 }
 
@@ -378,7 +580,7 @@ void NewGeom_Module::createPreferences()
   LightApp_Preferences* pref = preferences();
   if (!pref)
     return;
-  XGUI_Preferences::updateConfigByResources();
+  ModuleBase_Preferences::updateConfigByResources();
   QString aModName = moduleName();
 
   QtxPreferenceItem* item = pref->findItem(aModName, true );
@@ -391,7 +593,7 @@ void NewGeom_Module::createPreferences()
   if ( catId == -1 )
     return;
   NewGeom_PrefMgr aMgr(pref, aModName);
-  XGUI_Preferences::createEditContent(&aMgr, catId);
+  ModuleBase_Preferences::createEditContent(&aMgr, catId);
   pref->retrieve();
 }
 
@@ -412,4 +614,19 @@ void NewGeom_Module::preferencesChanged(const QString& theSection, const QString
   }
   aProp->setValue(aValue);
 
+  myWorkshop->displayer()->redisplayObjects();
+}
+
+void NewGeom_Module::inspectSalomeModules()
+{
+  QStringList aModuleNames;
+  getApp()->modules(aModuleNames, false);
+  foreach(QString eachModule, aModuleNames) {
+    Config_ModuleReader::addDependencyModule(eachModule.toStdString());
+  }
+}
+
+bool NewGeom_Module::abortAllOperations()
+{
+  return workshop()->operationMgr()->abortAllOperations();
 }