PerObjectSettingVisibilityHandler.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 None: # Setting was not added already.
  58. definition = self._stack.getSettingDefinition(item)
  59. if definition:
  60. new_instance = SettingInstance(definition, settings)
  61. stack_nr = -1
  62. stack = None
  63. # Check from what stack we should copy the raw property of the setting from.
  64. if self._stack.getProperty("machine_extruder_count", "value") > 1:
  65. if definition.limit_to_extruder != "-1":
  66. # A limit to extruder function was set and it's a multi extrusion machine. Check what stack we do need to use.
  67. stack_nr = str(int(round(float(self._stack.getProperty(item, "limit_to_extruder")))))
  68. # Check if the found stack_number is in the extruder list of extruders.
  69. if stack_nr not in ExtruderManager.getInstance().extruderIds and self._stack.getProperty("extruder_nr", "value") is not None:
  70. stack_nr = -1
  71. # Use the found stack number to get the right stack to copy the value from.
  72. if stack_nr in ExtruderManager.getInstance().extruderIds:
  73. stack = ContainerRegistry.getInstance().findContainerStacks(id = ExtruderManager.getInstance().extruderIds[stack_nr])[0]
  74. else:
  75. stack = self._stack
  76. # Use the raw property to set the value (so the inheritance doesn't break)
  77. if stack is not None:
  78. new_instance.setProperty("value", stack.getRawProperty(item, "value"))
  79. else:
  80. new_instance.setProperty("value", None)
  81. new_instance.resetState() # Ensure that the state is not seen as a user state.
  82. settings.addInstance(new_instance)
  83. visibility_changed = True
  84. else:
  85. Logger.log("w", "Unable to add instance (%s) to per-object visibility because we couldn't find the matching definition", item)
  86. if visibility_changed:
  87. self.visibilityChanged.emit()
  88. def getVisible(self):
  89. visible_settings = set()
  90. if not self._node:
  91. return visible_settings
  92. if not self._stack:
  93. return visible_settings
  94. settings = self._stack.getTop()
  95. if not settings:
  96. return visible_settings
  97. visible_settings = set(map(lambda i: i.definition.key, settings.findInstances()))
  98. return visible_settings