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 "***"
40 Converts rich text to plain text
42 static QString plainText( const QString& richText )
44 QString aText = richText;
45 int startTag = aText.find('<');
49 int finishTag = aText.find('>',startTag);
52 aText = aText.remove(startTag, finishTag-startTag+1);
53 startTag = aText.find('<');
61 LogWindow::LogWindow( QWidget* parent )
65 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
67 QString fntSet = resMgr ? resMgr->stringValue( "Log Window", "font", QString::null ) : QString::null;
69 setFont( SUIT_Tools::stringToFont( fntSet ) );
71 myView = new QTextBrowser(this,"myView");
72 #if QT_VERSION>0x030007
73 myView->setTextFormat( Qt::LogText );
75 myView->viewport()->installEventFilter( this );
77 QVBoxLayout* main = new QVBoxLayout( this );
78 main->addWidget( myView );
81 myBanner = "<b>Message Log</b>\n********************";
82 mySeparator = DEFAULT_SEPARATOR;
92 LogWindow::~LogWindow()
99 bool LogWindow::eventFilter( QObject* o, QEvent* e )
101 if ( o == myView->viewport() && e->type() == QEvent::ContextMenu )
103 contextMenuRequest( (QContextMenuEvent*)e );
106 return QFrame::eventFilter( o, e );
110 Sets banner (title of message log)
111 \param banner - new title
113 void LogWindow::setBanner( const QString& banner )
121 Set separator (line printing between messages)
122 \param separator - new separator
124 void LogWindow::setSeparator( const QString& separator )
126 mySeparator = separator;
132 Puts message to log window
133 \param message - text of message
134 \addSeparator - if it is true, then separator is added to tail of message log
136 void LogWindow::putMessage( const QString& message, bool addSeparator )
138 myView->append( message );
139 myHistory.append( plainText( message ) );
141 if ( addSeparator && !mySeparator.isNull() )
143 myView->append( mySeparator ); // add separator
144 myHistory.append( plainText( mySeparator ) );
146 myView->scrollToBottom();
151 \param clearHistory - if it is true, then also history is cleared
153 void LogWindow::clear( bool clearHistory )
159 if ( !myBanner.isEmpty() )
161 myView->append( myBanner );
162 myBannerSize = myView->paragraphs();
170 \param fileName - name of file
172 bool LogWindow::saveLog( const QString& fileName )
174 QFile file( fileName );
175 if ( !file.open( IO_WriteOnly ) )
178 QTextStream stream( &file );
180 stream << "*****************************************" << endl;
181 stream << "Message Log" << endl;
182 stream << QDate::currentDate().toString( "dd.MM:yyyy" ) << " ";
183 stream << QTime::currentTime().toString( "hh:mm:ss" ) << endl;
184 stream << "*****************************************" << endl;
186 for ( uint i = 0; i < myHistory.count(); i++ )
187 stream << myHistory[ i ] << endl;
196 void LogWindow::createActions()
198 QAction* a = new QAction( "", tr( "&Copy" ), 0, this );
199 a->setStatusTip( tr( "&Copy" ) );
200 connect( a, SIGNAL( activated() ), SLOT( onCopy()));
201 myActions.insert( CopyId, a );
203 a = new QAction( "", tr( "Clea&r" ), 0, this );
204 a->setStatusTip( tr( "Clea&r" ) );
205 connect( a, SIGNAL( activated() ), SLOT( onClear()));
206 myActions.insert( ClearId, a );
208 a = new QAction( "", tr( "Select &All" ), 0, this );
209 a->setStatusTip( tr( "Select &All" ) );
210 connect( a, SIGNAL( activated() ), SLOT( onSelectAll()));
211 myActions.insert( SelectAllId, a );
213 a = new QAction( "", tr( "&Save log to file..." ), 0, this );
214 a->setStatusTip( tr( "&Save log to file..." ) );
215 connect( a, SIGNAL( activated() ), SLOT( onSaveToFile()));
216 myActions.insert( SaveToFileId, a );
220 Redefined virtual method for popup filling
222 void LogWindow::contextMenuPopup( QPopupMenu* popup )
224 myActions[ CopyId ]->addTo( popup );
225 myActions[ ClearId ]->addTo( popup );
227 popup->insertSeparator();
229 myActions[ SelectAllId ]->addTo( popup );
231 popup->insertSeparator();
233 myActions[ SaveToFileId ]->addTo( popup );
239 Updates enable status of actions
241 void LogWindow::updateActions()
243 int paraFrom, paraTo, indexFrom, indexTo;
244 myView->getSelection( ¶From, &indexFrom, ¶To, &indexTo );
245 bool allSelected = myView->hasSelectedText() &&
246 !paraFrom && paraTo == myView->paragraphs() - 1 &&
247 !indexFrom && indexTo == myView->paragraphLength( paraTo );
248 myActions[ CopyId ]->setEnabled( myView->hasSelectedText() );
249 myActions[ ClearId ]->setEnabled( myView->paragraphs() > myBannerSize );
250 myActions[ SelectAllId ]->setEnabled( !allSelected );
251 myActions[ SaveToFileId ]->setEnabled( myHistory.count() > 0 );
255 SLOT: called if user click "Save" in popup
257 void LogWindow::onSaveToFile()
259 SUIT_Application* app = SUIT_Session::session()->activeApplication();
263 // call application-specific "Save file" dialog box
264 QString aName = app->getFileName( false, QString::null, QString( "*.log" ), QString::null, 0 );
265 if ( aName.isNull() )
268 QApplication::setOverrideCursor( Qt::waitCursor );
270 bool bOk = saveLog( aName );
272 QApplication::restoreOverrideCursor();
275 SUIT_MessageBox::error1( this, tr( "Error" ), tr( "Can't save file" ), tr( "OK" ) );
279 SLOT: called if user click "Select all" in popup
281 void LogWindow::onSelectAll()
288 SLOT: called if user click "Clear" in popup
290 void LogWindow::onClear()
296 SLOT: called if user click "Copy" in popup
298 void LogWindow::onCopy()