MaterialManager.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QObject, pyqtSlot #To expose data to QML.
  4. from cura.Settings.ContainerManager import ContainerManager
  5. from UM.Logger import Logger
  6. from UM.Message import Message #To create a warning message about material diameter.
  7. from UM.i18n import i18nCatalog #Translated strings.
  8. catalog = i18nCatalog("cura")
  9. ## Handles material-related data, processing requests to change them and
  10. # providing data for the GUI.
  11. #
  12. # TODO: Move material-related managing over from the machine manager to here.
  13. class MaterialManager(QObject):
  14. ## Creates the global values for the material manager to use.
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. #Material diameter changed warning message.
  18. self._material_diameter_warning_message = Message(catalog.i18nc("@info:status Has a cancel button next to it.",
  19. "The selected material diameter causes the material to become incompatible with the current printer."), title = catalog.i18nc("@info:title", "Incompatible Material"))
  20. self._material_diameter_warning_message.addAction("Undo", catalog.i18nc("@action:button", "Undo"), None, catalog.i18nc("@action", "Undo changing the material diameter."))
  21. self._material_diameter_warning_message.actionTriggered.connect(self._materialWarningMessageAction)
  22. ## Creates an instance of the MaterialManager.
  23. #
  24. # This should only be called by PyQt to create the singleton instance of
  25. # this class.
  26. @staticmethod
  27. def createMaterialManager(engine = None, script_engine = None):
  28. return MaterialManager()
  29. @pyqtSlot(str, str)
  30. def showMaterialWarningMessage(self, material_id, previous_diameter):
  31. self._material_diameter_warning_message.previous_diameter = previous_diameter #Make sure that the undo button can properly undo the action.
  32. self._material_diameter_warning_message.material_id = material_id
  33. self._material_diameter_warning_message.show()
  34. ## Called when clicking "undo" on the warning dialogue for disappeared
  35. # materials.
  36. #
  37. # This executes the undo action, restoring the material diameter.
  38. #
  39. # \param button The identifier of the button that was pressed.
  40. def _materialWarningMessageAction(self, message, button):
  41. if button == "Undo":
  42. container_manager = ContainerManager.getInstance()
  43. container_manager.setContainerMetaDataEntry(self._material_diameter_warning_message.material_id, "properties/diameter", self._material_diameter_warning_message.previous_diameter)
  44. approximate_previous_diameter = str(round(float(self._material_diameter_warning_message.previous_diameter)))
  45. container_manager.setContainerMetaDataEntry(self._material_diameter_warning_message.material_id, "approximate_diameter", approximate_previous_diameter)
  46. container_manager.setContainerProperty(self._material_diameter_warning_message.material_id, "material_diameter", "value", self._material_diameter_warning_message.previous_diameter);
  47. message.hide()
  48. else:
  49. Logger.log("w", "Unknown button action for material diameter warning message: {action}".format(action = button))