IntentCategoryModel.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #Copyright (c) 2019 Ultimaker B.V.
  2. #Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import Qt
  4. import collections
  5. from cura.Settings.IntentManager import IntentManager
  6. from UM.Qt.ListModel import ListModel
  7. from UM.Settings.ContainerRegistry import ContainerRegistry #To update the list if anything changes.
  8. from UM.i18n import i18nCatalog
  9. catalog = i18nCatalog("cura")
  10. ## Lists the intent categories that are available for the current printer
  11. # configuration.
  12. class IntentCategoryModel(ListModel):
  13. NameRole = Qt.UserRole + 1
  14. IntentCategoryRole = Qt.UserRole + 2
  15. WeightRole = Qt.UserRole + 3
  16. #Translations to user-visible string. Ordered by weight.
  17. #TODO: Create a solution for this name and weight to be used dynamically.
  18. name_translation = collections.OrderedDict() #type: "collections.OrderedDict[str,str]"
  19. name_translation["default"] = catalog.i18nc("@label", "Default")
  20. name_translation["engineering"] = catalog.i18nc("@label", "Engineering")
  21. name_translation["smooth"] = catalog.i18nc("@label", "Smooth")
  22. ## Creates a new model for a certain intent category.
  23. # \param The category to list the intent profiles for.
  24. def __init__(self, intent_category: str) -> None:
  25. super().__init__()
  26. self._intent_category = intent_category
  27. self.addRoleName(self.NameRole, "name")
  28. self.addRoleName(self.IntentCategoryRole, "intent_category")
  29. self.addRoleName(self.WeightRole, "weight")
  30. ContainerRegistry.getInstance().containerAdded.connect(self.update)
  31. ContainerRegistry.getInstance().containerRemoved.connect(self.update)
  32. IntentManager.getInstance().configurationChanged.connect(self.update)
  33. self.update()
  34. ## Updates the list of intents.
  35. def update(self) -> None:
  36. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  37. result = []
  38. for category in available_categories:
  39. result.append({
  40. "name": self.name_translation.get(category, catalog.i18nc("@label", "Unknown")),
  41. "intent_category": category,
  42. "weight": list(self.name_translation.items()).index(category)
  43. })
  44. super().update(result)