IntentCategoryModel.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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
  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()
  59. ## Updates the list of intents if an intent profile was added or removed.
  60. def _onContainerChange(self, container: "ContainerInterface") -> None:
  61. if container.getMetaDataEntry("type") == "intent":
  62. self.update()
  63. ## Updates the list of intents.
  64. def update(self) -> None:
  65. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  66. result = []
  67. for category in available_categories:
  68. qualities = IntentModel()
  69. qualities.setIntentCategory(category)
  70. result.append({
  71. "name": IntentCategoryModel.translation(category, "name", catalog.i18nc("@label", "Unknown")),
  72. "description": IntentCategoryModel.translation(category, "description", None),
  73. "intent_category": category,
  74. "weight": list(self._translations.keys()).index(category),
  75. "qualities": qualities
  76. })
  77. result.sort(key = lambda k: k["weight"])
  78. self.setItems(result)
  79. ## Get a display value for a category. See IntenCategoryModel._translations
  80. ## for categories and keys
  81. @staticmethod
  82. def translation(category: str, key: str, default: Optional[str] = None):
  83. display_strings = IntentCategoryModel._translations.get(category, {})
  84. return display_strings.get(key, default)