ObjectsModel.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from UM.Application import Application
  2. from UM.Qt.ListModel import ListModel
  3. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  4. from UM.Scene.SceneNode import SceneNode
  5. from UM.Scene.Selection import Selection
  6. from UM.Preferences import Preferences
  7. from UM.i18n import i18nCatalog
  8. catalog = i18nCatalog("cura")
  9. ## Keep track of all objects in the project
  10. class ObjectsModel(ListModel):
  11. def __init__(self):
  12. super().__init__()
  13. Application.getInstance().getController().getScene().sceneChanged.connect(self._update)
  14. Preferences.getInstance().preferenceChanged.connect(self._update)
  15. self._build_plate_number = -1
  16. def setActiveBuildPlate(self, nr):
  17. self._build_plate_number = nr
  18. self._update()
  19. def _update(self, *args):
  20. nodes = []
  21. filter_current_build_plate = Preferences.getInstance().getValue("view/filter_current_build_plate")
  22. active_build_plate_number = self._build_plate_number
  23. group_nr = 1
  24. for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()):
  25. if not isinstance(node, SceneNode):
  26. continue
  27. if (not node.getMeshData() and not node.callDecoration("getLayerData")) and not node.callDecoration("isGroup"):
  28. continue
  29. if node.getParent() and node.getParent().callDecoration("isGroup"):
  30. continue # Grouped nodes don't need resetting as their parent (the group) is resetted)
  31. if not node.callDecoration("isSliceable") and not node.callDecoration("isGroup"):
  32. continue
  33. node_build_plate_number = node.callDecoration("getBuildPlateNumber")
  34. if filter_current_build_plate and node_build_plate_number != active_build_plate_number:
  35. continue
  36. if not node.callDecoration("isGroup"):
  37. name = node.getName()
  38. else:
  39. name = catalog.i18nc("@label", "Group #{group_nr}").format(group_nr = str(group_nr))
  40. group_nr += 1
  41. nodes.append({
  42. "name": name,
  43. "isSelected": Selection.isSelected(node),
  44. "isOutsideBuildArea": node.isOutsideBuildArea(),
  45. "buildPlateNumber": node_build_plate_number,
  46. "node": node
  47. })
  48. nodes = sorted(nodes, key=lambda n: n["name"])
  49. self.setItems(nodes)
  50. self.itemsChanged.emit()
  51. @staticmethod
  52. def createObjectsModel():
  53. return ObjectsModel()