IntentCategoryModel.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #Copyright (c) 2019 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 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 PyQt5.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.UserRole + 1
  19. IntentCategoryRole = Qt.UserRole + 2
  20. WeightRole = Qt.UserRole + 3
  21. QualitiesRole = Qt.UserRole + 4
  22. DescriptionRole = Qt.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. return cls._translations
  47. def __init__(self, intent_category: str) -> None:
  48. """Creates a new model for a certain intent category.
  49. :param intent_category: category to list the intent profiles for.
  50. """
  51. super().__init__()
  52. self._intent_category = intent_category
  53. self.addRoleName(self.NameRole, "name")
  54. self.addRoleName(self.IntentCategoryRole, "intent_category")
  55. self.addRoleName(self.WeightRole, "weight")
  56. self.addRoleName(self.QualitiesRole, "qualities")
  57. self.addRoleName(self.DescriptionRole, "description")
  58. application = cura.CuraApplication.CuraApplication.getInstance()
  59. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  60. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  61. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  62. machine_manager.activeMaterialChanged.connect(self.update)
  63. machine_manager.activeVariantChanged.connect(self.update)
  64. machine_manager.extruderChanged.connect(self.update)
  65. extruder_manager = application.getExtruderManager()
  66. extruder_manager.extrudersChanged.connect(self.update)
  67. self._update_timer = QTimer()
  68. self._update_timer.setInterval(500)
  69. self._update_timer.setSingleShot(True)
  70. self._update_timer.timeout.connect(self._update)
  71. self.update()
  72. def _onContainerChange(self, container: "ContainerInterface") -> None:
  73. """Updates the list of intents if an intent profile was added or removed."""
  74. if container.getMetaDataEntry("type") == "intent":
  75. self.update()
  76. def update(self):
  77. self._update_timer.start()
  78. def _update(self) -> None:
  79. """Updates the list of intents."""
  80. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  81. result = []
  82. for category in available_categories:
  83. qualities = IntentModel()
  84. qualities.setIntentCategory(category)
  85. try:
  86. weight = list(IntentCategoryModel._get_translations().keys()).index(category)
  87. except ValueError:
  88. weight = 99
  89. result.append({
  90. "name": IntentCategoryModel.translation(category, "name", category),
  91. "description": IntentCategoryModel.translation(category, "description", None),
  92. "intent_category": category,
  93. "weight": weight,
  94. "qualities": qualities
  95. })
  96. result.sort(key = lambda k: k["weight"])
  97. self.setItems(result)
  98. @staticmethod
  99. def translation(category: str, key: str, default: Optional[str] = None):
  100. """Get a display value for a category.for categories and keys"""
  101. display_strings = IntentCategoryModel._get_translations().get(category, {})
  102. return display_strings.get(key, default)