CuraSceneController.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from UM.Logger import Logger
  2. from PyQt5.QtCore import Qt, pyqtSlot, QObject
  3. from PyQt5.QtWidgets import QApplication
  4. from UM.Scene.Camera import Camera
  5. from cura.ObjectsModel import ObjectsModel
  6. from cura.Machines.Models.MultiBuildPlateModel import MultiBuildPlateModel
  7. from UM.Application import Application
  8. from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator
  9. from UM.Scene.SceneNode import SceneNode
  10. from UM.Scene.Selection import Selection
  11. from UM.Signal import Signal
  12. class CuraSceneController(QObject):
  13. activeBuildPlateChanged = Signal()
  14. def __init__(self, objects_model: ObjectsModel, multi_build_plate_model: MultiBuildPlateModel) -> None:
  15. super().__init__()
  16. self._objects_model = objects_model
  17. self._multi_build_plate_model = multi_build_plate_model
  18. self._active_build_plate = -1
  19. self._last_selected_index = 0
  20. self._max_build_plate = 1 # default
  21. Application.getInstance().getController().getScene().sceneChanged.connect(self.updateMaxBuildPlate) # it may be a bit inefficient when changing a lot simultaneously
  22. def updateMaxBuildPlate(self, *args):
  23. if args:
  24. source = args[0]
  25. else:
  26. source = None
  27. if not isinstance(source, SceneNode) or isinstance(source, Camera):
  28. return
  29. max_build_plate = self._calcMaxBuildPlate()
  30. changed = False
  31. if max_build_plate != self._max_build_plate:
  32. self._max_build_plate = max_build_plate
  33. changed = True
  34. if changed:
  35. self._multi_build_plate_model.setMaxBuildPlate(self._max_build_plate)
  36. build_plates = [{"name": "Build Plate %d" % (i + 1), "buildPlateNumber": i} for i in range(self._max_build_plate + 1)]
  37. self._multi_build_plate_model.setItems(build_plates)
  38. if self._active_build_plate > self._max_build_plate:
  39. build_plate_number = 0
  40. if self._last_selected_index >= 0: # go to the buildplate of the item you last selected
  41. item = self._objects_model.getItem(self._last_selected_index)
  42. if "node" in item:
  43. node = item["node"]
  44. build_plate_number = node.callDecoration("getBuildPlateNumber")
  45. self.setActiveBuildPlate(build_plate_number)
  46. # self.buildPlateItemsChanged.emit() # TODO: necessary after setItems?
  47. def _calcMaxBuildPlate(self):
  48. max_build_plate = 0
  49. for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()):
  50. if node.callDecoration("isSliceable"):
  51. build_plate_number = node.callDecoration("getBuildPlateNumber")
  52. if build_plate_number is None:
  53. build_plate_number = 0
  54. max_build_plate = max(build_plate_number, max_build_plate)
  55. return max_build_plate
  56. ## Either select or deselect an item
  57. @pyqtSlot(int)
  58. def changeSelection(self, index):
  59. modifiers = QApplication.keyboardModifiers()
  60. ctrl_is_active = modifiers & Qt.ControlModifier
  61. shift_is_active = modifiers & Qt.ShiftModifier
  62. if ctrl_is_active:
  63. item = self._objects_model.getItem(index)
  64. node = item["node"]
  65. if Selection.isSelected(node):
  66. Selection.remove(node)
  67. else:
  68. Selection.add(node)
  69. elif shift_is_active:
  70. polarity = 1 if index + 1 > self._last_selected_index else -1
  71. for i in range(self._last_selected_index, index + polarity, polarity):
  72. item = self._objects_model.getItem(i)
  73. node = item["node"]
  74. Selection.add(node)
  75. else:
  76. # Single select
  77. item = self._objects_model.getItem(index)
  78. node = item["node"]
  79. build_plate_number = node.callDecoration("getBuildPlateNumber")
  80. if build_plate_number is not None and build_plate_number != -1:
  81. self.setActiveBuildPlate(build_plate_number)
  82. Selection.clear()
  83. Selection.add(node)
  84. self._last_selected_index = index
  85. @pyqtSlot(int)
  86. def setActiveBuildPlate(self, nr):
  87. if nr == self._active_build_plate:
  88. return
  89. Logger.log("d", "Select build plate: %s" % nr)
  90. self._active_build_plate = nr
  91. Selection.clear()
  92. self._multi_build_plate_model.setActiveBuildPlate(nr)
  93. self._objects_model.setActiveBuildPlate(nr)
  94. self.activeBuildPlateChanged.emit()
  95. @staticmethod
  96. def createCuraSceneController():
  97. objects_model = Application.getInstance().getObjectsModel()
  98. multi_build_plate_model = Application.getInstance().getMultiBuildPlateModel()
  99. return CuraSceneController(objects_model = objects_model, multi_build_plate_model = multi_build_plate_model)