MaterialManagementModel.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, pyqtSlot # To allow the preference page proxy to be used from the actual preferences page.
  4. from typing import TYPE_CHECKING
  5. from UM.Logger import Logger
  6. from cura.Settings.CuraContainerRegistry import CuraContainerRegistry # To find the sets of materials belonging to each other, and currently loaded extruder stacks.
  7. if TYPE_CHECKING:
  8. from cura.Machines.MaterialNode import MaterialNode
  9. ## Proxy class to the materials page in the preferences.
  10. #
  11. # This class handles the actions in that page, such as creating new materials,
  12. # renaming them, etc.
  13. class MaterialManagementModel(QObject):
  14. ## Can a certain material be deleted, or is it still in use in one of the
  15. # container stacks anywhere?
  16. #
  17. # We forbid the user from deleting a material if it's in use in any stack.
  18. # Deleting it while it's in use can lead to corrupted stacks. In the
  19. # future we might enable this functionality again (deleting the material
  20. # from those stacks) but for now it is easier to prevent the user from
  21. # doing this.
  22. # \param material_node The ContainerTree node of the material to check.
  23. # \return Whether or not the material can be removed.
  24. @pyqtSlot("QVariant", result = bool)
  25. def canMaterialBeRemoved(self, material_node: "MaterialNode"):
  26. container_registry = CuraContainerRegistry.getInstance()
  27. ids_to_remove = {metadata.get("id", "") for metadata in container_registry.findInstanceContainersMetadata(base_file = material_node.base_file)}
  28. for extruder_stack in container_registry.findContainerStacks(type = "extruder_train"):
  29. if extruder_stack.material.getId() in ids_to_remove:
  30. return False
  31. return True
  32. ## Change the user-visible name of a material.
  33. # \param material_node The ContainerTree node of the material to rename.
  34. # \param name The new name for the material.
  35. @pyqtSlot("QVariant", str)
  36. def setMaterialName(self, material_node: "MaterialNode", name: str) -> None:
  37. container_registry = CuraContainerRegistry.getInstance()
  38. root_material_id = material_node.base_file
  39. if container_registry.isReadOnly(root_material_id):
  40. Logger.log("w", "Cannot set name of read-only container %s.", root_material_id)
  41. return
  42. return container_registry.findContainers(id = root_material_id)[0].setName(name)
  43. ## Deletes a material from Cura.
  44. #
  45. # This function does not do any safety checking any more. Please call this
  46. # function only if:
  47. # - The material is not read-only.
  48. # - The material is not used in any stacks.
  49. # If the material was not lazy-loaded yet, this will fully load the
  50. # container. When removing this material node, all other materials with
  51. # the same base fill will also be removed.
  52. # \param material_node The material to remove.
  53. @pyqtSlot("QVariant")
  54. def removeMaterial(self, material_node: "MaterialNode") -> None:
  55. container_registry = CuraContainerRegistry.getInstance()
  56. materials_this_base_file = container_registry.findContainersMetadata(base_file = material_node.base_file)
  57. for material_metadata in materials_this_base_file:
  58. container_registry.removeContainer(material_metadata["id"])