From b67d555af6ecd18d03b967821ca13fbf8379a35c Mon Sep 17 00:00:00 2001 From: vsr Date: Tue, 15 May 2007 07:06:44 +0000 Subject: [PATCH] Porting to Qt4 --- src/LogWindow/LogWindow.cxx | 317 ++++++++++++++++++++++++------------ src/LogWindow/LogWindow.h | 95 ++++++----- src/LogWindow/LogWindow.pro | 23 +++ src/LogWindow/Makefile.am | 3 +- 4 files changed, 298 insertions(+), 140 deletions(-) create mode 100644 src/LogWindow/LogWindow.pro diff --git a/src/LogWindow/LogWindow.cxx b/src/LogWindow/LogWindow.cxx index 161011c45..201374785 100755 --- a/src/LogWindow/LogWindow.cxx +++ b/src/LogWindow/LogWindow.cxx @@ -1,66 +1,94 @@ // 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 -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include -#include #include #include +#include +#include #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(); @@ -68,13 +96,12 @@ SUIT_PopupClient() 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; @@ -87,28 +114,35 @@ SUIT_PopupClient() } /*! - 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 ) { @@ -118,8 +152,8 @@ 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 ) { @@ -129,26 +163,85 @@ 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( "%2" ).arg( color.name() ).arg( msg ); + + QString dStr; + if ( flags & DisplayDate ) + { + dStr = QDate::currentDate().toString( Qt::SystemLocaleDate ); + if ( !noColor ) + dStr = QString( "%1" ).arg( dStr ); + } + + QString tStr; + if ( flags & DisplayTime ) + { + tStr = QTime::currentTime().toString( Qt::SystemLocaleDate ); + if ( !noColor ) + tStr = QString( "%1" ).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 ) { @@ -159,20 +252,21 @@ 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 ); @@ -183,7 +277,7 @@ bool LogWindow::saveLog( const QString& fileName ) 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(); @@ -191,68 +285,67 @@ bool LogWindow::saveLog( const QString& fileName ) } /*! - 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() { @@ -265,8 +358,8 @@ void LogWindow::onSaveToFile() if ( aName.isNull() ) return; - QApplication::setOverrideCursor( Qt::waitCursor ); - + QApplication::setOverrideCursor( Qt::WaitCursor ); + bool bOk = saveLog( aName ); QApplication::restoreOverrideCursor(); @@ -276,7 +369,7 @@ void LogWindow::onSaveToFile() } /*! - SLOT: called if user click "Select all" in popup + \brief Called when user selects "Select all" command in the popup menu. */ void LogWindow::onSelectAll() { @@ -285,7 +378,7 @@ 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() { @@ -293,10 +386,32 @@ 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 +*/ diff --git a/src/LogWindow/LogWindow.h b/src/LogWindow/LogWindow.h index 0b8b39e98..0b6a3d285 100755 --- a/src/LogWindow/LogWindow.h +++ b/src/LogWindow/LogWindow.h @@ -20,64 +20,83 @@ // 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 -#include -#include +#include +#include +#include #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(); @@ -90,16 +109,16 @@ private: void updateActions(); private: - QTextBrowser* myView; - QString myBanner; - QString mySeparator; - QStringList myHistory; - int myBannerSize; - QMap 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 myActions; //!< popup menu actions }; #ifdef WIN32 #pragma warning( default:4251 ) #endif -#endif +#endif // LOGWINDOW_H diff --git a/src/LogWindow/LogWindow.pro b/src/LogWindow/LogWindow.pro new file mode 100644 index 000000000..22315f2df --- /dev/null +++ b/src/LogWindow/LogWindow.pro @@ -0,0 +1,23 @@ +TEMPLATE = lib +TARGET = LogWindow +DESTDIR = ../../lib +MOC_DIR = ../../moc +OBJECTS_DIR = ../../obj/$$TARGET + +INCLUDEPATH += ../../include ../Qtx ../SUIT +LIBS += -L../../lib -lqtx -lsuit + +CONFIG -= debug release debug_and_release +CONFIG += qt thread debug dll shared + +win32:DEFINES += WIN32 +DEFINES += LOGWINDOW_EXPORTS + +HEADERS = LogWindow.h + +SOURCES = LogWindow.cxx + +includes.files = $$HEADERS +includes.path = ../../include + +INSTALLS += includes diff --git a/src/LogWindow/Makefile.am b/src/LogWindow/Makefile.am index 83dcacd43..898f1deaf 100755 --- a/src/LogWindow/Makefile.am +++ b/src/LogWindow/Makefile.am @@ -34,6 +34,7 @@ nodist_libLogWindow_la_SOURCES = $(MOC_FILES) libLogWindow_la_CPPFLAGS=$(QT_INCLUDES) -I$(srcdir)/../SUIT -I$(srcdir)/../Qtx -libLogWindow_la_LDFLAGS=$(QT_MT_LIBS) ../SUIT/libsuit.la ../Qtx/libqtx.la +libLogWindow_la_LDFLAGS=$(QT_MT_LIBS) +libLogWindow_la_LIBADD=../Qtx/libqtx.la ../SUIT/libsuit.la -- 2.39.2