UM2UpgradeSelection.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Uranium is released under the terms of the LGPLv3 or higher.
  3. from UM.Settings.ContainerRegistry import ContainerRegistry
  4. from cura.MachineAction import MachineAction
  5. from PyQt5.QtCore import pyqtSlot, pyqtSignal, pyqtProperty
  6. from UM.i18n import i18nCatalog
  7. from UM.Application import Application
  8. from UM.Util import parseBool
  9. catalog = i18nCatalog("cura")
  10. ## The Ultimaker 2 can have a few revisions & upgrades.
  11. class UM2UpgradeSelection(MachineAction):
  12. def __init__(self):
  13. super().__init__("UM2UpgradeSelection", catalog.i18nc("@action", "Select upgrades"))
  14. self._qml_url = "UM2UpgradeSelectionMachineAction.qml"
  15. self._container_registry = ContainerRegistry.getInstance()
  16. self._current_global_stack = None
  17. Application.getInstance().globalContainerStackChanged.connect(self._onGlobalStackChanged)
  18. self._reset()
  19. def _reset(self):
  20. self.hasVariantsChanged.emit()
  21. def _onGlobalStackChanged(self):
  22. if self._current_global_stack:
  23. self._current_global_stack.metaDataChanged.disconnect(self._onGlobalStackMetaDataChanged)
  24. self._current_global_stack = Application.getInstance().getGlobalContainerStack()
  25. if self._current_global_stack:
  26. self._current_global_stack.metaDataChanged.connect(self._onGlobalStackMetaDataChanged)
  27. self._reset()
  28. def _onGlobalStackMetaDataChanged(self):
  29. self._reset()
  30. hasVariantsChanged = pyqtSignal()
  31. def setHasVariants(self, has_variants = True):
  32. global_container_stack = Application.getInstance().getGlobalContainerStack()
  33. if global_container_stack:
  34. variant_container = global_container_stack.extruders["0"].variant
  35. if has_variants:
  36. if "has_variants" in global_container_stack.getMetaData():
  37. global_container_stack.setMetaDataEntry("has_variants", True)
  38. else:
  39. global_container_stack.addMetaDataEntry("has_variants", True)
  40. # Set the variant container to a sane default
  41. empty_container = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  42. if type(variant_container) == type(empty_container):
  43. search_criteria = { "type": "variant", "definition": "ultimaker2", "id": "*0.4*" }
  44. containers = self._container_registry.findInstanceContainers(**search_criteria)
  45. if containers:
  46. global_container_stack.extruders["0"].variant = containers[0]
  47. else:
  48. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  49. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  50. if "has_variants" in global_container_stack.getMetaData():
  51. global_container_stack.removeMetaDataEntry("has_variants")
  52. # Set the variant container to an empty variant
  53. global_container_stack.extruders["0"].variant = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  54. Application.getInstance().globalContainerStackChanged.emit()
  55. self._reset()
  56. @pyqtProperty(bool, fset = setHasVariants, notify = hasVariantsChanged)
  57. def hasVariants(self):
  58. if self._current_global_stack:
  59. return parseBool(self._current_global_stack.getMetaDataEntry("has_variants", "false"))