1
/***************************************************************************
2
qgspythondialog.h - dialog with embedded python console
5
copyright : (C) 2006 by Martin Dobias
6
email : wonder.sk at gmail dot com
7
***************************************************************************
9
* This program is free software; you can redistribute it and/or modify *
10
* it under the terms of the GNU General Public License as published by *
11
* the Free Software Foundation; either version 2 of the License, or *
12
* (at your option) any later version. *
14
***************************************************************************/
17
#include "qgspythondialog.h"
18
#include "qgspythonutils.h"
19
#include "qgslogger.h"
22
#include <QCloseEvent>
24
QgsPythonDialog::QgsPythonDialog( QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent )
29
// Qt4.3+ bug?: Mac window minimize control isn't enabled
30
setWindowFlags( windowFlags() | Qt::WindowMinimizeButtonHint );
33
mPythonUtils = pythonUtils;
38
QgsPythonDialog::~QgsPythonDialog()
42
QString QgsPythonDialog::escapeHtml( QString text )
44
return text.replace( "<", "<" ).replace( ">", ">" );
47
void QgsPythonDialog::on_pbnPrev_clicked()
51
if ( pos == history.size() )
52
history << edtCmdLine->toPlainText();
54
history[pos] = edtCmdLine->toPlainText();
56
edtCmdLine->setText( history[pos] );
60
void QgsPythonDialog::on_pbnNext_clicked()
62
if ( pos < history.size() - 1 )
64
history[pos] = edtCmdLine->toPlainText();
66
edtCmdLine->setText( history[pos] );
70
void QgsPythonDialog::execute( bool single )
72
QString command = edtCmdLine->toPlainText();
74
QgsDebugMsg( QString( "command: |%1| %2" ).arg( command ).arg( single ) );
76
if ( !command.isEmpty() )
84
// when using Py_single_input the return value will be always null
85
// we're using custom hooks for output and exceptions to show output in console
86
if ( mPythonUtils->runStringUnsafe( command, single ) )
88
mPythonUtils->evalString( "sys.stdout.get_and_clean_data()", output );
89
QString result = mPythonUtils->getResult();
90
// escape the result so python objects display properly and
91
// we can still use html output to get nicely formatted display
92
output = escapeHtml( output ) + escapeHtml( result );
94
if ( !output.isEmpty() )
99
QString className, errorText;
100
mPythonUtils->getError( className, errorText );
102
output = "<font color=\"red\">" + escapeHtml( className ) + ": " + escapeHtml( errorText ) + "</font><br>";
105
QString str = "<b><font color=\"green\">>>></font> " + escapeHtml( command ) + "</b><br>" + output;
107
edtCmdLine->setText( "" );
109
txtHistory->moveCursor( QTextCursor::End );
110
txtHistory->insertHtml( str );
111
txtHistory->moveCursor( QTextCursor::End );
112
txtHistory->ensureCursorVisible();
115
void QgsPythonDialog::on_pbnExecute_clicked()
120
void QgsPythonDialog::on_pbnEval_clicked()
125
void QgsPythonDialog::showEvent( QShowEvent* event )
127
QDialog::showEvent( event );
129
mPythonUtils->installConsoleHooks();
132
void QgsPythonDialog::closeEvent( QCloseEvent* event )
134
mPythonUtils->uninstallConsoleHooks();
136
QDialog::closeEvent( event );