ObjectsModel.py 3.5 KB

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