Back to index

salome-gui  6.5.0
LogWindow.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 //  KERNEL SALOME_Event : Define event posting mechanism
00024 // File   : LogWindow.cxx
00025 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
00026 //
00027 #include "LogWindow.h"
00028 
00029 #include <QAction>
00030 #include <QApplication>
00031 #include <QDate>
00032 #include <QFile>
00033 #include <QMenu>
00034 #include <QTextEdit>
00035 #include <QTextStream>
00036 #include <QTime>
00037 #include <QVBoxLayout>
00038 
00039 #include <SUIT_MessageBox.h>
00040 #include <SUIT_ResourceMgr.h>
00041 #include <SUIT_Session.h>
00042 #include <SUIT_Tools.h>
00043 
00044 #define DEFAULT_SEPARATOR "***"
00045 
00052 static QString plainText( const QString& richText )
00053 {
00054   QString aText = richText;
00055   int startTag = aText.indexOf( '<' );
00056   while ( true )
00057   {
00058     if ( startTag < 0 )
00059       break;
00060 
00061     int finishTag = aText.indexOf( '>', startTag );
00062     if ( finishTag < 0 )
00063       break;
00064 
00065     aText = aText.remove( startTag, finishTag - startTag + 1 );
00066     startTag = aText.indexOf( '<' );
00067   }
00068   return aText;
00069 }
00070 
00090 LogWindow::LogWindow( QWidget* parent )
00091 : QWidget( parent ),
00092   SUIT_PopupClient()
00093 {
00094   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
00095 
00096   QString fntSet = resMgr ? resMgr->stringValue( "Log Window", "font", QString() ) : QString();
00097 
00098   setFont( SUIT_Tools::stringToFont( fntSet ) );
00099 
00100   myView = new QTextEdit( this );
00101   myView->setReadOnly( true );
00102   myView->viewport()->installEventFilter( this );
00103 
00104   QVBoxLayout* main = new QVBoxLayout( this );
00105   main->setMargin( 0 );
00106   main->addWidget( myView );
00107 
00108   myBannerSize = 0;
00109   myBanner = "<b>Message Log</b>\n********************";
00110   mySeparator = DEFAULT_SEPARATOR;
00111 
00112   clear();
00113 
00114   createActions();
00115 }
00116 
00122 LogWindow::~LogWindow()
00123 {
00124 }
00125 
00130 QString LogWindow::banner() const
00131 {
00132   return myBanner;
00133 }
00134 
00139 QString LogWindow::separator() const
00140 {
00141   return mySeparator;
00142 }
00143 
00148 void LogWindow::setBanner( const QString& banner )
00149 {
00150   myBanner = banner;
00151 
00152   clear( false );
00153 }
00154 
00159 void LogWindow::setSeparator( const QString& separator )
00160 {
00161   mySeparator = separator;
00162 
00163   clear( false );
00164 }
00165 
00175 bool LogWindow::eventFilter( QObject* o, QEvent* e )
00176 {
00177   if ( o == myView->viewport() && e->type() == QEvent::ContextMenu )
00178   {
00179     contextMenuRequest( (QContextMenuEvent*)e );
00180     return true;
00181   }
00182   return QWidget::eventFilter( o, e );
00183 }
00184 
00190 void LogWindow::putMessage( const QString& message, const int flags )
00191 {
00192   putMessage( message, QColor(), flags );
00193 }
00194 
00201 void LogWindow::putMessage( const QString& message, const QColor& color, const int flags )
00202 {
00203   QString msg = message;
00204   if ( msg.isEmpty() )
00205     return;
00206 
00207   bool noColor = flags & DisplayNoColor;
00208 
00209   if ( color.isValid() )
00210     msg = QString( "<font color=\"%1\">%2</font>" ).arg( color.name() ).arg( msg );
00211 
00212   QString dStr;
00213   if ( flags & DisplayDate )
00214   {
00215     dStr = QDate::currentDate().toString( Qt::SystemLocaleDate );
00216     if ( !noColor )
00217       dStr = QString( "<font color=\"#003380\">%1</font>" ).arg( dStr );
00218   }
00219 
00220   QString tStr;
00221   if ( flags & DisplayTime )
00222   {
00223     tStr = QTime::currentTime().toString( Qt::SystemLocaleDate );
00224     if ( !noColor )
00225       tStr = QString( "<font color=\"#008033\">%1</font>" ).arg( tStr );
00226   }
00227 
00228   QString dateTime = QString( "%1 %2" ).arg( dStr ).arg( tStr ).trimmed();
00229   if ( !dateTime.isEmpty() )
00230     msg = QString( "[%1] %2" ).arg( dateTime ).arg( msg );
00231 
00232   myView->append( msg );
00233   myHistory.append( plainText( message ) );
00234 
00235   if ( flags & DisplaySeparator && !mySeparator.isEmpty() )
00236   {
00237     myView->append( mySeparator );   // add separator
00238     myHistory.append( plainText( mySeparator ) );
00239   }
00240   myView->moveCursor( QTextCursor::End );
00241 }
00242 
00247 void LogWindow::clear( bool clearHistory )
00248 {
00249   myView->clear();
00250   if ( clearHistory )
00251     myHistory.clear();
00252 
00253   if ( !myBanner.isEmpty() )
00254   {
00255     myView->append( myBanner );
00256     myBannerSize = myView->document()->blockCount();
00257   }
00258   else
00259     myBannerSize = 0;
00260 }
00261 
00267 bool LogWindow::saveLog( const QString& fileName )
00268 {
00269   QFile file( fileName );
00270   if ( !file.open( QFile::WriteOnly ) )
00271     return false;
00272 
00273   QTextStream stream( &file );
00274 
00275   stream << "*****************************************"   << endl;
00276   stream << "Message Log"                                 << endl;
00277   stream << QDate::currentDate().toString( "dd.MM:yyyy" ) << "  ";
00278   stream << QTime::currentTime().toString( "hh:mm:ss" )   << endl;
00279   stream << "*****************************************"   << endl;
00280 
00281   for ( int i = 0; i < myHistory.count(); i++ )
00282     stream << myHistory[ i ] << endl;
00283 
00284   file.close();
00285   return true;
00286 }
00287 
00291 void LogWindow::createActions()
00292 {
00293   QAction* a = new QAction( tr( "EDIT_COPY_CMD" ), this );
00294   a->setStatusTip( tr( "EDIT_COPY_CMD" ) );
00295   connect( a, SIGNAL( triggered( bool ) ), SLOT( onCopy() ) );
00296   myActions.insert( CopyId, a );
00297 
00298   a = new QAction( tr( "EDIT_CLEAR_CMD" ), this );
00299   a->setStatusTip( tr( "EDIT_CLEAR_CMD" ) );
00300   connect( a, SIGNAL( triggered( bool ) ), SLOT( onClear() ) );
00301   myActions.insert( ClearId, a );
00302 
00303   a = new QAction( tr( "EDIT_SELECTALL_CMD" ), this );
00304   a->setStatusTip( tr( "EDIT_SELECTALL_CMD" ) );
00305   connect( a, SIGNAL( triggered( bool ) ), SLOT( onSelectAll() ) );
00306   myActions.insert( SelectAllId, a );
00307 
00308   a = new QAction( tr( "EDIT_SAVETOFILE_CMD" ), this );
00309   a->setStatusTip( tr( "EDIT_SAVETOFILE_CMD" ) );
00310   connect( a, SIGNAL( triggered( bool ) ), SLOT( onSaveToFile() ) );
00311   myActions.insert( SaveToFileId, a );
00312 }
00313 
00321 void LogWindow::contextMenuPopup( QMenu* popup )
00322 {
00323   popup->addAction( myActions[ CopyId ] );
00324   popup->addAction( myActions[ ClearId ] );
00325   popup->addSeparator();
00326   popup->addAction( myActions[ SelectAllId ] );
00327   popup->addSeparator();
00328   popup->addAction( myActions[ SaveToFileId ] );
00329 
00330   Qtx::simplifySeparators( popup );
00331 
00332   updateActions();
00333 }
00334 
00340 void LogWindow::updateActions()
00341 {
00342   myActions[CopyId]->setEnabled( myView->textCursor().hasSelection() );
00343   myActions[ ClearId ]->setEnabled( myView->document()->blockCount() > myBannerSize );
00344   myActions[SelectAllId]->setEnabled( !myView->document()->isEmpty() );
00345   myActions[ SaveToFileId ]->setEnabled( myHistory.count() > 0 );
00346 }
00347 
00351 void LogWindow::onSaveToFile()
00352 {
00353   SUIT_Application* app = SUIT_Session::session()->activeApplication();
00354   if ( !app )
00355     return;
00356 
00357   // call application-specific "Save file" dialog box
00358   QString aName = app->getFileName( false, QString(), QString( "*.log" ), QString(), 0 );
00359   if ( aName.isNull() )
00360     return;
00361 
00362   QApplication::setOverrideCursor( Qt::WaitCursor );
00363 
00364   bool bOk = saveLog( aName );
00365 
00366   QApplication::restoreOverrideCursor();
00367 
00368   if ( !bOk )
00369     SUIT_MessageBox::critical( this, tr( "ERR_ERROR" ), tr( "ERR_CANT_SAVE_FILE" ) );
00370 }
00371 
00375 void LogWindow::onSelectAll()
00376 {
00377   if ( myView )
00378     myView->selectAll();
00379 }
00380 
00384 void LogWindow::onClear()
00385 {
00386   clear( false );
00387 }
00388 
00392 void LogWindow::onCopy()
00393 {
00394   if ( myView )
00395     myView->copy();
00396 }
00397 
00406 void LogWindow::setMenuActions( const int flags )
00407 {
00408   myActions[CopyId]->setVisible( flags & CopyId );
00409   myActions[ClearId]->setVisible( flags & ClearId );
00410   myActions[SelectAllId]->setVisible( flags & SelectAllId );
00411   myActions[SaveToFileId]->setVisible( flags & SaveToFileId );
00412 }
00413 
00419 int LogWindow::menuActions() const
00420 {
00421   int ret = 0;
00422   ret = ret | ( myActions[CopyId]->isVisible() ? CopyId : 0 );
00423   ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 );
00424   ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 );
00425   ret = ret | ( myActions[SaveToFileId]->isVisible() ? SaveToFileId : 0 );
00426   return ret;
00427 }
00428