PerObjectSettingVisibilityHandler.py 5.4 KB

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