UpdatableMachinesModel.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (c) 2021 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Dict, List, Optional
  4. from PyQt5.QtCore import Qt
  5. from UM.Qt.ListModel import ListModel
  6. from cura.Settings.GlobalStack import GlobalStack
  7. create_new_list_item = {
  8. "id": "new",
  9. "name": "Create new",
  10. "displayName": "Create new",
  11. "type": "default_option" # to make sure we are not mixing the "Create new" option with a printer with id "new"
  12. } # type: Dict[str, str]
  13. class UpdatableMachinesModel(ListModel):
  14. """Model that holds cura packages.
  15. By setting the filter property the instances held by this model can be changed.
  16. """
  17. def __init__(self, parent: Optional["QObject"] = None) -> None:
  18. super(UpdatableMachinesModel, self).__init__(parent = parent)
  19. self.addRoleName(Qt.UserRole + 1, "id")
  20. self.addRoleName(Qt.UserRole + 2, "name")
  21. self.addRoleName(Qt.UserRole + 3, "displayName")
  22. self.addRoleName(Qt.UserRole + 4, "type") # Either "default_option" or "machine"
  23. def update(self, machines: List[GlobalStack]) -> None:
  24. items = [create_new_list_item] # type: List[Dict[str, str]]
  25. for machine in sorted(machines, key = lambda printer: printer.name):
  26. items.append({
  27. "id": machine.id,
  28. "name": machine.name,
  29. "displayName": "Update " + machine.name,
  30. "type": "machine"
  31. })
  32. self.setItems(items)