PerObjectSettingVisibilityHandler.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (c) 2020 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import pyqtProperty
  4. from UM.FlameProfiler import pyqtSlot
  5. from UM.Application import Application
  6. from UM.PluginRegistry import PluginRegistry
  7. from UM.Settings.ContainerRegistry import ContainerRegistry
  8. from UM.Settings.SettingInstance import SettingInstance
  9. from UM.Logger import Logger
  10. import UM.Settings.Models.SettingVisibilityHandler
  11. from cura.Settings.ExtruderManager import ExtruderManager #To get global-inherits-stack setting values from different extruders.
  12. from cura.Settings.SettingOverrideDecorator import SettingOverrideDecorator
  13. class PerObjectSettingVisibilityHandler(UM.Settings.Models.SettingVisibilityHandler.SettingVisibilityHandler):
  14. """The per object setting visibility handler ensures that only setting
  15. definitions that have a matching instance Container are returned as visible.
  16. """
  17. def __init__(self, parent = None, *args, **kwargs):
  18. super().__init__(parent = parent, *args, **kwargs)
  19. self._selected_object_id = None
  20. self._node = None
  21. self._stack = None
  22. PluginRegistry.getInstance().getPluginObject("PerObjectSettingsTool").visibility_handler = self
  23. # this is a set of settings that will be skipped if the user chooses to reset.
  24. self._skip_reset_setting_set = set()
  25. def setSelectedObjectId(self, id):
  26. if id != self._selected_object_id:
  27. self._selected_object_id = id
  28. self._node = Application.getInstance().getController().getScene().findObject(self._selected_object_id)
  29. if self._node:
  30. self._stack = self._node.callDecoration("getStack")
  31. self.visibilityChanged.emit()
  32. @pyqtProperty("quint64", fset = setSelectedObjectId)
  33. def selectedObjectId(self):
  34. return self._selected_object_id
  35. @pyqtSlot(str)
  36. def addSkipResetSetting(self, setting_name):
  37. self._skip_reset_setting_set.add(setting_name)
  38. def setVisible(self, visible):
  39. if not self._node:
  40. return
  41. if not self._stack:
  42. self._node.addDecorator(SettingOverrideDecorator())
  43. self._stack = self._node.callDecoration("getStack")
  44. settings = self._stack.getTop()
  45. all_instances = settings.findInstances()
  46. visibility_changed = False # Flag to check if at the end the signal needs to be emitted
  47. # Remove all instances that are not in visibility list
  48. for instance in all_instances:
  49. # exceptionally skip setting
  50. if instance.definition.key in self._skip_reset_setting_set:
  51. continue
  52. if instance.definition.key not in visible:
  53. settings.removeInstance(instance.definition.key)
  54. visibility_changed = True
  55. # Add all instances that are not added, but are in visibility list
  56. for item in visible:
  57. if settings.getInstance(item) is not None: # Setting was added already.
  58. continue
  59. definition = self._stack.getSettingDefinition(item)
  60. if not definition:
  61. Logger.log("w", f"Unable to add instance ({item}) to per-object visibility because we couldn't find the matching definition.")
  62. continue
  63. new_instance = SettingInstance(definition, settings)
  64. stack_nr = -1
  65. stack = None
  66. # Check from what stack we should copy the raw property of the setting from.
  67. if self._stack.getProperty("machine_extruder_count", "value") > 1:
  68. if definition.limit_to_extruder != "-1":
  69. # A limit to extruder function was set and it's a multi extrusion machine. Check what stack we do need to use.
  70. stack_nr = str(int(round(float(self._stack.getProperty(item, "limit_to_extruder")))))
  71. # Check if the found stack_number is in the extruder list of extruders.
  72. if stack_nr not in ExtruderManager.getInstance().extruderIds and self._stack.getProperty("extruder_nr", "value") is not None:
  73. stack_nr = -1
  74. # Use the found stack number to get the right stack to copy the value from.
  75. if stack_nr in ExtruderManager.getInstance().extruderIds:
  76. stack = ContainerRegistry.getInstance().findContainerStacks(id = ExtruderManager.getInstance().extruderIds[stack_nr])[0]
  77. else:
  78. stack = self._stack
  79. # Use the raw property to set the value (so the inheritance doesn't break)
  80. if stack is not None:
  81. new_instance.setProperty("value", stack.getRawProperty(item, "value"))
  82. else:
  83. new_instance.setProperty("value", None)
  84. new_instance.resetState() # Ensure that the state is not seen as a user state.
  85. settings.addInstance(new_instance)
  86. visibility_changed = True
  87. if visibility_changed:
  88. self.visibilityChanged.emit()
  89. def getVisible(self):
  90. visible_settings = set()
  91. if not self._node:
  92. return visible_settings
  93. if not self._stack:
  94. return visible_settings
  95. settings = self._stack.getTop()
  96. if not settings:
  97. return visible_settings
  98. visible_settings = set(map(lambda i: i.definition.key, settings.findInstances()))
  99. return visible_settings