FirstStartMachineActionsModel.py 4.1 KB

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