UserProfilesModel.py 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from UM.Application import Application
  4. from UM.Settings.ContainerRegistry import ContainerRegistry
  5. from cura.QualityManager import QualityManager
  6. from cura.Settings.ProfilesModel import ProfilesModel
  7. from cura.Settings.ExtruderManager import ExtruderManager
  8. ## QML Model for listing the current list of valid quality changes profiles.
  9. #
  10. class UserProfilesModel(ProfilesModel):
  11. def __init__(self, parent = None):
  12. super().__init__(parent)
  13. #Need to connect to the metaDataChanged signal of the active materials.
  14. self.__current_extruders = []
  15. self.__current_materials = []
  16. Application.getInstance().getExtruderManager().extrudersChanged.connect(self.__onExtrudersChanged)
  17. self.__onExtrudersChanged()
  18. self.__current_materials = [extruder.material for extruder in self.__current_extruders]
  19. for material in self.__current_materials:
  20. material.metaDataChanged.connect(self._onContainerChanged)
  21. self._empty_quality = ContainerRegistry.getInstance().findContainers(id = "empty_quality")[0]
  22. ## Fetch the list of containers to display.
  23. #
  24. # See UM.Settings.Models.InstanceContainersModel._fetchInstanceContainers().
  25. def _fetchInstanceContainers(self):
  26. global_container_stack = Application.getInstance().getGlobalContainerStack()
  27. if not global_container_stack:
  28. return {}, {}
  29. # Fetch the list of quality changes.
  30. quality_manager = QualityManager.getInstance()
  31. machine_definition = quality_manager.getParentMachineDefinition(global_container_stack.definition)
  32. quality_changes_list = quality_manager.findAllQualityChangesForMachine(machine_definition)
  33. extruder_manager = ExtruderManager.getInstance()
  34. active_extruder = extruder_manager.getActiveExtruderStack()
  35. extruder_stacks = self._getOrderedExtruderStacksList()
  36. # Fetch the list of usable qualities across all extruders.
  37. # The actual list of quality profiles come from the first extruder in the extruder list.
  38. quality_list = quality_manager.findAllUsableQualitiesForMachineAndExtruders(global_container_stack, extruder_stacks)
  39. # Filter the quality_change by the list of available quality_types
  40. quality_type_set = set([x.getMetaDataEntry("quality_type") for x in quality_list])
  41. quality_type_set.add(self._empty_quality.getMetaDataEntry("quality_type"))
  42. filtered_quality_changes = {qc.getId():qc for qc in quality_changes_list if
  43. qc.getMetaDataEntry("quality_type") in quality_type_set and
  44. qc.getMetaDataEntry("extruder") is not None and
  45. (qc.getMetaDataEntry("extruder") == active_extruder.definition.getMetaDataEntry("quality_definition") or
  46. qc.getMetaDataEntry("extruder") == active_extruder.definition.getId())}
  47. return filtered_quality_changes, {} #Only return true profiles for now, no metadata. The quality manager is not able to get only metadata yet.
  48. ## Called when a container changed on an extruder stack.
  49. #
  50. # If it's the material we need to connect to the metaDataChanged signal of
  51. # that.
  52. def __onContainerChanged(self, new_container):
  53. #Careful not to update when a quality or quality changes profile changed!
  54. #If you then update you're going to have an infinite recursion because the update may change the container.
  55. if new_container.getMetaDataEntry("type") == "material":
  56. for material in self.__current_materials:
  57. material.metaDataChanged.disconnect(self._onContainerChanged)
  58. self.__current_materials = [extruder.material for extruder in self.__current_extruders]
  59. for material in self.__current_materials:
  60. material.metaDataChanged.connect(self._onContainerChanged)
  61. ## Called when the current set of extruders change.
  62. #
  63. # This makes sure that we are listening to the signal for when the
  64. # materials change.
  65. def __onExtrudersChanged(self):
  66. for extruder in self.__current_extruders:
  67. extruder.containersChanged.disconnect(self.__onContainerChanged)
  68. self.__current_extruders = Application.getInstance().getExtruderManager().getExtruderStacks()
  69. for extruder in self.__current_extruders:
  70. extruder.containersChanged.connect(self.__onContainerChanged)