UM2UpgradeSelection.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. global_container_stack.setMetaDataEntry("has_variants", True)
  37. # Set the variant container to a sane default
  38. empty_container = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  39. if type(variant_container) == type(empty_container):
  40. search_criteria = { "type": "variant", "definition": "ultimaker2", "id": "*0.4*" }
  41. containers = self._container_registry.findInstanceContainers(**search_criteria)
  42. if containers:
  43. global_container_stack.extruders["0"].variant = containers[0]
  44. else:
  45. # The metadata entry is stored in an ini, and ini files are parsed as strings only.
  46. # Because any non-empty string evaluates to a boolean True, we have to remove the entry to make it False.
  47. if "has_variants" in global_container_stack.getMetaData():
  48. global_container_stack.removeMetaDataEntry("has_variants")
  49. # Set the variant container to an empty variant
  50. global_container_stack.extruders["0"].variant = ContainerRegistry.getInstance().getEmptyInstanceContainer()
  51. Application.getInstance().globalContainerStackChanged.emit()
  52. self._reset()
  53. @pyqtProperty(bool, fset = setHasVariants, notify = hasVariantsChanged)
  54. def hasVariants(self):
  55. if self._current_global_stack:
  56. return parseBool(self._current_global_stack.getMetaDataEntry("has_variants", "false"))