FavoriteMaterialsModel.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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._onChanged()
  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._onChanged()
  17. def _update(self):
  18. if not self._canUpdate():
  19. return
  20. super()._update()
  21. item_list = []
  22. for root_material_id, container_node in self._available_materials.items():
  23. # Do not include the materials from a to-be-removed package
  24. if bool(container_node.getMetaDataEntry("removed", False)):
  25. continue
  26. # Only add results for favorite materials
  27. if root_material_id not in self._favorite_ids:
  28. continue
  29. item = self._createMaterialItem(root_material_id, container_node)
  30. if item:
  31. item_list.append(item)
  32. # Sort the item list alphabetically by name
  33. item_list = sorted(item_list, key = lambda d: d["brand"].upper())
  34. self.setItems(item_list)