Back to index

salome-gui  6.5.0
STD_Application.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "STD_Application.h"
00024 
00025 #include "STD_MDIDesktop.h"
00026 
00027 #include <SUIT_Tools.h>
00028 #include <SUIT_Study.h>
00029 #include <SUIT_Desktop.h>
00030 #include <SUIT_Session.h>
00031 #include <SUIT_MessageBox.h>
00032 #include <SUIT_ViewManager.h>
00033 #include <SUIT_ResourceMgr.h>
00034 
00035 #include <QtxDockAction.h>
00036 #include <QtxMenu.h>
00037 #include <QtxActionMenuMgr.h>
00038 #include <QtxActionToolMgr.h>
00039 
00040 #include <QMenu>
00041 #include <QStatusBar>
00042 #include <QCloseEvent>
00043 #include <QFileDialog>
00044 #include <QApplication>
00045 
00047 extern "C" STD_EXPORT SUIT_Application* createApplication()
00048 {
00049   return new STD_Application();
00050 }
00051 
00053 STD_Application::STD_Application()
00054 : SUIT_Application(),
00055   myActiveViewMgr( 0 ),
00056   myExitConfirm( true ),
00057   myEditEnabled( true )
00058 {
00059   setDesktop( new STD_MDIDesktop() );
00060 }
00061 
00063 STD_Application::~STD_Application()
00064 {
00065   clearViewManagers();
00066 }
00067 
00069 bool STD_Application::exitConfirmation() const
00070 {
00071   return myExitConfirm;
00072 }
00073 
00075 void STD_Application::setExitConfirmation( const bool on )
00076 {
00077   myExitConfirm = on;
00078 }
00079 
00081 QString STD_Application::applicationName() const
00082 {
00083   return QString( "StdApplication" );
00084 }
00085 
00087 void STD_Application::start()
00088 {
00089   createActions();
00090 
00091   updateDesktopTitle();
00092   updateCommandsStatus();
00093   setEditEnabled( myEditEnabled );
00094 
00095   loadPreferences();
00096 
00097   SUIT_Application::start();
00098 }
00099 
00103 void STD_Application::closeApplication()
00104 {
00105   if ( desktop() )
00106     savePreferences();
00107   SUIT_Study* study = activeStudy();
00108 
00109   if ( study )
00110   {
00111     beforeCloseDoc( study );
00112 
00113     study->closeDocument();
00114 
00115     setActiveStudy( 0 );
00116     delete study;
00117 
00118     afterCloseDoc();
00119   }
00120 
00121   setDesktop( 0 );
00122 
00123   SUIT_Application::closeApplication();
00124 }
00125 
00127 void STD_Application::onDesktopClosing( SUIT_Desktop*, QCloseEvent* e )
00128 {
00129   if ( SUIT_Session::session()->applications().count() < 2 )
00130   {
00131     onExit();
00132     return;
00133   }
00134 
00135   bool closePermanently;
00136   if ( !isPossibleToClose( closePermanently ) )
00137   {
00138     e->ignore();
00139     return;
00140   }
00141 
00142   closeApplication();
00143 }
00144 
00146 void STD_Application::createActions()
00147 {
00148   SUIT_Desktop* desk = desktop();
00149   SUIT_ResourceMgr* resMgr = resourceMgr();
00150   if ( !desk || !resMgr )
00151     return;
00152 
00153   // Create actions
00154 
00155   createAction( FileNewId, tr( "TOT_DESK_FILE_NEW" ),
00156                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_NEW" ) ),
00157                 tr( "MEN_DESK_FILE_NEW" ), tr( "PRP_DESK_FILE_NEW" ),
00158                 Qt::CTRL+Qt::Key_N, desk, false, this, SLOT( onNewDoc() ) );
00159 
00160   createAction( FileOpenId, tr( "TOT_DESK_FILE_OPEN" ),
00161                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_OPEN" ) ),
00162                 tr( "MEN_DESK_FILE_OPEN" ), tr( "PRP_DESK_FILE_OPEN" ),
00163                 Qt::CTRL+Qt::Key_O, desk, false, this, SLOT( onOpenDoc() ) );
00164 
00165   createAction( FileReopenId, tr( "TOT_DESK_FILE_REOPEN" ), QIcon(),
00166                 tr( "MEN_DESK_FILE_REOPEN" ), tr( "PRP_DESK_FILE_REOPEN" ),
00167                 0, desk, false, this, SLOT( onReopenDoc() ) );
00168 
00169   createAction( FileCloseId, tr( "TOT_DESK_FILE_CLOSE" ),
00170                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_CLOSE" ) ),
00171                 tr( "MEN_DESK_FILE_CLOSE" ), tr( "PRP_DESK_FILE_CLOSE" ),
00172                 Qt::CTRL+Qt::Key_W, desk, false, this, SLOT( onCloseDoc() ) );
00173 
00174   createAction( FileExitId, tr( "TOT_DESK_FILE_EXIT" ), QIcon(),
00175                 tr( "MEN_DESK_FILE_EXIT" ), tr( "PRP_DESK_FILE_EXIT" ),
00176                 Qt::CTRL+Qt::Key_Q, desk, false, this, SLOT( onExit() ) );
00177 
00178   createAction( FileSaveId, tr( "TOT_DESK_FILE_SAVE" ),
00179                 resMgr->loadPixmap( "STD", tr( "ICON_FILE_SAVE" ) ),
00180                 tr( "MEN_DESK_FILE_SAVE" ), tr( "PRP_DESK_FILE_SAVE" ),
00181                 Qt::CTRL+Qt::Key_S, desk, false, this, SLOT( onSaveDoc() ) );
00182 
00183   createAction( FileSaveAsId, tr( "TOT_DESK_FILE_SAVEAS" ), QIcon(),
00184                 tr( "MEN_DESK_FILE_SAVEAS" ), tr( "PRP_DESK_FILE_SAVEAS" ),
00185                 Qt::CTRL+Qt::SHIFT+Qt::Key_S, desk, false, this, SLOT( onSaveAsDoc() ) );
00186 
00187   createAction( EditCopyId, tr( "TOT_DESK_EDIT_COPY" ),
00188                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_COPY" ) ),
00189                 tr( "MEN_DESK_EDIT_COPY" ), tr( "PRP_DESK_EDIT_COPY" ),
00190                 Qt::CTRL+Qt::Key_C, desk, false, this, SLOT( onCopy() ) );
00191 
00192   createAction( EditPasteId, tr( "TOT_DESK_EDIT_PASTE" ),
00193                 resMgr->loadPixmap( "STD", tr( "ICON_EDIT_PASTE" ) ),
00194                 tr( "MEN_DESK_EDIT_PASTE" ), tr( "PRP_DESK_EDIT_PASTE" ),
00195                 Qt::CTRL+Qt::Key_V, desk, false, this, SLOT( onPaste() ) );
00196 
00197   QAction* a = createAction( ViewStatusBarId, tr( "TOT_DESK_VIEW_STATUSBAR" ),
00198                              QIcon(), tr( "MEN_DESK_VIEW_STATUSBAR" ),
00199                              tr( "PRP_DESK_VIEW_STATUSBAR" ), Qt::ALT+Qt::SHIFT+Qt::Key_S, desk, true );
00200   a->setChecked( desk->statusBar()->isVisibleTo( desk ) );
00201   connect( a, SIGNAL( toggled( bool ) ), this, SLOT( onViewStatusBar( bool ) ) );
00202 
00203   createAction( NewWindowId, tr( "TOT_DESK_NEWWINDOW" ), QIcon(),
00204                 tr( "MEN_DESK_NEWWINDOW" ), tr( "PRP_DESK_NEWWINDOW" ), 0, desk  );
00205 
00206   createAction( HelpAboutId, tr( "TOT_DESK_HELP_ABOUT" ), QIcon(),
00207                 tr( "MEN_DESK_HELP_ABOUT" ), tr( "PRP_DESK_HELP_ABOUT" ),
00208                 Qt::ALT+Qt::SHIFT+Qt::Key_A, desk, false, this, SLOT( onHelpAbout() ) );
00209 
00210 
00211   QtxDockAction* dwa = new QtxDockAction( tr( "TOT_DOCKWINDOWS" ), tr( "MEN_DESK_VIEW_DOCKWINDOWS" ), desk );
00212   dwa->setDockType( QtxDockAction::DockWidget );
00213   registerAction( ViewWindowsId, dwa );
00214 
00215   QtxDockAction* tba = new QtxDockAction( tr( "TOT_TOOLBARS" ), tr( "MEN_DESK_VIEW_TOOLBARS" ), desk );
00216   tba->setDockType( QtxDockAction::ToolBar );
00217   registerAction( ViewToolBarsId, tba );
00218 
00219   // Create menus
00220 
00221   int fileMenu = createMenu( tr( "MEN_DESK_FILE" ), -1, MenuFileId, 0 );
00222   int editMenu = createMenu( tr( "MEN_DESK_EDIT" ), -1, MenuEditId, 10 );
00223   int viewMenu = createMenu( tr( "MEN_DESK_VIEW" ), -1, MenuViewId, 10 );
00224   int helpMenu = createMenu( tr( "MEN_DESK_HELP" ), -1, MenuHelpId, 1000 );
00225 
00226   // Create menu items
00227 
00228   createMenu( FileNewId,    fileMenu, 0 );
00229   createMenu( FileOpenId,   fileMenu, 0 );
00230   createMenu( FileReopenId, fileMenu, 0 ); 
00231   createMenu( FileCloseId,  fileMenu, 5 );
00232   createMenu( separator(),  fileMenu, -1, 5 );
00233   createMenu( FileSaveId,   fileMenu, 5 );
00234   createMenu( FileSaveAsId, fileMenu, 5 );
00235   createMenu( separator(),  fileMenu, -1, 5 );
00236 
00237   createMenu( separator(),  fileMenu );
00238   createMenu( FileExitId,   fileMenu );
00239 
00240   createMenu( EditCopyId,  editMenu );
00241   createMenu( EditPasteId, editMenu );
00242   createMenu( separator(), editMenu );
00243 
00244   createMenu( ViewToolBarsId,  viewMenu, 0 );
00245   createMenu( ViewWindowsId,   viewMenu, 0 );
00246   createMenu( separator(),     viewMenu, -1, 10 );
00247   createMenu( ViewStatusBarId, viewMenu, 10 );
00248   createMenu( separator(),     viewMenu );
00249 
00250   createMenu( HelpAboutId, helpMenu );
00251   createMenu( separator(), helpMenu );
00252 
00253   // Create tool bars
00254 
00255   int stdTBar = createTool( tr( "INF_DESK_TOOLBAR_STANDARD" ) );
00256 
00257   // Create tool items
00258 
00259   createTool( FileNewId, stdTBar );
00260   createTool( FileOpenId, stdTBar );
00261   createTool( FileSaveId, stdTBar );
00262   createTool( FileCloseId, stdTBar );
00263   createTool( separator(), stdTBar );
00264   createTool( EditCopyId, stdTBar );
00265   createTool( EditPasteId, stdTBar );
00266 }
00267 
00269 void STD_Application::onNewDoc()
00270 {
00271   onNewDoc( QString() );
00272 }
00273 
00275 bool STD_Application::onNewDoc( const QString& name )
00276 {
00277   QApplication::setOverrideCursor( Qt::WaitCursor );
00278 
00279   bool res = true;
00280   if ( !activeStudy() )
00281   {
00282     createEmptyStudy();
00283     res = activeStudy()->createDocument( name );
00284     if ( res )
00285       studyCreated( activeStudy() );
00286     else
00287     {
00288       SUIT_Study* st = activeStudy();
00289       setActiveStudy( 0 );
00290       delete st;
00291     }
00292   }
00293   else
00294   {
00295     SUIT_Application* aApp = startApplication( 0, 0 );
00296     if ( aApp->inherits( "STD_Application" ) )
00297       res = ((STD_Application*)aApp)->onNewDoc( name );
00298     else
00299     {
00300       aApp->createEmptyStudy();
00301       res = aApp->activeStudy()->createDocument( name );
00302     }
00303     if ( !res )
00304       aApp->closeApplication();
00305   }
00306 
00307   QApplication::restoreOverrideCursor();
00308 
00309   return res;
00310 }
00311 
00313 void STD_Application::onOpenDoc()
00314 {
00315   // It is preferrable to use OS-specific file dialog box here !!!
00316   QString aName = getFileName( true, QString(), getFileFilter(), QString(), 0 );
00317   if ( aName.isNull() )
00318     return;
00319 
00320   onOpenDoc( aName );
00321 }
00322 
00324 bool STD_Application::onOpenDoc( const QString& aName )
00325 {
00326   QApplication::setOverrideCursor( Qt::WaitCursor );
00327 
00328   bool res = openAction( openChoice( aName ), aName );
00329   
00330   QApplication::restoreOverrideCursor();
00331 
00332   return res;
00333 }
00334 
00336 bool STD_Application::onReopenDoc()
00337 {
00338   bool res = false;
00339 
00340   SUIT_Study* study = activeStudy();
00341   if ( study && study->isSaved() ) {
00342     // ask user for the confirmation
00343     if ( SUIT_MessageBox::question( desktop(), tr( "REOPEN_STUDY" ), tr( "REOPEN_QUESTION" ),
00344                                     SUIT_MessageBox::Yes | SUIT_MessageBox::No, SUIT_MessageBox::No
00345                                     ) == SUIT_MessageBox::No )
00346       return false;
00347 
00348     // remember study name
00349     QString studyName = study->studyName();
00350 
00351     // close study
00352     beforeCloseDoc( study );
00353     study->closeDocument( true );
00354 
00355     // update views / windows / status bar / title
00356     clearViewManagers();
00357     setActiveStudy( 0 );
00358     updateDesktopTitle();
00359     updateCommandsStatus();
00360 
00361     // delete study
00362     delete study;
00363     study = 0;
00364     
00365     // post closing actions
00366     afterCloseDoc();
00367 
00368     // reload study from the file
00369     res = useFile( studyName ) && activeStudy();
00370 
00371     // if reloading is failed, close the desktop
00372     if ( !res ) {
00373       setDesktop( 0 );
00374       closeApplication();
00375     }
00376   }
00377   return res;
00378 }
00379 
00381 void STD_Application::beforeCloseDoc( SUIT_Study* )
00382 {
00383 }
00384 
00386 void STD_Application::afterCloseDoc()
00387 {
00388 }
00389 
00391 void STD_Application::onCloseDoc( bool ask )
00392 {
00393   bool closePermanently = true;
00394 
00395   if ( ask && !isPossibleToClose( closePermanently ) )
00396     return;
00397 
00398   SUIT_Study* study = activeStudy();
00399 
00400   beforeCloseDoc( study );
00401 
00402   if ( study )
00403     study->closeDocument( closePermanently );
00404 
00405   clearViewManagers();
00406 
00407   setActiveStudy( 0 );
00408 
00409   int aNbStudies = 0;
00410   QList<SUIT_Application*> apps = SUIT_Session::session()->applications();
00411   for ( int i = 0; i < apps.count(); i++ )
00412     aNbStudies += apps.at( i )->getNbStudies();
00413 
00414   if ( aNbStudies )
00415   {
00416     savePreferences();
00417     setDesktop( 0 );
00418   }
00419   else
00420   {
00421     updateDesktopTitle();
00422     updateCommandsStatus();
00423   }
00424 
00425   // IPAL19532: deleting study should be performed after calling setDesktop(0)
00426   delete study;
00427 
00428   afterCloseDoc();
00429 
00430   if ( !desktop() )
00431     closeApplication();
00432 }
00433 
00437 bool STD_Application::isPossibleToClose( bool& closePermanently )
00438 {
00439   if ( activeStudy() )
00440   {
00441     activeStudy()->abortAllOperations();
00442     if ( activeStudy()->isModified() )
00443     {
00444       QString sName = activeStudy()->studyName().trimmed();
00445       return closeAction( closeChoice( sName ), closePermanently );
00446     }
00447   }
00448   return true;
00449 }
00450 
00451 int STD_Application::closeChoice( const QString& docName )
00452 {
00453   int answer = SUIT_MessageBox::question( desktop(), tr( "CLOSE_STUDY" ), tr( "CLOSE_QUESTION" ).arg( docName ),
00454                                           SUIT_MessageBox::Save | SUIT_MessageBox::Discard | SUIT_MessageBox::Cancel,
00455                                           SUIT_MessageBox::Save );
00456 
00457   int res = CloseCancel;
00458   if ( answer == SUIT_MessageBox::Save )
00459     res = CloseSave;
00460   else if ( answer == SUIT_MessageBox::Discard )
00461     res = CloseDiscard;
00462 
00463   return res;
00464 }
00465 
00466 bool STD_Application::closeAction( const int choice, bool& closePermanently )
00467 {
00468   bool res = true;
00469   switch( choice )
00470   {
00471   case CloseSave:
00472     if ( activeStudy()->isSaved() )
00473       onSaveDoc();
00474     else if ( !onSaveAsDoc() )
00475       res = false;
00476     break;
00477   case CloseDiscard:
00478     break;
00479   case CloseCancel:
00480   default:
00481     res = false;
00482   }
00483 
00484   return res;
00485 }
00486 
00487 int STD_Application::openChoice( const QString& aName )
00488 {
00489   SUIT_Session* aSession = SUIT_Session::session();
00490 
00491   bool isAlreadyOpen = false;
00492   QList<SUIT_Application*> aAppList = aSession->applications();
00493   for ( QList<SUIT_Application*>::iterator it = aAppList.begin(); it != aAppList.end() && !isAlreadyOpen; ++it )
00494     isAlreadyOpen = (*it)->activeStudy() && (*it)->activeStudy()->studyName() == aName;
00495   return isAlreadyOpen ? OpenExist : OpenNew;
00496 }
00497 
00498 bool STD_Application::openAction( const int choice, const QString& aName )
00499 {
00500   bool res = true;
00501   switch ( choice )
00502   {
00503   case OpenExist:
00504     {
00505       SUIT_Application* aApp = 0;
00506       SUIT_Session* aSession = SUIT_Session::session();
00507       QList<SUIT_Application*> aAppList = aSession->applications();
00508       for ( QList<SUIT_Application*>::iterator it = aAppList.begin(); it != aAppList.end() && !aApp; ++it )
00509       {
00510         if ( (*it)->activeStudy() && (*it)->activeStudy()->studyName() == aName )
00511           aApp = *it;
00512       }
00513       if ( aApp )
00514         aApp->desktop()->activateWindow();
00515       else
00516         res = false;
00517     }
00518     break;
00519   case OpenNew:
00520     if ( !activeStudy() )
00521       res = useFile( aName );
00522     else
00523     {
00524       SUIT_Application* aApp = startApplication( 0, 0 );
00525       if ( aApp )
00526         res = aApp->useFile( aName );
00527       if ( !res )
00528         aApp->closeApplication();
00529     }
00530     break;
00531   case OpenCancel:
00532   default:
00533     res = false;
00534   }
00535 
00536   return res;
00537 }
00538 
00540 void STD_Application::onSaveDoc()
00541 {
00542   if ( !activeStudy() )
00543     return;
00544 
00545   bool isOk = false;
00546   if ( activeStudy()->isSaved() )
00547   {
00548     putInfo( tr( "INF_DOC_SAVING" ) + activeStudy()->studyName() );
00549 
00550     QApplication::setOverrideCursor( Qt::WaitCursor );
00551 
00552     isOk = activeStudy()->saveDocument();
00553 
00554     QApplication::restoreOverrideCursor();
00555 
00556     if ( !isOk )
00557     {
00558       putInfo( "" );
00559       // displaying a message box as SUIT_Validator in case file can't be written (the most frequent case)
00560       SUIT_MessageBox::critical( desktop(), tr( "ERR_ERROR" ),
00561                                  tr( "INF_DOC_SAVING_FAILS" ).arg( activeStudy()->studyName() ) );
00562     }
00563     else
00564       putInfo( tr( "INF_DOC_SAVED" ).arg( "" ) );
00565   }
00566 
00567   if ( isOk )
00568     studySaved( activeStudy() );
00569   else
00570     onSaveAsDoc();
00571 }
00572 
00574 bool STD_Application::onSaveAsDoc()
00575 {
00576   SUIT_Study* study = activeStudy();
00577   if ( !study )
00578     return false;
00579 
00580   bool isOk = false;
00581   while ( !isOk )
00582   {
00583     QString aName = getFileName( false, study->studyName(), getFileFilter(), QString(), 0 );
00584     if ( aName.isNull() )
00585       return false;
00586 
00587     QApplication::setOverrideCursor( Qt::WaitCursor );
00588 
00589     putInfo( tr( "INF_DOC_SAVING" ) + aName );
00590     isOk = study->saveDocumentAs( aName );
00591 
00592     putInfo( isOk ? tr( "INF_DOC_SAVED" ).arg( aName ) : "" );
00593 
00594     QApplication::restoreOverrideCursor();
00595 
00596     if ( !isOk )
00597       SUIT_MessageBox::critical( desktop(), tr( "ERROR" ), tr( "INF_DOC_SAVING_FAILS" ).arg( aName ) );
00598   }
00599 
00600   studySaved( activeStudy() );
00601 
00602   return isOk;
00603 }
00604 
00606 void STD_Application::onExit()
00607 {
00608   int aAnswer = SUIT_MessageBox::Ok;
00609   if ( exitConfirmation() )
00610     aAnswer = SUIT_MessageBox::question( desktop(), tr( "INF_DESK_EXIT" ), tr( "QUE_DESK_EXIT" ),
00611                                          SUIT_MessageBox::Ok | SUIT_MessageBox::Cancel, SUIT_MessageBox::Cancel );
00612   if ( aAnswer == SUIT_MessageBox::Ok )
00613     SUIT_Session::session()->closeSession();
00614 }
00615 
00617 void STD_Application::onCopy()
00618 {
00619 }
00620 
00622 void STD_Application::onPaste()
00623 {
00624 }
00625 
00627 void STD_Application::setEditEnabled( bool theEnable )
00628 {
00629   myEditEnabled = theEnable;
00630 
00631   QtxActionMenuMgr* mMgr = desktop()->menuMgr();
00632   QtxActionToolMgr* tMgr = desktop()->toolMgr();
00633 
00634   for ( int i = EditCopyId; i <= EditPasteId; i++ )
00635   {
00636     mMgr->setShown( mMgr->actionId(action(i)), myEditEnabled );
00637     tMgr->setShown( tMgr->actionId(action(i)), myEditEnabled );
00638   }
00639 }
00640 
00642 bool STD_Application::useFile(const QString& theFileName)
00643 {
00644   bool res = SUIT_Application::useFile( theFileName );
00645 
00646   if ( res )
00647     studyOpened( activeStudy() );
00648 
00649   return res;
00650 }
00651 
00653 void STD_Application::updateDesktopTitle()
00654 {
00655   QString aTitle = applicationName();
00656   QString aVer = applicationVersion();
00657   if ( !aVer.isEmpty() )
00658     aTitle += QString( " " ) + aVer;
00659 
00660   if ( activeStudy() )
00661   {
00662     QString sName = SUIT_Tools::file( activeStudy()->studyName().trimmed(), false );
00663     if ( !sName.isEmpty() )
00664       aTitle += QString( " - [%1]" ).arg( sName );
00665   }
00666 
00667   desktop()->setWindowTitle( aTitle );
00668 }
00669 
00671 void STD_Application::updateCommandsStatus()
00672 {
00673   SUIT_Application::updateCommandsStatus();
00674 
00675   bool aHasStudy     = activeStudy() != 0;
00676   bool aSaved        = aHasStudy && activeStudy()->isSaved();
00677   bool aModified     = aHasStudy && activeStudy()->isModified();
00678   bool aIsNeedToSave = aHasStudy && ( !aSaved || aModified );
00679 
00680  if ( action( FileReopenId ) )
00681     action( FileReopenId )->setEnabled( aSaved );
00682  if ( action( FileSaveId ) )
00683     action( FileSaveId )->setEnabled( aIsNeedToSave );
00684   if ( action( FileSaveAsId ) )
00685     action( FileSaveAsId )->setEnabled( aHasStudy );
00686   if ( action( FileCloseId ) )
00687     action( FileCloseId )->setEnabled( aHasStudy );
00688   if ( action( NewWindowId ) )
00689     action( NewWindowId )->setEnabled( aHasStudy );
00690 }
00691 
00693 SUIT_ViewManager* STD_Application::viewManager( const QString& vmType ) const
00694 {
00695   SUIT_ViewManager* vm = 0;
00696   for ( QList<SUIT_ViewManager*>::const_iterator it = myViewMgrs.begin(); it != myViewMgrs.end() && !vm; ++it )
00697   {
00698     if ( (*it)->getType() == vmType )
00699       vm = *it;
00700   }
00701   return vm;
00702 }
00703 
00707 void STD_Application::viewManagers( const QString& vmType, ViewManagerList& lst ) const
00708 {
00709   for ( QList<SUIT_ViewManager*>::const_iterator it = myViewMgrs.begin(); it != myViewMgrs.end(); ++it )
00710   {
00711     if ( (*it)->getType() == vmType )
00712       lst.append( *it );
00713   }
00714 }
00715 
00717 void STD_Application::viewManagers( ViewManagerList& lst ) const
00718 {
00719   for ( QList<SUIT_ViewManager*>::const_iterator it = myViewMgrs.begin(); it != myViewMgrs.end(); ++it )
00720     lst.append( *it );
00721 }
00722 
00724 ViewManagerList STD_Application::viewManagers() const
00725 {
00726   ViewManagerList lst;
00727   viewManagers( lst );
00728   return lst;
00729 }
00730 
00732 SUIT_ViewManager* STD_Application::activeViewManager() const
00733 {
00734   return myActiveViewMgr;
00735 }
00736 
00738 void STD_Application::addViewManager( SUIT_ViewManager* vm )
00739 {
00740   if ( !vm )
00741     return;
00742 
00743   if ( !containsViewManager( vm ) )
00744   {
00745     myViewMgrs.append( vm );
00746     connect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
00747              this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
00748     vm->connectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
00749 
00750     emit viewManagerAdded( vm );
00751   }
00752 /*
00753   if ( !activeViewManager() && myViewMgrs.count() == 1 )
00754     setActiveViewManager( vm );
00755 */
00756 }
00757 
00759 void STD_Application::removeViewManager( SUIT_ViewManager* vm )
00760 {
00761   if ( !vm )
00762     return;
00763 
00764   vm->closeAllViews();
00765 
00766   emit viewManagerRemoved( vm );
00767 
00768   vm->disconnectPopupRequest( this, SLOT( onConnectPopupRequest( SUIT_PopupClient*, QContextMenuEvent* ) ) );
00769   disconnect( vm, SIGNAL( activated( SUIT_ViewManager* ) ),
00770              this, SLOT( onViewManagerActivated( SUIT_ViewManager* ) ) );
00771   myViewMgrs.removeAll( vm );
00772 
00773   if ( myActiveViewMgr == vm )
00774     myActiveViewMgr = 0;
00775 }
00776 
00778 void STD_Application::clearViewManagers()
00779 {
00780   ViewManagerList lst;
00781   viewManagers( lst );
00782 
00783   for ( QList<SUIT_ViewManager*>::iterator it = lst.begin(); it != lst.end(); ++it )
00784   {
00785     QPointer<SUIT_ViewManager> vm = *it;
00786     removeViewManager( vm );
00787     delete vm;
00788   }
00789 }
00790 
00792 bool STD_Application::containsViewManager( SUIT_ViewManager* vm ) const
00793 {
00794   return myViewMgrs.contains( vm );
00795 }
00796 
00798 void STD_Application::onViewManagerActivated( SUIT_ViewManager* vm )
00799 {
00800   setActiveViewManager( vm );
00801 }
00802 
00804 void STD_Application::onViewStatusBar( bool on )
00805 {
00806   if ( on )
00807     desktop()->statusBar()->show();
00808   else
00809     desktop()->statusBar()->hide();
00810 }
00811 
00813 void STD_Application::onHelpAbout()
00814 {
00815   SUIT_MessageBox::information( desktop(), tr( "About" ), tr( "ABOUT_INFO" ) );
00816 }
00817 
00821 void STD_Application::createEmptyStudy()
00822 {
00823   SUIT_Application::createEmptyStudy();
00824 }
00825 
00827 void STD_Application::setActiveViewManager( SUIT_ViewManager* vm )
00828 {
00829   if ( !containsViewManager( vm ) )
00830     return;
00831 
00832   myActiveViewMgr = vm;
00833   emit viewManagerActivated( vm );
00834 }
00835 
00837 void STD_Application::onConnectPopupRequest( SUIT_PopupClient* client, QContextMenuEvent* e )
00838 {
00839   QtxMenu* popup = new QtxMenu();
00840   // fill popup by own items
00841   QString title;
00842   contextMenuPopup( client->popupClientType(), popup, title );
00843   popup->setTitleText( title );
00844 
00845   popup->addSeparator();
00846   // add items from popup client
00847   client->contextMenuPopup( popup );
00848 
00849   SUIT_Tools::simplifySeparators( popup );
00850 
00851   if ( !popup->actions().isEmpty() )
00852     popup->exec( e->globalPos() );
00853   delete popup;
00854 }
00855 
00857 QString STD_Application::getFileName( bool open, const QString& initial, const QString& filters,
00858                                       const QString& caption, QWidget* parent )
00859 {
00860   if ( !parent )
00861     parent = desktop();
00862   if ( open )
00863     return QFileDialog::getOpenFileName( parent, caption, initial, filters );
00864   else
00865   {
00866     QString aName;
00867     QString aUsedFilter;
00868     QString anOldPath = initial;
00869 
00870     bool isOk = false;
00871     while ( !isOk )
00872     {
00873       // It is preferrable to use OS-specific file dialog box here !!!
00874       aName = QFileDialog::getSaveFileName( parent, caption, anOldPath, filters, &aUsedFilter );
00875 
00876       if ( aName.isNull() )
00877         isOk = true;
00878       else
00879       {
00880         int aEnd = aUsedFilter.lastIndexOf( ')' );
00881         int aStart = aUsedFilter.lastIndexOf( '(', aEnd );
00882         QString wcStr = aUsedFilter.mid( aStart + 1, aEnd - aStart - 1 );
00883 
00884         int idx = 0;
00885         QStringList extList;
00886         QRegExp rx( "[\b\\*]*\\.([\\w]+)" );
00887         while ( ( idx = rx.indexIn( wcStr, idx ) ) != -1 )
00888         {
00889           extList.append( rx.cap( 1 ) );
00890           idx += rx.matchedLength();
00891         }
00892 
00893         if ( !extList.isEmpty() && !extList.contains( SUIT_Tools::extension( aName ) ) )
00894           aName += QString( ".%1" ).arg( extList.first() );
00895 
00896         if ( QFileInfo( aName ).exists() )
00897         {
00898           int aAnswer = SUIT_MessageBox::question( desktop(), tr( "TIT_FILE_SAVEAS" ),
00899                                                    tr( "MSG_FILE_EXISTS" ).arg( aName ),
00900                                                    SUIT_MessageBox::Yes | SUIT_MessageBox::No | SUIT_MessageBox::Cancel, SUIT_MessageBox::Yes );
00901           if ( aAnswer == SUIT_MessageBox::Cancel )
00902           {     // cancelled
00903             aName = QString();
00904             isOk = true;
00905           }
00906           else if ( aAnswer == SUIT_MessageBox::No ) // not save to this file
00907             anOldPath = aName;             // not to return to the same initial dir at each "while" step
00908           else                     // overwrite the existing file
00909             isOk = true;
00910         }
00911         else
00912           isOk = true;
00913       }
00914     }
00915     return aName;
00916   }
00917 }
00918 
00920 QString STD_Application::getDirectory( const QString& initial, const QString& caption, QWidget* parent )
00921 {
00922   if ( !parent )
00923     parent = desktop();
00924 
00925   return QFileDialog::getExistingDirectory( parent, caption, initial );
00926 }
00927 
00932 void STD_Application::setDesktop( SUIT_Desktop* desk )
00933 {
00934   SUIT_Application::setDesktop( desk );
00935 
00936   if ( desk ) {
00937     connect( desk, SIGNAL( closing( SUIT_Desktop*, QCloseEvent* ) ),
00938              this, SLOT( onDesktopClosing( SUIT_Desktop*, QCloseEvent* ) ), Qt::UniqueConnection );
00939   }
00940 }
00941 
00945 void STD_Application::loadPreferences()
00946 {
00947 }
00948 
00952 void STD_Application::savePreferences()
00953 {
00954 }
00955 
00960 void STD_Application::studyCreated( SUIT_Study* )
00961 {
00962   updateDesktopTitle();
00963   updateCommandsStatus();
00964 }
00965 
00970 void STD_Application::studyOpened( SUIT_Study* )
00971 {
00972   updateDesktopTitle();
00973   updateCommandsStatus();
00974 }
00975 
00980 void STD_Application::studySaved( SUIT_Study* )
00981 {
00982   updateDesktopTitle();
00983   updateCommandsStatus();
00984 }
00985 
00989 int STD_Application::viewManagerId( const SUIT_ViewManager* theManager) const
00990 {
00991   return myViewMgrs.indexOf(const_cast<SUIT_ViewManager*>(theManager));
00992 }
00993