1 // KERNEL SALOME_Event : Define event posting mechanism
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 #include "LogWindow.h"
27 #include <qpopupmenu.h>
28 #include <qtextbrowser.h>
29 #include <qapplication.h>
30 #include <qdatetime.h>
32 #include <SUIT_Tools.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_MessageBox.h>
35 #include <SUIT_ResourceMgr.h>
37 #define DEFAULT_SEPARATOR "***"
39 //****************************************************************
40 static QString plainText( const QString& richText )
42 QString aText = richText;
43 int startTag = aText.find('<');
47 int finishTag = aText.find('>',startTag);
50 aText = aText.remove(startTag, finishTag-startTag+1);
51 startTag = aText.find('<');
56 //****************************************************************
58 LogWindow::LogWindow( QWidget* parent )
62 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
64 QString fntSet = resMgr ? resMgr->stringValue( "Log Window", "font", QString::null ) : QString::null;
66 setFont( SUIT_Tools::stringToFont( fntSet ) );
68 myView = new QTextBrowser(this,"myView");
69 #if QT_VERSION>0x030007
70 myView->setTextFormat( Qt::LogText );
72 myView->viewport()->installEventFilter( this );
74 QVBoxLayout* main = new QVBoxLayout( this );
75 main->addWidget( myView );
78 myBanner = "<b>Message Log</b>\n********************";
79 mySeparator = DEFAULT_SEPARATOR;
86 LogWindow::~LogWindow()
90 bool LogWindow::eventFilter( QObject* o, QEvent* e )
92 if ( o == myView->viewport() && e->type() == QEvent::ContextMenu )
94 contextMenuRequest( (QContextMenuEvent*)e );
97 return QFrame::eventFilter( o, e );
100 void LogWindow::setBanner( const QString& banner )
107 void LogWindow::setSeparator( const QString& separator )
109 mySeparator = separator;
114 void LogWindow::putMessage( const QString& message, bool addSeparator )
116 myView->append( message );
117 myHistory.append( plainText( message ) );
119 if ( addSeparator && !mySeparator.isNull() )
121 myView->append( mySeparator ); // add separator
122 myHistory.append( plainText( mySeparator ) );
124 myView->scrollToBottom();
127 void LogWindow::clear( bool clearHistory )
133 if ( !myBanner.isEmpty() )
135 myView->append( myBanner );
136 myBannerSize = myView->paragraphs();
142 bool LogWindow::saveLog( const QString& fileName )
144 QFile file( fileName );
145 if ( !file.open( IO_WriteOnly ) )
148 QTextStream stream( &file );
150 stream << "*****************************************" << endl;
151 stream << "Message Log" << endl;
152 stream << QDate::currentDate().toString( "dd.MM:yyyy" ) << " ";
153 stream << QTime::currentTime().toString( "hh:mm:ss" ) << endl;
154 stream << "*****************************************" << endl;
156 for ( uint i = 0; i < myHistory.count(); i++ )
157 stream << myHistory[ i ] << endl;
163 void LogWindow::createActions()
165 QAction* a = new QAction( "", tr( "&Copy" ), 0, this );
166 a->setStatusTip( tr( "&Copy" ) );
167 connect( a, SIGNAL( activated() ), SLOT( onCopy()));
168 myActions.insert( CopyId, a );
170 a = new QAction( "", tr( "Clea&r" ), 0, this );
171 a->setStatusTip( tr( "Clea&r" ) );
172 connect( a, SIGNAL( activated() ), SLOT( onClear()));
173 myActions.insert( ClearId, a );
175 a = new QAction( "", tr( "Select &All" ), 0, this );
176 a->setStatusTip( tr( "Select &All" ) );
177 connect( a, SIGNAL( activated() ), SLOT( onSelectAll()));
178 myActions.insert( SelectAllId, a );
180 a = new QAction( "", tr( "&Save log to file..." ), 0, this );
181 a->setStatusTip( tr( "&Save log to file..." ) );
182 connect( a, SIGNAL( activated() ), SLOT( onSaveToFile()));
183 myActions.insert( SaveToFileId, a );
186 void LogWindow::contextMenuPopup( QPopupMenu* popup )
188 myActions[ CopyId ]->addTo( popup );
189 myActions[ ClearId ]->addTo( popup );
191 popup->insertSeparator();
193 myActions[ SelectAllId ]->addTo( popup );
195 popup->insertSeparator();
197 myActions[ SaveToFileId ]->addTo( popup );
202 void LogWindow::updateActions()
204 int paraFrom, paraTo, indexFrom, indexTo;
205 myView->getSelection( ¶From, &indexFrom, ¶To, &indexTo );
206 bool allSelected = myView->hasSelectedText() &&
207 !paraFrom && paraTo == myView->paragraphs() - 1 &&
208 !indexFrom && indexTo == myView->paragraphLength( paraTo );
209 myActions[ CopyId ]->setEnabled( myView->hasSelectedText() );
210 myActions[ ClearId ]->setEnabled( myView->paragraphs() > myBannerSize );
211 myActions[ SelectAllId ]->setEnabled( !allSelected );
212 myActions[ SaveToFileId ]->setEnabled( myHistory.count() > 0 );
215 void LogWindow::onSaveToFile()
217 SUIT_Application* app = SUIT_Session::session()->activeApplication();
221 // call application-specific "Save file" dialog box
222 QString aName = app->getFileName( false, QString::null, QString( "*.log" ), QString::null, 0 );
223 if ( aName.isNull() )
226 QApplication::setOverrideCursor( Qt::waitCursor );
228 bool bOk = saveLog( aName );
230 QApplication::restoreOverrideCursor();
233 SUIT_MessageBox::error1( this, tr( "Error" ), tr( "Can't save file" ), tr( "OK" ) );
236 void LogWindow::onSelectAll()
242 void LogWindow::onClear()
247 void LogWindow::onCopy()