// KERNEL SALOME_Event : Define event posting mechanism
//
// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
-//
-// This library is free software; you can redistribute it and/or
-// modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either
-// version 2.1 of the License.
-//
-// This library is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-// Lesser General Public License for more details.
-//
-// You should have received a copy of the GNU Lesser General Public
-// License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-//
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+// File : LogWindow.cxx
+// Author : Vadim SANDLER, Open CASCADE S.A. (vadim.sandler@opencascade.com)
+// Module : SALOME
#include "LogWindow.h"
-#include <qfile.h>
-#include <qlayout.h>
-#include <qaction.h>
-#include <qpopupmenu.h>
-#include <qtextbrowser.h>
-#include <qapplication.h>
-#include <qdatetime.h>
+#include <QAction>
+#include <QApplication>
+#include <QDate>
+#include <QFile>
+#include <QMenu>
+#include <QTextEdit>
+#include <QTextStream>
+#include <QTime>
+#include <QVBoxLayout>
-#include <SUIT_Tools.h>
-#include <SUIT_Session.h>
#include <SUIT_MessageBox.h>
#include <SUIT_ResourceMgr.h>
+#include <SUIT_Session.h>
+#include <SUIT_Tools.h>
#define DEFAULT_SEPARATOR "***"
/*!
- Converts rich text to plain text
+ \brief Convert rich text to plain text.
+ \internal
+ \param richText rich text string
+ \return converted plain text string
*/
static QString plainText( const QString& richText )
{
QString aText = richText;
- int startTag = aText.find('<');
- while ( 1 ) {
+ int startTag = aText.indexOf( '<' );
+ while ( true )
+ {
if ( startTag < 0 )
break;
- int finishTag = aText.find('>',startTag);
- if (finishTag < 0)
+
+ int finishTag = aText.indexOf( '>', startTag );
+ if ( finishTag < 0 )
break;
- aText = aText.remove(startTag, finishTag-startTag+1);
- startTag = aText.find('<');
+
+ aText = aText.remove( startTag, finishTag - startTag + 1 );
+ startTag = aText.indexOf( '<' );
}
return aText;
}
/*!
- Default constructor
+ \class LogWindow
+ \brief Widget, displaying log messages.
+
+ The log messages window provides operations like:
+ - show messages
+ - display timestamps at the message beginning
+ - color messages according to their purposes (e.g., errors/warning)
+ - clear log output
+ - copy messages to clipvoard
+ - save message log to to the text file
+*/
+
+/*!
+ \brief Constructor.
+
+ Creates new messages log window widget.
+ \param parent parent widget
*/
LogWindow::LogWindow( QWidget* parent )
: QFrame( parent ),
-SUIT_PopupClient()
+ SUIT_PopupClient()
{
SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
setFont( SUIT_Tools::stringToFont( fntSet ) );
- myView = new QTextBrowser(this,"myView");
-#if QT_VERSION>0x030007
- myView->setTextFormat( Qt::LogText );
-#endif
+ myView = new QTextEdit( this );
+ myView->setReadOnly( true );
myView->viewport()->installEventFilter( this );
QVBoxLayout* main = new QVBoxLayout( this );
+ main->setMargin( 5 );
main->addWidget( myView );
myBannerSize = 0;
}
/*!
- Destructor
+ \brief Destructor.
+
+ Does nothing for the moment.
*/
LogWindow::~LogWindow()
{
}
/*!
- Custom event handler
+ \brief Get current banner (message log window header text).
+ \return string representing the current banner
*/
-bool LogWindow::eventFilter( QObject* o, QEvent* e )
+QString LogWindow::banner() const
{
- if ( o == myView->viewport() && e->type() == QEvent::ContextMenu )
- {
- contextMenuRequest( (QContextMenuEvent*)e );
- return true;
- }
- return QFrame::eventFilter( o, e );
+ return myBanner;
+}
+
+/*!
+ \brief Get current separator (text which is printed between messages).
+ \return string representing the current separator
+*/
+QString LogWindow::separator() const
+{
+ return mySeparator;
}
/*!
- Sets banner (title of message log)
- \param banner - new title
+ \brief Set current banner (message log window header text).
+ \param banner new banner
*/
void LogWindow::setBanner( const QString& banner )
{
}
/*!
- Set separator (line printing between messages)
- \param separator - new separator
+ Set current separator (text which is printed between messages).
+ \param separator new separator
*/
void LogWindow::setSeparator( const QString& separator )
{
}
/*!
- Puts message to log window
- \param message - text of message
- \addSeparator - if it is true, then separator is added to tail of message log
+ \brief Custom event handler.
+
+ Process context popup menu request event.
+
+ \param o object
+ \param e event
+ \return True if the event is processed and further processing should be stopped
+*/
+bool LogWindow::eventFilter( QObject* o, QEvent* e )
+{
+ if ( o == myView->viewport() && e->type() == QEvent::ContextMenu )
+ {
+ contextMenuRequest( (QContextMenuEvent*)e );
+ return true;
+ }
+ return QFrame::eventFilter( o, e );
+}
+
+/*!
+ \brief Put new message to the log window.
+ \param message text of the message
+ \param flags ORed flags which define how the message should be printed
+*/
+void LogWindow::putMessage( const QString& message, const int flags )
+{
+ putMessage( message, QColor(), flags );
+}
+
+/*!
+ \brief Put new message to the log window.
+ \param message text of the message
+ \param color text color of the message
+ \param flags ORed flags which define how the message should be printed
*/
-void LogWindow::putMessage( const QString& message, bool addSeparator )
+void LogWindow::putMessage( const QString& message, const QColor& color, const int flags )
{
- myView->append( message );
+ QString msg = message;
+ if ( msg.isEmpty() )
+ return;
+
+ bool noColor = flags & DisplayNoColor;
+
+ if ( color.isValid() )
+ msg = QString( "<font color=\"%1\">%2</font>" ).arg( color.name() ).arg( msg );
+
+ QString dStr;
+ if ( flags & DisplayDate )
+ {
+ dStr = QDate::currentDate().toString( Qt::SystemLocaleDate );
+ if ( !noColor )
+ dStr = QString( "<font color=\"#003380\">%1</font>" ).arg( dStr );
+ }
+
+ QString tStr;
+ if ( flags & DisplayTime )
+ {
+ tStr = QTime::currentTime().toString( Qt::SystemLocaleDate );
+ if ( !noColor )
+ tStr = QString( "<font color=\"#008033\">%1</font>" ).arg( tStr );
+ }
+
+ QString dateTime = QString( "%1 %2" ).arg( dStr ).arg( tStr ).trimmed();
+ if ( !dateTime.isEmpty() )
+ msg = QString( "[%1] %2" ).arg( dateTime ).arg( msg );
+
+ myView->append( msg );
myHistory.append( plainText( message ) );
- if ( addSeparator && !mySeparator.isNull() )
+ if ( flags & DisplaySeparator && !mySeparator.isEmpty() )
{
myView->append( mySeparator ); // add separator
myHistory.append( plainText( mySeparator ) );
}
- myView->scrollToBottom();
+ myView->moveCursor( QTextCursor::End );
}
/*!
- Clears message log
- \param clearHistory - if it is true, then also history is cleared
+ \brief Clear message log.
+ \param clearHistory if True, clear also the messages history
*/
void LogWindow::clear( bool clearHistory )
{
if ( !myBanner.isEmpty() )
{
myView->append( myBanner );
- myBannerSize = myView->paragraphs();
+ myBannerSize = myView->document()->blockCount();
}
else
myBannerSize = 0;
}
/*!
- Saves log to file
- \param fileName - name of file
+ \brief Save messages log to the file.
+ \param fileName name of the file
+ \return \c true on success and \c false on error
*/
bool LogWindow::saveLog( const QString& fileName )
{
QFile file( fileName );
- if ( !file.open( IO_WriteOnly ) )
+ if ( !file.open( QFile::WriteOnly ) )
return false;
QTextStream stream( &file );
stream << QTime::currentTime().toString( "hh:mm:ss" ) << endl;
stream << "*****************************************" << endl;
- for ( uint i = 0; i < myHistory.count(); i++ )
+ for ( int i = 0; i < myHistory.count(); i++ )
stream << myHistory[ i ] << endl;
file.close();
}
/*!
- Creates actions
+ \brief Create context popup menu actions.
*/
void LogWindow::createActions()
{
- QAction* a = new QAction( "", tr( "&Copy" ), 0, this );
+ QAction* a = new QAction( tr( "&Copy" ), this );
a->setStatusTip( tr( "&Copy" ) );
- connect( a, SIGNAL( activated() ), SLOT( onCopy()));
+ connect( a, SIGNAL( triggered( bool ) ), SLOT( onCopy() ) );
myActions.insert( CopyId, a );
- a = new QAction( "", tr( "Clea&r" ), 0, this );
+ a = new QAction( tr( "Clea&r" ), this );
a->setStatusTip( tr( "Clea&r" ) );
- connect( a, SIGNAL( activated() ), SLOT( onClear()));
+ connect( a, SIGNAL( triggered( bool ) ), SLOT( onClear() ) );
myActions.insert( ClearId, a );
- a = new QAction( "", tr( "Select &All" ), 0, this );
+ a = new QAction( tr( "Select &All" ), this );
a->setStatusTip( tr( "Select &All" ) );
- connect( a, SIGNAL( activated() ), SLOT( onSelectAll()));
+ connect( a, SIGNAL( triggered( bool ) ), SLOT( onSelectAll() ) );
myActions.insert( SelectAllId, a );
- a = new QAction( "", tr( "&Save log to file..." ), 0, this );
+ a = new QAction( tr( "&Save log to file..." ), this );
a->setStatusTip( tr( "&Save log to file..." ) );
- connect( a, SIGNAL( activated() ), SLOT( onSaveToFile()));
+ connect( a, SIGNAL( triggered( bool ) ), SLOT( onSaveToFile() ) );
myActions.insert( SaveToFileId, a );
}
/*!
- Redefined virtual method for popup filling
+ \brief Create the context popup menu.
+
+ Fill in the popup menu with the commands.
+
+ \param menu context popup menu
*/
-void LogWindow::contextMenuPopup( QPopupMenu* popup )
+void LogWindow::contextMenuPopup( QMenu* popup )
{
- myActions[ CopyId ]->addTo( popup );
- myActions[ ClearId ]->addTo( popup );
-
- popup->insertSeparator();
-
- myActions[ SelectAllId ]->addTo( popup );
-
- popup->insertSeparator();
-
- myActions[ SaveToFileId ]->addTo( popup );
+ popup->addAction( myActions[ CopyId ] );
+ popup->addAction( myActions[ ClearId ] );
+ popup->addSeparator();
+ popup->addAction( myActions[ SelectAllId ] );
+ popup->addSeparator();
+ popup->addAction( myActions[ SaveToFileId ] );
+
+ Qtx::simplifySeparators( popup );
updateActions();
}
/*!
- Updates enable status of actions
+ \brief Update menu actions.
+
+ Update context popup menu action state.
*/
void LogWindow::updateActions()
{
- int paraFrom, paraTo, indexFrom, indexTo;
- myView->getSelection( ¶From, &indexFrom, ¶To, &indexTo );
- bool allSelected = myView->hasSelectedText() &&
- !paraFrom && paraTo == myView->paragraphs() - 1 &&
- !indexFrom && indexTo == myView->paragraphLength( paraTo );
- myActions[ CopyId ]->setEnabled( myView->hasSelectedText() );
- myActions[ ClearId ]->setEnabled( myView->paragraphs() > myBannerSize );
- myActions[ SelectAllId ]->setEnabled( !allSelected );
+ myActions[CopyId]->setEnabled( myView->textCursor().hasSelection() );
+ myActions[ ClearId ]->setEnabled( myView->document()->blockCount() > myBannerSize );
+ myActions[SelectAllId]->setEnabled( !myView->document()->isEmpty() );
myActions[ SaveToFileId ]->setEnabled( myHistory.count() > 0 );
}
/*!
- SLOT: called if user click "Save" in popup
+ \brief Called when user selects "Save To File" command in the popup menu.
*/
void LogWindow::onSaveToFile()
{
if ( aName.isNull() )
return;
- QApplication::setOverrideCursor( Qt::waitCursor );
-
+ QApplication::setOverrideCursor( Qt::WaitCursor );
+
bool bOk = saveLog( aName );
QApplication::restoreOverrideCursor();
}
/*!
- SLOT: called if user click "Select all" in popup
+ \brief Called when user selects "Select all" command in the popup menu.
*/
void LogWindow::onSelectAll()
{
}
/*!
- SLOT: called if user click "Clear" in popup
+ \brief Called when user click "Clear" command in the popup menu.
*/
void LogWindow::onClear()
{
}
/*!
- SLOT: called if user click "Copy" in popup
+ \brief Called when user click "Copy" command in the popup menu.
*/
void LogWindow::onCopy()
{
if ( myView )
myView->copy();
}
+
+/*!
+ \brief Set actions to be visible in the context popup menu.
+
+ Actions, which IDs are set in \a flags parameter, will be shown in the
+ context popup menu. Other actions will not be shown.
+
+ \param ORed together actions flags
+*/
+void LogWindow::setMenuActions( const int flags )
+{
+ myActions[CopyId]->setVisible( flags & CopyId );
+ myActions[ClearId]->setVisible( flags & ClearId );
+ myActions[SelectAllId]->setVisible( flags & SelectAllId );
+ myActions[SaveToFileId]->setVisible( flags & SaveToFileId );
+}
+
+/*!
+ \fn virtual QString LogWindow::popupClientType() const;
+ \brief Get popup client symbolic name, used in popup menu management system.
+ \return symbolic name
+*/
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
//
// File : LogWindow.h
-// Author :
-// Module : KERNEL
+// Author : Vadim SANDLER, Open CASCADE S.A. (vadim.sandler@opencascade.com)
+// Module : SALOME
-#ifndef LogWindow_HeaderFile
-#define LogWindow_HeaderFile
+#ifndef LOGWINDOW_H
+#define LOGWINDOW_H
-#if defined LOGWINDOW_EXPORTS
#if defined WIN32
-#define LOGWINDOW_EXPORT __declspec( dllexport )
+# if defined LOGWINDOW_EXPORTS
+# define LOGWINDOW_EXPORT __declspec( dllexport )
+# else
+# define LOGWINDOW_EXPORT __declspec( dllimport )
+# endif
#else
-#define LOGWINDOW_EXPORT
-#endif
-#else
-#if defined WIN32
-#define LOGWINDOW_EXPORT __declspec( dllimport )
-#else
-#define LOGWINDOW_EXPORT
-#endif
+# define LOGWINDOW_EXPORT
#endif
#include <SUIT_PopupClient.h>
-#include <qframe.h>
-#include <qstringlist.h>
+#include <QFrame>
+#include <QMap>
+#include <QStringList>
#ifdef WIN32
#pragma warning( disable:4251 )
#endif
class QAction;
-class QTextBrowser;
+class QTextEdit;
-/*!
- \class LogWindow
- Widget, showing logs message. Allows to show, to clear, to copy messages and to save then to file
-*/
class LOGWINDOW_EXPORT LogWindow : public QFrame, public SUIT_PopupClient
{
Q_OBJECT
- enum { CopyId, ClearId, SelectAllId, SaveToFileId };
+public:
+ //! Context popup menu actions flags
+ enum
+ {
+ CopyId = 0x01, //!< "Copy" menu action
+ ClearId = 0x02, //!< "Clear" menu action
+ SelectAllId = 0x04, //!< "Select All" menu action
+ SaveToFileId = 0x08, //!< "Save To File" menu action
+ All = CopyId | ClearId | SelectAllId | SaveToFileId //!< all menu actions
+ };
+
+ //! Display messages flags
+ enum
+ {
+ DisplayNormal = 0x00, //!< do not display extra data
+ DisplayDate = 0x01, //!< display message date
+ DisplayTime = 0x02, //!< display message time
+ DisplaySeparator = 0x04, //!< display separator between messages
+ DisplayNoColor = 0x08, //!< display colored message
+ DisplayDateTime = DisplayDate | DisplayTime //!< display date & time
+ };
public:
- LogWindow( QWidget* theParent );
- virtual ~LogWindow();
+ LogWindow( QWidget* theParent );
+ virtual ~LogWindow();
virtual QString popupClientType() const { return QString( "LogWindow" ); }
- virtual void contextMenuPopup( QPopupMenu* );
+ virtual void contextMenuPopup( QMenu* );
+
+ virtual bool eventFilter( QObject*, QEvent* );
- bool eventFilter( QObject* o, QEvent* e );
+ QString banner() const;
+ QString separator() const;
- void setBanner( const QString& banner );
- void setSeparator( const QString& separator );
+ void setBanner( const QString& );
+ void setSeparator( const QString& );
- virtual void putMessage( const QString& message, bool addSeparator = true );
- void clear( bool clearHistory = false );
+ void putMessage( const QString&, const int = DisplayNormal );
+ virtual void putMessage( const QString&, const QColor&, const int = DisplayNormal );
+ void clear( const bool = false );
- bool saveLog( const QString& fileName );
+ bool saveLog( const QString& );
+
+ void setMenuActions( const int );
protected slots:
void onSaveToFile();
void updateActions();
private:
- QTextBrowser* myView;
- QString myBanner;
- QString mySeparator;
- QStringList myHistory;
- int myBannerSize;
- QMap<int, QAction*> myActions;
+ QTextEdit* myView; //!< internal view window
+ QString myBanner; //!< current banner
+ QStringList myHistory; //!< messages history
+ QString mySeparator; //!< current separator
+ int myBannerSize; //!< current banner's size
+ QMap<int, QAction*> myActions; //!< popup menu actions
};
#ifdef WIN32
#pragma warning( default:4251 )
#endif
-#endif
+#endif // LOGWINDOW_H