FavoriteMaterialsModel.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. ## Model that shows the list of favorite materials.
  6. class FavoriteMaterialsModel(BaseMaterialsModel):
  7. def __init__(self, parent = None):
  8. super().__init__(parent)
  9. cura.CuraApplication.CuraApplication.getInstance().getPreferences().preferenceChanged.connect(self._onFavoritesChanged)
  10. self._update()
  11. ## Triggered when any preference changes, but only handles it when the list
  12. # of favourites is changed.
  13. def _onFavoritesChanged(self, preference_key: str) -> None:
  14. if preference_key != "cura/favorite_materials":
  15. return
  16. self._update()
  17. def _update(self):
  18. if not self._canUpdate():
  19. return
  20. # Get updated list of favorites
  21. self._favorite_ids = self._material_manager.getFavorites()
  22. item_list = []
  23. for root_material_id, container_node in self._available_materials.items():
  24. # Do not include the materials from a to-be-removed package
  25. if bool(container_node.container.getMetaDataEntry("removed", False)):
  26. continue
  27. # Only add results for favorite materials
  28. if root_material_id not in self._favorite_ids:
  29. continue
  30. item = self._createMaterialItem(root_material_id, container_node)
  31. if item:
  32. item_list.append(item)
  33. # Sort the item list alphabetically by name
  34. item_list = sorted(item_list, key = lambda d: d["brand"].upper())
  35. self.setItems(item_list)