IntentCategoryModel.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. from typing import TYPE_CHECKING, Optional, Dict
  5. from cura.Machines.Models.IntentTranslations import intent_translations
  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. ## Creates a new model for a certain intent category.
  26. # \param The category to list the intent profiles for.
  27. def __init__(self, intent_category: str) -> None:
  28. super().__init__()
  29. self._intent_category = intent_category
  30. self.addRoleName(self.NameRole, "name")
  31. self.addRoleName(self.IntentCategoryRole, "intent_category")
  32. self.addRoleName(self.WeightRole, "weight")
  33. self.addRoleName(self.QualitiesRole, "qualities")
  34. self.addRoleName(self.DescriptionRole, "description")
  35. application = cura.CuraApplication.CuraApplication.getInstance()
  36. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  37. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  38. machine_manager = cura.CuraApplication.CuraApplication.getInstance().getMachineManager()
  39. machine_manager.activeMaterialChanged.connect(self.update)
  40. machine_manager.activeVariantChanged.connect(self.update)
  41. machine_manager.extruderChanged.connect(self.update)
  42. extruder_manager = application.getExtruderManager()
  43. extruder_manager.extrudersChanged.connect(self.update)
  44. self._update_timer = QTimer()
  45. self._update_timer.setInterval(500)
  46. self._update_timer.setSingleShot(True)
  47. self._update_timer.timeout.connect(self._update)
  48. self.update()
  49. ## Updates the list of intents if an intent profile was added or removed.
  50. def _onContainerChange(self, container: "ContainerInterface") -> None:
  51. if container.getMetaDataEntry("type") == "intent":
  52. self.update()
  53. def update(self):
  54. self._update_timer.start()
  55. ## Updates the list of intents.
  56. def _update(self) -> None:
  57. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  58. result = []
  59. for category in available_categories:
  60. qualities = IntentModel()
  61. qualities.setIntentCategory(category)
  62. result.append({
  63. "name": IntentCategoryModel.translation(category, "name", catalog.i18nc("@label", "Unknown")),
  64. "description": IntentCategoryModel.translation(category, "description", None),
  65. "intent_category": category,
  66. "weight": list(intent_translations).index(category),
  67. "qualities": qualities
  68. })
  69. result.sort(key = lambda k: k["weight"])
  70. self.setItems(result)
  71. ## Get a display value for a category.
  72. ## for categories and keys
  73. @staticmethod
  74. def translation(category: str, key: str, default: Optional[str] = None):
  75. display_strings = intent_translations.get(category, {})
  76. return display_strings.get(key, default)