IntentCategoryModel.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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", "Balanced"),
  33. "description": catalog.i18nc("@text",
  34. "The balanced profile is designed to strike a balance between productivity, surface quality, mechanical properties and dimensional accuracy.")
  35. }
  36. cls._translations["visual"] = {
  37. "name": catalog.i18nc("@label", "Visual"),
  38. "description": catalog.i18nc("@text", "The visual profile is designed to print visual prototypes and models with the intent of high visual and surface quality.")
  39. }
  40. cls._translations["engineering"] = {
  41. "name": catalog.i18nc("@label", "Engineering"),
  42. "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.")
  43. }
  44. cls._translations["quick"] = {
  45. "name": catalog.i18nc("@label", "Draft"),
  46. "description": catalog.i18nc("@text", "The draft profile is designed to print initial prototypes and concept validation with the intent of significant print time reduction.")
  47. }
  48. cls._translations["annealing"] = {
  49. "name": catalog.i18nc("@label", "Annealing"),
  50. "description": catalog.i18nc("@text",
  51. "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.")
  52. }
  53. cls._translations["solid"] = {
  54. "name": catalog.i18nc("@label", "Solid"),
  55. "description": catalog.i18nc("@text",
  56. "A highly dense and strong part but at a slower print time. Great for functional parts.")
  57. }
  58. return cls._translations
  59. def __init__(self, intent_category: str) -> None:
  60. """Creates a new model for a certain intent category.
  61. :param intent_category: category to list the intent profiles for.
  62. """
  63. super().__init__()
  64. self._intent_category = intent_category
  65. self.addRoleName(self.NameRole, "name")
  66. self.addRoleName(self.IntentCategoryRole, "intent_category")
  67. self.addRoleName(self.WeightRole, "weight")
  68. self.addRoleName(self.QualitiesRole, "qualities")
  69. self.addRoleName(self.DescriptionRole, "description")
  70. application = cura.CuraApplication.CuraApplication.getInstance()
  71. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  72. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  73. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  74. machine_manager.activeMaterialChanged.connect(self.update)
  75. machine_manager.activeVariantChanged.connect(self.update)
  76. machine_manager.extruderChanged.connect(self.update)
  77. extruder_manager = application.getExtruderManager()
  78. extruder_manager.extrudersChanged.connect(self.update)
  79. self._update_timer = QTimer()
  80. self._update_timer.setInterval(500)
  81. self._update_timer.setSingleShot(True)
  82. self._update_timer.timeout.connect(self._update)
  83. self.update()
  84. def _onContainerChange(self, container: "ContainerInterface") -> None:
  85. """Updates the list of intents if an intent profile was added or removed."""
  86. if container.getMetaDataEntry("type") == "intent":
  87. self.update()
  88. def update(self):
  89. self._update_timer.start()
  90. def _update(self) -> None:
  91. """Updates the list of intents."""
  92. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  93. result = []
  94. for category in available_categories:
  95. qualities = IntentModel()
  96. qualities.setIntentCategory(category)
  97. try:
  98. weight = list(IntentCategoryModel._get_translations().keys()).index(category)
  99. except ValueError:
  100. weight = 99
  101. result.append({
  102. "name": IntentCategoryModel.translation(category, "name", category.title()),
  103. "description": IntentCategoryModel.translation(category, "description", None),
  104. "intent_category": category,
  105. "weight": weight,
  106. "qualities": qualities
  107. })
  108. result.sort(key = lambda k: k["weight"])
  109. self.setItems(result)
  110. @staticmethod
  111. def translation(category: str, key: str, default: Optional[str] = None):
  112. """Get a display value for a category.for categories and keys"""
  113. display_strings = IntentCategoryModel._get_translations().get(category, {})
  114. return display_strings.get(key, default)