PerObjectSettingVisibilityHandler.py 5.5 KB

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