IntentCategoryModel.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 typing import TYPE_CHECKING
  6. from cura.Settings.IntentManager import IntentManager
  7. from UM.Qt.ListModel import ListModel
  8. from UM.Settings.ContainerRegistry import ContainerRegistry #To update the list if anything changes.
  9. if TYPE_CHECKING:
  10. from UM.Settings.ContainerRegistry import ContainerInterface
  11. from UM.i18n import i18nCatalog
  12. catalog = i18nCatalog("cura")
  13. ## Lists the intent categories that are available for the current printer
  14. # configuration.
  15. class IntentCategoryModel(ListModel):
  16. NameRole = Qt.UserRole + 1
  17. IntentCategoryRole = Qt.UserRole + 2
  18. WeightRole = Qt.UserRole + 3
  19. #Translations to user-visible string. Ordered by weight.
  20. #TODO: Create a solution for this name and weight to be used dynamically.
  21. name_translation = collections.OrderedDict() #type: "collections.OrderedDict[str,str]"
  22. name_translation["default"] = catalog.i18nc("@label", "Default")
  23. name_translation["engineering"] = catalog.i18nc("@label", "Engineering")
  24. name_translation["smooth"] = catalog.i18nc("@label", "Smooth")
  25. ## Creates a new model for a certain intent category.
  26. # \param The category to list the intent profiles for.
  27. def __init__(self, intent_category: str) -> None:
  28. super().__init__()
  29. self._intent_category = intent_category
  30. self.addRoleName(self.NameRole, "name")
  31. self.addRoleName(self.IntentCategoryRole, "intent_category")
  32. self.addRoleName(self.WeightRole, "weight")
  33. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  34. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  35. IntentManager.getInstance().configurationChanged.connect(self.update)
  36. self.update()
  37. ## Updates the list of intents if an intent profile was added or removed.
  38. def _onContainerChange(self, container: "ContainerInterface") -> None:
  39. if container.getMetaDataEntry("type") == "intent":
  40. self.update()
  41. ## Updates the list of intents.
  42. def update(self) -> None:
  43. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  44. result = []
  45. for category in available_categories:
  46. result.append({
  47. "name": self.name_translation.get(category, catalog.i18nc("@label", "Unknown")),
  48. "intent_category": category,
  49. "weight": list(self.name_translation.keys()).index(category)
  50. })
  51. super().update(result)