From 9f2a9aba3aff73e31ea15eb4a7a04b0e50f4ee4e Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 5 Jan 2018 15:58:35 +0100 Subject: Move examples from submodule to pyside-setup Move PySide2 examples that are owned by the Qt Company to a new examples directory. Done-with: Venugopal Shivashankar Task-number: PYSIDE-363 Change-Id: I14099764d9eef2bc35e067086121427955862e3a Reviewed-by: Alexandru Croitor --- examples/scriptableapplication/README.txt | 33 +++ examples/scriptableapplication/main.cpp | 64 ++++++ examples/scriptableapplication/mainwindow.cpp | 141 +++++++++++++ examples/scriptableapplication/mainwindow.h | 76 +++++++ examples/scriptableapplication/pyside2.pri | 19 ++ examples/scriptableapplication/pyside2_config.py | 225 +++++++++++++++++++++ examples/scriptableapplication/pythonutils.cpp | 169 ++++++++++++++++ examples/scriptableapplication/pythonutils.h | 81 ++++++++ .../scriptableapplication.pro | 92 +++++++++ .../scriptableapplication.xml | 56 +++++ examples/scriptableapplication/wrappedclasses.h | 56 +++++ 11 files changed, 1012 insertions(+) create mode 100644 examples/scriptableapplication/README.txt create mode 100644 examples/scriptableapplication/main.cpp create mode 100644 examples/scriptableapplication/mainwindow.cpp create mode 100644 examples/scriptableapplication/mainwindow.h create mode 100644 examples/scriptableapplication/pyside2.pri create mode 100644 examples/scriptableapplication/pyside2_config.py create mode 100644 examples/scriptableapplication/pythonutils.cpp create mode 100644 examples/scriptableapplication/pythonutils.h create mode 100644 examples/scriptableapplication/scriptableapplication.pro create mode 100644 examples/scriptableapplication/scriptableapplication.xml create mode 100644 examples/scriptableapplication/wrappedclasses.h (limited to 'examples/scriptableapplication') diff --git a/examples/scriptableapplication/README.txt b/examples/scriptableapplication/README.txt new file mode 100644 index 000000000..28bdb44ae --- /dev/null +++ b/examples/scriptableapplication/README.txt @@ -0,0 +1,33 @@ +scriptableapplication demonstrates how to make a Qt C++ application scriptable. + +It has a class MainWindow inheriting QMainWindow for which bindings are generated +using PySide2's shiboken2 bindings generator. + +The header wrappedclasses.h is passed to shiboken2 which generates class +wrappers and headers in a subdirectory which are linked into the application. + +pythonutils.cpp has some code which binds the instance of MainWindow +to a variable 'mainWindow' in the global (__main___) namespace. +It is then possible to run Python script snippets like +mainWindow.testFunction1() which trigger the underlying C++ function. + +Virtualenv Support +If the application is started from a terminal with an activated python virtual environment, that +environment's packages will be used for the python module import process. In this case, make sure +that the application was built while the virtualenv was active, so that the build system picks up +the correct python shared library. + +Windows Notes +The build config of the application (Debug or Release) should match the PySide2 build config, +otherwise the application will not function correctly. In practice this means the only supported +configurations are: +1) qmake release config build of the application + PySide2 setup.py without "--debug" flag + + python.exe for the PySide2 build process + python36.dll for the linked in shared library + + release build of Qt. +2) qmake debug config build of the application + PySide2 setup.py WITH "--debug" flag + + python_d.exe for the PySide2 build process + python36_d.dll for the linked in shared library + + debug build of Qt. +This is necessary because all the shared libraries in question have to link to the same C++ runtime +library (msvcrt.dll or msvcrtd.dll). +To make the example as self-contained as possible, the shared libraries in use (pyside2.dll, +shiboken2.dll) are hard-linked into the build folder of the application. diff --git a/examples/scriptableapplication/main.cpp b/examples/scriptableapplication/main.cpp new file mode 100644 index 000000000..167eeb0fa --- /dev/null +++ b/examples/scriptableapplication/main.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the PySide examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" + +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow mainWindow; + const QRect availableGeometry = a.desktop()->availableGeometry(&mainWindow); + mainWindow.resize(availableGeometry.width() / 2, availableGeometry.height() / 2); + mainWindow.show(); + return a.exec(); +} diff --git a/examples/scriptableapplication/mainwindow.cpp b/examples/scriptableapplication/mainwindow.cpp new file mode 100644 index 000000000..e754bcb93 --- /dev/null +++ b/examples/scriptableapplication/mainwindow.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the PySide examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "mainwindow.h" +#include "pythonutils.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +static const char defaultScript[] = + "print(\"Hello, world\")\n" + "mainWindow.testFunction1()\n"; + +MainWindow::MainWindow() + : m_scriptEdit(new QPlainTextEdit(QLatin1String(defaultScript), this)) +{ + setWindowTitle(tr("Scriptable Application")); + + QMenu *fileMenu = menuBar()->addMenu(tr("&File")); + const QIcon runIcon = QIcon::fromTheme(QStringLiteral("system-run")); + QAction *runAction = fileMenu->addAction(runIcon, tr("&Run..."), this, &MainWindow::slotRunScript); + runAction->setShortcut(Qt::CTRL + Qt::Key_R); + QAction *diagnosticAction = fileMenu->addAction(tr("&Print Diagnostics"), this, &MainWindow::slotPrintDiagnostics); + diagnosticAction->setShortcut(Qt::CTRL + Qt::Key_D); + fileMenu->addAction(tr("&Invoke testFunction1()"), this, &MainWindow::testFunction1); + const QIcon quitIcon = QIcon::fromTheme(QStringLiteral("application-exit")); + QAction *quitAction = fileMenu->addAction(quitIcon, tr("&Quit"), qApp, &QCoreApplication::quit); + quitAction->setShortcut(Qt::CTRL + Qt::Key_Q); + + QMenu *editMenu = menuBar()->addMenu(tr("&Edit")); + const QIcon clearIcon = QIcon::fromTheme(QStringLiteral("edit-clear")); + QAction *clearAction = editMenu->addAction(clearIcon, tr("&Clear"), m_scriptEdit, &QPlainTextEdit::clear); + + QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); + const QIcon aboutIcon = QIcon::fromTheme(QStringLiteral("help-about")); + QAction *aboutAction = helpMenu->addAction(aboutIcon, tr("&About Qt"), qApp, &QApplication::aboutQt); + + QToolBar *toolBar = new QToolBar; + addToolBar(toolBar); + toolBar->addAction(quitAction); + toolBar->addSeparator(); + toolBar->addAction(clearAction); + toolBar->addSeparator(); + toolBar->addAction(runAction); + toolBar->addSeparator(); + toolBar->addAction(aboutAction); + + m_scriptEdit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); + setCentralWidget(m_scriptEdit); + + if (!PythonUtils::bindAppObject("__main__", "mainWindow", PythonUtils::MainWindowType, this)) + statusBar()->showMessage(tr("Error loading the application module")); +} + +void MainWindow::slotRunScript() +{ + const QStringList script = m_scriptEdit->toPlainText().trimmed().split(QLatin1Char('\n'), QString::SkipEmptyParts); + if (!script.isEmpty()) + runScript(script); +} + +void MainWindow::slotPrintDiagnostics() +{ + const QStringList script = QStringList() + << "import sys" << "print('Path=', sys.path)" << "print('Executable=', sys.executable)"; + runScript(script); +} + +void MainWindow::runScript(const QStringList &script) +{ + if (!::PythonUtils::runScript(script)) + statusBar()->showMessage(tr("Error running script")); +} + +void MainWindow::testFunction1() +{ + static int n = 1; + QString message; + QTextStream(&message) << __FUNCTION__ << " called #" << n++; + qDebug().noquote() << message; + statusBar()->showMessage(message); +} diff --git a/examples/scriptableapplication/mainwindow.h b/examples/scriptableapplication/mainwindow.h new file mode 100644 index 000000000..4dcafc731 --- /dev/null +++ b/examples/scriptableapplication/mainwindow.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the PySide examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +class QPlainTextEdit; + +class MainWindow : public QMainWindow +{ + Q_OBJECT +public: + MainWindow(); + + void testFunction1(); + +private Q_SLOTS: + void slotRunScript(); + void slotPrintDiagnostics(); + +private: + void runScript(const QStringList &); + + QPlainTextEdit *m_scriptEdit; +}; + +#endif // MAINWINDOW_H diff --git a/examples/scriptableapplication/pyside2.pri b/examples/scriptableapplication/pyside2.pri new file mode 100644 index 000000000..bd0eeef9e --- /dev/null +++ b/examples/scriptableapplication/pyside2.pri @@ -0,0 +1,19 @@ +PYTHON_INCLUDE = $$system(python $$PWD/pyside2_config.py --python-include) +isEmpty(PYTHON_INCLUDE): error(Unable to locate Python) +PYTHON_LFLAGS = $$system(python $$PWD/pyside2_config.py --python-link) + +PYSIDE2 = $$system(python $$PWD/pyside2_config.py --pyside2) +isEmpty(PYSIDE2): error(Unable to locate PySide2) +PYSIDE2_INCLUDE = $$system(python $$PWD/pyside2_config.py --pyside2-include) +PYSIDE2_LFLAGS = $$system(python $$PWD/pyside2_config.py --pyside2-link) +PYSIDE2_SHARED_LIBRARIES = $$system(python $$PWD/pyside2_config.py --pyside2-shared-libraries) +CLANG_BIN_DIR = $$system(python $$PWD/pyside2_config.py --clang-bin-dir) + +INCLUDEPATH += $$PYTHON_INCLUDE $$PYSIDE2_INCLUDE +LIBS += $$PYTHON_LFLAGS $$PYSIDE2_LFLAGS + +!build_pass:message(Using $$PYSIDE2) + +!win32 { + QMAKE_RPATHDIR += $$PYSIDE2 +} diff --git a/examples/scriptableapplication/pyside2_config.py b/examples/scriptableapplication/pyside2_config.py new file mode 100644 index 000000000..c81d81827 --- /dev/null +++ b/examples/scriptableapplication/pyside2_config.py @@ -0,0 +1,225 @@ +############################################################################# +## +## Copyright (C) 2017 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the PySide examples of the Qt Toolkit. +## +## $QT_BEGIN_LICENSE:BSD$ +## You may use this file under the terms of the BSD license as follows: +## +## "Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are +## met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in +## the documentation and/or other materials provided with the +## distribution. +## * Neither the name of The Qt Company Ltd nor the names of its +## contributors may be used to endorse or promote products derived +## from this software without specific prior written permission. +## +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os, glob, re, sys, imp +from distutils import sysconfig +if sys.platform == 'win32': + import winreg + +usage = """ +Utility to determine include/link options of PySide2 and Python for qmake + +Usage: pyside2_config.py [option] +Options: + --python-include Print Python include path + --python-link Print Python link flags + --pyside2 Print PySide2 location + --pyside2-include Print PySide2 include paths + --pyside2-link Print PySide2 link flags + --pyside2-shared-libraries Print paths of PySide2 shared libraries (.so's, .dylib's, .dll's) + --clang-bin-dir Print path to the clang bin directory + -a Print all + --help/-h Print this help +""" + +def cleanPath(path): + return path if sys.platform != 'win32' else path.replace('\\', '/') + +def sharedLibrarySuffix(): + if sys.platform == 'win32': + return 'lib' + elif sys.platform == 'darwin': + return 'dylib' + return 'so' + +def sharedLibraryGlobPattern(): + glob = '*.' + sharedLibrarySuffix() + return glob if sys.platform == 'win32' else 'lib' + glob + +# Return qmake link option for a library file name +def linkOption(lib): + baseName = os.path.splitext(os.path.basename(lib))[0] + link = ' -l' + if sys.platform in ['linux', 'linux2', 'darwin']: # Linux: 'libfoo.so' -> '-lfoo' + link += baseName[3:] + else: + link += baseName + return link + +# Locate PySide2 via package path +def findPySide2(): + for p in sys.path: + if 'site-' in p: + pyside2 = os.path.join(p, 'PySide2') + if os.path.exists(pyside2): + return cleanPath(os.path.realpath(pyside2)) + return None + +# Return version as "3.5" +def pythonVersion(): + return str(sys.version_info[0]) + '.' + str(sys.version_info[1]) + +def pythonInclude(): + return sysconfig.get_python_inc() + +def pythonLink(): + # @TODO Fix to work with static builds of Python + libdir = sysconfig.get_config_var('LIBDIR') + version = pythonVersion() + version_no_dots = version.replace('.', '') + + if sys.platform == 'win32': + suffix = '_d' if any([tup[0].endswith('_d.pyd') for tup in imp.get_suffixes()]) else '' + return "-L%s -lpython%s%s" % (libdir, version_no_dots, suffix) + + if sys.platform == 'darwin': + return '-L%s -lpython%s' % (libdir, version) + + # Linux and anything else + if sys.version_info[0] < 3: + suffix = '_d' if any([tup[0].endswith('_d.so') for tup in imp.get_suffixes()]) else '' + return "-lpython%s%s" % (version, suffix) + else: + return "-lpython%s%s" % (version, sys.abiflags) + +def pyside2Include(): + pySide2 = findPySide2() + if pySide2 is None: + return None + return "%s/include/PySide2 %s/include/shiboken2" % (pySide2, pySide2) + +def pyside2Link(): + pySide2 = findPySide2() + if pySide2 is None: + return None + link = "-L%s" % pySide2 + for lib in glob.glob(os.path.join(pySide2, sharedLibraryGlobPattern())): + link += ' ' + link += linkOption(lib) + return link + +def pyside2SharedLibraries(): + pySide2 = findPySide2() + if pySide2 is None: + return None + + if sys.platform == 'win32': + libs = [] + for lib in glob.glob(os.path.join(pySide2, sharedLibraryGlobPattern())): + libs.append(os.path.realpath(lib)) + if not libs: + return '' + + dlls = '' + for lib in libs: + dll = os.path.splitext(lib)[0] + '.dll' + dlls += dll + ' ' + + return dlls + else: + libs = '' + for lib in glob.glob(os.path.join(pySide2, sharedLibraryGlobPattern())): + libs += ' ' + lib + return libs + +def clangBinPath(): + source = 'LLVM_INSTALL_DIR' + clangDir = os.environ.get(source, None) + if not clangDir: + source = 'CLANG_INSTALL_DIR' + clangDir = os.environ.get(source, None) + if not clangDir: + source = 'llvm-config' + try: + output = run_process_output([source, '--prefix']) + if output: + clangDir = output[0] + except OSError: + pass + if clangDir: + return os.path.realpath(clangDir + os.path.sep + 'bin') + return '' + +option = sys.argv[1] if len(sys.argv) == 2 else '-a' +if option == '-h' or option == '--help': + print(usage) + sys.exit(0) + +if option == '--pyside2' or option == '-a': + pySide2 = findPySide2() + if pySide2 is None: + sys.exit('Unable to locate PySide2') + print(pySide2) + +if option == '--pyside2-link' or option == '-a': + l = pyside2Link() + if l is None: + sys.exit('Unable to locate PySide2') + print(l) + +if option == '--pyside2-include' or option == '-a': + i = pyside2Include() + if i is None: + sys.exit('Unable to locate PySide2') + print(i) + +if option == '--python-include' or option == '-a': + i = pythonInclude() + if i is None: + sys.exit('Unable to locate Python') + print(i) + +if option == '--python-link' or option == '-a': + l = pythonLink() + if l is None: + sys.exit('Unable to locate Python') + print(l) + +if option == '--pyside2-shared-libraries' or option == '-a': + l = pyside2SharedLibraries() + if l is None: + sys.exit('Unable to locate the PySide sahred libraries') + print(l) + +if option == '--clang-bin-dir' or option == '-a': + l = clangBinPath() + if l is None: + sys.exit('Unable to locate Clang') + print(l) diff --git a/examples/scriptableapplication/pythonutils.cpp b/examples/scriptableapplication/pythonutils.cpp new file mode 100644 index 000000000..27f1bc71c --- /dev/null +++ b/examples/scriptableapplication/pythonutils.cpp @@ -0,0 +1,169 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the PySide examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "pythonutils.h" + +#include +#include +#include +#include + +#include +#include +#include + +/* from AppLib bindings */ + +#if PY_MAJOR_VERSION >= 3 + extern "C" PyObject *PyInit_AppLib(); +#else + extern "C" void initAppLib(); +#endif + +// This variable stores all Python types exported by this module. +extern PyTypeObject **SbkAppLibTypes; + +// This variable stores all type converters exported by this module. +extern SbkConverter **SbkAppLibTypeConverters; + +namespace PythonUtils { + +static State state = PythonUninitialized; + +static void cleanup() +{ + if (state > PythonUninitialized) { + Py_Finalize(); + state = PythonUninitialized; + } +} + +State init() +{ + if (state > PythonUninitialized) + return state; + + // If there is an active python virtual environment, use that environment's packages location. + QByteArray virtualEnvPath = qgetenv("VIRTUAL_ENV"); + if (!virtualEnvPath.isEmpty()) + qputenv("PYTHONHOME", virtualEnvPath); + + Py_Initialize(); + qAddPostRoutine(cleanup); + state = PythonInitialized; +#if PY_MAJOR_VERSION >= 3 + const bool pythonInitialized = PyInit_AppLib() != nullptr; +#else + const bool pythonInitialized = true; + initAppLib(); +#endif + const bool pyErrorOccurred = PyErr_Occurred() != nullptr; + if (pythonInitialized && !pyErrorOccurred) { + state = AppModuleLoaded; + } else { + if (pyErrorOccurred) + PyErr_Print(); + qWarning("Failed to initialize the module."); + } + return state; +} + +bool bindAppObject(const QString &moduleName, const QString &name, + int index, QObject *o) +{ + if (init() != AppModuleLoaded) + return false; + PyTypeObject *typeObject = SbkAppLibTypes[index]; + + PyObject *po = Shiboken::Conversions::pointerToPython(reinterpret_cast(typeObject), o); + if (!po) { + qWarning() << __FUNCTION__ << "Failed to create wrapper for" << o; + return false; + } + Py_INCREF(po); + + PyObject *module = PyImport_AddModule(moduleName.toLocal8Bit().constData()); + if (!module) { + Py_DECREF(po); + if (PyErr_Occurred()) + PyErr_Print(); + qWarning() << __FUNCTION__ << "Failed to locate module" << moduleName; + return false; + } + + if (PyModule_AddObject(module, name.toLocal8Bit().constData(), po) < 0) { + if (PyErr_Occurred()) + PyErr_Print(); + qWarning() << __FUNCTION__ << "Failed add object" << name << "to" << moduleName; + return false; + } + + return true; +} + +bool runScript(const QStringList &script) +{ + if (init() == PythonUninitialized) + return false; + bool result = true; + for (const QString& lineS : script) { + const QByteArray line = lineS.toUtf8(); + if (PyRun_SimpleString(line.constData()) == -1) { + if (PyErr_Occurred()) + PyErr_Print(); + qWarning() << __FUNCTION__ << "Error at" << line; + result = false; + break; + } + } + return result; +} + +} // namespace PythonUtils diff --git a/examples/scriptableapplication/pythonutils.h b/examples/scriptableapplication/pythonutils.h new file mode 100644 index 000000000..53e8c4dab --- /dev/null +++ b/examples/scriptableapplication/pythonutils.h @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the PySide examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PYTHONUTILS_H +#define PYTHONUTILS_H + +class QObject; +class QString; +class QStringList; + +namespace PythonUtils { + +enum AppLibTypes +{ + MainWindowType = 0 // SBK_MAINWINDOW_IDX +}; + +enum State +{ + PythonUninitialized, + PythonInitialized, + AppModuleLoaded +}; + +State init(); + +bool bindAppObject(const QString &moduleName, const QString &name, + int index, QObject *o); + +bool runScript(const QStringList &script); + +} // namespace PythonUtils + +#endif // PYTHONUTILS_H diff --git a/examples/scriptableapplication/scriptableapplication.pro b/examples/scriptableapplication/scriptableapplication.pro new file mode 100644 index 000000000..2719160f3 --- /dev/null +++ b/examples/scriptableapplication/scriptableapplication.pro @@ -0,0 +1,92 @@ +TEMPLATE = app +CONFIG += no_keywords # avoid clash with slots in Python.h +CONFIG += console force_debug_info +QT += widgets + +include(pyside2.pri) + +WRAPPED_HEADER = wrappedclasses.h +WRAPPER_DIR = $$OUT_PWD/AppLib +TYPESYSTEM_FILE = scriptableapplication.xml + +QT_INCLUDEPATHS = -I$$[QT_INSTALL_HEADERS] -I$$[QT_INSTALL_HEADERS]/QtCore \ + -I$$[QT_INSTALL_HEADERS]/QtGui -I$$[QT_INSTALL_HEADERS]/QtWidgets + +SHIBOKEN_OPTIONS = --generator-set=shiboken --enable-parent-ctor-heuristic \ + --enable-pyside-extensions --enable-return-value-heuristic --use-isnull-as-nb_nonzero \ + $$QT_INCLUDEPATHS -I$$PWD -T$$PWD -T$$PYSIDE2/typesystems --output-directory=$$OUT_PWD + +# MSVC does not honor #define protected public... +win32:SHIBOKEN_OPTIONS += --avoid-protected-hack + +# Prepare the shiboken tool +QT_TOOL.shiboken.binary = $$system_path($$PYSIDE2/shiboken2) +win32 { + # Add the libclang/bin subdir to PATH. + CLANG_PATH.name = PATH + CLANG_PATH.value = $$CLANG_BIN_DIR + CLANG_PATH.CONFIG += prepend + exists($$CLANG_PATH.value): QT_TOOL_ENV = CLANG_PATH +} +qtPrepareTool(SHIBOKEN, shiboken) +QT_TOOL_ENV = + +# Shiboken run that adds the module wrapper to GENERATED_SOURCES +shiboken.output = $$WRAPPER_DIR/applib_module_wrapper.cpp +shiboken.commands = $$SHIBOKEN $$SHIBOKEN_OPTIONS $$PWD/wrappedclasses.h ${QMAKE_FILE_IN} +shiboken.input = TYPESYSTEM_FILE +shiboken.dependency_type = TYPE_C +shiboken.variable_out = GENERATED_SOURCES + +# A dummy command that pretends to produce the class wrappers from the headers +# depending on the module wrapper +WRAPPED_CLASSES = mainwindow.h +module_wrapper_dummy_command.output = $$WRAPPER_DIR/${QMAKE_FILE_BASE}_wrapper.cpp +module_wrapper_dummy_command.commands = echo ${QMAKE_FILE_IN} +module_wrapper_dummy_command.depends = $$WRAPPER_DIR/applib_module_wrapper.cpp +module_wrapper_dummy_command.input = WRAPPED_CLASSES +module_wrapper_dummy_command.dependency_type = TYPE_C +module_wrapper_dummy_command.variable_out = GENERATED_SOURCES + +# Get the path component to the active config build folder +defineReplace(getOutDir) { + out_dir = $$OUT_PWD + CONFIG(release, debug|release): out_dir = $$out_dir/release + else:out_dir = $$out_dir/debug + return($$out_dir) +} + +# Create hardlinks to the PySide2 shared libraries, so the example can be executed without manually +# setting the PATH. +win32 { + out_dir = $$getOutDir() + # no_link tell not to link to the output files, target_predeps forces the command to actually + # execute, explicit_dependencies is a magic value that tells qmake not to run the commands + # if the output files already exist. + hard_link_libraries.CONFIG = no_link target_predeps explicit_dependencies + hard_link_libraries.output = $$out_dir/${QMAKE_FILE_BASE}${QMAKE_FILE_EXT} + hard_link_libraries.commands = mklink /H $$shell_path($$out_dir/${QMAKE_FILE_BASE}${QMAKE_FILE_EXT}) $$shell_path(${QMAKE_FILE_IN}) + hard_link_libraries.input = PYSIDE2_SHARED_LIBRARIES +} + +QMAKE_EXTRA_COMPILERS += shiboken module_wrapper_dummy_command +win32:QMAKE_EXTRA_COMPILERS += hard_link_libraries + +INCLUDEPATH += $$WRAPPER_DIR + +# fixme: Hack to find wrappers +PACKAGE_DIR = $$PWD/../../pyside_package/PySide2 + +INCLUDEPATH += $$PACKAGE_DIR/include/PySide2/QtWidgets \ + $$PACKAGE_DIR/include/PySide2/QtGui $$PACKAGE_DIR/include/PySide2/QtCore + +SOURCES += \ + main.cpp \ + mainwindow.cpp \ + pythonutils.cpp + +HEADERS += \ + mainwindow.h \ + pythonutils.h + +OTHER_FILES += $$TYPESYSTEM_FILE $$WRAPPED_HEADER pyside2_config.py README.txt diff --git a/examples/scriptableapplication/scriptableapplication.xml b/examples/scriptableapplication/scriptableapplication.xml new file mode 100644 index 000000000..18e8277ff --- /dev/null +++ b/examples/scriptableapplication/scriptableapplication.xml @@ -0,0 +1,56 @@ + + + + + + diff --git a/examples/scriptableapplication/wrappedclasses.h b/examples/scriptableapplication/wrappedclasses.h new file mode 100644 index 000000000..c905e2356 --- /dev/null +++ b/examples/scriptableapplication/wrappedclasses.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the PySide examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef WRAPPEDCLASSES_H +#define WRAPPEDCLASSES_H + +#include + +#endif // WRAPPEDCLASSES_H -- cgit v1.2.3