MaterialsModel.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Any, List
  4. from UM.Settings.ContainerRegistry import ContainerRegistry #To listen for changes to the materials.
  5. from UM.Settings.Models.InstanceContainersModel import InstanceContainersModel #We're extending this class.
  6. ## A model that shows a list of currently valid materials.
  7. class MaterialsModel(InstanceContainersModel):
  8. def __init__(self, parent = None):
  9. super().__init__(parent)
  10. ContainerRegistry.getInstance().containerMetaDataChanged.connect(self._onContainerMetaDataChanged)
  11. ## Called when the metadata of the container was changed.
  12. #
  13. # This makes sure that we only update when it was a material that changed.
  14. #
  15. # \param container The container whose metadata was changed.
  16. def _onContainerMetaDataChanged(self, container):
  17. if container.getMetaDataEntry("type") == "material": #Only need to update if a material was changed.
  18. self._container_change_timer.start()
  19. def _onContainerChanged(self, container):
  20. if container.getMetaDataEntry("type", "") == "material":
  21. super()._onContainerChanged(container)
  22. ## Group brand together
  23. def _sortKey(self, item) -> List[Any]:
  24. result = []
  25. result.append(item["metadata"]["brand"])
  26. result.append(item["metadata"]["material"])
  27. result.append(item["metadata"]["name"])
  28. result.append(item["metadata"]["color_name"])
  29. result.append(item["metadata"]["id"])
  30. result.extend(super()._sortKey(item))
  31. return result