DiscrepanciesPresenter.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from typing import Optional
  5. from PyQt6.QtCore import QObject
  6. from UM.Qt.QtApplication import QtApplication
  7. from UM.Signal import Signal
  8. from .SubscribedPackagesModel import SubscribedPackagesModel
  9. class DiscrepanciesPresenter(QObject):
  10. """Shows a list of packages to be added or removed. The user can select which packages to (un)install. The user's
  11. choices are emitted on the `packageMutations` Signal.
  12. """
  13. def __init__(self, app: QtApplication) -> None:
  14. super().__init__()
  15. self.packageMutations = Signal() # Emits SubscribedPackagesModel
  16. self._app = app
  17. self._package_manager = app.getPackageManager()
  18. self._dialog: Optional[QObject] = None
  19. self._compatibility_dialog_path = "resources/qml/CompatibilityDialog.qml"
  20. def present(self, plugin_path: str, model: SubscribedPackagesModel) -> None:
  21. path = os.path.join(plugin_path, self._compatibility_dialog_path)
  22. self._dialog = self._app.createQmlComponent(path, {"subscribedPackagesModel": model, "handler": self})
  23. assert self._dialog
  24. self._dialog.accepted.connect(lambda: self._onConfirmClicked(model))
  25. def _onConfirmClicked(self, model: SubscribedPackagesModel) -> None:
  26. # If there are incompatible packages - automatically dismiss them
  27. if model.getIncompatiblePackages():
  28. self._package_manager.dismissAllIncompatiblePackages(model.getIncompatiblePackages())
  29. # For now, all compatible packages presented to the user should be installed.
  30. # Later, we might remove items for which the user unselected the package
  31. if model.getCompatiblePackages():
  32. model.setItems(model.getCompatiblePackages())
  33. self.packageMutations.emit(model)