pyside_component_utils.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """
  2. All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. its licensors.
  4. For complete copyright and license terms please see the LICENSE at the root of this
  5. distribution (the "License"). All use of this software is governed by the License,
  6. or, if provided, by the license below or the license accompanying this file. Do not
  7. remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. """
  10. import PySide2
  11. import pyside_utils
  12. def get_component_combobox_values(component_name, property_name, log_fn=None):
  13. """
  14. Retrieves the Combo box values from a Component. Assumes the Entity has been selected and that the component is
  15. visible in the Entity Inspector.
  16. Works by inspecting the list of child widgets for the Entity from the Entity Inspector, looking for the right type
  17. of widget (QFrame) with a label containing the name of the component. Then, the QFrame is inspected to find a
  18. QComboBox with the property name.
  19. :param component_name: Name of the component to inspect.
  20. :param property_name: Name of the property to inspect.
  21. :param log_fn: Function used to log messages, should take a string as argument like log_fn('message to log').
  22. :return: A list containing the values from the Combo box.
  23. """
  24. def _log_fn_wrapper(message):
  25. log_fn(message) if log_fn else None
  26. editor_window = pyside_utils.get_editor_main_window()
  27. entity_inspector = editor_window.findChild(PySide2.QtWidgets.QDockWidget, 'Entity Inspector')
  28. assert entity_inspector, 'Entity Inspector widget is not valid.'
  29. entity_inspector.update()
  30. component_list_widget = entity_inspector.findChild(PySide2.QtWidgets.QWidget, 'm_componentListContents')
  31. component_list_children = component_list_widget.children()
  32. assert component_list_children, 'Could not retrieve components for the entity.'
  33. # Iterate over the widgets that are children of the component list. On each one, retrieve the first child QFrame
  34. # that has a label with the "component_name" as text.
  35. # If that label is found, the same QFrame is inspected for a child frame with the name matching the "property_name".
  36. # This property frame should have the combo box as child.
  37. for component_widget in component_list_children:
  38. if type(component_widget) is PySide2.QtWidgets.QFrame:
  39. component_label = pyside_utils.find_child_by_pattern(component_widget, {'text': component_name})
  40. if component_label:
  41. property_frame = component_widget.findChild(PySide2.QtWidgets.QFrame, property_name)
  42. if not property_frame:
  43. _log_fn_wrapper(f'QFrame not found as child of the component widget {component_widget}.')
  44. continue
  45. combobox = pyside_utils.find_child_by_pattern(property_frame, {'type': PySide2.QtWidgets.QComboBox})
  46. if not combobox:
  47. _log_fn_wrapper(f'QComboBox not found as child of the property frame {property_frame}.')
  48. continue
  49. item_count = combobox.count()
  50. values = []
  51. for index in range(item_count):
  52. values.append(combobox.itemText(index))
  53. if not values:
  54. _log_fn_wrapper('The QComboBox does not have values to retrieve.')
  55. return values
  56. _log_fn_wrapper('Matching component and property not found in Component list, or the list is empty.')
  57. return None