MultiBuildPlateModel.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, pyqtSignal, pyqtProperty
  4. from UM.Application import Application
  5. from UM.Scene.Camera import Camera
  6. from UM.Scene.Selection import Selection
  7. from UM.Qt.ListModel import ListModel
  8. #
  9. # This is the model for multi build plate feature.
  10. # This has nothing to do with the build plate types you can choose on the sidebar for a machine.
  11. #
  12. class MultiBuildPlateModel(ListModel):
  13. maxBuildPlateChanged = pyqtSignal()
  14. activeBuildPlateChanged = pyqtSignal()
  15. selectionChanged = pyqtSignal()
  16. def __init__(self, parent = None):
  17. super().__init__(parent)
  18. self._update_timer = QTimer()
  19. self._update_timer.setInterval(100)
  20. self._update_timer.setSingleShot(True)
  21. self._update_timer.timeout.connect(self._updateSelectedObjectBuildPlateNumbers)
  22. self._application = Application.getInstance()
  23. self._application.getController().getScene().sceneChanged.connect(self._updateSelectedObjectBuildPlateNumbersDelayed)
  24. Selection.selectionChanged.connect(self._updateSelectedObjectBuildPlateNumbers)
  25. self._max_build_plate = 1 # default
  26. self._active_build_plate = -1
  27. def setMaxBuildPlate(self, max_build_plate):
  28. if self._max_build_plate != max_build_plate:
  29. self._max_build_plate = max_build_plate
  30. self.maxBuildPlateChanged.emit()
  31. ## Return the highest build plate number
  32. @pyqtProperty(int, notify = maxBuildPlateChanged)
  33. def maxBuildPlate(self):
  34. return self._max_build_plate
  35. def setActiveBuildPlate(self, nr):
  36. if self._active_build_plate != nr:
  37. self._active_build_plate = nr
  38. self.activeBuildPlateChanged.emit()
  39. @pyqtProperty(int, notify = activeBuildPlateChanged)
  40. def activeBuildPlate(self):
  41. return self._active_build_plate
  42. def _updateSelectedObjectBuildPlateNumbersDelayed(self, *args):
  43. if not isinstance(args[0], Camera):
  44. self._update_timer.start()
  45. def _updateSelectedObjectBuildPlateNumbers(self, *args):
  46. result = set()
  47. for node in Selection.getAllSelectedObjects():
  48. result.add(node.callDecoration("getBuildPlateNumber"))
  49. self._selection_build_plates = list(result)
  50. self.selectionChanged.emit()
  51. @pyqtProperty("QVariantList", notify = selectionChanged)
  52. def selectionBuildPlates(self):
  53. return self._selection_build_plates