CuraSceneController.py 4.6 KB

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