Salome HOME
0022652: [CEA 1194] Redirect the traces from embedded Python console in a log file
authorimn <imn@opencascade.com>
Wed, 17 Sep 2014 08:34:18 +0000 (12:34 +0400)
committerimn <imn@opencascade.com>
Wed, 17 Sep 2014 08:34:18 +0000 (12:34 +0400)
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
src/PyConsole/resources/PyConsole_msg_fr.ts
src/PyConsole/resources/PyConsole_msg_ja.ts

index a14eca1d0c976acb9dff9d577030187b23421e8a..c613866d1e10cadf7cec28eb0739f42e6a8eba8e 100644 (file)
@@ -248,6 +248,7 @@ void PyConsole_Console::contextMenuPopup( QMenu* menu )
   menu->addAction( myActions[SelectAllId] );
   menu->addSeparator();
   menu->addAction( myActions[DumpCommandsId] );
+  menu->addAction( myActions[SaveLogId] );
 
   Qtx::simplifySeparators( menu );
 
@@ -269,6 +270,7 @@ void PyConsole_Console::setMenuActions( const int flags )
   myActions[ClearId]->setVisible( flags & ClearId );
   myActions[SelectAllId]->setVisible( flags & SelectAllId );
   myActions[DumpCommandsId]->setVisible( flags & DumpCommandsId );
+  myActions[SaveLogId]->setVisible( flags & SaveLogId );
 }
 
 /*!
@@ -284,6 +286,7 @@ int PyConsole_Console::menuActions() const
   ret = ret | ( myActions[ClearId]->isVisible() ? ClearId : 0 );
   ret = ret | ( myActions[SelectAllId]->isVisible() ? SelectAllId : 0 );
   ret = ret | ( myActions[DumpCommandsId]->isVisible() ? DumpCommandsId : 0 );
+  ret = ret | ( myActions[SaveLogId]->isVisible() ? SaveLogId : 0 );
   return ret;
 }
 
@@ -318,6 +321,11 @@ void PyConsole_Console::createActions()
   a->setStatusTip( tr( "EDIT_DUMPCOMMANDS_CMD" ) );
   connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( dump() ) );
   myActions.insert( DumpCommandsId, a );
+
+  a = new QAction( tr( "EDIT_SAVELOG_CMD" ), this );
+  a->setStatusTip( tr( "EDIT_SAVELOG_CMD" ) );
+  connect( a, SIGNAL( triggered( bool ) ), myEditor, SLOT( saveLog() ) );
+  myActions.insert( SaveLogId, a );
 }
 
 /*!
index 75163a9ae1e4b877d34412f3e82f31ae4a0c1f95..783c6567a1b7cec292a18863ba329d37e6cf9f1b 100644 (file)
@@ -44,12 +44,13 @@ 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
-    DumpCommandsId = 0x16,                                           //!< "DumpCommands" menu action
-    All = CopyId | PasteId | ClearId | SelectAllId | DumpCommandsId  //!< 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 = 0x10,                                                      //!< "DumpCommands" menu action
+    SaveLogId      = 0x20,                                                      //!< "Save log" menu action
+    All = CopyId | PasteId | ClearId | SelectAllId | DumpCommandsId | SaveLogId //!< all menu actions
   };
 
 public:
index 09a3455405723fa69b93fcceb4a5571dfc2d89d0..29c8d1ade656e92f2b9b74a7834035daaa5a82ea 100644 (file)
@@ -339,8 +339,11 @@ void PyConsole_Editor::exec( const QString& command )
   if ( !cmd.endsWith( "\n" ) ) cmd += "\n";
   QStringList lines = command.split( "\n" );
   for ( int i = 0; i < lines.size(); i++ ) {
-    if ( !lines[i].trimmed().isEmpty() )
-      myHistory.push_back( lines[i] );
+    if ( !lines[i].trimmed().isEmpty() ) {
+      PyCommand aCommand;
+      aCommand.command = lines[i];
+      myHistory.append( aCommand );
+    }
     addText( ( i == 0 ? READY_PROMPT : DOTS_PROMPT ) + lines[i], i != 0 );
   }
   // IPAL20182
@@ -409,8 +412,11 @@ void PyConsole_Editor::handleReturn()
   // extend the command buffer with the current command 
   myCommandBuffer.append( cmd );
   // add command to the history
-  if ( !cmd.trimmed().isEmpty() )
-    myHistory.push_back( cmd );
+  if ( !cmd.trimmed().isEmpty() ) {
+    PyCommand aCommand;
+    aCommand.command = cmd;
+    myHistory.append( aCommand );
+  }
 
   // IPAL19397
   addText( "", true ); 
@@ -596,7 +602,7 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event )
         if ( myCmdInHistory > 0 ) {
           myCmdInHistory--;
           // get previous command in the history
-          QString previousCommand = myHistory.at( myCmdInHistory );
+          QString previousCommand = myHistory.at( myCmdInHistory ).command;
           // print previous command
           moveCursor( QTextCursor::End );
           moveCursor( QTextCursor::StartOfBlock, QTextCursor::KeepAnchor );
@@ -633,7 +639,7 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event )
           QString nextCommand;
           if ( myCmdInHistory < myHistory.count() ) {
             // next command in history
-            nextCommand = myHistory.at( myCmdInHistory );
+            nextCommand = myHistory.at( myCmdInHistory ).command;
           }
           else {
             // end of history is reached
@@ -740,7 +746,7 @@ void PyConsole_Editor::keyPressEvent( QKeyEvent* event )
         if ( myCmdInHistory > 0 ) {
           myCmdInHistory = 0;
           // get very first command in the history
-          QString firstCommand = myHistory.at( myCmdInHistory );
+          QString firstCommand = myHistory.at( myCmdInHistory ).command;
           // print first command
           moveCursor( QTextCursor::End );
           moveCursor( QTextCursor::StartOfBlock, QTextCursor::KeepAnchor );
@@ -935,6 +941,7 @@ void PyConsole_Editor::customEvent( QEvent* event )
     {
       PrintEvent* pe=(PrintEvent*)event;
       addText( pe->text(), false, pe->isError());
+      myHistory.last().output = myHistory.last().output + pe->text();
       return;
     }
   case PyInterp_Event::ES_OK:
@@ -1126,7 +1133,32 @@ void PyConsole_Editor::dump()
     QTextStream out (&file);
   
     for( int i=0; i<myHistory.count(); i++ ) {
-         out<<myHistory[i]<<endl;
+         out<<myHistory.at(i).command<<endl;
+    }
+    file.close();
+  }
+}
+/*!
+  \brief "Save log" operation.
+ */
+void PyConsole_Editor::saveLog()
+{
+  QStringList aFilters;
+  aFilters.append( tr( "PYTHON_FILES_FILTER" ) );
+
+  QString fileName = SUIT_FileDlg::getFileName( this, QString(),
+                     aFilters, tr( "TOT_SAVE_PYLOG" ),
+                 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.at(i).command << endl;
+         out << myHistory.at(i).output;
     }
     file.close();
   }
index c3e5ca1dd59e43ac0f8031ff8fc97611ee26510f..e9d5ad2ccaefbacd4e382d340123268701294a1b 100644 (file)
@@ -35,6 +35,10 @@ class PyConsole_Interp;
 class PyInterp_Request;
 class QEventLoop;
 
+typedef struct {
+  QString command, output;
+} PyCommand;
+
 class PYCONSOLE_EXPORT PyConsole_Editor : public QTextEdit
 {
   Q_OBJECT;
@@ -67,6 +71,7 @@ public slots:
     void           handleReturn();
     void           onPyInterpChanged( PyConsole_Interp* );
     void           dump();
+    void           saveLog();
 
 protected:
   virtual void   dropEvent( QDropEvent* event );
@@ -85,7 +90,7 @@ protected:
   QString           myCurrentCommand;   //!< currently being printed command
   QString           myPrompt;           //!< current command line prompt
   int               myCmdInHistory;     //!< current history command index
-  QStringList       myHistory;          //!< commands history buffer
+  QList<PyCommand>  myHistory;          //!< commands history buffer
   QEventLoop*       myEventLoop;        //!< internal event loop
   QString           myBanner;           //!< current banner
   bool              myShowBanner;       //!< 'show banner' flag
index 85393a2bf43151b66038132a164f075b373c5955..bb060d8a2bc89f8124961c2027ba2d3d3f254ac5 100644 (file)
         <source>EDIT_DUMPCOMMANDS_CMD</source>
         <translation>D&amp;ump commands</translation>
     </message>
+    <message>
+        <source>EDIT_SAVELOG_CMD</source>
+        <translation>&amp;Save log</translation>
+    </message>
 </context>
 <context>
     <name>PyConsole_Editor</name>
         <source>TOT_DUMP_PYCOMMANDS</source>
         <translation>Dump commands</translation>
     </message>
+    <message>
+        <source>TOT_SAVE_PYLOG</source>
+        <translation>Save log</translation>
+    </message>
     <message>
         <source>PYTHON_FILES_FILTER</source>
         <translation>PYTHON Files (*.py)</translation>
index 91091156235c46f2acc544d096440651a337557b..5c7ba5fe1ad4b490e9a5595271d73a5a03805684 100755 (executable)
         <source>EDIT_DUMPCOMMANDS_CMD</source>
         <translation>&amp;Générer le script des commandes</translation>
     </message>
+    <message>
+        <source>EDIT_SAVELOG_CMD</source>
+        <translation type="unfinished">&amp;Save log</translation>
+    </message>
 </context>
 <context>
     <name>PyConsole_Editor</name>
         <source>TOT_DUMP_PYCOMMANDS</source>
         <translation>&amp;Générer le script des commandes</translation>
     </message>
+    <message>
+        <source>TOT_SAVE_PYLOG</source>
+        <translation type="unfinished">Save log</translation>
+    </message>
     <message>
         <source>PYTHON_FILES_FILTER</source>
         <translation>Fichiers PYTHON (*.py)</translation>
index dde0da06bddca9533ee8f63b5959df4c74dcacd1..82c2bbdb50352c1b0261d7d23025c976ca9f7596 100644 (file)
       <source>EDIT_DUMPCOMMANDS_CMD</source>
       <translation>スクリプト コマンドを生成します。(&amp;u)</translation>
     </message>
+    <message>
+        <source>EDIT_SAVELOG_CMD</source>
+        <translation type="unfinished">&amp;Save log</translation>
+    </message>
   </context>
   <context>
     <name>PyConsole_Editor</name>
       <source>TOT_DUMP_PYCOMMANDS</source>
       <translation>スクリプト コマンドを生成します。</translation>
     </message>
+    <message>
+        <source>TOT_SAVE_PYLOG</source>
+        <translation type="unfinished">Save log</translation>
+    </message>
     <message>
       <source>PYTHON_FILES_FILTER</source>
       <translation>ファイル (*.py) PYTHON</translation>