ObjectsModel.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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.Camera import Camera
  7. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  8. from UM.Scene.SceneNode import SceneNode
  9. from UM.Scene.Selection import Selection
  10. from UM.i18n import i18nCatalog
  11. from collections import defaultdict
  12. catalog = i18nCatalog("cura")
  13. ## Keep track of all objects in the project
  14. class ObjectsModel(ListModel):
  15. def __init__(self):
  16. super().__init__()
  17. Application.getInstance().getController().getScene().sceneChanged.connect(self._updateSceneDelayed)
  18. Application.getInstance().getPreferences().preferenceChanged.connect(self._updateDelayed)
  19. self._update_timer = QTimer()
  20. self._update_timer.setInterval(200)
  21. self._update_timer.setSingleShot(True)
  22. self._update_timer.timeout.connect(self._update)
  23. self._build_plate_number = -1
  24. def setActiveBuildPlate(self, nr):
  25. if self._build_plate_number != nr:
  26. self._build_plate_number = nr
  27. self._update()
  28. def _updateSceneDelayed(self, source):
  29. if not isinstance(source, Camera):
  30. self._update_timer.start()
  31. def _updateDelayed(self, *args):
  32. self._update_timer.start()
  33. def _update(self, *args):
  34. nodes = []
  35. filter_current_build_plate = Application.getInstance().getPreferences().getValue("view/filter_current_build_plate")
  36. active_build_plate_number = self._build_plate_number
  37. group_nr = 1
  38. name_count_dict = defaultdict(int)
  39. for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()):
  40. if not isinstance(node, SceneNode):
  41. continue
  42. if (not node.getMeshData() and not node.callDecoration("getLayerData")) and not node.callDecoration("isGroup"):
  43. continue
  44. if node.getParent() and node.getParent().callDecoration("isGroup"):
  45. continue # Grouped nodes don't need resetting as their parent (the group) is resetted)
  46. if not node.callDecoration("isSliceable") and not node.callDecoration("isGroup"):
  47. continue
  48. node_build_plate_number = node.callDecoration("getBuildPlateNumber")
  49. if filter_current_build_plate and node_build_plate_number != active_build_plate_number:
  50. continue
  51. if not node.callDecoration("isGroup"):
  52. name = node.getName()
  53. else:
  54. name = catalog.i18nc("@label", "Group #{group_nr}").format(group_nr = str(group_nr))
  55. group_nr += 1
  56. if hasattr(node, "isOutsideBuildArea"):
  57. is_outside_build_area = node.isOutsideBuildArea()
  58. else:
  59. is_outside_build_area = False
  60. #check if we already have an instance of the object based on name
  61. name_count_dict[name] += 1
  62. name_count = name_count_dict[name]
  63. if name_count > 1:
  64. name = "{0}({1})".format(name, name_count-1)
  65. node.setName(name)
  66. nodes.append({
  67. "name": name,
  68. "isSelected": Selection.isSelected(node),
  69. "isOutsideBuildArea": is_outside_build_area,
  70. "buildPlateNumber": node_build_plate_number,
  71. "node": node
  72. })
  73. nodes = sorted(nodes, key=lambda n: n["name"])
  74. self.setItems(nodes)
  75. self.itemsChanged.emit()
  76. @staticmethod
  77. def createObjectsModel():
  78. return ObjectsModel()