~ubuntu-branches/ubuntu/trusty/qgis/trusty

« back to all changes in this revision

Viewing changes to src/app/qgspythondialog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
    qgspythondialog.h - dialog with embedded python console
 
3
    ---------------------
 
4
    begin                : October 2006
 
5
    copyright            : (C) 2006 by Martin Dobias
 
6
    email                : wonder.sk at gmail dot com
 
7
 ***************************************************************************
 
8
 *                                                                         *
 
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.                                   *
 
13
 *                                                                         *
 
14
 ***************************************************************************/
 
15
/* $Id$ */
 
16
 
 
17
#include "qgspythondialog.h"
 
18
#include "qgspythonutils.h"
 
19
#include "qgslogger.h"
 
20
 
 
21
#include <QShowEvent>
 
22
#include <QCloseEvent>
 
23
 
 
24
QgsPythonDialog::QgsPythonDialog( QgisInterface* pIface, QgsPythonUtils* pythonUtils, QWidget *parent )
 
25
    : QDialog( parent )
 
26
{
 
27
  setupUi( this );
 
28
#ifdef Q_WS_MAC
 
29
  // Qt4.3+ bug?: Mac window minimize control isn't enabled
 
30
  setWindowFlags( windowFlags() | Qt::WindowMinimizeButtonHint );
 
31
#endif
 
32
  mIface = pIface;
 
33
  mPythonUtils = pythonUtils;
 
34
 
 
35
  pos = 0;
 
36
}
 
37
 
 
38
QgsPythonDialog::~QgsPythonDialog()
 
39
{
 
40
}
 
41
 
 
42
QString QgsPythonDialog::escapeHtml( QString text )
 
43
{
 
44
  return text.replace( "<", "&lt;" ).replace( ">", "&gt;" );
 
45
}
 
46
 
 
47
void QgsPythonDialog::on_pbnPrev_clicked()
 
48
{
 
49
  if ( pos > 0 )
 
50
  {
 
51
    if ( pos == history.size() )
 
52
      history << edtCmdLine->toPlainText();
 
53
    else
 
54
      history[pos] = edtCmdLine->toPlainText();
 
55
    pos--;
 
56
    edtCmdLine->setText( history[pos] );
 
57
  }
 
58
}
 
59
 
 
60
void QgsPythonDialog::on_pbnNext_clicked()
 
61
{
 
62
  if ( pos < history.size() - 1 )
 
63
  {
 
64
    history[pos] = edtCmdLine->toPlainText();
 
65
    pos++;
 
66
    edtCmdLine->setText( history[pos] );
 
67
  }
 
68
}
 
69
 
 
70
void QgsPythonDialog::execute( bool single )
 
71
{
 
72
  QString command = edtCmdLine->toPlainText();
 
73
 
 
74
  QgsDebugMsg( QString( "command: |%1| %2" ).arg( command ).arg( single ) );
 
75
 
 
76
  if ( !command.isEmpty() )
 
77
  {
 
78
    history << command;
 
79
    pos = history.size();
 
80
  }
 
81
 
 
82
  QString output;
 
83
 
 
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 ) )
 
87
  {
 
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 );
 
93
 
 
94
    if ( !output.isEmpty() )
 
95
      output += "<br>";
 
96
  }
 
97
  else
 
98
  {
 
99
    QString className, errorText;
 
100
    mPythonUtils->getError( className, errorText );
 
101
 
 
102
    output = "<font color=\"red\">" + escapeHtml( className ) + ": " + escapeHtml( errorText ) + "</font><br>";
 
103
  }
 
104
 
 
105
  QString str = "<b><font color=\"green\">&gt;&gt;&gt;</font> " + escapeHtml( command ) + "</b><br>" + output;
 
106
 
 
107
  edtCmdLine->setText( "" );
 
108
 
 
109
  txtHistory->moveCursor( QTextCursor::End );
 
110
  txtHistory->insertHtml( str );
 
111
  txtHistory->moveCursor( QTextCursor::End );
 
112
  txtHistory->ensureCursorVisible();
 
113
}
 
114
 
 
115
void QgsPythonDialog::on_pbnExecute_clicked()
 
116
{
 
117
  execute( false );
 
118
}
 
119
 
 
120
void QgsPythonDialog::on_pbnEval_clicked()
 
121
{
 
122
  execute( true );
 
123
}
 
124
 
 
125
void QgsPythonDialog::showEvent( QShowEvent* event )
 
126
{
 
127
  QDialog::showEvent( event );
 
128
 
 
129
  mPythonUtils->installConsoleHooks();
 
130
}
 
131
 
 
132
void QgsPythonDialog::closeEvent( QCloseEvent* event )
 
133
{
 
134
  mPythonUtils->uninstallConsoleHooks();
 
135
 
 
136
  QDialog::closeEvent( event );
 
137
}