ObjectsModel.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from PyQt5.QtCore import QTimer
  4. from UM.Application import Application
  5. from UM.Qt.ListModel import ListModel
  6. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  7. from UM.Scene.SceneNode import SceneNode
  8. from UM.Scene.Selection import Selection
  9. from UM.i18n import i18nCatalog
  10. catalog = i18nCatalog("cura")
  11. ## Keep track of all objects in the project
  12. class ObjectsModel(ListModel):
  13. def __init__(self):
  14. super().__init__()
  15. Application.getInstance().getController().getScene().sceneChanged.connect(self._updateDelayed)
  16. Application.getInstance().getPreferences().preferenceChanged.connect(self._updateDelayed)
  17. self._update_timer = QTimer()
  18. self._update_timer.setInterval(100)
  19. self._update_timer.setSingleShot(True)
  20. self._update_timer.timeout.connect(self._update)
  21. self._build_plate_number = -1
  22. def setActiveBuildPlate(self, nr):
  23. self._build_plate_number = nr
  24. self._update()
  25. def _updateDelayed(self, *args):
  26. self._update_timer.start()
  27. def _update(self, *args):
  28. nodes = []
  29. filter_current_build_plate = Application.getInstance().getPreferences().getValue("view/filter_current_build_plate")
  30. active_build_plate_number = self._build_plate_number
  31. group_nr = 1
  32. for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()):
  33. if not isinstance(node, SceneNode):
  34. continue
  35. if (not node.getMeshData() and not node.callDecoration("getLayerData")) and not node.callDecoration("isGroup"):
  36. continue
  37. if node.getParent() and node.getParent().callDecoration("isGroup"):
  38. continue # Grouped nodes don't need resetting as their parent (the group) is resetted)
  39. if not node.callDecoration("isSliceable") and not node.callDecoration("isGroup"):
  40. continue
  41. node_build_plate_number = node.callDecoration("getBuildPlateNumber")
  42. if filter_current_build_plate and node_build_plate_number != active_build_plate_number:
  43. continue
  44. if not node.callDecoration("isGroup"):
  45. name = node.getName()
  46. else:
  47. name = catalog.i18nc("@label", "Group #{group_nr}").format(group_nr = str(group_nr))
  48. group_nr += 1
  49. if hasattr(node, "isOutsideBuildArea"):
  50. is_outside_build_area = node.isOutsideBuildArea()
  51. else:
  52. is_outside_build_area = False
  53. nodes.append({
  54. "name": name,
  55. "isSelected": Selection.isSelected(node),
  56. "isOutsideBuildArea": is_outside_build_area,
  57. "buildPlateNumber": node_build_plate_number,
  58. "node": node
  59. })
  60. nodes = sorted(nodes, key=lambda n: n["name"])
  61. self.setItems(nodes)
  62. self.itemsChanged.emit()
  63. @staticmethod
  64. def createObjectsModel():
  65. return ObjectsModel()