ContainerSettingsModel.py 3.3 KB

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