IntentCategoryModel.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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["engineering"] = {
  32. "name": catalog.i18nc("@label", "Engineering"),
  33. "description": catalog.i18nc("@text", "Suitable for engineering work")
  34. }
  35. _translations["smooth"] = {
  36. "name": catalog.i18nc("@label", "Smooth"),
  37. "description": catalog.i18nc("@text", "Optimized for a smooth surfaces")
  38. }
  39. ## Creates a new model for a certain intent category.
  40. # \param The category to list the intent profiles for.
  41. def __init__(self, intent_category: str) -> None:
  42. super().__init__()
  43. self._intent_category = intent_category
  44. self.addRoleName(self.NameRole, "name")
  45. self.addRoleName(self.IntentCategoryRole, "intent_category")
  46. self.addRoleName(self.WeightRole, "weight")
  47. self.addRoleName(self.QualitiesRole, "qualities")
  48. self.addRoleName(self.DescriptionRole, "description")
  49. application = cura.CuraApplication.CuraApplication.getInstance()
  50. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  51. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  52. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  53. machine_manager.activeMaterialChanged.connect(self.update)
  54. machine_manager.activeVariantChanged.connect(self.update)
  55. machine_manager.extruderChanged.connect(self.update)
  56. extruder_manager = application.getExtruderManager()
  57. extruder_manager.extrudersChanged.connect(self.update)
  58. self._update_timer = QTimer()
  59. self._update_timer.setInterval(500)
  60. self._update_timer.setSingleShot(True)
  61. self._update_timer.timeout.connect(self._update)
  62. self.update()
  63. ## Updates the list of intents if an intent profile was added or removed.
  64. def _onContainerChange(self, container: "ContainerInterface") -> None:
  65. if container.getMetaDataEntry("type") == "intent":
  66. self.update()
  67. def update(self):
  68. self._update_timer.start()
  69. ## Updates the list of intents.
  70. def _update(self) -> None:
  71. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  72. result = []
  73. for category in available_categories:
  74. qualities = IntentModel()
  75. qualities.setIntentCategory(category)
  76. result.append({
  77. "name": IntentCategoryModel.translation(category, "name", catalog.i18nc("@label", "Unknown")),
  78. "description": IntentCategoryModel.translation(category, "description", None),
  79. "intent_category": category,
  80. "weight": list(self._translations.keys()).index(category),
  81. "qualities": qualities
  82. })
  83. result.sort(key = lambda k: k["weight"])
  84. self.setItems(result)
  85. ## Get a display value for a category. See IntenCategoryModel._translations
  86. ## for categories and keys
  87. @staticmethod
  88. def translation(category: str, key: str, default: Optional[str] = None):
  89. display_strings = IntentCategoryModel._translations.get(category, {})
  90. return display_strings.get(key, default)