IntentCategoryModel.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, QTimer
  4. import collections
  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 pyqtProperty, 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. ## Lists the intent categories that are available for the current printer
  17. # configuration.
  18. class IntentCategoryModel(ListModel):
  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 to user-visible string. Ordered by weight.
  26. # TODO: Create a solution for this name and weight to be used dynamically.
  27. _translations = collections.OrderedDict() # type: "collections.OrderedDict[str,Dict[str,Optional[str]]]"
  28. _translations["default"] = {
  29. "name": catalog.i18nc("@label", "Default")
  30. }
  31. _translations["visual"] = {
  32. "name": catalog.i18nc("@label", "Visual"),
  33. "description": catalog.i18nc("@text", "Optimized for appearance")
  34. }
  35. _translations["engineering"] = {
  36. "name": catalog.i18nc("@label", "Engineering"),
  37. "description": catalog.i18nc("@text", "Optimized for higher accuracy")
  38. }
  39. _translations["quick"] = {
  40. "name": catalog.i18nc("@label", "Draft"),
  41. "description": catalog.i18nc("@text", "Optimized for fast results")
  42. }
  43. ## Creates a new model for a certain intent category.
  44. # \param The category to list the intent profiles for.
  45. def __init__(self, intent_category: str) -> None:
  46. super().__init__()
  47. self._intent_category = intent_category
  48. self.addRoleName(self.NameRole, "name")
  49. self.addRoleName(self.IntentCategoryRole, "intent_category")
  50. self.addRoleName(self.WeightRole, "weight")
  51. self.addRoleName(self.QualitiesRole, "qualities")
  52. self.addRoleName(self.DescriptionRole, "description")
  53. application = cura.CuraApplication.CuraApplication.getInstance()
  54. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  55. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  56. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  57. machine_manager.activeMaterialChanged.connect(self.update)
  58. machine_manager.activeVariantChanged.connect(self.update)
  59. machine_manager.extruderChanged.connect(self.update)
  60. extruder_manager = application.getExtruderManager()
  61. extruder_manager.extrudersChanged.connect(self.update)
  62. self._update_timer = QTimer()
  63. self._update_timer.setInterval(500)
  64. self._update_timer.setSingleShot(True)
  65. self._update_timer.timeout.connect(self._update)
  66. self.update()
  67. ## Updates the list of intents if an intent profile was added or removed.
  68. def _onContainerChange(self, container: "ContainerInterface") -> None:
  69. if container.getMetaDataEntry("type") == "intent":
  70. self.update()
  71. def update(self):
  72. self._update_timer.start()
  73. ## Updates the list of intents.
  74. def _update(self) -> None:
  75. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  76. result = []
  77. for category in available_categories:
  78. qualities = IntentModel()
  79. qualities.setIntentCategory(category)
  80. result.append({
  81. "name": IntentCategoryModel.translation(category, "name", catalog.i18nc("@label", "Unknown")),
  82. "description": IntentCategoryModel.translation(category, "description", None),
  83. "intent_category": category,
  84. "weight": list(self._translations.keys()).index(category),
  85. "qualities": qualities
  86. })
  87. result.sort(key = lambda k: k["weight"])
  88. self.setItems(result)
  89. ## Get a display value for a category. See IntenCategoryModel._translations
  90. ## for categories and keys
  91. @staticmethod
  92. def translation(category: str, key: str, default: Optional[str] = None):
  93. display_strings = IntentCategoryModel._translations.get(category, {})
  94. return display_strings.get(key, default)