MultiBuildPlateModel.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. class MultiBuildPlateModel(ListModel):
  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. maxBuildPlateChanged = pyqtSignal()
  13. activeBuildPlateChanged = pyqtSignal()
  14. selectionChanged = pyqtSignal()
  15. def __init__(self, parent = None):
  16. super().__init__(parent)
  17. self._update_timer = QTimer()
  18. self._update_timer.setInterval(100)
  19. self._update_timer.setSingleShot(True)
  20. self._update_timer.timeout.connect(self._updateSelectedObjectBuildPlateNumbers)
  21. self._application = Application.getInstance()
  22. self._application.getController().getScene().sceneChanged.connect(self._updateSelectedObjectBuildPlateNumbersDelayed)
  23. Selection.selectionChanged.connect(self._updateSelectedObjectBuildPlateNumbers)
  24. self._max_build_plate = 1 # default
  25. self._active_build_plate = -1
  26. def setMaxBuildPlate(self, max_build_plate):
  27. if self._max_build_plate != max_build_plate:
  28. self._max_build_plate = max_build_plate
  29. self.maxBuildPlateChanged.emit()
  30. @pyqtProperty(int, notify = maxBuildPlateChanged)
  31. def maxBuildPlate(self):
  32. """Return the highest build plate number"""
  33. return self._max_build_plate
  34. def setActiveBuildPlate(self, nr):
  35. if self._active_build_plate != nr:
  36. self._active_build_plate = nr
  37. self.activeBuildPlateChanged.emit()
  38. @pyqtProperty(int, notify = activeBuildPlateChanged)
  39. def activeBuildPlate(self):
  40. return self._active_build_plate
  41. def _updateSelectedObjectBuildPlateNumbersDelayed(self, *args):
  42. if not isinstance(args[0], Camera):
  43. self._update_timer.start()
  44. def _updateSelectedObjectBuildPlateNumbers(self, *args):
  45. result = set()
  46. for node in Selection.getAllSelectedObjects():
  47. result.add(node.callDecoration("getBuildPlateNumber"))
  48. self._selection_build_plates = list(result)
  49. self.selectionChanged.emit()
  50. @pyqtProperty("QVariantList", notify = selectionChanged)
  51. def selectionBuildPlates(self):
  52. return self._selection_build_plates