FirstStartMachineActionsModel.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 PyQt5.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. #
  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. class FirstStartMachineActionsModel(ListModel):
  15. TitleRole = Qt.UserRole + 1
  16. ContentRole = Qt.UserRole + 2
  17. ActionRole = Qt.UserRole + 3
  18. def __init__(self, application: "CuraApplication", parent: Optional[QObject] = None) -> None:
  19. super().__init__(parent)
  20. self.addRoleName(self.TitleRole, "title")
  21. self.addRoleName(self.ContentRole, "content")
  22. self.addRoleName(self.ActionRole, "action")
  23. self._current_action_index = 0
  24. self._application = application
  25. self._application.initializationFinished.connect(self.initialize)
  26. self._previous_global_stack = None
  27. def initialize(self) -> None:
  28. self._application.getMachineManager().globalContainerChanged.connect(self._update)
  29. self._update()
  30. currentActionIndexChanged = pyqtSignal()
  31. allFinished = pyqtSignal() # Emitted when all actions have been finished.
  32. @pyqtProperty(int, notify = currentActionIndexChanged)
  33. def currentActionIndex(self) -> int:
  34. return self._current_action_index
  35. @pyqtProperty("QVariantMap", notify = currentActionIndexChanged)
  36. def currentItem(self) -> Optional[Dict[str, Any]]:
  37. if self._current_action_index >= self.count:
  38. return dict()
  39. else:
  40. return self.getItem(self._current_action_index)
  41. @pyqtProperty(bool, notify = currentActionIndexChanged)
  42. def hasMoreActions(self) -> bool:
  43. return self._current_action_index < self.count - 1
  44. @pyqtSlot()
  45. def goToNextAction(self) -> None:
  46. # finish the current item
  47. if "action" in self.currentItem:
  48. self.currentItem["action"].setFinished()
  49. if not self.hasMoreActions:
  50. self.allFinished.emit()
  51. self.reset()
  52. return
  53. self._current_action_index += 1
  54. self.currentActionIndexChanged.emit()
  55. # Resets the current action index to 0 so the wizard panel can show actions from the beginning.
  56. @pyqtSlot()
  57. def reset(self) -> None:
  58. self._current_action_index = 0
  59. self.currentActionIndexChanged.emit()
  60. if self.count == 0:
  61. self.allFinished.emit()
  62. def _update(self) -> None:
  63. global_stack = self._application.getMachineManager().activeMachine
  64. if global_stack is None:
  65. self.setItems([])
  66. return
  67. # Do not update if the machine has not been switched. This can cause the SettingProviders on the Machine
  68. # Setting page to do a force update, but they can use potential outdated cached values.
  69. if self._previous_global_stack is not None and global_stack.getId() == self._previous_global_stack.getId():
  70. return
  71. self._previous_global_stack = global_stack
  72. definition_id = global_stack.definition.getId()
  73. first_start_actions = self._application.getMachineActionManager().getFirstStartActions(definition_id)
  74. item_list = []
  75. for item in first_start_actions:
  76. item_list.append({"title": item.label,
  77. "content": item.getDisplayItem(),
  78. "action": item,
  79. })
  80. item.reset()
  81. self.setItems(item_list)
  82. self.reset()
  83. __all__ = ["FirstStartMachineActionsModel"]