IntentCategoryModel.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #Copyright (c) 2019 Ultimaker B.V.
  2. #Cura is released under the terms of the LGPLv3 or higher.
  3. import collections
  4. from PyQt6.QtCore import Qt, QTimer
  5. from typing import TYPE_CHECKING, Optional, Dict
  6. from cura.Machines.Models.IntentModel import IntentModel
  7. from cura.Settings.IntentManager import IntentManager
  8. from UM.Qt.ListModel import ListModel
  9. from UM.Settings.ContainerRegistry import ContainerRegistry #To update the list if anything changes.
  10. from PyQt6.QtCore import pyqtSignal
  11. import cura.CuraApplication
  12. if TYPE_CHECKING:
  13. from UM.Settings.ContainerRegistry import ContainerInterface
  14. from UM.i18n import i18nCatalog
  15. catalog = i18nCatalog("cura")
  16. class IntentCategoryModel(ListModel):
  17. """Lists the intent categories that are available for the current printer configuration. """
  18. NameRole = Qt.ItemDataRole.UserRole + 1
  19. IntentCategoryRole = Qt.ItemDataRole.UserRole + 2
  20. WeightRole = Qt.ItemDataRole.UserRole + 3
  21. QualitiesRole = Qt.ItemDataRole.UserRole + 4
  22. DescriptionRole = Qt.ItemDataRole.UserRole + 5
  23. modelUpdated = pyqtSignal()
  24. _translations = collections.OrderedDict() # type: "collections.OrderedDict[str,Dict[str,Optional[str]]]"
  25. @classmethod
  26. def _get_translations(cls):
  27. """Translations to user-visible string. Ordered by weight.
  28. TODO: Create a solution for this name and weight to be used dynamically.
  29. """
  30. if len(cls._translations) == 0:
  31. cls._translations["default"] = {
  32. "name": catalog.i18nc("@label", "Default")
  33. }
  34. cls._translations["visual"] = {
  35. "name": catalog.i18nc("@label", "Visual"),
  36. "description": catalog.i18nc("@text", "The visual profile is designed to print visual prototypes and models with the intent of high visual and surface quality.")
  37. }
  38. cls._translations["engineering"] = {
  39. "name": catalog.i18nc("@label", "Engineering"),
  40. "description": catalog.i18nc("@text", "The engineering profile is designed to print functional prototypes and end-use parts with the intent of better accuracy and for closer tolerances.")
  41. }
  42. cls._translations["quick"] = {
  43. "name": catalog.i18nc("@label", "Draft"),
  44. "description": catalog.i18nc("@text", "The draft profile is designed to print initial prototypes and concept validation with the intent of significant print time reduction.")
  45. }
  46. cls._translations["annealing"] = {
  47. "name": catalog.i18nc("@label", "Annealing"),
  48. "description": catalog.i18nc("@text",
  49. "The annealing profile requires post-processing in an oven after the print is finished. This profile retains the dimensional accuracy of the printed part after annealing and improves strength, stiffness, and thermal resistance.")
  50. }
  51. return cls._translations
  52. def __init__(self, intent_category: str) -> None:
  53. """Creates a new model for a certain intent category.
  54. :param intent_category: category to list the intent profiles for.
  55. """
  56. super().__init__()
  57. self._intent_category = intent_category
  58. self.addRoleName(self.NameRole, "name")
  59. self.addRoleName(self.IntentCategoryRole, "intent_category")
  60. self.addRoleName(self.WeightRole, "weight")
  61. self.addRoleName(self.QualitiesRole, "qualities")
  62. self.addRoleName(self.DescriptionRole, "description")
  63. application = cura.CuraApplication.CuraApplication.getInstance()
  64. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  65. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  66. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  67. machine_manager.activeMaterialChanged.connect(self.update)
  68. machine_manager.activeVariantChanged.connect(self.update)
  69. machine_manager.extruderChanged.connect(self.update)
  70. extruder_manager = application.getExtruderManager()
  71. extruder_manager.extrudersChanged.connect(self.update)
  72. self._update_timer = QTimer()
  73. self._update_timer.setInterval(500)
  74. self._update_timer.setSingleShot(True)
  75. self._update_timer.timeout.connect(self._update)
  76. self.update()
  77. def _onContainerChange(self, container: "ContainerInterface") -> None:
  78. """Updates the list of intents if an intent profile was added or removed."""
  79. if container.getMetaDataEntry("type") == "intent":
  80. self.update()
  81. def update(self):
  82. self._update_timer.start()
  83. def _update(self) -> None:
  84. """Updates the list of intents."""
  85. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  86. result = []
  87. for category in available_categories:
  88. qualities = IntentModel()
  89. qualities.setIntentCategory(category)
  90. try:
  91. weight = list(IntentCategoryModel._get_translations().keys()).index(category)
  92. except ValueError:
  93. weight = 99
  94. result.append({
  95. "name": IntentCategoryModel.translation(category, "name", category.title()),
  96. "description": IntentCategoryModel.translation(category, "description", None),
  97. "intent_category": category,
  98. "weight": weight,
  99. "qualities": qualities
  100. })
  101. result.sort(key = lambda k: k["weight"])
  102. self.setItems(result)
  103. @staticmethod
  104. def translation(category: str, key: str, default: Optional[str] = None):
  105. """Get a display value for a category.for categories and keys"""
  106. display_strings = IntentCategoryModel._get_translations().get(category, {})
  107. return display_strings.get(key, default)