FavoriteMaterialsModel.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from cura.Machines.Models.BaseMaterialsModel import BaseMaterialsModel
  5. import cura.CuraApplication # To listen to changes to the preferences.
  6. class FavoriteMaterialsModel(BaseMaterialsModel):
  7. """Model that shows the list of favorite materials."""
  8. def __init__(self, parent: Optional["QObject"] = None) -> None:
  9. super(FavoriteMaterialsModel, self).__init__(parent = parent)
  10. cura.CuraApplication.CuraApplication.getInstance().getPreferences().preferenceChanged.connect(self._onFavoritesChanged)
  11. self._onChanged()
  12. def _onFavoritesChanged(self, preference_key: str) -> None:
  13. """Triggered when any preference changes, but only handles it when the list of favourites is changed. """
  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)