ContainerSettingsModel.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from UM.Application import Application
  2. from UM.Qt.ListModel import ListModel
  3. from PyQt5.QtCore import pyqtProperty, Qt, pyqtSignal, pyqtSlot, QUrl
  4. from UM.Settings.ContainerRegistry import ContainerRegistry
  5. from UM.Settings.InstanceContainer import InstanceContainer
  6. from UM.Settings.SettingFunction import SettingFunction
  7. class ContainerSettingsModel(ListModel):
  8. LabelRole = Qt.UserRole + 1
  9. CategoryRole = Qt.UserRole + 2
  10. UnitRole = Qt.UserRole + 3
  11. ValuesRole = Qt.UserRole + 4
  12. def __init__(self, parent = None):
  13. super().__init__(parent)
  14. self.addRoleName(self.LabelRole, "label")
  15. self.addRoleName(self.CategoryRole, "category")
  16. self.addRoleName(self.UnitRole, "unit")
  17. self.addRoleName(self.ValuesRole, "values")
  18. self._container_ids = []
  19. self._containers = []
  20. def _onPropertyChanged(self, key, property_name):
  21. if property_name == "value":
  22. self._update()
  23. def _update(self):
  24. self.clear()
  25. if len(self._container_ids) == 0:
  26. return
  27. keys = []
  28. for container in self._containers:
  29. keys = keys + list(container.getAllKeys())
  30. keys = list(set(keys)) # remove duplicate keys
  31. keys.sort()
  32. for key in keys:
  33. definition = None
  34. category = None
  35. values = []
  36. for container in self._containers:
  37. instance = container.getInstance(key)
  38. if instance:
  39. definition = instance.definition
  40. # Traverse up to find the category
  41. category = definition
  42. while category.type != "category":
  43. category = category.parent
  44. value = container.getProperty(key, "value")
  45. if type(value) == SettingFunction:
  46. values.append("=\u0192")
  47. else:
  48. values.append(container.getProperty(key, "value"))
  49. else:
  50. values.append("")
  51. self.appendItem({
  52. "key": key,
  53. "values": values,
  54. "label": definition.label,
  55. "unit": definition.unit,
  56. "category": category.label
  57. })
  58. ## Set the ids of the containers which have the settings this model should list.
  59. # Also makes sure the model updates when the containers have property changes
  60. def setContainers(self, container_ids):
  61. for container in self._containers:
  62. container.propertyChanged.disconnect(self._onPropertyChanged)
  63. self._container_ids = container_ids
  64. self._containers = []
  65. for container_id in self._container_ids:
  66. containers = ContainerRegistry.getInstance().findContainers(id = container_id)
  67. if containers:
  68. containers[0].propertyChanged.connect(self._onPropertyChanged)
  69. self._containers.append(containers[0])
  70. self._update()
  71. containersChanged = pyqtSignal()
  72. @pyqtProperty("QVariantList", fset = setContainers, notify = containersChanged)
  73. def containers(self):
  74. return self.container_ids