Back to index

salome-gui  6.5.0
ToolsGUI_RegWidget.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 "ToolsGUI_RegWidget.h"
00024 
00025 #include <QApplication>
00026 #include <QInputDialog>
00027 #include <QMenuBar>
00028 #include <QStatusBar>
00029 #include <QTabWidget>
00030 #include <QTextEdit>
00031 #include <QTimer>
00032 #include <QToolBar>
00033 #include <QTreeWidget>
00034 #include <QVBoxLayout>
00035 
00036 #include <Qtx.h>
00037 
00038 #include <SALOME_NamingService.hxx>
00039 #include <Utils_SINGLETON.hxx>
00040 #include <Utils_CommException.hxx>
00041 
00042 #define MARGIN_SIZE      11
00043 #define SPACING_SIZE      6
00044 #define MIN_SPIN_WIDTH  100 
00045 
00046 #define BOLD( text ) QString( "<b>%1</b>" ).arg( QString( text ) )
00047 
00048 static const char* const time_data[] = { 
00049 "16 16 6 1",
00050 ". c None",
00051 "b c None",
00052 "# c #000000",
00053 "a c #4c4c4c",
00054 "d c #878787",
00055 "c c #ffffff",
00056 ".....#####ab....",
00057 "...##cc#dc##ab..",
00058 "..#ccc###dcc#ab.",
00059 ".#ccccc#dcccc#a.",
00060 ".#ccccc#dcccc#ab",
00061 "#cccccc#dccccc#a",
00062 "#cccccc#dccc#c#a",
00063 "#c##ccc########a",
00064 "#ccddcccdddd#d#a",
00065 "#cccccccccccdc#a",
00066 ".#ccccccccccc#aa",
00067 ".#ccccc#ccccc#a.",
00068 "..#cccc#dccc#aa.",
00069 "...##cccdc##aa..",
00070 ".....#####aaa...",
00071 "......aaaaa....."};
00072 
00073 static const char* const close_data[] = { 
00074 "16 16 6 1",
00075 "d c None",
00076 ". c None",
00077 "# c #000000",
00078 "a c #4c4c4c",
00079 "c c #5b5b5b",
00080 "b c #ffffff",
00081 ".....#####a.....",
00082 "...##bbbbb##a...",
00083 "..#bbbbbbbbb#a..",
00084 ".#b#cbbbbbb#d#a.",
00085 ".#bc#cbbbb#cb#a.",
00086 "#bbbc#cbb#cbbb#a",
00087 "#bbbbc#c#cbbbb#a",
00088 "#bbbbbc#cbbbbb#a",
00089 "#bbbbc#c#cbbbb#a",
00090 "#bbb#ccbb##bbb#a",
00091 ".#b#c#bbbbc#b#aa",
00092 ".#bc#bbbbbb#c#a.",
00093 "..#bbbbbbbbb#aa.",
00094 "...##bbbbb##aa..",
00095 ".....#####aaa...",
00096 "......aaaaa....."};
00097 
00098 static const char* const refresh_data[] = { 
00099 "16 16 5 1",
00100 ". c None",
00101 "# c #000000",
00102 "a c #4c4c4c",
00103 "c c #878787",
00104 "b c #ffffff",
00105 ".....#####a.....",
00106 "...##bb#bb##a...",
00107 "..#bbbbc#bbb#a..",
00108 ".#bbbb####cbb#a.",
00109 ".#bbb#cc#cbbb#a.",
00110 "#bbb#cb#cbbbbb#a",
00111 "#bb#cbbbbbb#bb#a",
00112 "#bb#cbbbbbb#cb#a",
00113 "#bb#cbbbbbb#cb#a",
00114 "#bbbbbb#bb#cbb#a",
00115 ".#bbbb#cb#cbb#aa",
00116 ".#bbb####cbbb#a.",
00117 "..#bbb#cbbbb#aa.",
00118 "...##bb#cb##aa..",
00119 ".....#####aaa...",
00120 "......aaaaa....."};
00121 
00128 static Registry::Components_ptr GetRegistry( CORBA::ORB_ptr orb )
00129 {
00130   static const char* registryName = "Registry";
00131   Registry::Components_var aRegistry;
00132   
00133   SALOME_NamingService& naming = *SINGLETON_<SALOME_NamingService>::Instance();
00134   naming.init_orb( orb );
00135   
00136   CORBA::Object_var object = 0;
00137   try
00138   {
00139     object = naming.Resolve( registryName );
00140     if ( CORBA::is_nil( object ) ) throw CommException( "Unable to find the Registry service" );
00141     aRegistry = Registry::Components::_narrow( object );
00142   }
00143   catch( const CORBA::Exception& ex )
00144   {
00145     MESSAGE( "Error: can't access Registry server" );
00146   }
00147 
00148   return aRegistry._retn();
00149 }
00150 
00156 
00157 ToolsGUI_RegWidget* ToolsGUI_RegWidget::myRegWidgetPtr = 0;
00158 
00164 ToolsGUI_RegWidget* ToolsGUI_RegWidget::GetRegWidget( CORBA::ORB_ptr orb,
00165                                                       QWidget* parent )
00166 {
00167   if ( !myRegWidgetPtr ) 
00168     myRegWidgetPtr = new ToolsGUI_RegWidget( orb, parent );
00169   return myRegWidgetPtr;
00170 }
00171 
00177 ToolsGUI_RegWidget::ToolsGUI_RegWidget( CORBA::ORB_ptr orb, QWidget* parent ) 
00178 : QMainWindow( parent )
00179 {
00180   myOrb = CORBA::ORB::_duplicate( orb );
00181 
00182   // set window attributes
00183   setAttribute( Qt::WA_DeleteOnClose );
00184   setWindowTitle( tr( "TLT_REGISTRY" ) );
00185 
00186   // show status bar
00187   statusBar();
00188 
00189   // get main menu bar
00190   QMenuBar* mainMenu = menuBar();
00191   
00192   // add 'Actions' menu
00193   QMenu* m = mainMenu->addMenu( tr( "MNU_ACTIONS" ) );
00194   // add 'Actions' toolbar
00195   QToolBar* tb = addToolBar( tr( "TB_ACTIONS" ) );
00196 
00197   // create actions
00198   myActions[Refresh] = new QAction( QPixmap( refresh_data ), tr( "MEN_REFRESH" ), this );
00199   myActions[Refresh]->setStatusTip( tr( "STB_REFRESH" ) );
00200   connect( myActions[Refresh], SIGNAL( triggered() ), this, SLOT( refresh() ) );
00201   m->addAction( myActions[Refresh] );
00202   tb->addAction( myActions[Refresh] );
00203   
00204   myActions[Interval] = new QAction( QPixmap( time_data ), tr( "MEN_INTERVAL" ), this );
00205   myActions[Interval]->setStatusTip( tr( "STB_INTERVAL" ) );
00206   connect( myActions[Interval], SIGNAL( triggered() ), this, SLOT( refreshInterval() ) );
00207   m->addAction( myActions[Interval] );
00208   tb->addAction( myActions[Interval] );
00209   
00210   m->addSeparator();
00211   tb->addSeparator();
00212 
00213   myActions[Close] = new QAction( QPixmap( close_data ), tr( "MEN_CLOSE" ), this );
00214   myActions[Close]->setStatusTip( tr( "STB_CLOSE" ) );
00215   connect( myActions[Close], SIGNAL( triggered() ), this, SLOT( close() ) );
00216   m->addAction( myActions[Close] );
00217   tb->addAction( myActions[Close] );
00218 
00219   // central widget
00220   setCentralWidget( new QWidget( this ) );
00221 
00222   // Create tab widget: SALOME serives list (running and history)
00223   QTabWidget* tabWidget = new QTabWidget( centralWidget() );
00224 
00225   myViews[Clients]   = new QTreeWidget( tabWidget );
00226   myViews[Clients]->setColumnCount( 6 );
00227   myViews[Clients]->setAllColumnsShowFocus( true );
00228   myViews[Clients]->setRootIsDecorated( false );
00229   QStringList runningLabels;
00230   runningLabels << tr( "HDR_COMPONENT" ) << tr( "HDR_PID" )     << tr( "HDR_USERNAME" )
00231               << tr( "HDR_HOSTNAME" )  << tr( "HDR_STARTED" ) << tr( "HDR_HELLO" );
00232   myViews[Clients]->setHeaderLabels( runningLabels );
00233   tabWidget->addTab( myViews[Clients], tr( "TAB_RUNNING" ) );
00234 
00235   myViews[History]   = new QTreeWidget( tabWidget );
00236   myViews[History]->setColumnCount(6);
00237   myViews[History]->setAllColumnsShowFocus( true );
00238   myViews[History]->setRootIsDecorated( false );
00239   QStringList historyLabels;
00240   historyLabels << tr( "HDR_COMPONENT" ) << tr( "HDR_PID" )     << tr( "HDR_USERNAME" )
00241               << tr( "HDR_HOSTNAME" )  << tr( "HDR_STARTED" ) << tr( "HDR_FINISHED" );
00242   myViews[History]->setHeaderLabels( historyLabels );
00243   tabWidget->addTab( myViews[History], tr( "TAB_HISTORY" ) );
00244 
00245   connect( tabWidget, SIGNAL( currentChanged( int ) ), this, SLOT( refresh() ) );
00246   connect( myViews[Clients], SIGNAL( itemActivated( QTreeWidgetItem*, int ) ), 
00247           this,             SLOT( showDetails( QTreeWidgetItem*, int ) ) );
00248   connect( myViews[History], SIGNAL( itemActivated( QTreeWidgetItem*, int ) ),
00249           this,             SLOT( showDetails( QTreeWidgetItem*, int ) ) );
00250 
00251   // create information window
00252   myDetails = new QTextEdit( centralWidget() );
00253   myDetails->setReadOnly( true );
00254   myDetails->setMinimumHeight( 100 );
00255 
00256   // layout widgets
00257   QVBoxLayout* topLayout = new QVBoxLayout( centralWidget() );
00258   topLayout->setMargin( 0 );
00259   topLayout->setSpacing( SPACING_SIZE );
00260   topLayout->addWidget( tabWidget );
00261   topLayout->addWidget( myDetails );
00262   
00263   // install auto-update timer
00264   myTimer = new QTimer( this );
00265   connect( myTimer, SIGNAL( timeout() ), this, SLOT( refresh() ) );
00266   myTimer->start( 10000 ); // 10 seconds by default
00267   
00268   // resize window and set its position
00269   resize( 800, 400 );
00270   Qtx::alignWidget( (QWidget*)this, (QWidget*)QApplication::desktop(), Qtx::AlignCenter );
00271 
00272   // refresh registry information
00273   refresh();
00274 }
00275 
00279 ToolsGUI_RegWidget::~ToolsGUI_RegWidget()
00280 {
00281   myRegWidgetPtr = 0;
00282 };
00283 
00289 QString ToolsGUI_RegWidget::getDetails( const Registry::Infos& c_info )
00290 {
00291   QString a;
00292   if ( c_info.name != NULL ) {
00293     time_t aStarted( c_info.tc_start );
00294     time_t aLastPing( c_info.tc_hello );
00295     time_t aFinished( c_info.tc_end );
00296 
00297     a.append( BOLD( tr( "INFO_SERVICE" ).arg( QString( c_info.name ) ) ) );
00298     a.append( "<br>" );
00299     a.append( "<br>" );
00300     a.append( tr( "INFO_PROCESS" ).arg( BOLD( QString::number( c_info.pid ) ),
00301                                    BOLD( c_info.machine ),
00302                                    BOLD( c_info.adip ),
00303                                    BOLD( c_info.pwname ),
00304                                    BOLD( QString::number( c_info.uid ) ),
00305                                    BOLD( c_info.cdir ) ) );
00306     a.append( "<br>" );
00307     a.append( "<br>" );
00308     a.append( tr( "INFO_STARTED" ).arg( BOLD( QString( ctime( &aStarted ) ).trimmed() ) ) );
00309     a.append( "<br>" );
00310     if ( c_info.tc_hello != 0 ) {
00311       a.append( tr( "INFO_LAST_PING" ).arg( BOLD( QString( ctime( &aLastPing ) ).trimmed() ) ) );
00312       a.append( "<br>" );
00313     }
00314     if ( c_info.tc_end - c_info.difftime != 0 ) {
00315       a.append( tr( "INFO_FINISHED" ).arg( BOLD( QString( ctime( &aFinished ) ).trimmed() ) ) );
00316       a.append( "<br>" );
00317     }
00318     else {
00319       a.append( tr( "INFO_RUNNING" ) );
00320       a.append( "<br>" );
00321     }
00322     if ( c_info.difftime != 0 ) {
00323       a.append( tr( "INFO_TIME_DIFF" ).arg( BOLD( c_info.machine ), BOLD( QString::number( c_info.difftime ) ) ) ); 
00324       a.append( "<br>" );
00325     }
00326     a.append( "</code>" ); // ASV: 28.07.06 : added <code> tags to make the text font be 
00327                            // fixed width (looks much better on Windows)
00328   }
00329   return a;
00330 }
00331 
00335 void ToolsGUI_RegWidget::refresh()
00336 {
00337   try {
00338     // get reference to the Registry service
00339     Registry::Components_var aRegistry = GetRegistry( myOrb );
00340     if ( aRegistry->_is_nil() ) {
00341       myActions[Interval]->setDisabled( true );
00342       myActions[Refresh]->setDisabled( true );
00343       myTimer->stop();
00344       MESSAGE( "Sorry, no more Registry Server" );
00345       statusBar()->showMessage( tr( "ERR_NO_REGISTRY" ) );
00346       return;
00347     }
00348 
00349     myData[Clients] = aRegistry->getall();
00350     myData[History] = aRegistry->history();
00351 
00352     // update current services list
00353     myViews[Clients]->clear();
00354     for ( int i = 0; i < myData[Clients]->length(); i++ ) {
00355       const Registry::Infos& c_info = (*myData[Clients])[i];
00356       time_t aStarted  = time_t( c_info.tc_start );
00357       time_t aLastPing = time_t( c_info.tc_hello );
00358       QStringList aData;
00359       aData << QString( c_info.name )
00360            << QString::number( c_info.pid )
00361            << QString( c_info.pwname )
00362            << QString( c_info.machine )
00363            << QString( ctime( &aStarted ) ).trimmed()
00364            << QString( ctime( &aLastPing ) ).trimmed();
00365       myViews[Clients]->addTopLevelItem( new QTreeWidgetItem( aData ) );
00366     }
00367 
00368     // update history
00369     myViews[History]->clear();
00370     for ( int i = 0; i < myData[History]->length(); i++ ) {       
00371       const Registry::Infos& c_info = (*myData[History])[i];
00372       time_t aStarted  = time_t( c_info.tc_start );
00373       time_t aFinished = time_t( c_info.tc_end );
00374       QStringList aData;
00375       aData << QString( c_info.name )
00376            << QString::number( c_info.pid )
00377            << QString( c_info.pwname )
00378            << QString( c_info.machine )
00379            << QString( ctime( &aStarted ) ).trimmed()
00380            << QString( ctime( &aFinished ) ).trimmed();
00381       myViews[History]->addTopLevelItem( new QTreeWidgetItem( aData ) );
00382     }
00383   }
00384   catch( ... ) {
00385     myActions[Interval]->setDisabled( true );
00386     myActions[Refresh]->setDisabled( true );
00387     myTimer->stop();
00388     MESSAGE( "Sorry, no more Registry Server" );
00389     statusBar()->showMessage( tr( "ERR_NO_REGISTRY" ) );
00390   }
00391 }
00392 
00397 void ToolsGUI_RegWidget::refreshInterval()
00398 {
00399   bool ok;
00400   int sec = QInputDialog::getInt( this, tr( "TLT_REFRESH_INTERVAL" ), tr( "LAB_REFRESH_INTERVAL" ), 
00401                               myTimer->interval() / 1000, 1, 24 * 60 * 60, 1, &ok );
00402   if ( ok )
00403     myTimer->start( sec * 1000 );
00404 }
00405 
00411 void ToolsGUI_RegWidget::showDetails( QTreeWidgetItem* item, int column )
00412 {
00413   if ( !item || column < 0 ) return;
00414   int i = sender() == myViews[Clients] ? Clients : History;
00415   int idx  = myViews[i]->indexOfTopLevelItem( item );
00416   int size = myData[i]->length();
00417   if ( idx < 0 || idx > size-1 ) return;
00418   const Registry::Infos& c_info = (*myData[i])[idx];
00419   myDetails->setText( getDetails( c_info ) );
00420 }