IntentModel.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from PyQt5.QtCore import QObject
  5. from UM.Qt.ListModel import ListModel
  6. from PyQt5.QtCore import Qt, pyqtProperty, pyqtSignal
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from cura.Settings.IntentManager import IntentManager
  9. import cura.CuraApplication
  10. class IntentModel(ListModel):
  11. NameRole = Qt.UserRole + 1
  12. QualityTypeRole = Qt.UserRole + 2
  13. def __init__(self, parent: Optional[QObject] = None) -> None:
  14. super().__init__(parent)
  15. self.addRoleName(self.NameRole, "name")
  16. self.addRoleName(self.QualityTypeRole, "quality_type")
  17. self._intent_category = "engineering"
  18. ContainerRegistry.getInstance().containerAdded.connect(self._onChanged)
  19. ContainerRegistry.getInstance().containerRemoved.connect(self._onChanged)
  20. self._update()
  21. intentCategoryChanged = pyqtSignal()
  22. def setIntentCategory(self, new_category: str) -> None:
  23. if self._intent_category != new_category:
  24. self._intent_category = new_category
  25. self.intentCategoryChanged.emit()
  26. self._update()
  27. @pyqtProperty(str, fset = setIntentCategory, notify = intentCategoryChanged)
  28. def intentCategory(self) -> str:
  29. return self._intent_category
  30. def _onChanged(self, container):
  31. if container.getMetaDataEntry("type") == "intent":
  32. self._update()
  33. def _update(self) -> None:
  34. new_items = []
  35. application = cura.CuraApplication.CuraApplication.getInstance()
  36. quality_manager = application.getQualityManager()
  37. global_stack = application.getGlobalContainerStack()
  38. if not global_stack:
  39. self.setItems(new_items)
  40. return
  41. quality_groups = quality_manager.getQualityGroups(global_stack)
  42. for intent_category, quality_type in IntentManager.getInstance().getCurrentAvailableIntents():
  43. if intent_category == self._intent_category:
  44. new_items.append({"name": quality_groups[quality_type].name, "quality_type": quality_type})
  45. if self._intent_category == "default": #For Default we always list all quality types. We can't filter on available profiles since the empty intent is not a specific quality type.
  46. for quality_type in quality_groups.keys():
  47. new_items.append({"name": quality_groups[quality_type].name, "quality_type": quality_type})
  48. self.setItems(new_items)