From 3412dc563119221bd730c1487937519df0761792 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 27 Sep 2018 15:00:08 +0200 Subject: Clean up import hierarchy PythonExtension.QtCreator.* -> QtCreator.* PythonExtension.PluginInstance -> QtCreator.PythonExtensions Also enables imports of the form "from QtCreator import Core" it is no longer necessary to write QtCreator.Core.... Change-Id: Ib9b433868dcc3fc7d1d534c6023bae7bf6d05fec Reviewed-by: Eike Ziller --- docs/extensions.md | 29 +++++++++++----------- examples/examples_common.py | 8 +++--- examples/macroexpander.py | 13 +++++----- examples/projects/__init__.py | 6 ++--- examples/projects/view.py | 6 ++--- examples/requirerequests/__init__.py | 6 ++--- examples/smallmenu/__init__.py | 8 +++--- examples/transform/__init__.py | 4 +-- examples/transform/actions.py | 4 +-- examples/transform/ui.py | 4 +-- optional/projectexplorer/binding.cpp | 2 +- optional/texteditor/binding.cpp | 2 +- plugins/pythonextensions/pythonextensions.pro | 2 +- .../pythonextensions/pythonextensionsplugin.cpp | 4 +-- plugins/pythonextensions/pyutil.cpp | 11 ++++---- plugins/pythonextensions/pyutil.h | 2 +- plugins/pythonextensions/typesystem_qtcreator.xml | 2 +- python/extensionmanager/__init__.py | 10 ++++---- python/extensionmanager/actions.py | 8 +++--- python/extensionmanager/list.py | 10 ++++---- 20 files changed, 72 insertions(+), 69 deletions(-) diff --git a/docs/extensions.md b/docs/extensions.md index 709acff..57a6f15 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -6,27 +6,28 @@ for Qt Creator. ## Importing Qt Creator specific modules Currently, the bindings are available from Python under the following names: ```Python -from PythonExtension import QtCreator # Imports the generated module for the typesystem -from PythonExtension import PluginInstance # Imports the plugin instance +from QtCreator import Utils # Imports Qt Creator's Utils helper library +from QtCreator import Core # Imports Qt Creator's Core plugin API +from QtCreator import PythonExtensions # Imports Python extension specific helper functions ``` -## `PythonExtension.PluginInstance` +## `QtCreator.PythonExtensions` This is the Python binding for the extension manager that works on the C++ side. It provides the -following functions, that can (and should) be used from Python: +following functions, that can be used from Python: ```Python -from PythonExtension import PluginInstance as inst +from QtCreator import PythonExtensions # Returns a PySide QDir which points to the extension directory -inst.extensionDir() +PythonExtensions.extensionDir() # Returns a list with the names of all Python extensions # Each extension has properties 'name' and 'loaded' -inst.extensionList() +PythonExtensions.extensionList() # Returns the path of the custom location to # where missing dependencies should be pip installed -inst.pythonPackagePath() +PythonExtensions.pythonPackagePath() ``` @@ -42,7 +43,7 @@ add a new action container, that holds a sub-menu. The following code snippet illustrates how to add a new menu. ```Python -from PythonExtension import QtCreator +from QtCreator import Core def hello(): print("Hello World.") @@ -50,24 +51,24 @@ def hello(): # By convention, the menuId starts with "Python" menuId = "Python.SmallMenu.Menu" -menu = QtCreator.Core.ActionManager.createMenu(menuId) +menu = Core.ActionManager.createMenu(menuId) menu.menu().setTitle("My menu") menu.menu().addAction("My action", hello) -# Add our new menu to the "Tools" menu in QtCreator -QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools").addMenu(menu) +# Add our new menu to the "Tools" menu in Qt Creator +Core.ActionManager.actionContainer("QtCreator.Menu.Tools").addMenu(menu) ``` ### Adding a new action directly The following code snippet illustrates how to add a new action to an existing action container. ```Python -from PythonExtension import QtCreator +from QtCreator import Core def hello(): print("Hello World.") # Add a new action to the "Tools" menu -menu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools") +menu = Core.ActionManager.actionContainer("QtCreator.Menu.Tools") menu.menu().addAction("My action", hello) ``` diff --git a/examples/examples_common.py b/examples/examples_common.py index db7f669..0bb93d4 100644 --- a/examples/examples_common.py +++ b/examples/examples_common.py @@ -38,15 +38,15 @@ ## ############################################################################# -from PythonExtension import QtCreator +from QtCreator import Core def ensureExamplesMenu(): menuId = 'PythonExtensions.Examples' - menu = QtCreator.Core.ActionManager.actionContainer(menuId) + menu = Core.ActionManager.actionContainer(menuId) if not menu: - menu = QtCreator.Core.ActionManager.createMenu(menuId) + menu = Core.ActionManager.createMenu(menuId) menu.menu().setTitle("Python Extensions Examples") - toolsMenu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools") + toolsMenu = Core.ActionManager.actionContainer("QtCreator.Menu.Tools") toolsMenu.addMenu(menu) return menu diff --git a/examples/macroexpander.py b/examples/macroexpander.py index 20e10a3..17c30cc 100644 --- a/examples/macroexpander.py +++ b/examples/macroexpander.py @@ -45,22 +45,23 @@ import examples_common from PySide2.QtCore import QObject, SIGNAL from PySide2.QtWidgets import QInputDialog, QMessageBox, QAction -from PythonExtension import QtCreator +from QtCreator import Core +from QtCreator import Utils # Register our macro (it can be used as %{Python:exp}) -QtCreator.Utils.MacroExpander().registerPrefix(b"Python", "Evaluate Python expressions.", lambda x: eval(x)) +Utils.MacroExpander().registerPrefix(b"Python", "Evaluate Python expressions.", lambda x: eval(x)) # Add a small menu item, that let's us test the macro def act(): - text = QInputDialog.getMultiLineText(QtCreator.Core.ICore.dialogParent(), + text = QInputDialog.getMultiLineText(Core.ICore.dialogParent(), "Input Text", "Input your text, including some macros", "3 + 3 = %{Python:3+3}") - text = QtCreator.Utils.MacroExpander().expand(text[0]) - QMessageBox.information(QtCreator.Core.ICore.dialogParent(), "Result", text) + text = Utils.MacroExpander().expand(text[0]) + QMessageBox.information(Core.ICore.dialogParent(), "Result", text) # Add this to the "Tools" menu action = QAction("Test Macro Expander...") -command = QtCreator.Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.MacroExpander') +command = Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.MacroExpander') QObject.connect(action, SIGNAL('triggered()'), act) examples_common.addExampleItem(command) diff --git a/examples/projects/__init__.py b/examples/projects/__init__.py index 7a17d77..a618e5b 100644 --- a/examples/projects/__init__.py +++ b/examples/projects/__init__.py @@ -45,13 +45,13 @@ from . import view import examples_common -from PythonExtension import QtCreator +from QtCreator import Core from PySide2.QtCore import QObject, SIGNAL from PySide2.QtWidgets import QAction # When importing optional bindings, we can warn the user in case things go south try: - from PythonExtension.QtCreator import ProjectExplorer + from QtCreator import ProjectExplorer except ImportError: view.error("The extension \"projects\" could not be loaded, since it depends on a disabled plugin.") raise Exception("Dependencies missing") @@ -65,7 +65,7 @@ def showProjectPath(): view.error("Please open a project") action = QAction("Show Project Directory") -command = QtCreator.Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.ShowProjectDirectory') +command = Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.ShowProjectDirectory') QObject.connect(action, SIGNAL('triggered()'), showProjectPath) examples_common.addExampleItem(command) diff --git a/examples/projects/view.py b/examples/projects/view.py index 31dbbe6..df1eeb0 100644 --- a/examples/projects/view.py +++ b/examples/projects/view.py @@ -41,10 +41,10 @@ # This contains all the display logic from PySide2 import QtWidgets -from PythonExtension import QtCreator +from QtCreator import Core def error(text, title="Error"): - QtWidgets.QMessageBox.critical(QtCreator.Core.ICore.instance().dialogParent(), title, text) + QtWidgets.QMessageBox.critical(Core.ICore.instance().dialogParent(), title, text) def show(text, title="Result"): - QtWidgets.QMessageBox.information(QtCreator.Core.ICore.instance().dialogParent(), title, text) + QtWidgets.QMessageBox.information(Core.ICore.instance().dialogParent(), title, text) diff --git a/examples/requirerequests/__init__.py b/examples/requirerequests/__init__.py index acbf9e6..3fb14c7 100644 --- a/examples/requirerequests/__init__.py +++ b/examples/requirerequests/__init__.py @@ -44,7 +44,7 @@ import examples_common -from PythonExtension import QtCreator +from QtCreator import Core from PySide2.QtCore import QObject, SIGNAL from PySide2.QtWidgets import QAction, QMessageBox @@ -52,14 +52,14 @@ import requests def load(url): r = requests.get(url) - box = QMessageBox(QtCreator.Core.ICore.dialogParent()) + box = QMessageBox(Core.ICore.dialogParent()) box.setWindowTitle("Request results") box.setText("The request status is {}".format(r.status_code)) box.setDetailedText(r.text) box.exec_() action = QAction("Load from the web...") -command = QtCreator.Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.LoadFromWeb') +command = Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.LoadFromWeb') QObject.connect(action, SIGNAL('triggered()'), lambda: load("https://www.qt.io/")) examples_common.addExampleItem(command) diff --git a/examples/smallmenu/__init__.py b/examples/smallmenu/__init__.py index f01a8b0..f26b637 100644 --- a/examples/smallmenu/__init__.py +++ b/examples/smallmenu/__init__.py @@ -44,24 +44,24 @@ from .actions import hello, goodbye import examples_common -from PythonExtension import QtCreator +from QtCreator import Core from PySide2.QtCore import QObject, SIGNAL from PySide2.QtWidgets import QAction # Create submenu and add it to examples menu menuId = "PythonExtensions.Examples.SmallMenu" -menu = QtCreator.Core.ActionManager.createMenu(menuId) +menu = Core.ActionManager.createMenu(menuId) menu.menu().setTitle("Small Menu") examplesMenu = examples_common.ensureExamplesMenu() examplesMenu.addMenu(menu) # Add actions to our new menu helloAction = QAction("Say Hello") -command = QtCreator.Core.ActionManager.registerAction(helloAction, 'PythonExtensions.Examples.SayHello') +command = Core.ActionManager.registerAction(helloAction, 'PythonExtensions.Examples.SayHello') QObject.connect(helloAction, SIGNAL('triggered()'), hello) menu.addAction(command) goodbyeAction = QAction("Say Goodbye") -command = QtCreator.Core.ActionManager.registerAction(goodbyeAction, 'PythonExtensions.Examples.SayGoodbye') +command = Core.ActionManager.registerAction(goodbyeAction, 'PythonExtensions.Examples.SayGoodbye') QObject.connect(goodbyeAction, SIGNAL('triggered()'), goodbye) menu.addAction(command) diff --git a/examples/transform/__init__.py b/examples/transform/__init__.py index 4934e72..f2647dc 100644 --- a/examples/transform/__init__.py +++ b/examples/transform/__init__.py @@ -46,7 +46,7 @@ from . import actions from . import ui import examples_common -from PythonExtension import QtCreator +from QtCreator import Core from PySide2.QtCore import QObject, SIGNAL from PySide2.QtWidgets import QAction @@ -56,7 +56,7 @@ def transform(): actions.run(code[0]) action = QAction("Transform files...") -command = QtCreator.Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.TransformFiles') +command = Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.TransformFiles') QObject.connect(action, SIGNAL('triggered()'), transform) examples_common.addExampleItem(command) diff --git a/examples/transform/actions.py b/examples/transform/actions.py index da65b21..e459ab2 100644 --- a/examples/transform/actions.py +++ b/examples/transform/actions.py @@ -40,12 +40,12 @@ from . import ui -from PythonExtension import QtCreator +from QtCreator import Core # Apply a function to each file def run(code): try: - files = QtCreator.Core.DocumentManager.getOpenFileNames("") + files = Core.DocumentManager.getOpenFileNames("") if len(files) == 0: ui.error("No files were selected.") return diff --git a/examples/transform/ui.py b/examples/transform/ui.py index 21892ef..cc5b7f1 100644 --- a/examples/transform/ui.py +++ b/examples/transform/ui.py @@ -38,7 +38,7 @@ ## ############################################################################# -from PythonExtension import QtCreator +from QtCreator import Core from PySide2 import QtWidgets template = """# Parameters: @@ -53,7 +53,7 @@ filebody def getCode(): return QtWidgets.QInputDialog.getMultiLineText( - QtCreator.Core.ICore.dialogParent(), + Core.ICore.dialogParent(), "Input transformation expression", "Transform expression:", template diff --git a/optional/projectexplorer/binding.cpp b/optional/projectexplorer/binding.cpp index 325027b..ae1bbc7 100644 --- a/optional/projectexplorer/binding.cpp +++ b/optional/projectexplorer/binding.cpp @@ -59,7 +59,7 @@ void bind() // Bind module into interpreter bool pythonError = PyErr_Occurred() != nullptr; if (pythonInitialized && !pythonError) { - PyUtil::bindSubPyObject("PythonExtension.QtCreator", "ProjectExplorer", (void *)SbkQtCreatorBindingProjectExplorerModuleObject); + PyUtil::bindSubPyObject("QtCreator", "ProjectExplorer", (void *)SbkQtCreatorBindingProjectExplorerModuleObject); } else { if (pythonError) PyErr_Print(); diff --git a/optional/texteditor/binding.cpp b/optional/texteditor/binding.cpp index d2d5b83..6b9007f 100644 --- a/optional/texteditor/binding.cpp +++ b/optional/texteditor/binding.cpp @@ -59,7 +59,7 @@ void bind() // Bind module into interpreter bool pythonError = PyErr_Occurred() != nullptr; if (pythonInitialized && !pythonError) { - PyUtil::bindSubPyObject("PythonExtension.QtCreator", "TextEditor", (void *)SbkQtCreatorBindingTextEditorModuleObject); + PyUtil::bindSubPyObject("QtCreator", "TextEditor", (void *)SbkQtCreatorBindingTextEditorModuleObject); } else { if (pythonError) PyErr_Print(); diff --git a/plugins/pythonextensions/pythonextensions.pro b/plugins/pythonextensions/pythonextensions.pro index e28b860..334e029 100644 --- a/plugins/pythonextensions/pythonextensions.pro +++ b/plugins/pythonextensions/pythonextensions.pro @@ -24,7 +24,7 @@ include($$IDE_SOURCE_TREE/src/qtcreatorplugin.pri) # Qt Creator Core bindings WRAPPED_HEADER = $$PWD/wrappedclasses.h -WRAPPER_DIR = $$OUT_PWD/PythonExtension/QtCreator +WRAPPER_DIR = $$OUT_PWD/QtCreator TYPESYSTEM_FILE = typesystem_qtcreator.xml TYPESYSTEM_NAME = qtcreator SHIBOKEN_QT = core gui widgets diff --git a/plugins/pythonextensions/pythonextensionsplugin.cpp b/plugins/pythonextensions/pythonextensionsplugin.cpp index 893819c..59b32a1 100644 --- a/plugins/pythonextensions/pythonextensionsplugin.cpp +++ b/plugins/pythonextensions/pythonextensionsplugin.cpp @@ -188,13 +188,13 @@ void PythonExtensionsPlugin::initializePythonBindings() QDir().mkpath(pythonPackagePath()); PyUtil::addToSysPath(pythonPackagePath().toStdString()); // Initialize the Python context and register global Qt Creator variable - if (!PyUtil::bindShibokenModuleObject("PythonExtension", "QtCreator")) { + if (!PyUtil::bindCoreModules()) { qWarning() << "Python bindings could not be initialized"; Core::MessageManager::write(Constants::MESSAGE_MANAGER_PREFIX + tr("Python bindings could not be initialized")); return; } // Bind the plugin instance - PyUtil::bindObject("PythonExtension", "PluginInstance", PyUtil::PythonExtensionsPluginType, this); + PyUtil::bindObject("QtCreator", "PythonExtensions", PyUtil::PythonExtensionsPluginType, this); } void PythonExtensionsPlugin::initializeOptionalBindings() diff --git a/plugins/pythonextensions/pyutil.cpp b/plugins/pythonextensions/pyutil.cpp index b563c55..b992e36 100644 --- a/plugins/pythonextensions/pyutil.cpp +++ b/plugins/pythonextensions/pyutil.cpp @@ -56,10 +56,10 @@ extern "C" void initQtCreator(); // These variables store all Python types exported by QtCreators bindings, // as well as the types exported for QtWidgets. -extern PyTypeObject **SbkPythonExtension_QtCreatorTypes; +extern PyTypeObject **SbkQtCreatorTypes; // This variable stores the Python module generated by Shiboken -extern PyObject *SbkPythonExtension_QtCreatorModuleObject; +extern PyObject *SbkQtCreatorModuleObject; namespace PyUtil { @@ -157,7 +157,7 @@ bool bindObject(const QString &moduleName, const QString &name, int index, void return false; // Generate the type - PyTypeObject *typeObject = SbkPythonExtension_QtCreatorTypes[index]; + PyTypeObject *typeObject = SbkQtCreatorTypes[index]; PyObject *po = Shiboken::Conversions::pointerToPython(reinterpret_cast(typeObject), o); if (!po) { @@ -169,9 +169,10 @@ bool bindObject(const QString &moduleName, const QString &name, int index, void return bindPyObject(moduleName, name, po); } -bool bindShibokenModuleObject(const QString &moduleName, const QString &name) +bool bindCoreModules() { - return bindPyObject(moduleName, name, SbkPythonExtension_QtCreatorModuleObject); + return bindSubPyObject("QtCreator", "Utils", SbkQtCreatorModuleObject) + && bindSubPyObject("QtCreator", "Core", SbkQtCreatorModuleObject); } bool bindPyObject(const QString &moduleName, const QString &name, void *obj) diff --git a/plugins/pythonextensions/pyutil.h b/plugins/pythonextensions/pyutil.h index 23fdb37..ca58f6e 100644 --- a/plugins/pythonextensions/pyutil.h +++ b/plugins/pythonextensions/pyutil.h @@ -53,7 +53,7 @@ PYTHONEXTENSIONSSHARED_EXPORT bool createModule(const std::string &moduleName); bool bindObject(const QString &moduleName, const QString &name, int index, void *o); -bool bindShibokenModuleObject(const QString &moduleName, const QString &name); +bool bindCoreModules(); PYTHONEXTENSIONSSHARED_EXPORT bool bindPyObject(const QString &moduleName, const QString &name, void *obj); diff --git a/plugins/pythonextensions/typesystem_qtcreator.xml b/plugins/pythonextensions/typesystem_qtcreator.xml index 49dd794..69f051e 100644 --- a/plugins/pythonextensions/typesystem_qtcreator.xml +++ b/plugins/pythonextensions/typesystem_qtcreator.xml @@ -41,7 +41,7 @@ --> - + diff --git a/python/extensionmanager/__init__.py b/python/extensionmanager/__init__.py index 8588796..f6caa37 100644 --- a/python/extensionmanager/__init__.py +++ b/python/extensionmanager/__init__.py @@ -42,20 +42,20 @@ from .list import * -from PythonExtension import QtCreator +from QtCreator import Core from PySide2.QtCore import * from PySide2.QtWidgets import * # Actions def showExtensionList(): - dialog = ListView(QtCreator.Core.ICore.dialogParent()) + dialog = ListView(Core.ICore.dialogParent()) dialog.exec_() pythonExtensionsAction = QAction("About Python Extensions...") pythonExtensionsAction.setMenuRole(QAction.ApplicationSpecificRole) -pythonExtensionsCommand = QtCreator.Core.ActionManager.registerAction(pythonExtensionsAction, - "PythonExtensions.About") +pythonExtensionsCommand = Core.ActionManager.registerAction(pythonExtensionsAction, + "PythonExtensions.About") QObject.connect(pythonExtensionsAction, SIGNAL("triggered()"), showExtensionList) -helpMenu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Help") +helpMenu = Core.ActionManager.actionContainer("QtCreator.Menu.Help") helpMenu.addAction(pythonExtensionsCommand, "QtCreator.Group.Help.About") diff --git a/python/extensionmanager/actions.py b/python/extensionmanager/actions.py index 7f05e77..48e5ae4 100644 --- a/python/extensionmanager/actions.py +++ b/python/extensionmanager/actions.py @@ -41,7 +41,7 @@ # Functions for installing and deleting extensions import sys, zipfile -from PythonExtension import PluginInstance as instance +from QtCreator import PythonExtensions from send2trash import send2trash def install(path): @@ -59,14 +59,14 @@ def install(path): return "The .zip file is malformed." # Make sure the extension manager does not overwrite # extensions that are already installed - for ext in instance.extensionList(): + for ext in PythonExtensions.extensionList(): if extName == ext.name: return "The extension \"{}\" is already installed. Uninstall it before trying again.".format(extName) - extZip.extractall(instance.extensionDir().absolutePath()) + extZip.extractall(PythonExtensions.extensionDir().absolutePath()) extZip.close() except Exception as e: return str(e) return True def uninstall(ext): - send2trash(instance.extensionDir().absolutePath() + "/" + ext) + send2trash(PythonExtensions.extensionDir().absolutePath() + "/" + ext) diff --git a/python/extensionmanager/list.py b/python/extensionmanager/list.py index 32002b1..c71b4d4 100644 --- a/python/extensionmanager/list.py +++ b/python/extensionmanager/list.py @@ -43,7 +43,7 @@ from . import actions from PySide2 import QtCore, QtWidgets, QtGui -from PythonExtension import PluginInstance as instance +from QtCreator import PythonExtensions class ExtensionList(QtWidgets.QListWidget): def __init__(self): @@ -56,7 +56,7 @@ class ExtensionList(QtWidgets.QListWidget): def loadExtensionList(self): i = 0 - for ext in instance.extensionList(): + for ext in PythonExtensions.extensionList(): item = QtWidgets.QListWidgetItem(self) if not ext.loaded: item.setText(ext.name + " [did not load]") @@ -79,7 +79,7 @@ class ListView(QtWidgets.QDialog): self.layout = QtWidgets.QVBoxLayout(self) self.label = QtWidgets.QLabel() - self.label.setText("Manage Python extensions installed to \"{0}\".".format(instance.extensionDir().absolutePath())) + self.label.setText("Manage Python extensions installed to \"{0}\".".format(PythonExtensions.extensionDir().absolutePath())) self.label.setWordWrap(True) self.layout.addWidget(self.label) @@ -116,7 +116,7 @@ class ListView(QtWidgets.QDialog): selected = self.list.selectedIndexes() if len(selected) >= 1: selected = selected[0].row() - ext = instance.extensionList()[selected] + ext = PythonExtensions.extensionList()[selected] if ext == "extensionmanager": QtWidgets.QMessageBox.warning(self, "Can not Uninstall", "The Extension Manager can not uninstall itself.") else: @@ -145,7 +145,7 @@ class ListView(QtWidgets.QDialog): "/", "Qt Creator Python Extensions (*.zip)" ) - oldExtensions = list(instance.extensionList()) + oldExtensions = list(PythonExtensions.extensionList()) result = actions.install(fileName[0]) if result == True: QtWidgets.QMessageBox.information( -- cgit v1.2.3