Skip to content

Commit

Permalink
Port UserSubWindow to use PythonRunner object. Refs #7468
Browse files Browse the repository at this point in the history
Isolates the code that does the python execution and output capture.
  • Loading branch information
martyngigg committed Jul 17, 2013
1 parent 364fbc4 commit 651e0e3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
4 changes: 4 additions & 0 deletions Code/Mantid/MantidQt/API/inc/MantidQtAPI/UserSubWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//----------------------------------
#include "DllOption.h"
#include "InterfaceFactory.h"
#include "PythonRunner.h"

#include <QWidget>
#include <QStringList>
Expand Down Expand Up @@ -153,6 +154,9 @@ class EXPORT_OPT_MANTIDQT_API UserSubWindow : public QWidget
bool m_isPyInitialized;
/// Store the name of the interface
QString m_ifacename;

/// Python executor
PythonRunner m_pythonRunner;
};

}
Expand Down
39 changes: 7 additions & 32 deletions Code/Mantid/MantidQt/API/src/UserSubWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ using namespace MantidQt::API;
* Default Constructor
*/
UserSubWindow::UserSubWindow(QWidget* parent) :
QWidget(parent), m_bIsInitialized(false), m_isPyInitialized(false), m_ifacename("")
QWidget(parent), m_bIsInitialized(false), m_isPyInitialized(false), m_ifacename(""), m_pythonRunner()
{
setAttribute(Qt::WA_DeleteOnClose, false);

// re-emit the run Python code from m_pyRunner, to work this signal must reach the slot in QtiPlot
connect(&m_pythonRunner, SIGNAL(runAsPythonScript(const QString&, bool)),
this, SIGNAL(runAsPythonScript(const QString&, bool)));

}

/**
Expand Down Expand Up @@ -110,37 +115,7 @@ void UserSubWindow::showInformationBox(const QString & message) const
*/
QString UserSubWindow::runPythonCode(const QString & code, bool no_output)
{
if( no_output )
{
emit runAsPythonScript(code, true);
return QString();
}

// Otherwise we need to gather the information from stdout. This is achieved by redirecting the stdout stream
// to a temporary file and then reading its contents
// A QTemporaryFile object is used since the file is automatically deleted when the object goes out of scope
QTemporaryFile tmp_file;
if( !tmp_file.open() )
{
showInformationBox("An error occurred opening a temporary file in " + QDir::tempPath());
return QString();
}
//The file name is only valid when the file is open
QString tmpstring = tmp_file.fileName();
tmp_file.close();
QString code_to_run = "import sys; sys.stdout = open('" + tmpstring + "', 'w')\n" + code;

emit runAsPythonScript(code_to_run, true);

//Now get the output
tmp_file.open();
QTextStream stream(&tmp_file);
tmpstring.clear();
while( !stream.atEnd() )
{
tmpstring.append(stream.readLine().trimmed() + "\n");
}
return tmpstring;
return m_pythonRunner.runPythonCode(code,no_output);
}

/**
Expand Down

0 comments on commit 651e0e3

Please sign in to comment.