IntentCategoryModel.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  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. #Translations to user-visible string. Ordered by weight.
  24. #TODO: Create a solution for this name and weight to be used dynamically.
  25. name_translation = collections.OrderedDict() #type: "collections.OrderedDict[str,str]"
  26. name_translation["default"] = catalog.i18nc("@label", "Default")
  27. name_translation["engineering"] = catalog.i18nc("@label", "Engineering")
  28. name_translation["smooth"] = catalog.i18nc("@label", "Smooth")
  29. modelUpdated = pyqtSignal()
  30. ## Creates a new model for a certain intent category.
  31. # \param The category to list the intent profiles for.
  32. def __init__(self, intent_category: str) -> None:
  33. super().__init__()
  34. self._intent_category = intent_category
  35. self.addRoleName(self.NameRole, "name")
  36. self.addRoleName(self.IntentCategoryRole, "intent_category")
  37. self.addRoleName(self.WeightRole, "weight")
  38. self.addRoleName(self.QualitiesRole, "qualities")
  39. ContainerRegistry.getInstance().containerAdded.connect(self._onContainerChange)
  40. ContainerRegistry.getInstance().containerRemoved.connect(self._onContainerChange)
  41. cura.CuraApplication.CuraApplication.getInstance().getMachineManager().activeStackChanged.connect(self.update)
  42. self.update()
  43. ## Updates the list of intents if an intent profile was added or removed.
  44. def _onContainerChange(self, container: "ContainerInterface") -> None:
  45. if container.getMetaDataEntry("type") == "intent":
  46. self.update()
  47. ## Updates the list of intents.
  48. def update(self) -> None:
  49. available_categories = IntentManager.getInstance().currentAvailableIntentCategories()
  50. result = []
  51. for category in available_categories:
  52. qualities = IntentModel()
  53. qualities.setIntentCategory(category)
  54. result.append({
  55. "name": self.name_translation.get(category, catalog.i18nc("@label", "Unknown")),
  56. "intent_category": category,
  57. "weight": list(self.name_translation.keys()).index(category),
  58. "qualities": qualities
  59. })
  60. result.sort(key = lambda k: k["weight"])
  61. self.setItems(result)