IntentModel.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. _intent_category_changed = pyqtSignal()
  22. def setIntentCategory(self, new_category: str) -> None:
  23. if self._intent_category != new_category:
  24. self._intent_category = new_category
  25. self._intent_category_changed.emit()
  26. self._update()
  27. @pyqtProperty(str, fset = setIntentCategory, notify = _intent_category_changed)
  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. for intent_category, quality_type in IntentManager.getInstance().currentAvailableIntents():
  39. if intent_category == self._intent_category:
  40. new_items.append({"name": quality_manager.getQualityGroups(global_stack)[quality_type].name, "quality_type": quality_type})
  41. self.setItems(new_items)