From 073e8a8ff0d02fa302cd3362c68dd1e163cf907a Mon Sep 17 00:00:00 2001 From: ana Date: Wed, 12 May 2010 11:39:27 +0000 Subject: [PATCH] Introduce "Dump commands" context popup menu action for Python console --- src/PyConsole/PyConsole_Console.cxx | 9 ++++ src/PyConsole/PyConsole_Console.h | 11 ++--- src/PyConsole/PyConsole_Editor.cxx | 50 +++++++++++++++++++++ src/PyConsole/PyConsole_Editor.h | 3 +- src/PyConsole/resources/PyConsole_msg_en.ts | 16 +++++++ 5 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/PyConsole/PyConsole_Console.cxx b/src/PyConsole/PyConsole_Console.cxx index fe29cafdb..bae645157 100644 --- a/src/PyConsole/PyConsole_Console.cxx +++ b/src/PyConsole/PyConsole_Console.cxx @@ -186,6 +186,8 @@ void PyConsole_Console::contextMenuPopup( QMenu* menu ) menu->addAction( myActions[ClearId] ); menu->addSeparator(); menu->addAction( myActions[SelectAllId] ); + menu->addSeparator(); + menu->addAction( myActions[DumpCommandsId] ); Qtx::simplifySeparators( menu ); @@ -206,6 +208,7 @@ void PyConsole_Console::setMenuActions( const int flags ) myActions[PasteId]->setVisible( flags & PasteId ); myActions[ClearId]->setVisible( flags & ClearId ); myActions[SelectAllId]->setVisible( flags & SelectAllId ); + myActions[DumpCommandsId]->setVisible( flags & DumpCommandsId ); } /*! @@ -220,6 +223,7 @@ int PyConsole_Console::menuActions() const ret = ret | ( myActions[PasteId]->isVisible() ? PasteId : 0 ); ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 ); ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 ); + ret = ret | ( myActions[DumpCommandsId]->isVisible() ? DumpCommandsId : 0 ); return ret; } @@ -249,6 +253,11 @@ void PyConsole_Console::createActions() a->setStatusTip( tr( "EDIT_SELECTALL_CMD" ) ); connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( selectAll() ) ); myActions.insert( SelectAllId, a ); + + a = new QAction( tr( "EDIT_DUMPCOMMANDS_CMD" ), this ); + a->setStatusTip( tr( "EDIT_DUMPCOMMANDS_CMD" ) ); + connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( dump() ) ); + myActions.insert( DumpCommandsId, a ); } /*! diff --git a/src/PyConsole/PyConsole_Console.h b/src/PyConsole/PyConsole_Console.h index 71dbf58a4..0666396fc 100644 --- a/src/PyConsole/PyConsole_Console.h +++ b/src/PyConsole/PyConsole_Console.h @@ -43,11 +43,12 @@ public: //! Context popup menu actions flags enum { - CopyId = 0x01, //!< "Copy" menu action - PasteId = 0x02, //!< "Paste" menu action - ClearId = 0x04, //!< "Clear" menu action - SelectAllId = 0x08, //!< "Select All" menu action - All = CopyId | PasteId | ClearId | SelectAllId //!< all menu actions + CopyId = 0x01, //!< "Copy" menu action + PasteId = 0x02, //!< "Paste" menu action + ClearId = 0x04, //!< "Clear" menu action + SelectAllId = 0x08, //!< "Select All" menu action + DumpCommandsId = 0x16, //!< "DumpCommands" menu action + All = CopyId | PasteId | ClearId | SelectAllId | DumpCommandsId //!< all menu actions }; public: diff --git a/src/PyConsole/PyConsole_Editor.cxx b/src/PyConsole/PyConsole_Editor.cxx index 6e0f9c9c5..c622c66a8 100644 --- a/src/PyConsole/PyConsole_Editor.cxx +++ b/src/PyConsole/PyConsole_Editor.cxx @@ -98,6 +98,9 @@ #include #include +#include +#include +#include #include #include @@ -109,6 +112,7 @@ #include #include #include +#include static QString READY_PROMPT = ">>> "; static QString DOTS_PROMPT = "... "; @@ -116,6 +120,27 @@ static QString DOTS_PROMPT = "... "; #define PRINT_EVENT 65432 + +class DumpCommandsFileValidator : public SUIT_FileValidator +{ + public: + DumpCommandsFileValidator( QWidget* parent = 0 ) : SUIT_FileValidator ( parent ) {}; + virtual ~DumpCommandsFileValidator() {}; + virtual bool canSave( const QString& file, bool permissions ); +}; + +bool DumpCommandsFileValidator::canSave(const QString& file, bool permissions) +{ + QFileInfo fi( file ); + if ( !QRegExp( "[A-Za-z_][A-Za-z0-9_]*" ).exactMatch( fi.completeBaseName() ) ) { + SUIT_MessageBox::critical( parent(), + QObject::tr("WRN_WARNING"), + QObject::tr("WRN_FILE_NAME_BAD") ); + return false; + } + return SUIT_FileValidator::canSave( file, permissions); +} + /*! \class ExecCommand \brief Python command execution request. @@ -1076,3 +1101,28 @@ void PyConsole_Editor::clear() myPrompt = READY_PROMPT; addText( myPrompt ); } + +/*! + \brief "Dump commands" operation. + */ +void PyConsole_Editor::dump() +{ + QStringList aFilters; + aFilters.append( tr( "PYTHON_FILES_FILTER" ) ); + + QString fileName = SUIT_FileDlg::getFileName( this, QString(), + aFilters, tr( "TOT_DUMP_PYCOMMANDS" ), + false, true, new DumpCommandsFileValidator( this ) ); + if ( fileName != "" ) { + QFile file( fileName ); + if ( !file.open( QFile::WriteOnly ) ) + return; + + QTextStream out (&file); + + for( int i=0; iEDIT_SELECTALL_CMD Select &All + + EDIT_DUMPCOMMANDS_CMD + D&ump commands + + + + + PyConsole_Editor + + TOT_DUMP_PYCOMMANDS + Dump commands + + + PYTHON_FILES_FILTER + PYTHON Files (*.py) + -- 2.39.2