FavoriteMaterialsModel.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from cura.Machines.Models.BaseMaterialsModel import BaseMaterialsModel
  4. import cura.CuraApplication # To listen to changes to the preferences.
  5. class FavoriteMaterialsModel(BaseMaterialsModel):
  6. """Model that shows the list of favorite materials."""
  7. def __init__(self, parent = None):
  8. super().__init__(parent)
  9. cura.CuraApplication.CuraApplication.getInstance().getPreferences().preferenceChanged.connect(self._onFavoritesChanged)
  10. self._onChanged()
  11. def _onFavoritesChanged(self, preference_key: str) -> None:
  12. """Triggered when any preference changes, but only handles it when the list of favourites is changed. """
  13. if preference_key != "cura/favorite_materials":
  14. return
  15. self._onChanged()
  16. def _update(self):
  17. if not self._canUpdate():
  18. return
  19. super()._update()
  20. item_list = []
  21. for root_material_id, container_node in self._available_materials.items():
  22. # Do not include the materials from a to-be-removed package
  23. if bool(container_node.getMetaDataEntry("removed", False)):
  24. continue
  25. # Only add results for favorite materials
  26. if root_material_id not in self._favorite_ids:
  27. continue
  28. item = self._createMaterialItem(root_material_id, container_node)
  29. if item:
  30. item_list.append(item)
  31. # Sort the item list alphabetically by name
  32. item_list = sorted(item_list, key = lambda d: d["brand"].upper())
  33. self.setItems(item_list)