]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
Introduce "Dump commands" context popup menu action for Python console
authorana <ana@opencascade.com>
Wed, 12 May 2010 11:39:27 +0000 (11:39 +0000)
committerana <ana@opencascade.com>
Wed, 12 May 2010 11:39:27 +0000 (11:39 +0000)
src/PyConsole/PyConsole_Console.cxx
src/PyConsole/PyConsole_Console.h
src/PyConsole/PyConsole_Editor.cxx
src/PyConsole/PyConsole_Editor.h
src/PyConsole/resources/PyConsole_msg_en.ts

index fe29cafdb336589be8af50879cc383731d410141..bae6451573514130de4b4b8add0a7ac53d2a905c 100644 (file)
@@ -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 );
 }
 
 /*!
index 71dbf58a407cc2e145a250c9e7d360855dbc2db5..0666396fc0b0e219c39c027a7955c0742ee3a5b3 100644 (file)
@@ -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:
index 6e0f9c9c57e112ac3afb51d7bc2013f0da04b376..c622c66a833e77a9b5339c12ba3d7d755187eb97 100644 (file)
@@ -98,6 +98,9 @@
 #include <PyInterp_Dispatcher.h>
 
 #include <SUIT_Tools.h>
+#include <SUIT_FileDlg.h>
+#include <SUIT_MessageBox.h>
+#include <SUIT_FileValidator.h>
 
 #include <QApplication>
 #include <QClipboard>
 #include <QTextBlock>
 #include <QTextCursor>
 #include <QTextDocument>
+#include <QTextStream>
 
 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; i<myHistory.count(); i++ ) {
+         out<<myHistory[i]<<endl;
+    }
+    file.close();
+  }
+}
index e07b86179cebca980aa191f4f0b57249fe349362..ae381467cd62b4413b2beae111fadd3ae06fcf19 100644 (file)
@@ -66,11 +66,12 @@ public slots:
   void           clear();
   void           handleReturn();
   void           onPyInterpChanged( PyConsole_Interp* );
+  void           dump();
   
 private:
   PyConsole_Interp* myInterp;           //!< python interpreter
 
-  QString           myCommandBuffer;    //!< python comman buffer
+  QString           myCommandBuffer;    //!< python command buffer
   QString           myCurrentCommand;   //!< currently being printed command
   QString           myPrompt;           //!< current command line prompt
   int               myCmdInHistory;     //!< current history command index
index 5e0a629cd00214c71918c534b0d43260e476a0ee..986b476b1a8ccdcc16e1661a604ef69855fa9730 100644 (file)
         <source>EDIT_SELECTALL_CMD</source>
         <translation>Select &amp;All</translation>
     </message>
+    <message>
+        <source>EDIT_DUMPCOMMANDS_CMD</source>
+        <translation>D&amp;ump commands</translation>
+    </message>
+</context>
+
+<context>
+    <name>PyConsole_Editor</name>
+    <message>
+        <source>TOT_DUMP_PYCOMMANDS</source>
+        <translation>Dump commands</translation>
+    </message>
+    <message>
+       <source>PYTHON_FILES_FILTER</source>
+       <translation>PYTHON Files (*.py)</translation>
+    </message>
 </context>
 </TS>