diff options
author | Eike Ziller <eike.ziller@qt.io> | 2018-09-14 16:51:16 +0200 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2018-09-17 10:52:28 +0000 |
commit | 0f35a83f35f9f785a67a5989dccc11c4828795b6 (patch) | |
tree | 00e29f8e81e6bb627f1f1e3e5e63892b36310d16 /examples | |
parent | 0fad82172952ced702af35bf4a7aaaa6ef10268e (diff) |
Clean up python extension examples
- create a single Tools submenu for the examples
- share the code that creates the menu if necessary between all examples
- make all the actions nice Qt Creator commands
Change-Id: Ia5ed41a1bb440a1a8087821cc9d9daaa4c8f585a
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/examples.pro | 9 | ||||
-rw-r--r-- | examples/examples_common.py | 55 | ||||
-rw-r--r-- | examples/macroexpander/__init__.py | 15 | ||||
-rw-r--r-- | examples/projects/__init__.py | 10 | ||||
-rw-r--r-- | examples/requirerequests/__init__.py | 17 | ||||
-rw-r--r-- | examples/smallmenu/__init__.py | 27 | ||||
-rw-r--r-- | examples/transform/__init__.py | 15 |
7 files changed, 123 insertions, 25 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index fc65149..57b1764 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -17,5 +17,12 @@ inst_examples.path = $$IDE_PLUGIN_PATH/python inst_examples.CONFIG += no_default_install directory INSTALLS += inst_examples -copyexamples.depends += install_inst_examples +inst_examplefiles.files = \ + examples_common.py + +inst_examplefiles.path = $$IDE_PLUGIN_PATH/python +inst_examplefiles.CONFIG += no_default_install +INSTALLS += inst_examplefiles + +copyexamples.depends += install_inst_examples install_inst_examplefiles QMAKE_EXTRA_TARGETS += copyexamples diff --git a/examples/examples_common.py b/examples/examples_common.py new file mode 100644 index 0000000..088d4bf --- /dev/null +++ b/examples/examples_common.py @@ -0,0 +1,55 @@ +############################################################################# +## +## Copyright (C) 2018 The Qt Company Ltd. +## Contact: http://www.qt.io/licensing/ +## +## This file is part of the Python Extensions Plugin for QtCreator. +## +## $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$ +## +############################################################################# + +from PythonExtension import QtCreator + +def ensureExamplesMenu(): + menuId = 'PythonExtensions.Examples' + menu = QtCreator.Core.ActionManager.actionContainer(menuId) + if not menu: + menu = QtCreator.Core.ActionManager.createMenu(menuId) + menu.menu().setTitle("Python Extensions Examples") + toolsMenu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools") + toolsMenu.addMenu(menu) + return menu + +def addExampleItem(command): + menu = ensureExamplesMenu() + menu.addAction(command) diff --git a/examples/macroexpander/__init__.py b/examples/macroexpander/__init__.py index bd64028..271783d 100644 --- a/examples/macroexpander/__init__.py +++ b/examples/macroexpander/__init__.py @@ -41,7 +41,10 @@ # This example demonstrates how to use the macro expander # API to register a macro that can evaluate Python expressions -from PySide2.QtWidgets import QInputDialog, QMessageBox +import examples_common + +from PySide2.QtCore import QObject, SIGNAL +from PySide2.QtWidgets import QInputDialog, QMessageBox, QAction from PythonExtension import QtCreator # Register our macro (it can be used as %{Python:exp}) @@ -51,11 +54,13 @@ QtCreator.Utils.MacroExpander().registerPrefix(b"Python", "Evaluate Python expre def act(): text = QInputDialog.getMultiLineText(QtCreator.Core.ICore.dialogParent(), "Input Text", "Input your text, including some macros", - "3 + 3 = %{Python:3+3}" - ) + "3 + 3 = %{Python:3+3}") text = QtCreator.Utils.MacroExpander().expand(text[0]) QMessageBox.information(QtCreator.Core.ICore.dialogParent(), "Result", text) # Add this to the "Tools" menu -menu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools") -menu.menu().addAction("Test MacroExpander...", act) +action = QAction("Test Macro Expander...") +command = QtCreator.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 29947e7..ec0cc08 100644 --- a/examples/projects/__init__.py +++ b/examples/projects/__init__.py @@ -42,9 +42,12 @@ # project. It also includes a dependency on a plugin # that can be disabled. +import examples_common import view from PythonExtension import QtCreator +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: @@ -61,5 +64,8 @@ def showProjectPath(): else: view.error("Please open a project") -helpMenu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Window") -helpMenu.menu().addAction("Show Project Directory", showProjectPath) +action = QAction("Show Project Directory") +command = QtCreator.Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.ShowProjectDirectory') +QObject.connect(action, SIGNAL('triggered()'), showProjectPath) + +examples_common.addExampleItem(command) diff --git a/examples/requirerequests/__init__.py b/examples/requirerequests/__init__.py index 9ca4ee9..6d31d09 100644 --- a/examples/requirerequests/__init__.py +++ b/examples/requirerequests/__init__.py @@ -42,17 +42,24 @@ # use of requirements.txt files in # extensions. -import requests +import examples_common + from PythonExtension import QtCreator -from PySide2 import QtWidgets +from PySide2.QtCore import QObject, SIGNAL +from PySide2.QtWidgets import QAction, QMessageBox + +import requests def load(url): r = requests.get(url) - box = QtWidgets.QMessageBox(QtCreator.Core.ICore.dialogParent()) + box = QMessageBox(QtCreator.Core.ICore.dialogParent()) box.setWindowTitle("Request results") box.setText("The request status is {}".format(r.status_code)) box.setDetailedText(r.text) box.exec_() -helpMenu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Window") -helpMenu.menu().addAction("Load from the web...", lambda: load("https://www.qt.io/")) +action = QAction("Load from the web...") +command = QtCreator.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 b2753c1..0ad7d05 100644 --- a/examples/smallmenu/__init__.py +++ b/examples/smallmenu/__init__.py @@ -41,16 +41,27 @@ # This example illustrates the use of action # containers to create new menus -from PythonExtension import QtCreator from actions import hello, goodbye +import examples_common -# By convention, the menuId starts with "Python" -menuId = "Python.SmallMenu.Menu" +from PythonExtension import QtCreator +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.menu().setTitle("Small menu") -menu.menu().addAction("Say hello", hello) -menu.menu().addAction("Say goodbye", goodbye) +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') +QObject.connect(helloAction, SIGNAL('triggered()'), hello) +menu.addAction(command) -# Add our new menu to the "Tools" menu in QtCreator -QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools").addMenu(menu) +goodbyeAction = QAction("Say Goodbye") +command = QtCreator.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 b0d47f1..f6b0087 100644 --- a/examples/transform/__init__.py +++ b/examples/transform/__init__.py @@ -42,14 +42,21 @@ # user supplied function that executes on # a set of files specified by the user +import actions +import examples_common +import ui + from PythonExtension import QtCreator -import actions, ui +from PySide2.QtCore import QObject, SIGNAL +from PySide2.QtWidgets import QAction def transform(): code = ui.getCode() if code[1]: actions.run(code[0]) -# Add our new action to the "Tools" menu -menu = QtCreator.Core.ActionManager.actionContainer("QtCreator.Menu.Tools") -menu.menu().addAction("Transform files...", transform) +action = QAction("Transform files...") +command = QtCreator.Core.ActionManager.registerAction(action, 'PythonExtensions.Examples.TransformFiles') +QObject.connect(action, SIGNAL('triggered()'), transform) + +examples_common.addExampleItem(command) |