IntentCategoryModel.py 5.4 KB

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