MultiBuildPlateModel.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional
  4. from PyQt5.QtCore import QTimer, pyqtSignal, pyqtProperty
  5. from UM.Application import Application
  6. from UM.Scene.Camera import Camera
  7. from UM.Scene.Selection import Selection
  8. from UM.Qt.ListModel import ListModel
  9. class MultiBuildPlateModel(ListModel):
  10. """This is the model for multi build plate feature.
  11. This has nothing to do with the build plate types you can choose on the sidebar for a machine.
  12. """
  13. maxBuildPlateChanged = pyqtSignal()
  14. activeBuildPlateChanged = pyqtSignal()
  15. selectionChanged = pyqtSignal()
  16. def __init__(self, parent: Optional["QObject"] = None):
  17. super(MultiBuildPlateModel, self).__init__(parent = 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. @pyqtProperty(int, notify = maxBuildPlateChanged)
  32. def maxBuildPlate(self):
  33. """Return the highest build plate number"""
  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