Salome HOME
no message
[modules/gui.git] / src / STD / STD_Application.cxx
index 73bd2eff84c1b8c8f60bf865714207e2e519eaca..681e7bc070ae5a3070e9887f4eea7027479e41da 100755 (executable)
@@ -2,6 +2,8 @@
 
 #include "STD_MDIDesktop.h"
 
+#include "STD_CloseDlg.h"
+
 #include <SUIT_Tools.h>
 #include <SUIT_Desktop.h>
 #include <SUIT_Session.h>
 #include <qfiledialog.h>
 #include <qapplication.h>
 
+#include <iostream>
+
+/*!Create and return new instance of STD_Application*/
 extern "C" STD_EXPORT SUIT_Application* createApplication()
 {
   return new STD_Application();
 }
 
+/*!Constructor.*/
 STD_Application::STD_Application()
 : SUIT_Application(),
 myEditEnabled( true ),
@@ -40,15 +46,18 @@ myActiveViewMgr( 0 )
   setDesktop( desk );
 }
 
+/*!Destructor.*/
 STD_Application::~STD_Application()
 {
 }
 
+/*! \retval QString "StdApplication"*/
 QString STD_Application::applicationName() const
 {
   return QString( "StdApplication" );
 }
 
+/*!Start STD_Application*/
 void STD_Application::start()
 {
   createActions();
@@ -60,6 +69,7 @@ void STD_Application::start()
   SUIT_Application::start();
 }
 
+/*!Event on closing desktop*/
 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
 {
   if ( !isPossibleToClose() )
@@ -81,6 +91,7 @@ void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
   closeApplication();
 }
 
+/*!Create actions, menus and tools*/
 void STD_Application::createActions()
 {
   SUIT_Desktop* desk = desktop();
@@ -118,11 +129,6 @@ void STD_Application::createActions()
                 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
                 0, desk, false, this, SLOT( onSaveAsDoc() ) );
 
-  createAction( EditCutId, tr( "TOT_DESK_EDIT_CUT" ),
-                resMgr->loadPixmap( "STD", tr( "ICON_EDIT_CUT" ) ),
-                tr( "MEN_DESK_EDIT_CUT" ), tr( "PRP_DESK_EDIT_CUT" ),
-                CTRL+Key_X, desk, false, this, SLOT( onCut() ) );
-
   createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
                 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
@@ -146,6 +152,13 @@ void STD_Application::createActions()
                 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
                 0, desk, false, this, SLOT( onHelpAbout() ) );
 
+  //SRN: BugID IPAL9021, add an action "Load"
+  createAction( FileLoadId, tr( "TOT_DESK_FILE_LOAD" ),
+                resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
+               tr( "MEN_DESK_FILE_LOAD" ), tr( "PRP_DESK_FILE_LOAD" ),
+               CTRL+Key_L, desk, false, this, SLOT( onLoadDoc() ) );      
+  //SRN: BugID IPAL9021: End 
+
   QtxDockAction* da = new QtxDockAction( tr( "TOT_DOCK_WINDOWS" ), tr( "MEN_DOCK_WINDOWS" ), desk );
   registerAction( ViewWindowsId, da );
   da->setAutoPlace( false );
@@ -161,6 +174,7 @@ void STD_Application::createActions()
 
   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 );
@@ -170,7 +184,6 @@ void STD_Application::createActions()
   createMenu( separator(), fileMenu );
   createMenu( FileExitId, fileMenu );
 
-  createMenu( EditCutId, editMenu );
   createMenu( EditCopyId, editMenu );
   createMenu( EditPasteId, editMenu );
   createMenu( separator(), editMenu );
@@ -193,14 +206,11 @@ void STD_Application::createActions()
   createTool( FileSaveId, stdTBar );
   createTool( FileCloseId, stdTBar );
   createTool( separator(), stdTBar );
-  createTool( EditCutId, stdTBar );
   createTool( EditCopyId, stdTBar );
   createTool( EditPasteId, stdTBar );
 }
 
-/*!
-  Opens new application
-*/
+/*!Opens new application*/
 void STD_Application::onNewDoc() 
 {
   if ( !activeStudy() )
@@ -222,20 +232,25 @@ void STD_Application::onNewDoc()
   }
 }
 
+/*!Put file name from file dialog to onOpenDoc(const QString&) function*/
 void STD_Application::onOpenDoc()
 {
   // It is preferrable to use OS-specific file dialog box here !!!
-  QString aName = QFileDialog::getOpenFileName( QString::null, getFileFilter(), desktop() );
+  QString aName = getFileName( true, QString::null, getFileFilter(), QString::null, 0 );
   if ( aName.isNull() )
     return;
 
+  onOpenDoc( aName );
+}
+
+/*! \retval true, if document was opened successful, else false.*/
+bool STD_Application::onOpenDoc( const QString& aName )
+{
+  bool res = true;
   if ( !activeStudy() )
   {
     // if no study - open in current desktop
-    createEmptyStudy();
-    activeStudy()->openDocument( aName );
-    updateDesktopTitle();
-    updateCommandsStatus();
+    res = useFile( aName );
   }
   else
   {
@@ -254,21 +269,64 @@ void STD_Application::onOpenDoc()
     {
       aApp = startApplication( 0, 0 );
       if ( aApp )
-        aApp->useFile( aName );
+        res = aApp->useFile( aName );
     }
     else
       aApp->desktop()->setActiveWindow();
   }
+  return res;
 }
 
+/*! called on loading the existent study */
+void STD_Application::onLoadDoc()
+{
+}
+
+/*! \retval true, if document was loaded successful, else false.*/
+bool STD_Application::onLoadDoc( const QString& aName )
+{
+  bool res = true;
+  if ( !activeStudy() )
+  {
+    // if no study - load in current desktop
+    res = useStudy( aName );
+  }
+  else
+  {
+    // if study exists - load in new desktop. Check: is the same file is loaded?
+    SUIT_Session* aSession = SUIT_Session::session();
+    QPtrList<SUIT_Application> aAppList = aSession->applications();
+    bool isAlreadyOpen = false;
+    SUIT_Application* aApp = 0;
+    for ( QPtrListIterator<SUIT_Application> it( aAppList ); it.current() && !isAlreadyOpen; ++it )
+    {
+      aApp = it.current();
+      if ( aApp->activeStudy()->studyName() == aName )
+        isAlreadyOpen = true;
+    }
+    if ( !isAlreadyOpen )
+    {
+      aApp = startApplication( 0, 0 );
+      if ( aApp )
+        res = aApp->useStudy( aName );
+    }
+    else
+      aApp->desktop()->setActiveWindow();
+  }
+  return res;
+}
+
+/*!Virtual function. Not implemented here.*/
 void STD_Application::beforeCloseDoc( SUIT_Study* )
 {
 }
 
+/*!Virtual function. Not implemented here.*/
 void STD_Application::afterCloseDoc()
 {
 }
 
+/*!Close document, if it's possible.*/
 void STD_Application::onCloseDoc()
 {
   if ( !isPossibleToClose() )
@@ -279,7 +337,7 @@ void STD_Application::onCloseDoc()
   beforeCloseDoc( study );
 
   if ( study )
-    study->closeDocument();
+    study->closeDocument(myClosePermanently);
 
   clearViewManagers();
 
@@ -307,8 +365,12 @@ void STD_Application::onCloseDoc()
     closeApplication();
 }
 
+/*!Check the application on closing.
+ * \retval true if possible, else false
+ */
 bool STD_Application::isPossibleToClose()
 {
+  myClosePermanently = true; //SRN: BugID: IPAL9021
   if ( activeStudy() )
   {
     activeStudy()->abortAllOperations();
@@ -316,28 +378,33 @@ bool STD_Application::isPossibleToClose()
     {
       QString sName = activeStudy()->studyName().stripWhiteSpace();
       QString msg = sName.isEmpty() ? tr( "INF_DOC_MODIFIED" ) : tr ( "INF_DOCUMENT_MODIFIED" ).arg( sName );
-      int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TOT_DESK_FILE_CLOSE" ), msg,
-                                            tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
-      switch ( aAnswer )
+
+      //SRN: BugID: IPAL9021: Begin
+      STD_CloseDlg dlg(desktop());
+      switch( dlg.exec() )
       {
       case 1:
-        if ( !activeStudy()->isSaved() )
-          if ( !onSaveAsDoc() )
-            return false;
-        else
+        if ( activeStudy()->isSaved() )
           onSaveDoc();
+        else if ( !onSaveAsDoc() )
+          return false;
         break;
       case 2:
         break;
       case 3:
+       myClosePermanently = false;
+        break;
+      case 4:
       default:
         return false;
       }
+     //SRN: BugID: IPAL9021: End 
     }
   }
   return true;
 }
 
+/*!Save document if all ok, else error message.*/
 void STD_Application::onSaveDoc()
 {
   if ( !activeStudy() )
@@ -358,84 +425,42 @@ void STD_Application::onSaveDoc()
     onSaveAsDoc();
 }
 
+/*! \retval TRUE, if doument saved successful, else FALSE.*/
 bool STD_Application::onSaveAsDoc()
 {
   SUIT_Study* study = activeStudy();
   if ( !study )
     return false;
 
-  QString aName;
-  QString aUsedFilter;
-  QString anOldPath = study->studyName();
-
-  bool isOk = false;
-  while ( !isOk )
-  {
-    // It is preferrable to use OS-specific file dialog box here !!!
-    aName = QFileDialog::getSaveFileName( anOldPath, getFileFilter(), desktop(),
-                                          0, QString::null, &aUsedFilter);
-
-    if ( aName.isNull() )
-      isOk = true;
-    else
-    {
-      if ( !getFileFilter().isNull() ) // check exstension and add if it is necessary
-      {
-       int aStart = aUsedFilter.find( '*' );
-       int aEnd = aUsedFilter.find( ')', aStart + 1 );
-       QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
-       if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
-       {
-         // Check that there is an extension at the end of the name
-         QString aNameTail = aName.right( aExt.length() );
-          if ( aNameTail != aExt )
-            aName += aExt;
-       }
-      }
-      if ( QFileInfo( aName ).exists() )
-      {
-       int aAnswer = SUIT_MessageBox::warn2( desktop(), tr( "TIT_FILE_SAVEAS" ),
-                                             tr( "MSG_FILE_EXISTS" ).arg( aName ),
-                                             tr( "BUT_YES" ), tr( "BUT_NO" ), 1, 2, 2 );
-       if ( aAnswer != 2 )
-         isOk = true;
-       else
-         anOldPath = aName; // Not to return to the same initial dir at each "while" step
-      }
-      else
-       isOk = true;
-
-      if ( isOk )
-       isOk = study->saveDocumentAs( aName );
-    }
-  }
+  QString aName = getFileName( false, study->studyName(), getFileFilter(), QString::null, 0 );
 
   if ( aName.isNull() ) 
     return false;
+  bool isOk = study->saveDocumentAs( aName );
 
   updateDesktopTitle();
   updateCommandsStatus();
 
-  return true;
+  return isOk;
 }
 
+/*!Closing session.*/
 void STD_Application::onExit()
 {
   SUIT_Session::session()->closeSession();
 }
 
-void STD_Application::onCut()
-{
-}
-
+/*!Virtual slot. Not implemented here.*/
 void STD_Application::onCopy()
 {
 }
 
+/*!Virtual slot. Not implemented here.*/
 void STD_Application::onPaste()
 {
 }
 
+/*!Sets \a theEnable for menu manager and for tool manager.*/
 void STD_Application::setEditEnabled( bool theEnable )
 {
   myEditEnabled = theEnable;
@@ -443,20 +468,23 @@ void STD_Application::setEditEnabled( bool theEnable )
   QtxActionMenuMgr* mMgr = desktop()->menuMgr();
   QtxActionToolMgr* tMgr = desktop()->toolMgr();
 
-  for ( int i = EditCutId; i <= EditPasteId; i++ )
+  for ( int i = EditCopyId; i <= EditPasteId; i++ )
   {
     mMgr->setShown( i, myEditEnabled );
     tMgr->setShown( i, myEditEnabled );
   }
 }
 
-void STD_Application::useFile(const QString& theFileName)
+/*!\retval true, if document opened successful, else false.*/
+bool STD_Application::useFile(const QString& theFileName)
 {
-  SUIT_Application::useFile(theFileName);
+  bool res = SUIT_Application::useFile(theFileName);
   updateDesktopTitle();
   updateCommandsStatus();
+  return res;
 }
 
+/*!Update desktop title.*/
 void STD_Application::updateDesktopTitle()
 {
   QString aTitle = applicationName();
@@ -474,6 +502,7 @@ void STD_Application::updateDesktopTitle()
   desktop()->setCaption( aTitle );
 }
 
+/*!Update commands status.*/
 void STD_Application::updateCommandsStatus()
 {
   bool aHasStudy = activeStudy() != 0;
@@ -491,6 +520,7 @@ void STD_Application::updateCommandsStatus()
     action( NewWindowId )->setEnabled( aHasStudy );
 }
 
+/*!\retval SUIT_ViewManager by viewer manager type name.*/
 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
 {
   SUIT_ViewManager* vm = 0;
@@ -502,6 +532,9 @@ SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
   return vm;
 }
 
+/*! \param vmType - input view manager type name
+ * \param lst - output list of view managers with types \a vmType.
+ */
 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
 {
   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
@@ -509,12 +542,14 @@ void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst
       lst.append( it.current() );
 }
 
+/*!\param lst - output list of all view managers.*/
 void STD_Application::viewManagers( ViewManagerList& lst ) const
 {
   for ( QPtrListIterator<SUIT_ViewManager> it( myViewMgrs ); it.current(); ++it )
     lst.append( it.current() );
 }
 
+/*!\retval ViewManagerList - const list of all view managers.*/
 ViewManagerList STD_Application::viewManagers() const
 {
   ViewManagerList lst;
@@ -522,11 +557,13 @@ ViewManagerList STD_Application::viewManagers() const
   return lst;
 }
 
+/*!\retval SUIT_ViewManager - return pointer to active view manager.*/
 SUIT_ViewManager* STD_Application::activeViewManager() const
 {
   return myActiveViewMgr;
 }
 
+/*!Add view manager to view managers list, if it not already there.*/
 void STD_Application::addViewManager( SUIT_ViewManager* vm )
 {
   if ( !vm )
@@ -547,6 +584,7 @@ void STD_Application::addViewManager( SUIT_ViewManager* vm )
 */
 }
 
+/*!Remove view manager from view managers list.*/
 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
 {
   if ( !vm )
@@ -564,6 +602,7 @@ void STD_Application::removeViewManager( SUIT_ViewManager* vm )
     myActiveViewMgr = 0;
 }
 
+/*!Remove all view managers from view managers list.*/
 void STD_Application::clearViewManagers()
 {
   ViewManagerList lst;
@@ -573,16 +612,19 @@ void STD_Application::clearViewManagers()
     removeViewManager( it.current() );
 }
 
+/*!\retval TRUE, if view manager \a vm, already in view manager list (\a myViewMgrs).*/
 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
 {
   return myViewMgrs.contains( vm ) > 0;
 }
 
+/*!Private slot, sets active manager to \vm, if \vm in view managers list.*/
 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
 {
   setActiveViewManager( vm );
 }
 
+/*!Sets status bar show, if \on = true, else status bar hide.*/
 void STD_Application::onViewStatusBar( bool on )
 {
   if ( on )
@@ -591,11 +633,15 @@ void STD_Application::onViewStatusBar( bool on )
     desktop()->statusBar()->hide();
 }
 
+/*!Call SUIT_MessageBox::info1(...) with about information.*/
 void STD_Application::onHelpAbout()
 {
   SUIT_MessageBox::info1( desktop(), tr( "About" ), tr( "ABOUT_INFO" ), "&OK" );
 }
 
+/*!Create empty study. \n
+ * Create new view manager and adding it to view managers list.
+ */
 void STD_Application::createEmptyStudy()
 {
   SUIT_Application::createEmptyStudy();
@@ -605,6 +651,7 @@ void STD_Application::createEmptyStudy()
   addViewManager( vm );
 }
 
+/*!Sets active manager to \vm, if \vm in view managers list.*/
 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
 {
   if ( !containsViewManager( vm ) )
@@ -614,6 +661,7 @@ void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
   emit viewManagerActivated( vm );
 }
 
+/*!Public slot. */
 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
 {
   QtxPopupMenu* popup = new QtxPopupMenu();
@@ -632,3 +680,84 @@ void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextM
     popup->exec( e->globalPos() );
   delete popup;
 }
+
+/*!\retval QString - return file name from dialog.*/
+QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters, 
+                                     const QString& caption, QWidget* parent )
+{
+  if ( !parent )
+    parent = desktop();
+  if ( open ) 
+  {
+    return QFileDialog::getOpenFileName( initial, filters, parent, 0, caption );
+  }
+  else
+  {
+    QString aName;
+    QString aUsedFilter;
+    QString anOldPath = initial;
+
+    bool isOk = false;
+    while ( !isOk )
+    {
+      // It is preferrable to use OS-specific file dialog box here !!!
+      aName = QFileDialog::getSaveFileName( anOldPath, filters, parent,
+                                           0, caption, &aUsedFilter);
+
+      if ( aName.isNull() )
+        isOk = true;
+      else
+      {
+        if ( !getFileFilter().isNull() ) // check extension and add if it is necessary
+        {
+         int aStart = aUsedFilter.find( '*' );
+         int aEnd = aUsedFilter.find( ')', aStart + 1 );
+         QString aExt = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
+         if ( aExt.contains( '*' ) == 0 ) // if it is not *.*
+         {
+           // Check that there is an extension at the end of the name
+           QString aNameTail = aName.right( aExt.length() );
+           if ( aNameTail != aExt )
+              aName += aExt;
+         }
+        }
+       if ( QFileInfo( aName ).exists() )
+        {
+         int aAnswer = SUIT_MessageBox::warn3( desktop(), tr( "TIT_FILE_SAVEAS" ),
+                                               tr( "MSG_FILE_EXISTS" ).arg( aName ),
+                                               tr( "BUT_YES" ), tr( "BUT_NO" ), tr( "BUT_CANCEL" ), 1, 2, 3, 1 );
+         if ( aAnswer == 3 ) {     // cancelled
+            aName = QString::null;
+           isOk = true;
+          }
+         else if ( aAnswer == 2 ) // not save to this file
+           anOldPath = aName;             // not to return to the same initial dir at each "while" step
+         else                     // overwrite the existing file
+           isOk = true;
+        }
+       else
+         isOk = true;
+      }
+    }
+    return aName;
+  }
+}
+
+/*!\retval QString - return directory name from dialog.*/
+QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
+{
+  if ( !parent )
+    parent = desktop();
+  return QFileDialog::getExistingDirectory( initial, parent, 0, caption, true );
+}
+
+void STD_Application::setDesktop( SUIT_Desktop* desk )
+{
+  SUIT_Desktop* prev = desktop();
+
+  SUIT_Application::setDesktop( desk );
+
+  if ( prev != desk && desk )
+    connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
+             this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ) );
+}