Salome HOME
Multi-study support removal: finalization
[modules/gui.git] / src / SalomeApp / SalomeApp_Application.cxx
index 282a26f300a4200a56eeb27a05c371b70d02418a..32d2f1d5d62271084f49fc87de8916c4080136b9 100644 (file)
@@ -164,8 +164,6 @@ extern "C" SALOMEAPP_EXPORT SUIT_Application* createApplication()
 SalomeApp_Application::SalomeApp_Application()
   : LightApp_Application()
 {
-  connect( desktop(), SIGNAL( message( const QString& ) ),
-           this, SLOT( onLoadDocMessage( const QString& ) ), Qt::UniqueConnection );
   myIsSiman = false; // default
 }
 
@@ -320,12 +318,13 @@ void SalomeApp_Application::createActions()
                 tr( "MEN_DESK_REGISTRY_DISPLAY" ), tr( "PRP_DESK_REGISTRY_DISPLAY" ),
                 /*Qt::SHIFT+Qt::Key_D*/0, desk, false, this, SLOT( onRegDisplay() ) );
 
-  //rnv commented : implementation of the mono-study in GUI
-  //
-  //createAction( FileLoadId, tr( "TOT_DESK_FILE_LOAD" ),
-  //              resourceMgr()->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
-  //              tr( "MEN_DESK_FILE_LOAD" ), tr( "PRP_DESK_FILE_LOAD" ),
-  //              Qt::CTRL+Qt::Key_L, desk, false, this, SLOT( onLoadDoc() ) );
+  createAction( ConnectId, tr( "TOT_DESK_CONNECT_STUDY" ), QIcon(),
+                tr( "MEN_DESK_CONNECT" ), tr( "PRP_DESK_CONNECT" ),
+                Qt::CTRL+Qt::Key_L, desk, false, this, SLOT( onLoadDoc() ) );
+
+  createAction( DisconnectId, tr( "TOT_DESK_DISCONNECT_STUDY" ), QIcon(),
+                tr( "MEN_DESK_DISCONNECT" ), tr( "PRP_DESK_DISCONNECT" ),
+                Qt::CTRL+Qt::Key_U, desk, false, this, SLOT( onUnloadDoc() ) );
 
 
 #ifdef WITH_SIMANIO
@@ -346,7 +345,9 @@ void SalomeApp_Application::createActions()
   // creation of menu item is moved to VISU
   //  createMenu( SaveGUIStateId, fileMenu, 10, -1 );
 
-  // createMenu( FileLoadId,   fileMenu, 0 );
+  createMenu( ConnectId,    fileMenu, 5 );
+  createMenu( DisconnectId, fileMenu, 5 );
+  createMenu( separator(),  fileMenu, -1, 5 );
 
 #ifdef WITH_SIMANIO
   if (myIsSiman) {
@@ -358,7 +359,6 @@ void SalomeApp_Application::createActions()
   }
 #endif
   createMenu( DumpStudyId, fileMenu, 10, -1 );
-  createMenu( separator(), fileMenu, -1, 10, -1 );
   createMenu( LoadScriptId, fileMenu, 10, -1 );
   createMenu( separator(), fileMenu, -1, 10, -1 );
   createMenu( PropertiesId, fileMenu, 10, -1 );
@@ -387,18 +387,6 @@ void SalomeApp_Application::createActions()
 
 }
 
-
-/*!Set desktop:*/
-void SalomeApp_Application::setDesktop( SUIT_Desktop* desk )
-{
-  LightApp_Application::setDesktop( desk );
-
-  if ( desk ) {
-    connect( desk, SIGNAL( message( const QString& ) ),
-             this, SLOT( onLoadDocMessage( const QString& ) ), Qt::UniqueConnection );
-  }
-}
-
 /*!
   \brief Close application.
 */
@@ -477,6 +465,31 @@ void SalomeApp_Application::onLoadDoc()
   }
 }
 
+/*!SLOT. Unload document.*/
+void SalomeApp_Application::onUnloadDoc( bool ask )
+{
+  if ( ask ) {
+    activeStudy()->abortAllOperations();
+    if ( activeStudy()->isModified() ) {
+      QString docName = activeStudy()->studyName().trimmed();
+      int answer = SUIT_MessageBox::question( desktop(), tr( "DISCONNECT_CAPTION" ),
+                                           tr( "DISCONNECT_DESCRIPTION" ),
+                                           tr( "DISCONNECT_SAVE" ), 
+                                           tr( "DISCONNECT_WO_SAVE" ),
+                                           tr( "APPCLOSE_CANCEL" ), 0 );
+      if ( answer == 0 ) { // save before unload
+       if ( activeStudy()->isSaved() )
+         onSaveDoc();
+       else if ( !onSaveAsDoc() )
+         return;
+      }
+      else if ( answer == 2 ) // Cancel
+       return;
+    }
+  }
+  closeActiveDoc( false );
+}
+
 /*!SLOT. Create new study and load script*/
 void SalomeApp_Application::onNewWithScript()
 {
@@ -543,18 +556,36 @@ bool SalomeApp_Application::onLoadDoc( const QString& aName )
   return res;
 }
 
-/*!SLOT. Load document with a name, specified in \a aMessage.*/
-void SalomeApp_Application::onLoadDocMessage(const QString& aMessage)
+/*!SLOT. Parse message for desktop.*/
+void SalomeApp_Application::onDesktopMessage( const QString& message )
 {
-  if (aMessage.indexOf("simanCheckoutDone ") == 0) {
+  if (message.indexOf("simanCheckoutDone ") == 0) {
 #ifdef WITH_SIMANIO
-    onLoadDoc(aMessage.section(' ', 1));
+    // Load document with a name, specified in aMessage.
+    onLoadDoc(message.section(' ', 1));
 #else
     printf( "****************************************************************\n" );
     printf( "*    Warning: SALOME is built without SIMAN support.\n" );
     printf( "****************************************************************\n" );
 #endif
   }
+  else if (message.indexOf("studyCreated:") == 0) {
+    // Enable 'Connect' action
+    updateCommandsStatus();
+  }
+  else if (message.indexOf("studyClosed:") == 0) {
+    /* message also contains ID of the closed study,
+       but as soon as SALOME is mono-study application for the moment,
+       this ID is not needed now.*/
+    //long aStudyId = message.section(':', 1).toLong();
+    // Disconnect GUI from active study, because it was closed on DS side.
+    closeActiveDoc( false );
+    // Disable 'Connect' action
+    QAction* a = action( ConnectId );
+    if ( a )
+      a->setEnabled( false );
+  }
+  LightApp_Application::onDesktopMessage( message );
 }
 
 /*!SLOT. Copy objects to study maneger from selection maneger..*/
@@ -641,7 +672,6 @@ void SalomeApp_Application::onCloseDoc( bool ask )
 
     }
   }
-
   LightApp_Application::onCloseDoc( ask );
 }
 
@@ -760,8 +790,8 @@ void SalomeApp_Application::updateCommandsStatus()
 
   // Load script menu
   a = action( LoadScriptId );
-  if ( a )
-    a->setEnabled( activeStudy() );
+  if( a )
+    a->setEnabled( pythonConsole() );
 
   // Properties menu
   a = action( PropertiesId );
@@ -773,6 +803,16 @@ void SalomeApp_Application::updateCommandsStatus()
   if( a )
     a->setEnabled( activeStudy() );
 
+  // Connect study menu
+  a = action( ConnectId );
+  if( a )
+    a->setEnabled( !activeStudy() && studyMgr()->GetOpenStudies().size() > 0 );
+
+  // Disconnect study menu
+  a = action( DisconnectId );
+  if( a )
+    a->setEnabled( activeStudy() );
+
   // update state of Copy/Paste menu items
   onSelectionChanged();
 }
@@ -894,14 +934,14 @@ void SalomeApp_Application::onDumpStudy( )
 void SalomeApp_Application::onLoadScript( )
 {
   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( activeStudy() );
-  if ( !appStudy ) return;
-  _PTR(Study) aStudy = appStudy->studyDS();
-
-  if ( aStudy->GetProperties()->IsLocked() ) {
-    SUIT_MessageBox::warning( desktop(),
-                              QObject::tr("WRN_WARNING"),
-                              QObject::tr("WRN_STUDY_LOCKED") );
-    return;
+  if ( appStudy ) {
+    _PTR(Study) aStudy = appStudy->studyDS();
+    if ( aStudy->GetProperties()->IsLocked() ) {
+      SUIT_MessageBox::warning( desktop(),
+                                QObject::tr("WRN_WARNING"),
+                                QObject::tr("WRN_STUDY_LOCKED") );
+      return;
+    }
   }
 
   QStringList filtersList;
@@ -1268,8 +1308,7 @@ QMap<int, QString> SalomeApp_Application::activateModuleActions() const
 {
   QMap<int, QString> opmap = LightApp_Application::activateModuleActions();
 
-  // rnv commented : implementation of the mono-study in GUI
-  // opmap.insert( LoadStudyId,     tr( "ACTIVATE_MODULE_OP_LOAD" ) );
+  opmap.insert( LoadStudyId,     tr( "ACTIVATE_MODULE_OP_LOAD" ) );
 
   opmap.insert( NewAndScriptId,  tr( "ACTIVATE_MODULE_OP_SCRIPT" ) );
   return opmap;
@@ -2050,42 +2089,15 @@ void SalomeApp_Application::afterCloseDoc()
 /*
   Asks to close existing document.
 */
-bool SalomeApp_Application::checkExistingDoc() {
-  bool result = true;
-  if( activeStudy() ) {
-    int answer = SUIT_MessageBox::question( desktop(), 
-                                           tr( "APPCLOSE_CAPTION" ), 
-                                           tr( "STUDYCLOSE_DESCRIPTION" ),
-                                           tr( "APPCLOSE_SAVE" ), 
-                                           tr( "APPCLOSE_CLOSE" ),
-                                           tr( "APPCLOSE_CANCEL" ), 0 );
-    if(answer == 0) {
-      if ( activeStudy()->isSaved() ) {
-       onSaveDoc();
-       closeDoc( false );
-      } else if ( onSaveAsDoc() ) {
-       if( !closeDoc( false ) ) {
-         result = false;
-       }
-      } else {
-       result = false;
-      }        
-    }
-    else if( answer == 1 ) {
-      closeDoc( false );
-    } else if( answer == 2 ) {
-      result = false;
-    }
-  } else {
+bool SalomeApp_Application::checkExistingDoc()
+{
+  bool result = LightApp_Application::checkExistingDoc();
+  if ( result && !activeStudy() ) {
     SALOMEDSClient_StudyManager* aMgr = studyMgr();
-    if( aMgr ) {
+    if ( aMgr ) {
       std::vector<std::string> List = studyMgr()->GetOpenStudies();
       if( List.size() > 0 ) {
-        int answer = SUIT_MessageBox::question( desktop(), tr( "WRN_WARNING" ), tr( "QUE_ACTIVEDOC_LOAD" ),
-                                                SUIT_MessageBox::Yes | SUIT_MessageBox::No, SUIT_MessageBox::No );
-        if ( answer == SUIT_MessageBox::Yes ) {
-         onLoadDoc();
-       }
+        SUIT_MessageBox::critical( desktop(), tr( "WRN_WARNING" ), tr( "ERR_ACTIVEDOC_LOAD" ));
        result = false;
       }
     }