]> SALOME platform Git repositories - modules/gui.git/blobdiff - src/STD/STD_Application.cxx
Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / STD / STD_Application.cxx
index 20b8f0837d9942d58cd2725e3a3c1b20bd782789..7c0f33498feb0c5c27a22b8d485fdb70a8f96300 100755 (executable)
@@ -1,3 +1,21 @@
+// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
+// 
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either 
+// version 2.1 of the License.
+// 
+// This library is distributed in the hope that it will be useful 
+// but WITHOUT ANY WARRANTY; without even the implied warranty of 
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public  
+// License along with this library; if not, write to the Free Software 
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/
+//
 #include "STD_Application.h"
 
 #include "STD_MDIDesktop.h"
@@ -63,13 +81,37 @@ void STD_Application::start()
   updateCommandsStatus();
   setEditEnabled( myEditEnabled );
 
+  loadPreferences();
+
   SUIT_Application::start();
 }
 
+/*!
+  Close the Application
+*/
+void STD_Application::closeApplication()
+{
+  if ( desktop() )
+    savePreferences();
+  SUIT_Study* study = activeStudy();
+
+  if ( study ){
+    study->closeDocument();
+
+    setActiveStudy( 0 );
+    delete study;
+  }
+
+  setDesktop( 0 );
+  
+  SUIT_Application::closeApplication();
+}
+
 /*!Event on closing desktop*/
 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
 {
-  if (SUIT_Session::session()->applications().count() < 2) {
+  if ( SUIT_Session::session()->applications().count() < 2 )
+  {
     onExit();
     return;
   }
@@ -80,16 +122,6 @@ void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
     return;
   }
 
-  SUIT_Study* study = activeStudy();
-
-  if ( study )
-    study->closeDocument();
-
-  setActiveStudy( 0 );
-  delete study;
-
-  setDesktop( 0 );
-
   closeApplication();
 }
 
@@ -129,7 +161,7 @@ void STD_Application::createActions()
 
   createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIconSet(),
                 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
-                0, desk, false, this, SLOT( onSaveAsDoc() ) );
+                CTRL+Key_A, desk, false, this, SLOT( onSaveAsDoc() ) );
 
   createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
@@ -143,7 +175,7 @@ void STD_Application::createActions()
 
   QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
                              QIconSet(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
-                             tr( "PRP_DESK_VIEW_STATUSBAR" ), 0, desk, true );
+                             tr( "PRP_DESK_VIEW_STATUSBAR" ), SHIFT+Key_S, desk, true );
   a->setOn( desk->statusBar()->isVisibleTo( desk ) );
   connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
 
@@ -152,7 +184,7 @@ void STD_Application::createActions()
 
   createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIconSet(),
                 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
-                0, desk, false, this, SLOT( onHelpAbout() ) );
+                SHIFT+Key_A, desk, false, this, SLOT( onHelpAbout() ) );
 
   //SRN: BugID IPAL9021, add an action "Load"
   createAction( FileLoadId, tr( "TOT_DESK_FILE_LOAD" ),
@@ -167,32 +199,32 @@ void STD_Application::createActions()
 
   // Create menus
 
-  int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, -1, 0 );
-  int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, -1, 10 );
-  int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, -1, 10 );
-  int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, -1, 1000 );
+  int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, MenuFileId, 0 );
+  int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, MenuEditId, 10 );
+  int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, MenuViewId, 10 );
+  int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, MenuHelpId, 1000 );
 
   // Create menu items
 
-  createMenu( FileNewId, fileMenu, 0 );
-  createMenu( FileOpenId, fileMenu, 0 );
-  createMenu( FileLoadId, fileMenu, 0 );  //SRN: BugID IPAL9021, add a menu item "Load"
-  createMenu( FileCloseId, fileMenu, 0 );
-  createMenu( separator(), fileMenu, -1, 0 );
-  createMenu( FileSaveId, fileMenu, 0 );
+  createMenu( FileNewId,    fileMenu, 0 );
+  createMenu( FileOpenId,   fileMenu, 0 );
+  createMenu( FileLoadId,   fileMenu, 0 );  //SRN: BugID IPAL9021, add a menu item "Load"
+  createMenu( FileCloseId,  fileMenu, 0 );
+  createMenu( separator(),  fileMenu, -1, 0 );
+  createMenu( FileSaveId,   fileMenu, 0 );
   createMenu( FileSaveAsId, fileMenu, 0 );
-  createMenu( separator(), fileMenu, -1, 0 );
+  createMenu( separator(),  fileMenu, -1, 0 );
 
-  createMenu( separator(), fileMenu );
-  createMenu( FileExitId, fileMenu );
+  createMenu( separator(),  fileMenu );
+  createMenu( FileExitId,   fileMenu );
 
-  createMenu( EditCopyId, editMenu );
+  createMenu( EditCopyId,  editMenu );
   createMenu( EditPasteId, editMenu );
   createMenu( separator(), editMenu );
 
-  createMenu( ViewWindowsId, viewMenu );
+  createMenu( ViewWindowsId,   viewMenu );
   createMenu( ViewStatusBarId, viewMenu );
-  createMenu( separator(), viewMenu );
+  createMenu( separator(),     viewMenu );
 
   createMenu( HelpAboutId, helpMenu );
   createMenu( separator(), helpMenu );
@@ -215,23 +247,27 @@ void STD_Application::createActions()
 /*!Opens new application*/
 void STD_Application::onNewDoc()
 {
+  QApplication::setOverrideCursor( Qt::waitCursor );
+
   if ( !activeStudy() )
   {
     createEmptyStudy();
     activeStudy()->createDocument();
-    updateDesktopTitle();
-    updateCommandsStatus();
+    studyCreated( activeStudy() );
   }
   else
   {
     SUIT_Application* aApp = startApplication( 0, 0 );
     if ( aApp->inherits( "STD_Application" ) )
       ((STD_Application*)aApp)->onNewDoc();
-    else {
+    else
+    {
       aApp->createEmptyStudy();
       aApp->activeStudy()->createDocument();
     }
   }
+
+  QApplication::restoreOverrideCursor();
 }
 
 /*!Put file name from file dialog to onOpenDoc(const QString&) function*/
@@ -248,6 +284,8 @@ void STD_Application::onOpenDoc()
 /*! \retval true, if document was opened successful, else false.*/
 bool STD_Application::onOpenDoc( const QString& aName )
 {
+  QApplication::setOverrideCursor( Qt::waitCursor );
+
   bool res = true;
   if ( !activeStudy() )
   {
@@ -272,10 +310,15 @@ bool STD_Application::onOpenDoc( const QString& aName )
       aApp = startApplication( 0, 0 );
       if ( aApp )
         res = aApp->useFile( aName );
+      if ( !res )
+        aApp->closeApplication();
     }
     else
       aApp->desktop()->setActiveWindow();
   }
+
+  QApplication::restoreOverrideCursor();
+
   return res;
 }
 
@@ -329,11 +372,10 @@ void STD_Application::afterCloseDoc()
 }
 
 /*!Close document, if it's possible.*/
-void STD_Application::onCloseDoc(bool ask)
+void STD_Application::onCloseDoc( bool ask )
 {
-  if (ask)
-    if ( !isPossibleToClose() )
-      return;
+  if ( ask && !isPossibleToClose() )
+    return;
 
   SUIT_Study* study = activeStudy();
 
@@ -355,7 +397,10 @@ void STD_Application::onCloseDoc(bool ask)
   // STV: aNbStudies - number of currently existing studies (exclude currently closed)
   // STV: aNbStudies should be compared with 0.
   if ( aNbStudies )
+  {
+    savePreferences();
     setDesktop( 0 );
+  }
   else
   {
     updateDesktopTitle();
@@ -395,7 +440,7 @@ bool STD_Application::isPossibleToClose()
       case 2:
         break;
       case 3:
-       myClosePermanently = false;
+             myClosePermanently = false;
         break;
       case 4:
       default:
@@ -416,20 +461,29 @@ void STD_Application::onSaveDoc()
   bool isOk = false;
   if ( activeStudy()->isSaved() )
   {
-    putInfo(tr("INF_DOC_SAVING") + activeStudy()->studyName());
+    putInfo( tr( "INF_DOC_SAVING" ) + activeStudy()->studyName() );
+
+    QApplication::setOverrideCursor( Qt::waitCursor );
+
     isOk = activeStudy()->saveDocument();
-    if ( !isOk ) {
-      putInfo("");
-      SUIT_MessageBox::error1( desktop(), tr( "TIT_FILE_SAVEAS" ),
-                              tr( "MSG_CANT_SAVE" ).arg( activeStudy()->studyName() ),
-                               tr( "BUT_OK" ) );
-    } else {
-      putInfo(tr("INF_DOC_SAVED").arg(""));
+
+    QApplication::restoreOverrideCursor();
+
+    if ( !isOk )
+    {
+      putInfo( "" );
+      // displaying a message box as SUIT_Validator in case file can't be written (the most frequent case)
+      SUIT_MessageBox::error1( desktop(), 
+                              tr( "ERR_ERROR" ),
+                              tr( "ERR_PERMISSION_DENIED" ).arg( activeStudy()->studyName() ),
+                              tr( "BUT_OK" ) );
     }
+    else
+      putInfo( tr( "INF_DOC_SAVED" ).arg( "" ) );
   }
 
   if ( isOk )
-    updateCommandsStatus();
+    studySaved( activeStudy() );
   else
     onSaveAsDoc();
 }
@@ -441,20 +495,29 @@ bool STD_Application::onSaveAsDoc()
   if ( !study )
     return false;
 
-  QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
-  if ( aName.isNull() )
-    return false;
+  bool isOk = false;
+  while ( !isOk )
+  {
+    QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
+    if ( aName.isNull() )
+      return false;
 
-  putInfo(tr("INF_DOC_SAVING") + aName);
-  bool isOk = study->saveDocumentAs( aName );
+    QApplication::setOverrideCursor( Qt::waitCursor );
 
-  if (isOk)
-    putInfo(tr("INF_DOC_SAVED").arg(aName));
-  else
-    putInfo("");
+    putInfo( tr( "INF_DOC_SAVING" ) + aName );
+    isOk = study->saveDocumentAs( aName );
 
-  updateDesktopTitle();
-  updateCommandsStatus();
+    putInfo( isOk ? tr( "INF_DOC_SAVED" ).arg( aName ) : "" );
+
+    QApplication::restoreOverrideCursor();
+
+    if ( !isOk )
+      SUIT_MessageBox::error1( desktop(), tr( "ERROR" ),
+                              tr( "INF_DOC_SAVING_FAILS" ).arg( aName ), 
+                              tr( "BUT_OK" ) );
+  }
+
+  studySaved( activeStudy() );
 
   return isOk;
 }
@@ -462,9 +525,9 @@ bool STD_Application::onSaveAsDoc()
 /*!Closing session.*/
 void STD_Application::onExit()
 {
-  int aAnswer = SUIT_MessageBox::info2(desktop(), tr("INF_DESK_EXIT"), tr("QUE_DESK_EXIT"),
-                                       tr ("BUT_OK"), tr ("BUT_CANCEL"), 1, 2, 2);
-  if (aAnswer == 1)
+  int aAnswer = SUIT_MessageBox::info2( desktop(), tr( "INF_DESK_EXIT" ), tr( "QUE_DESK_EXIT" ),
+                                        tr( "BUT_OK" ), tr( "BUT_CANCEL" ), 1, 2, 2 );
+  if ( aAnswer == 1 )
     SUIT_Session::session()->closeSession();
 }
 
@@ -488,17 +551,19 @@ void STD_Application::setEditEnabled( bool theEnable )
 
   for ( int i = EditCopyId; i <= EditPasteId; i++ )
   {
-    mMgr->setShown( i, myEditEnabled );
-    tMgr->setShown( i, myEditEnabled );
+    mMgr->setShown( mMgr->actionId(action(i)), myEditEnabled );
+    tMgr->setShown( tMgr->actionId(action(i)), myEditEnabled );
   }
 }
 
 /*!\retval true, if document opened successful, else false.*/
 bool STD_Application::useFile(const QString& theFileName)
 {
-  bool res = SUIT_Application::useFile(theFileName);
-  updateDesktopTitle();
-  updateCommandsStatus();
+  bool res = SUIT_Application::useFile( theFileName );
+
+  if ( res )
+    studyOpened( activeStudy() );
+
   return res;
 }
 
@@ -774,6 +839,10 @@ QString STD_Application::getDirectory( const QString& initial, const QString& ca
   return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );
 }
 
+/*!
+  Changes desktop
+  \param desk - new desktop
+*/
 void STD_Application::setDesktop( SUIT_Desktop* desk )
 {
   SUIT_Desktop* prev = desktop();
@@ -784,3 +853,47 @@ void STD_Application::setDesktop( SUIT_Desktop* desk )
     connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
              this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
 }
+
+/*!
+  Allow to load preferences before the desktop will be shown.
+*/
+void STD_Application::loadPreferences()
+{
+}
+
+/*!
+  Allow to save preferences before the application will be closed.
+*/
+void STD_Application::savePreferences()
+{
+}
+
+/*!
+  Custom activity after study is created
+  Updates desktop and actions
+*/
+void STD_Application::studyCreated( SUIT_Study* )
+{
+  updateDesktopTitle();
+  updateCommandsStatus();
+}
+
+/*!
+  Custom activity after study is opened
+  Updates desktop and actions
+*/
+void STD_Application::studyOpened( SUIT_Study* )
+{
+  updateDesktopTitle();
+  updateCommandsStatus();
+}
+
+/*!
+  Custom activity after study is opened
+  Updates desktop and actions
+*/
+void STD_Application::studySaved( SUIT_Study* )
+{
+  updateDesktopTitle();
+  updateCommandsStatus();
+}