/
qgspythondialog.cpp
134 lines (114 loc) · 3.67 KB
/
qgspythondialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
qgspythondialog.h - dialog with embedded python console
---------------------
begin : October 2006
copyright : (C) 2006 by Martin Dobias
email : wonder.sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/* $Id$ */
#include "qgspythondialog.h"
#include "qgspythonutils.h"
#include <QShowEvent>
#include <QCloseEvent>
QgsPythonDialog::QgsPythonDialog( QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent )
: QDialog( parent )
{
setupUi( this );
#ifdef Q_WS_MAC
// Qt4.3+ bug?: Mac window minimize control isn't enabled
setWindowFlags( windowFlags() | Qt::WindowMinimizeButtonHint );
#endif
mIface = pIface;
mPythonUtils = pythonUtils;
pos = 0;
}
QgsPythonDialog::~QgsPythonDialog()
{
}
QString QgsPythonDialog::escapeHtml( QString text )
{
return text.replace( "<", "<" ).replace( ">", ">" );
}
void QgsPythonDialog::keyPressEvent( QKeyEvent *ev )
{
switch ( ev->key() )
{
case Qt::Key_Up:
{
if ( pos > 0 )
{
if ( pos == history.size() )
history << edtCmdLine->text();
else
history[pos] = edtCmdLine->text();
pos--;
edtCmdLine->setText( history[pos] );
}
}
break;
case Qt::Key_Down:
{
if ( pos < history.size() - 1 )
{
history[pos] = edtCmdLine->text();
pos++;
edtCmdLine->setText( history[pos] );
}
}
break;
default:
QWidget::keyPressEvent( ev );
break;
}
}
void QgsPythonDialog::on_edtCmdLine_returnPressed()
{
QString command = edtCmdLine->text();
if ( !command.isEmpty() )
{
history << command;
pos = history.size();
}
QString output;
// when using Py_single_input the return value will be always null
// we're using custom hooks for output and exceptions to show output in console
if ( mPythonUtils->runStringUnsafe( command ) )
{
mPythonUtils->evalString( "sys.stdout.get_and_clean_data()", output );
QString result = mPythonUtils->getResult();
// escape the result so python objects display properly and
// we can still use html output to get nicely formatted display
output = escapeHtml( output ) + escapeHtml( result );
if ( !output.isEmpty() )
output += "<br>";
}
else
{
QString className, errorText;
mPythonUtils->getError( className, errorText );
output = "<font color=\"red\">" + escapeHtml( className ) + ": " + escapeHtml( errorText ) + "</font><br>";
}
QString str = "<b><font color=\"green\">>>></font> " + escapeHtml( command ) + "</b><br>" + output;
edtCmdLine->setText( "" );
txtHistory->moveCursor( QTextCursor::End );
txtHistory->insertHtml( str );
txtHistory->moveCursor( QTextCursor::End );
txtHistory->ensureCursorVisible();
}
void QgsPythonDialog::showEvent( QShowEvent* event )
{
QDialog::showEvent( event );
mPythonUtils->installConsoleHooks();
}
void QgsPythonDialog::closeEvent( QCloseEvent* event )
{
mPythonUtils->uninstallConsoleHooks();
QDialog::closeEvent( event );
}