FirstStartMachineActionsModel.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from typing import Optional, Dict, Any, TYPE_CHECKING
  4. from PyQt6.QtCore import QObject, Qt, pyqtProperty, pyqtSignal, pyqtSlot
  5. from UM.Qt.ListModel import ListModel
  6. if TYPE_CHECKING:
  7. from cura.CuraApplication import CuraApplication
  8. class FirstStartMachineActionsModel(ListModel):
  9. """This model holds all first-start machine actions for the currently active machine. It has 2 roles:
  10. - title : the title/name of the action
  11. - content : the QObject of the QML content of the action
  12. - action : the MachineAction object itself
  13. """
  14. TitleRole = Qt.ItemDataRole.UserRole + 1
  15. ContentRole = Qt.ItemDataRole.UserRole + 2
  16. ActionRole = Qt.ItemDataRole.UserRole + 3
  17. def __init__(self, application: "CuraApplication", parent: Optional[QObject] = None) -> None:
  18. super().__init__(parent)
  19. self.addRoleName(self.TitleRole, "title")
  20. self.addRoleName(self.ContentRole, "content")
  21. self.addRoleName(self.ActionRole, "action")
  22. self._current_action_index = 0
  23. self._application = application
  24. self._application.initializationFinished.connect(self.initialize)
  25. self._previous_global_stack = None
  26. def initialize(self) -> None:
  27. self._application.getMachineManager().globalContainerChanged.connect(self._update)
  28. self._update()
  29. currentActionIndexChanged = pyqtSignal()
  30. allFinished = pyqtSignal() # Emitted when all actions have been finished.
  31. @pyqtProperty(int, notify = currentActionIndexChanged)
  32. def currentActionIndex(self) -> int:
  33. return self._current_action_index
  34. @pyqtProperty("QVariantMap", notify = currentActionIndexChanged)
  35. def currentItem(self) -> Optional[Dict[str, Any]]:
  36. if self._current_action_index >= self.count:
  37. return dict()
  38. else:
  39. return self.getItem(self._current_action_index)
  40. @pyqtProperty(bool, notify = currentActionIndexChanged)
  41. def hasMoreActions(self) -> bool:
  42. return self._current_action_index < self.count - 1
  43. @pyqtSlot()
  44. def goToNextAction(self) -> None:
  45. # finish the current item
  46. if "action" in self.currentItem:
  47. self.currentItem["action"].setFinished()
  48. if not self.hasMoreActions:
  49. self.allFinished.emit()
  50. self.reset()
  51. return
  52. self._current_action_index += 1
  53. self.currentActionIndexChanged.emit()
  54. @pyqtSlot()
  55. def reset(self) -> None:
  56. """Resets the current action index to 0 so the wizard panel can show actions from the beginning."""
  57. self._current_action_index = 0
  58. self.currentActionIndexChanged.emit()
  59. if self.count == 0:
  60. self.allFinished.emit()
  61. def _update(self) -> None:
  62. global_stack = self._application.getMachineManager().activeMachine
  63. if global_stack is None:
  64. self.setItems([])
  65. return
  66. # Do not update if the machine has not been switched. This can cause the SettingProviders on the Machine
  67. # Setting page to do a force update, but they can use potential outdated cached values.
  68. if self._previous_global_stack is not None and global_stack.getId() == self._previous_global_stack.getId():
  69. return
  70. self._previous_global_stack = global_stack
  71. definition_id = global_stack.definition.getId()
  72. first_start_actions = self._application.getMachineActionManager().getFirstStartActions(definition_id)
  73. item_list = []
  74. for item in first_start_actions:
  75. item_list.append({"title": item.label,
  76. "content": item.getDisplayItem(),
  77. "action": item,
  78. })
  79. item.reset()
  80. self.setItems(item_list)
  81. self.reset()
  82. __all__ = ["FirstStartMachineActionsModel"]