PythonConsolePy.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net>     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QByteArray>
00027 # include <QInputDialog>
00028 # include <QEventLoop>
00029 #endif
00030 
00031 #include "PythonConsolePy.h"
00032 #include "PythonConsole.h"
00033 #include "MainWindow.h"
00034 
00035 #include <Base/Console.h>
00036 #include <Base/Exception.h>
00037 
00038 using namespace Gui;
00039 
00040 void PythonStdout::init_type()
00041 {
00042     behaviors().name("PythonStdout");
00043     behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
00044     // you must have overwritten the virtual functions
00045     behaviors().supportRepr();
00046     add_varargs_method("write",&PythonStdout::write,"write()");
00047     add_varargs_method("flush",&PythonStdout::flush,"flush()");
00048 }
00049 
00050 PythonStdout::PythonStdout(PythonConsole *pc)
00051   : pyConsole(pc)
00052 {
00053 }
00054 
00055 PythonStdout::~PythonStdout()
00056 {
00057 }
00058 
00059 Py::Object PythonStdout::repr()
00060 {
00061     std::string s;
00062     std::ostringstream s_out;
00063     s_out << "PythonStdout";
00064     return Py::String(s_out.str());
00065 }
00066 
00067 Py::Object PythonStdout::write(const Py::Tuple& args)
00068 {
00069     try {
00070         Py::Object output(args[0]);
00071         if (PyUnicode_Check(output.ptr())) {
00072             PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
00073             if (unicode) {
00074                 const char* string = PyString_AsString(unicode);
00075                 int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
00076                 pyConsole->insertPythonOutput(QString::fromUtf8(string, maxlen));
00077                 Py_DECREF(unicode);
00078             }
00079         }
00080         else {
00081             Py::String text(args[0]);
00082             std::string string = (std::string)text;
00083             int maxlen = string.size() > 10000 ? 10000 : -1;
00084             pyConsole->insertPythonOutput(QString::fromUtf8(string.c_str(), maxlen));
00085         }
00086     }
00087     catch (Py::Exception& e) {
00088         // Do not provoke error messages 
00089         e.clear();
00090     }
00091 
00092     return Py::None();
00093 }
00094 
00095 Py::Object PythonStdout::flush(const Py::Tuple&)
00096 {
00097     return Py::None();
00098 }
00099 
00100 // -------------------------------------------------------------------------
00101 
00102 void PythonStderr::init_type()
00103 {
00104     behaviors().name("PythonStderr");
00105     behaviors().doc("Redirection of stdout to FreeCAD's Python console window");
00106     // you must have overwritten the virtual functions
00107     behaviors().supportRepr();
00108     add_varargs_method("write",&PythonStderr::write,"write()");
00109     add_varargs_method("flush",&PythonStderr::flush,"flush()");
00110 }
00111 
00112 PythonStderr::PythonStderr(PythonConsole *pc)
00113   : pyConsole(pc)
00114 {
00115 }
00116 
00117 PythonStderr::~PythonStderr()
00118 {
00119 }
00120 
00121 Py::Object PythonStderr::repr()
00122 {
00123     std::string s;
00124     std::ostringstream s_out;
00125     s_out << "PythonStderr";
00126     return Py::String(s_out.str());
00127 }
00128 
00129 Py::Object PythonStderr::write(const Py::Tuple& args)
00130 {
00131     try {
00132         Py::Object output(args[0]);
00133         if (PyUnicode_Check(output.ptr())) {
00134             PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
00135             if (unicode) {
00136                 const char* string = PyString_AsString(unicode);
00137                 int maxlen = qstrlen(string) > 10000 ? 10000 : -1;
00138                 pyConsole->insertPythonError(QString::fromUtf8(string, maxlen));
00139                 Py_DECREF(unicode);
00140             }
00141         }
00142         else {
00143             Py::String text(args[0]);
00144             std::string string = (std::string)text;
00145             int maxlen = string.size() > 10000 ? 10000 : -1;
00146             pyConsole->insertPythonError(QString::fromUtf8(string.c_str(), maxlen));
00147         }
00148     }
00149     catch (Py::Exception& e) {
00150         // Do not provoke error messages
00151         e.clear();
00152     }
00153 
00154     return Py::None();
00155 }
00156 
00157 Py::Object PythonStderr::flush(const Py::Tuple&)
00158 {
00159     return Py::None();
00160 }
00161 
00162 // -------------------------------------------------------------------------
00163 
00164 void OutputStdout::init_type()
00165 {
00166     behaviors().name("OutputStdout");
00167     behaviors().doc("Redirection of stdout to FreeCAD's output window");
00168     // you must have overwritten the virtual functions
00169     behaviors().supportRepr();
00170     add_varargs_method("write",&OutputStdout::write,"write()");
00171     add_varargs_method("flush",&OutputStdout::flush,"flush()");
00172 }
00173 
00174 OutputStdout::OutputStdout()
00175 {
00176 }
00177 
00178 OutputStdout::~OutputStdout()
00179 {
00180 }
00181 
00182 Py::Object OutputStdout::repr()
00183 {
00184     std::string s;
00185     std::ostringstream s_out;
00186     s_out << "OutputStdout";
00187     return Py::String(s_out.str());
00188 }
00189 
00190 Py::Object OutputStdout::write(const Py::Tuple& args)
00191 {
00192     try {
00193         Py::Object output(args[0]);
00194         if (PyUnicode_Check(output.ptr())) {
00195             PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
00196             if (unicode) {
00197                 const char* string = PyString_AsString(unicode);
00198                 Base::Console().Message("%s",string);
00199                 Py_DECREF(unicode);
00200             }
00201         }
00202         else {
00203             Py::String text(args[0]);
00204             std::string string = (std::string)text;
00205             Base::Console().Message("%s",string.c_str());
00206         }
00207     }
00208     catch (Py::Exception& e) {
00209         // Do not provoke error messages
00210         e.clear();
00211     }
00212 
00213     return Py::None();
00214 }
00215 
00216 Py::Object OutputStdout::flush(const Py::Tuple&)
00217 {
00218     return Py::None();
00219 }
00220 
00221 // -------------------------------------------------------------------------
00222 
00223 void OutputStderr::init_type()
00224 {
00225     behaviors().name("OutputStderr");
00226     behaviors().doc("Redirection of stdout to FreeCAD's output window");
00227     // you must have overwritten the virtual functions
00228     behaviors().supportRepr();
00229     add_varargs_method("write",&OutputStderr::write,"write()");
00230     add_varargs_method("flush",&OutputStderr::flush,"flush()");
00231 }
00232 
00233 OutputStderr::OutputStderr()
00234 {
00235 }
00236 
00237 OutputStderr::~OutputStderr()
00238 {
00239 }
00240 
00241 Py::Object OutputStderr::repr()
00242 {
00243     std::string s;
00244     std::ostringstream s_out;
00245     s_out << "OutputStderr";
00246     return Py::String(s_out.str());
00247 }
00248 
00249 Py::Object OutputStderr::write(const Py::Tuple& args)
00250 {
00251     try {
00252         Py::Object output(args[0]);
00253         if (PyUnicode_Check(output.ptr())) {
00254             PyObject* unicode = PyUnicode_AsEncodedObject(output.ptr(), "utf-8", "strict");
00255             if (unicode) {
00256                 const char* string = PyString_AsString(unicode);
00257                 Base::Console().Error("%s",string);
00258                 Py_DECREF(unicode);
00259             }
00260         }
00261         else {
00262             Py::String text(args[0]);
00263             std::string string = (std::string)text;
00264             Base::Console().Error("%s",string.c_str());
00265         }
00266     }
00267     catch (Py::Exception& e) {
00268         // Do not provoke error messages
00269         e.clear();
00270     }
00271 
00272     return Py::None();
00273 }
00274 
00275 Py::Object OutputStderr::flush(const Py::Tuple&)
00276 {
00277     return Py::None();
00278 }
00279 
00280 // -------------------------------------------------------------------------
00281 
00282 void PythonStdin::init_type()
00283 {
00284     behaviors().name("PythonStdin");
00285     behaviors().doc("Redirection of stdin to FreeCAD to open an input dialog");
00286     // you must have overwritten the virtual functions
00287     behaviors().supportRepr();
00288     add_varargs_method("readline",&PythonStdin::readline,"readline()");
00289 }
00290 
00291 PythonStdin::PythonStdin(PythonConsole *pc)
00292   : pyConsole(pc)
00293 {
00294     editField = new PythonInputField(/*getMainWindow()*/);
00295 }
00296 
00297 PythonStdin::~PythonStdin()
00298 {
00299     // call deleteLater() because deleting immediately causes problems
00300     editField->deleteLater();
00301 }
00302 
00303 Py::Object PythonStdin::repr()
00304 {
00305     std::string s;
00306     std::ostringstream s_out;
00307     s_out << "PythonStdin";
00308     return Py::String(s_out.str());
00309 }
00310 
00311 Py::Object PythonStdin::readline(const Py::Tuple& args)
00312 {
00313     QEventLoop loop;
00314     QObject::connect(editField, SIGNAL(textEntered()), &loop, SLOT(quit()));
00315     editField->clear();
00316     editField->show();
00317     loop.exec();
00318     QString txt = editField->getText();
00319     if (txt.isEmpty())
00320         editField->hide();
00321 
00322     return Py::String((const char*)txt.toAscii());
00323 }

Generated on Wed Nov 23 19:00:32 2011 for FreeCAD by  doxygen 1.6.1