Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
One more fix for redirecting output when python console is open
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8503 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed May 23, 2008
1 parent 74c788d commit 46acd23
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/app/qgspythondialog.cpp
Expand Up @@ -17,15 +17,14 @@
#include "qgspythondialog.h"
#include "qgspythonutils.h"

#include <QShowEvent>
#include <QCloseEvent>

QgsPythonDialog::QgsPythonDialog(QgisInterface* pIface, QWidget *parent)
: QDialog(parent)
{
setupUi(this);
mIface = pIface;

QgsPythonUtils::installConsoleHooks();
}

QgsPythonDialog::~QgsPythonDialog()
Expand All @@ -46,8 +45,7 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
// we're using custom hooks for output and exceptions to show output in console
if (QgsPythonUtils::runStringUnsafe(command))
{
QgsPythonUtils::evalString("sys.stdout.data", output);
QgsPythonUtils::runString("sys.stdout.data = ''");
QgsPythonUtils::evalString("sys.stdout.get_and_clean_data()", output);
QString result = QgsPythonUtils::getResult();
// escape the result so python objects display properly and
// we can still use html output to get nicely formatted display
Expand All @@ -74,6 +72,13 @@ void QgsPythonDialog::on_edtCmdLine_returnPressed()
txtHistory->ensureCursorVisible();
}

void QgsPythonDialog::showEvent(QShowEvent* event)
{
QDialog::showEvent(event);

QgsPythonUtils::installConsoleHooks();
}

void QgsPythonDialog::closeEvent(QCloseEvent* event)
{
QgsPythonUtils::uninstallConsoleHooks();
Expand Down
4 changes: 3 additions & 1 deletion src/app/qgspythondialog.h
Expand Up @@ -21,6 +21,7 @@

class QgisInterface;
class QCloseEvent;
class QShowEvent;

class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog
{
Expand All @@ -40,7 +41,8 @@ class QgsPythonDialog : public QDialog, private Ui::QgsPythonDialog
protected:

void closeEvent(QCloseEvent* event);

void showEvent(QShowEvent* event);

private:

QgisInterface* mIface;
Expand Down
19 changes: 11 additions & 8 deletions src/app/qgspythonutils.cpp
Expand Up @@ -97,6 +97,16 @@ void QgsPythonUtils::initPython(QgisInterface* interface)
runString(
"def qgis_except_hook(type, value, tb):\n"
" qgis_except_hook_msg(type, value, tb, None)\n");
runString(
"class QgisOutputCatcher:\n"
" def __init__(self):\n"
" self.data = ''\n"
" def write(self, stuff):\n"
" self.data += stuff\n"
" def get_and_clean_data(self):\n"
" tmp = self.data\n"
" self.data = ''\n"
" return tmp\n");

// hook for python console so all output will be redirected
// and then shown in console
Expand Down Expand Up @@ -135,15 +145,8 @@ void QgsPythonUtils::installConsoleHooks()
{
runString("sys.displayhook = console_display_hook\n");

runString(
"class MyOutputCatcher:\n"
" def __init__(self):\n"
" self.data = ''\n"
" def write(self, stuff):\n"
" self.data += stuff\n");
runString("_old_stdout = sys.stdout\n");
runString("sys.stdout = MyOutputCatcher()\n");

runString("sys.stdout = QgisOutputCatcher()\n");
}

void QgsPythonUtils::uninstallConsoleHooks()
Expand Down

0 comments on commit 46acd23

Please sign in to comment.