az_qt_helpers.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import azlmbr
  7. import azlmbr.bus
  8. import azlmbr.editor as editor
  9. from PySide2 import QtWidgets
  10. from shiboken2 import wrapInstance, getCppPointer
  11. # The `view_pane_handlers` holds onto the callback handlers that get created
  12. # for responding to requests for when the Editor needs to construct the view pane
  13. view_pane_handlers = {}
  14. registration_handlers = {}
  15. # Helper method for retrieving the Editor QMainWindow instance
  16. def get_editor_main_window():
  17. params = azlmbr.qt.QtForPythonRequestBus(azlmbr.bus.Broadcast, "GetQtBootstrapParameters")
  18. editor_id = QtWidgets.QWidget.find(params.mainWindowId)
  19. editor_main_window = wrapInstance(int(getCppPointer(editor_id)[0]), QtWidgets.QMainWindow)
  20. return editor_main_window
  21. # Helper method for registering a Python widget as a tool/view pane with the Editor
  22. def register_view_pane(name, widget_type, category="Tools", options=editor.ViewPaneOptions()):
  23. global view_pane_handlers
  24. # The view pane names are unique in the Editor, so make sure one with the same name doesn't exist already
  25. if name in view_pane_handlers:
  26. return
  27. # This method will be invoked by the ViewPaneCallbackBus::CreateViewPaneWidget
  28. # when our view pane needs to be created
  29. def on_create_view_pane_widget(parameters):
  30. editor_main_window = get_editor_main_window()
  31. dock_main_window = editor_main_window.findChild(QtWidgets.QMainWindow)
  32. # Create the view pane widget parented to the Editor QMainWindow, so it can be found
  33. new_widget = widget_type(dock_main_window)
  34. return new_widget.winId()
  35. def on_notify_register_views(parameters, my_name=name, my_category=category, my_options=options):
  36. # Register our widget as an Editor view pane
  37. print('Calling on_notify_register_views RegisterCustomViewPane')
  38. editor.EditorRequestBus(azlmbr.bus.Broadcast, 'RegisterCustomViewPane', my_name, my_category, my_options)
  39. # We keep a handler around in case a request for registering custom view panes comes later
  40. print('Initializing callback for RegisterCustomViewPane')
  41. registration_handler = azlmbr.bus.NotificationHandler("EditorEventBus")
  42. registration_handler.connect()
  43. registration_handler.add_callback("NotifyRegisterViews", on_notify_register_views)
  44. global registration_handlers
  45. registration_handlers[name] = registration_handler
  46. editor.EditorRequestBus(azlmbr.bus.Broadcast, 'RegisterCustomViewPane', name, category, options)
  47. # Connect to the ViewPaneCallbackBus in order to respond to requests to create our widget
  48. # We also need to store our handler so it will exist for the life of the Editor
  49. handler = azlmbr.bus.NotificationHandler("ViewPaneCallbackBus")
  50. handler.connect(name)
  51. handler.add_callback("CreateViewPaneWidget", on_create_view_pane_widget)
  52. view_pane_handlers[name] = handler
  53. # Helper method for unregistering a Python widget as a tool/view pane with the Editor
  54. def unregister_view_pane(name):
  55. global view_pane_handlers
  56. # No need to proceed if we don't have this view pane registered
  57. if name not in view_pane_handlers:
  58. return
  59. # Unregister our view pane from the Editor and remove our stored handler for it
  60. editor.EditorRequestBus(azlmbr.bus.Broadcast, 'UnregisterViewPane', name)
  61. del view_pane_handlers[name]