DiscrepanciesPresenter.py 1.7 KB

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