From 59f352c6d9fb31da81f5a82b6f2ba1c564dd2a40 2009-04-09 13:41:13 From: florianlink Date: 2009-04-09 13:41:13 Subject: [PATCH] added PyLauncher example added hadError() to scripting console added addSysPath() git-svn-id: svn://svn.code.sf.net/p/pythonqt/code/trunk@50 ea8d5007-eb21-0410-b261-ccb3ea6e24a9 --- diff --git a/examples/PyLauncher/PyLauncher.pro b/examples/PyLauncher/PyLauncher.pro new file mode 100644 index 0000000..e2ffef8 --- /dev/null +++ b/examples/PyLauncher/PyLauncher.pro @@ -0,0 +1,19 @@ +# --------- PyLauncher profile ------------------- +# Last changed by $Author: florian $ +# $Id: PythonQt.pro 35381 2006-03-16 13:05:52Z florian $ +# $Source$ +# -------------------------------------------------- + +TARGET = PyLauncher +TEMPLATE = app + +mac:CONFIG -= app_bundle + +DESTDIR = ../../lib + +include ( ../../build/common.prf ) +include ( ../../build/PythonQt.prf ) +include ( ../../build/PythonQt_QtAll.prf ) + +SOURCES += \ + main.cpp diff --git a/examples/PyLauncher/main.cpp b/examples/PyLauncher/main.cpp new file mode 100644 index 0000000..d3a5dd7 --- /dev/null +++ b/examples/PyLauncher/main.cpp @@ -0,0 +1,90 @@ +/* + * + * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * Further, this software is distributed without any warranty that it is + * free of the rightful claim of any third person regarding infringement + * or the like. Any license provided herein, whether implied or + * otherwise, applies only to this software file. Patent licenses, if + * any, provided herein do not apply to combinations of this program with + * other software, or any other product whatsoever. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Contact information: MeVis Research GmbH, Universitaetsallee 29, + * 28359 Bremen, Germany or: + * + * http://www.mevis.de + * + */ + +//---------------------------------------------------------------------------------- +/*! +// \file PyGuiExample.cpp +// \author Florian Link +// \author Last changed by $Author: florian $ +// \date 2007-04 +*/ +//---------------------------------------------------------------------------------- + +#include "PythonQt.h" +#include "PythonQt_QtAll.h" +#include "gui/PythonQtScriptingConsole.h" + +#include +#include +#include + + +int main( int argc, char **argv ) +{ + QApplication qapp(argc, argv); + + PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut); + PythonQt_QtAll::init(); + + PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule(); + + bool showConsole = false; + QStringList files; + for (int i = 1; i < argc; i++) { + QString arg = argv[i]; + QString argLower = arg.toLower(); + if (argLower == "-console" || argLower == "-c") { + showConsole = true; + } else { + QString file = arg; + QFileInfo info(file); + if (info.exists()) { + files << info.absoluteFilePath(); + // add the file's absolute path for local importing + PythonQt::self()->addSysPath(info.absolutePath()); + } else { + QMessageBox::warning(NULL, "PyLauncher", QString("File does not exist: %1").arg(file)); + } + } + } + PythonQtScriptingConsole console(NULL, mainContext); + + foreach(QString file, files) { + mainContext.evalFile(file); + } + if (showConsole || console.hadError()) { + console.show(); + } + + return qapp.exec(); +} + diff --git a/examples/examples.pro b/examples/examples.pro index 81cff26..14610c9 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -5,4 +5,5 @@ SUBDIRS = CPPPyWrapperExample \ PyCustomMetaTypeExample \ PyGuiExample \ PyDecoratorsExample \ - PyScriptingConsole + PyScriptingConsole \ + PyLauncher diff --git a/src/PythonQt.cpp b/src/PythonQt.cpp index 4ce0f60..86c6506 100644 --- a/src/PythonQt.cpp +++ b/src/PythonQt.cpp @@ -930,6 +930,14 @@ bool PythonQt::handleError() return flag; } +void PythonQt::addSysPath(const QString& path) +{ + PythonQtObjectPtr sys; + sys.setNewRef(PyImport_ImportModule("sys")); + PythonQtObjectPtr obj = lookupObject(sys, "path"); + PyList_Insert(obj, 0, PythonQtConv::QStringToPyObject(path)); +} + void PythonQt::overwriteSysPath(const QStringList& paths) { PythonQtObjectPtr sys; diff --git a/src/PythonQt.h b/src/PythonQt.h index 609c069..d6ce9dc 100644 --- a/src/PythonQt.h +++ b/src/PythonQt.h @@ -112,6 +112,9 @@ public: //! overwrite the python sys path (call this directly after PythonQt::init() if you want to change the std python sys path) void overwriteSysPath(const QStringList& paths); + //! prepend a path to sys.path to allow importing from it + void addSysPath(const QString& path); + //! sets the __path__ list of a module to the given list (important for local imports) void setModuleImportPath(PyObject* module, const QStringList& paths); diff --git a/src/gui/PythonQtScriptingConsole.cpp b/src/gui/PythonQtScriptingConsole.cpp index 2784a1c..9193d9e 100644 --- a/src/gui/PythonQtScriptingConsole.cpp +++ b/src/gui/PythonQtScriptingConsole.cpp @@ -63,6 +63,7 @@ PythonQtScriptingConsole::PythonQtScriptingConsole(QWidget* parent, const Python _defaultTextCharacterFormat = currentCharFormat(); _context = context; _historyPosition = 0; + _hadError = false; _completer = new QCompleter(this); _completer->setWidget(this); @@ -90,11 +91,12 @@ void PythonQtScriptingConsole::stdOut(const QString& s) void PythonQtScriptingConsole::stdErr(const QString& s) { + _hadError = true; _stdErr += s; int idx; while ((idx = _stdErr.indexOf('\n'))!=-1) { consoleMessage(_stdErr.left(idx)); - std::cout << _stdErr.left(idx).toLatin1().data() << std::endl; + std::cerr << _stdErr.left(idx).toLatin1().data() << std::endl; _stdErr = _stdErr.mid(idx+1); } } diff --git a/src/gui/PythonQtScriptingConsole.h b/src/gui/PythonQtScriptingConsole.h index eec4c72..88d4804 100644 --- a/src/gui/PythonQtScriptingConsole.h +++ b/src/gui/PythonQtScriptingConsole.h @@ -90,6 +90,16 @@ public slots: //! Appends a newline and command prompt at the end of the document. void appendCommandPrompt(bool storeOnly = false); + +public: + //! returns true if python cerr had an error + bool hadError() { return _hadError; } + + //! returns true if python cerr had an error + void clearError() { + _hadError = false; + } + protected: //! handle the pressing of tab void handleTabCompletion(); @@ -110,6 +120,7 @@ protected: //! flush output that was not yet printed void flushStdOut(); + private: void executeCode(const QString& code); @@ -129,6 +140,8 @@ private: QTextCharFormat _defaultTextCharacterFormat; QCompleter* _completer; + + bool _hadError; };