Back to index

salome-gui  6.5.0
SUIT_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 "SUIT_Application.h"
00024 
00025 #include "SUIT_Study.h"
00026 #include "SUIT_Session.h"
00027 #include "SUIT_Desktop.h"
00028 #include "SUIT_ResourceMgr.h"
00029 #include "SUIT_ShortcutMgr.h"
00030 
00031 #include <QTimer>
00032 #include <QLabel>
00033 #include <QStatusBar>
00034 #include <QApplication>
00035 #include <QSize>
00036 
00037 #include <QtxAction.h>
00038 #include <QtxActionMenuMgr.h>
00039 #include <QtxActionToolMgr.h>
00040 
00041 
00047 class StatusLabel : public QLabel
00048 {
00049 public:
00050   StatusLabel( QWidget* parent ) : QLabel( parent ) {}
00051   QSize minimumSizeHint () const { return QSize( 0, QLabel::minimumSizeHint().height() ); }
00052 };
00053 
00057 SUIT_Application::SUIT_Application()
00058 : QObject( 0 ),
00059   myStudy( 0 ),
00060   myDesktop( 0 ),
00061   myStatusLabel( 0 )
00062 {
00063   if ( SUIT_Session::session() )
00064     SUIT_Session::session()->insertApplication( this );
00065 }
00066 
00070 SUIT_Application::~SUIT_Application()
00071 {
00072   SUIT_Study* s = myStudy;
00073   setActiveStudy( 0 );
00074   delete s;
00075 
00076   setDesktop( 0 );
00077 }
00078 
00082 SUIT_Desktop* SUIT_Application::desktop()
00083 {
00084   return myDesktop;
00085 }
00086 
00091 bool SUIT_Application::isPossibleToClose( bool& )
00092 {
00093   return true;
00094 }
00095 
00100 void SUIT_Application::closeApplication()
00101 {
00102   emit applicationClosed( this );
00103 }
00104 
00108 SUIT_Study* SUIT_Application::activeStudy() const
00109 {
00110   return myStudy;
00111 }
00112 
00116 QString SUIT_Application::applicationVersion() const
00117 {
00118   return QString();
00119 }
00120 
00124 void SUIT_Application::start()
00125 {
00126   if ( desktop() )
00127     desktop()->show();
00128 
00129   // Initialize shortcut manager
00130   SUIT_ShortcutMgr::Init();
00131 }
00132 
00137 bool SUIT_Application::useFile( const QString& theFileName )
00138 {
00139   createEmptyStudy();
00140   SUIT_Study* study = activeStudy();
00141 
00142   bool status = study ? study->openDocument( theFileName ) : false;
00143 
00144   if ( !status )
00145   {
00146     setActiveStudy( 0 );
00147     delete study;
00148   }
00149 
00150   return status;
00151 }
00152 
00156 void SUIT_Application::createEmptyStudy()
00157 {
00158   if ( !activeStudy() )
00159     setActiveStudy( createNewStudy() );
00160 }
00161 
00166 int SUIT_Application::getNbStudies() const
00167 {
00168   return activeStudy() ? 1 : 0;
00169 }
00170 
00174 SUIT_ResourceMgr* SUIT_Application::resourceMgr() const
00175 {
00176   if ( !SUIT_Session::session() )
00177     return 0;
00178 
00179   return SUIT_Session::session()->resourceMgr();
00180 }
00181 
00186 SUIT_ShortcutMgr* SUIT_Application::shortcutMgr() const
00187 {
00188   return SUIT_ShortcutMgr::getShortcutMgr();
00189 }
00190 
00191 #define DEFAULT_MESSAGE_DELAY 3000
00192 
00198 void SUIT_Application::putInfo( const QString& msg, const int msec )
00199 {
00200   if ( !desktop() )
00201     return;
00202 
00203   if ( !myStatusLabel )
00204   {
00205     myStatusLabel = new StatusLabel( desktop()->statusBar() );
00206     desktop()->statusBar()->addWidget( myStatusLabel, 1 );
00207     myStatusLabel->show();
00208   }
00209 
00210   QString prev = myStatusLabel->text();
00211 
00212   myStatusLabel->setText( msg );
00213   if ( msec != -1 )
00214     QTimer::singleShot( msec <= 0 ? DEFAULT_MESSAGE_DELAY : msec, this, SLOT( onInfoClear() ) );
00215 
00216   if ( prev != msg )
00217     emit infoChanged( msg );
00218 }
00219 
00223 void SUIT_Application::onInfoClear()
00224 {
00225   if ( !myStatusLabel )
00226     return;
00227 
00228   bool changed = !myStatusLabel->text().isEmpty();
00229   myStatusLabel->clear();
00230   if ( changed )
00231     emit infoChanged( QString() );
00232 }
00233 
00237 void SUIT_Application::updateCommandsStatus()
00238 {
00239 }
00240 
00246 SUIT_Application* SUIT_Application::startApplication( int argc, char** argv ) const
00247 {
00248   return startApplication( objectName(), argc, argv );
00249 }
00250 
00257 SUIT_Application* SUIT_Application::startApplication( const QString& name, int argc, char** argv ) const
00258 {
00259   SUIT_Session* session = SUIT_Session::session();
00260   if ( !session )
00261     return 0;
00262 
00263   return session->startApplication( name, argc, argv );
00264 }
00265 
00270 void SUIT_Application::setDesktop( SUIT_Desktop* desk )
00271 {
00272   if ( myDesktop == desk )
00273     return;
00274 
00275   // >> VSR 09/06/2009: workaround about the Qt 4.5.0 bug: SIGSEGV on desktop delete
00276   myDesktop->deleteLater(); // delete myDesktop;
00277   // << VSR 09/06/2009
00278   myDesktop = desk;
00279   if ( myDesktop ) {
00280     connect( myDesktop, SIGNAL( activated() ), this, SLOT( onDesktopActivated() ) );
00281     // Force desktop activation (NPAL16628)
00282     QApplication::postEvent(myDesktop, new QEvent(QEvent::WindowActivate));
00283   }
00284 }
00285 
00291 SUIT_Study* SUIT_Application::createNewStudy()
00292 {
00293   return new SUIT_Study( this );
00294 }
00295 
00300 void SUIT_Application::setActiveStudy( SUIT_Study* study )
00301 {
00302   if ( myStudy == study )
00303     return;
00304 
00305   if ( myStudy )
00306     disconnect( myStudy, SIGNAL( studyModified( SUIT_Study* ) ),
00307                 this, SLOT( updateCommandsStatus() ) );
00308   if ( study )
00309     connect( study, SIGNAL( studyModified( SUIT_Study* ) ),
00310              this, SLOT( updateCommandsStatus() ) );
00311 
00312   myStudy = study;
00313 }
00314 
00320 int SUIT_Application::createTool( const QString& name )
00321 {
00322   if ( !desktop() || !desktop()->toolMgr() )
00323     return -1;
00324 
00325   return desktop()->toolMgr()->createToolBar( name );
00326 }
00327 
00336 int SUIT_Application::createTool( QAction* a, const int tBar, const int id, const int idx )
00337 {
00338   if ( !desktop() || !desktop()->toolMgr() )
00339     return -1;
00340 
00341   int regId = registerAction( id, a );
00342   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
00343   return intId != -1 ? regId : -1;
00344 }
00345 
00354 int SUIT_Application::createTool( QAction* a, const QString& tBar, const int id, const int idx )
00355 {
00356   if ( !desktop() || !desktop()->toolMgr() )
00357     return -1;
00358 
00359   int regId = registerAction( id, a );
00360   int intId = desktop()->toolMgr()->insert( a, tBar, idx );
00361   return intId != -1 ? regId : -1;
00362 }
00363 
00371 int SUIT_Application::createTool( const int id, const int tBar, const int idx )
00372 {
00373   if ( !desktop() || !desktop()->toolMgr() )
00374     return -1;
00375 
00376   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
00377   return intId != -1 ? id : -1;
00378 }
00379 
00387 int SUIT_Application::createTool( const int id, const QString& tBar, const int idx )
00388 {
00389   if ( !desktop() || !desktop()->toolMgr() )
00390     return -1;
00391 
00392   int intId = desktop()->toolMgr()->insert( action( id ), tBar, idx );
00393   return intId != -1 ? id : -1;
00394 }
00395 
00405 int SUIT_Application::createMenu( const QString& subMenu, const int menu,
00406                                   const int id, const int group, const int index )
00407 {
00408   if ( !desktop() || !desktop()->menuMgr() )
00409     return -1;
00410 
00411   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
00412 }
00413 
00423 int SUIT_Application::createMenu( const QString& subMenu, const QString& menu,
00424                                   const int id, const int group, const int index )
00425 {
00426   if ( !desktop() || !desktop()->menuMgr() )
00427     return -1;
00428 
00429   return desktop()->menuMgr()->insert( subMenu, menu, group, id, index );
00430 }
00431 
00441 int SUIT_Application::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
00442 {
00443   if ( !a || !desktop() || !desktop()->menuMgr() )
00444     return -1;
00445 
00446   int regId = registerAction( id, a );
00447   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
00448   return intId != -1 ? regId : -1;
00449 }
00450 
00460 int SUIT_Application::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
00461 {
00462   if ( !a || !desktop() || !desktop()->menuMgr() )
00463     return -1;
00464 
00465   int regId = registerAction( id, a );
00466   int intId = desktop()->menuMgr()->insert( a, menu, group, index );
00467   return intId != -1 ? regId : -1;
00468 }
00469 
00478 int SUIT_Application::createMenu( const int id, const int menu, const int group, const int index )
00479 {
00480   if ( !desktop() || !desktop()->menuMgr() )
00481     return -1;
00482 
00483   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
00484   return intId != -1 ? id : -1;
00485 }
00486 
00495 int SUIT_Application::createMenu( const int id, const QString& menu, const int group, const int index )
00496 {
00497   if ( !desktop() || !desktop()->menuMgr() )
00498     return -1;
00499 
00500   int intId = desktop()->menuMgr()->insert( action( id ), menu, group, index );
00501   return intId != -1 ? id : -1;
00502 }
00503 
00509 void SUIT_Application::setMenuShown( QAction* a, const bool on )
00510 {
00511   if ( !a || !desktop() )
00512     return;
00513 
00514   QtxActionMenuMgr* mMgr = desktop()->menuMgr();
00515   if ( mMgr )
00516     mMgr->setShown( mMgr->actionId( a ), on );
00517 }
00518 
00524 void SUIT_Application::setMenuShown( const int id, const bool on )
00525 {
00526   setMenuShown( action( id ), on );
00527 }
00528 
00534 void SUIT_Application::setToolShown( QAction* a, const bool on )
00535 {
00536   if ( !a || !desktop() )
00537     return;
00538 
00539   QtxActionToolMgr* tMgr = desktop()->toolMgr();
00540   if ( tMgr )
00541     tMgr->setShown( tMgr->actionId( a ), on );
00542 }
00543 
00549 void SUIT_Application::setToolShown( const int id, const bool on )
00550 {
00551   setToolShown( action( id ), on );
00552 }
00553 
00559 void SUIT_Application::setActionShown( QAction* a, const bool on )
00560 {
00561   setMenuShown( a, on );
00562   setToolShown( a, on );
00563 }
00564 
00570 void SUIT_Application::setActionShown( const int id, const bool on )
00571 {
00572   setMenuShown( id, on );
00573   setToolShown( id, on );
00574 }
00575 
00580 QAction* SUIT_Application::action( const int id ) const
00581 {
00582   QAction* a = 0;
00583   if ( myActionMap.contains( id ) )
00584     a = myActionMap[id];
00585   return a;
00586 }
00587 
00592 int SUIT_Application::actionId( const QAction* a ) const
00593 {
00594   int id = -1;
00595   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
00596   {
00597     if ( it.value() == a )
00598       id = it.key();
00599   }
00600   return id;
00601 }
00602 
00603 QList<QAction*> SUIT_Application::actions() const
00604 {
00605   return myActionMap.values();
00606 }
00607 
00608 QList<int> SUIT_Application::actionIds() const
00609 {
00610   return myActionMap.keys();
00611 }
00612 
00627 QAction* SUIT_Application::createAction( const int id, const QString& text, const QIcon& icon,
00628                                          const QString& menu, const QString& tip, const int key,
00629                                          QObject* parent, const bool toggle, QObject* reciever, 
00630                                     const char* member, const QString& shortcutAction )
00631 {
00632   QtxAction* a = new QtxAction( text, icon, menu, key, parent, toggle, shortcutAction );
00633   a->setStatusTip( tip );
00634 
00635   if ( reciever && member )
00636     connect( a, SIGNAL( triggered( bool ) ), reciever, member );
00637 
00638   registerAction( id, a );
00639 
00640   return a;
00641 }
00642 
00648 int SUIT_Application::registerAction( const int id, QAction* a )
00649 {
00650   int ident = actionId( a );
00651   if ( ident != -1 )
00652     return ident;
00653 
00654   static int generatedId = -1;
00655   ident = id == -1 ? --generatedId : id;
00656 
00657   if ( action( ident ) )
00658     qWarning( "Action registration id is already in use: %d", ident );
00659 
00660   myActionMap.insert( ident, a );
00661 
00662   if ( desktop() && desktop()->menuMgr() )
00663     desktop()->menuMgr()->registerAction( a );
00664 
00665   if ( desktop() && desktop()->toolMgr() )
00666     desktop()->toolMgr()->registerAction( a );
00667 
00668   if ( desktop() )
00669     desktop()->addAction( a );
00670 
00671   return ident;
00672 }
00673 
00677 QAction* SUIT_Application::separator()
00678 {
00679   return QtxActionMgr::separator();
00680 }
00681 
00686 void SUIT_Application::onDesktopActivated()
00687 {
00688   emit activated( this );
00689 }
00690 
00694 void SUIT_Application::onHelpContextModule( const QString& /*theComponentName*/,
00695                                             const QString& /*theFileName*/,
00696                                             const QString& /*theContext*/ )
00697 {
00698 }