@@ -0,0 +1,19 | |||||
|
1 | # --------- PyLauncher profile ------------------- | |||
|
2 | # Last changed by $Author: florian $ | |||
|
3 | # $Id: PythonQt.pro 35381 2006-03-16 13:05:52Z florian $ | |||
|
4 | # $Source$ | |||
|
5 | # -------------------------------------------------- | |||
|
6 | ||||
|
7 | TARGET = PyLauncher | |||
|
8 | TEMPLATE = app | |||
|
9 | ||||
|
10 | mac:CONFIG -= app_bundle | |||
|
11 | ||||
|
12 | DESTDIR = ../../lib | |||
|
13 | ||||
|
14 | include ( ../../build/common.prf ) | |||
|
15 | include ( ../../build/PythonQt.prf ) | |||
|
16 | include ( ../../build/PythonQt_QtAll.prf ) | |||
|
17 | ||||
|
18 | SOURCES += \ | |||
|
19 | main.cpp |
@@ -0,0 +1,90 | |||||
|
1 | /* | |||
|
2 | * | |||
|
3 | * Copyright (C) 2006 MeVis Research GmbH All Rights Reserved. | |||
|
4 | * | |||
|
5 | * This library is free software; you can redistribute it and/or | |||
|
6 | * modify it under the terms of the GNU Lesser General Public | |||
|
7 | * License as published by the Free Software Foundation; either | |||
|
8 | * version 2.1 of the License, or (at your option) any later version. | |||
|
9 | * | |||
|
10 | * This library is distributed in the hope that it will be useful, | |||
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
|
13 | * Lesser General Public License for more details. | |||
|
14 | * | |||
|
15 | * Further, this software is distributed without any warranty that it is | |||
|
16 | * free of the rightful claim of any third person regarding infringement | |||
|
17 | * or the like. Any license provided herein, whether implied or | |||
|
18 | * otherwise, applies only to this software file. Patent licenses, if | |||
|
19 | * any, provided herein do not apply to combinations of this program with | |||
|
20 | * other software, or any other product whatsoever. | |||
|
21 | * | |||
|
22 | * You should have received a copy of the GNU Lesser General Public | |||
|
23 | * License along with this library; if not, write to the Free Software | |||
|
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |||
|
25 | * | |||
|
26 | * Contact information: MeVis Research GmbH, Universitaetsallee 29, | |||
|
27 | * 28359 Bremen, Germany or: | |||
|
28 | * | |||
|
29 | * http://www.mevis.de | |||
|
30 | * | |||
|
31 | */ | |||
|
32 | ||||
|
33 | //---------------------------------------------------------------------------------- | |||
|
34 | /*! | |||
|
35 | // \file PyGuiExample.cpp | |||
|
36 | // \author Florian Link | |||
|
37 | // \author Last changed by $Author: florian $ | |||
|
38 | // \date 2007-04 | |||
|
39 | */ | |||
|
40 | //---------------------------------------------------------------------------------- | |||
|
41 | ||||
|
42 | #include "PythonQt.h" | |||
|
43 | #include "PythonQt_QtAll.h" | |||
|
44 | #include "gui/PythonQtScriptingConsole.h" | |||
|
45 | ||||
|
46 | #include <QApplication> | |||
|
47 | #include <QDir> | |||
|
48 | #include <QMessageBox> | |||
|
49 | ||||
|
50 | ||||
|
51 | int main( int argc, char **argv ) | |||
|
52 | { | |||
|
53 | QApplication qapp(argc, argv); | |||
|
54 | ||||
|
55 | PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut); | |||
|
56 | PythonQt_QtAll::init(); | |||
|
57 | ||||
|
58 | PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule(); | |||
|
59 | ||||
|
60 | bool showConsole = false; | |||
|
61 | QStringList files; | |||
|
62 | for (int i = 1; i < argc; i++) { | |||
|
63 | QString arg = argv[i]; | |||
|
64 | QString argLower = arg.toLower(); | |||
|
65 | if (argLower == "-console" || argLower == "-c") { | |||
|
66 | showConsole = true; | |||
|
67 | } else { | |||
|
68 | QString file = arg; | |||
|
69 | QFileInfo info(file); | |||
|
70 | if (info.exists()) { | |||
|
71 | files << info.absoluteFilePath(); | |||
|
72 | // add the file's absolute path for local importing | |||
|
73 | PythonQt::self()->addSysPath(info.absolutePath()); | |||
|
74 | } else { | |||
|
75 | QMessageBox::warning(NULL, "PyLauncher", QString("File does not exist: %1").arg(file)); | |||
|
76 | } | |||
|
77 | } | |||
|
78 | } | |||
|
79 | PythonQtScriptingConsole console(NULL, mainContext); | |||
|
80 | ||||
|
81 | foreach(QString file, files) { | |||
|
82 | mainContext.evalFile(file); | |||
|
83 | } | |||
|
84 | if (showConsole || console.hadError()) { | |||
|
85 | console.show(); | |||
|
86 | } | |||
|
87 | ||||
|
88 | return qapp.exec(); | |||
|
89 | } | |||
|
90 |
@@ -5,4 +5,5 SUBDIRS = CPPPyWrapperExample \ | |||||
5 | PyCustomMetaTypeExample \ |
|
5 | PyCustomMetaTypeExample \ | |
6 | PyGuiExample \ |
|
6 | PyGuiExample \ | |
7 | PyDecoratorsExample \ |
|
7 | PyDecoratorsExample \ | |
8 | PyScriptingConsole |
|
8 | PyScriptingConsole \ | |
|
9 | PyLauncher |
@@ -930,6 +930,14 bool PythonQt::handleError() | |||||
930 | return flag; |
|
930 | return flag; | |
931 | } |
|
931 | } | |
932 |
|
932 | |||
|
933 | void PythonQt::addSysPath(const QString& path) | |||
|
934 | { | |||
|
935 | PythonQtObjectPtr sys; | |||
|
936 | sys.setNewRef(PyImport_ImportModule("sys")); | |||
|
937 | PythonQtObjectPtr obj = lookupObject(sys, "path"); | |||
|
938 | PyList_Insert(obj, 0, PythonQtConv::QStringToPyObject(path)); | |||
|
939 | } | |||
|
940 | ||||
933 | void PythonQt::overwriteSysPath(const QStringList& paths) |
|
941 | void PythonQt::overwriteSysPath(const QStringList& paths) | |
934 | { |
|
942 | { | |
935 | PythonQtObjectPtr sys; |
|
943 | PythonQtObjectPtr sys; |
@@ -112,6 +112,9 public: | |||||
112 | //! overwrite the python sys path (call this directly after PythonQt::init() if you want to change the std python sys path) |
|
112 | //! overwrite the python sys path (call this directly after PythonQt::init() if you want to change the std python sys path) | |
113 | void overwriteSysPath(const QStringList& paths); |
|
113 | void overwriteSysPath(const QStringList& paths); | |
114 |
|
114 | |||
|
115 | //! prepend a path to sys.path to allow importing from it | |||
|
116 | void addSysPath(const QString& path); | |||
|
117 | ||||
115 | //! sets the __path__ list of a module to the given list (important for local imports) |
|
118 | //! sets the __path__ list of a module to the given list (important for local imports) | |
116 | void setModuleImportPath(PyObject* module, const QStringList& paths); |
|
119 | void setModuleImportPath(PyObject* module, const QStringList& paths); | |
117 |
|
120 |
@@ -63,6 +63,7 PythonQtScriptingConsole::PythonQtScriptingConsole(QWidget* parent, const Python | |||||
63 | _defaultTextCharacterFormat = currentCharFormat(); |
|
63 | _defaultTextCharacterFormat = currentCharFormat(); | |
64 | _context = context; |
|
64 | _context = context; | |
65 | _historyPosition = 0; |
|
65 | _historyPosition = 0; | |
|
66 | _hadError = false; | |||
66 |
|
67 | |||
67 | _completer = new QCompleter(this); |
|
68 | _completer = new QCompleter(this); | |
68 | _completer->setWidget(this); |
|
69 | _completer->setWidget(this); | |
@@ -90,11 +91,12 void PythonQtScriptingConsole::stdOut(const QString& s) | |||||
90 |
|
91 | |||
91 | void PythonQtScriptingConsole::stdErr(const QString& s) |
|
92 | void PythonQtScriptingConsole::stdErr(const QString& s) | |
92 | { |
|
93 | { | |
|
94 | _hadError = true; | |||
93 | _stdErr += s; |
|
95 | _stdErr += s; | |
94 | int idx; |
|
96 | int idx; | |
95 | while ((idx = _stdErr.indexOf('\n'))!=-1) { |
|
97 | while ((idx = _stdErr.indexOf('\n'))!=-1) { | |
96 | consoleMessage(_stdErr.left(idx)); |
|
98 | consoleMessage(_stdErr.left(idx)); | |
97 |
std::c |
|
99 | std::cerr << _stdErr.left(idx).toLatin1().data() << std::endl; | |
98 | _stdErr = _stdErr.mid(idx+1); |
|
100 | _stdErr = _stdErr.mid(idx+1); | |
99 | } |
|
101 | } | |
100 | } |
|
102 | } |
@@ -90,6 +90,16 public slots: | |||||
90 |
|
90 | |||
91 | //! Appends a newline and command prompt at the end of the document. |
|
91 | //! Appends a newline and command prompt at the end of the document. | |
92 | void appendCommandPrompt(bool storeOnly = false); |
|
92 | void appendCommandPrompt(bool storeOnly = false); | |
|
93 | ||||
|
94 | public: | |||
|
95 | //! returns true if python cerr had an error | |||
|
96 | bool hadError() { return _hadError; } | |||
|
97 | ||||
|
98 | //! returns true if python cerr had an error | |||
|
99 | void clearError() { | |||
|
100 | _hadError = false; | |||
|
101 | } | |||
|
102 | ||||
93 | protected: |
|
103 | protected: | |
94 | //! handle the pressing of tab |
|
104 | //! handle the pressing of tab | |
95 | void handleTabCompletion(); |
|
105 | void handleTabCompletion(); | |
@@ -110,6 +120,7 protected: | |||||
110 | //! flush output that was not yet printed |
|
120 | //! flush output that was not yet printed | |
111 | void flushStdOut(); |
|
121 | void flushStdOut(); | |
112 |
|
122 | |||
|
123 | ||||
113 | private: |
|
124 | private: | |
114 | void executeCode(const QString& code); |
|
125 | void executeCode(const QString& code); | |
115 |
|
126 | |||
@@ -129,6 +140,8 private: | |||||
129 |
|
140 | |||
130 | QTextCharFormat _defaultTextCharacterFormat; |
|
141 | QTextCharFormat _defaultTextCharacterFormat; | |
131 | QCompleter* _completer; |
|
142 | QCompleter* _completer; | |
|
143 | ||||
|
144 | bool _hadError; | |||
132 | }; |
|
145 | }; | |
133 |
|
146 | |||
134 |
|
147 |
General Comments 0
You need to be logged in to leave comments.
Login now