InstallMissingPackagesDialog.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Copyright (c) 2022 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os
  4. from PyQt6.QtCore import QObject, pyqtSignal, pyqtProperty
  5. from typing import Optional, List, Dict, cast, Callable
  6. from cura.CuraApplication import CuraApplication
  7. from UM.PluginRegistry import PluginRegistry
  8. from cura.CuraPackageManager import CuraPackageManager
  9. from UM.i18n import i18nCatalog
  10. from UM.FlameProfiler import pyqtSlot
  11. from .MissingPackageList import MissingPackageList
  12. i18n_catalog = i18nCatalog("cura")
  13. class InstallMissingPackageDialog(QObject):
  14. """Dialog used to display packages that need to be installed to load 3mf file materials"""
  15. def __init__(self, packages_metadata: List[Dict[str, str]], show_missing_materials_warning: Callable[[], None]) -> None:
  16. """Initialize
  17. :param packages_metadata: List of dictionaries containing information about missing packages.
  18. """
  19. super().__init__()
  20. self._plugin_registry: PluginRegistry = CuraApplication.getInstance().getPluginRegistry()
  21. self._package_manager: CuraPackageManager = cast(CuraPackageManager, CuraApplication.getInstance().getPackageManager())
  22. self._package_manager.installedPackagesChanged.connect(self.checkIfRestartNeeded)
  23. self._dialog: Optional[QObject] = None
  24. self._restart_needed = False
  25. self._package_metadata: List[Dict[str, str]] = packages_metadata
  26. self._package_model: MissingPackageList = MissingPackageList(packages_metadata)
  27. self._show_missing_materials_warning = show_missing_materials_warning
  28. def show(self) -> None:
  29. plugin_path = self._plugin_registry.getPluginPath("Marketplace")
  30. if plugin_path is None:
  31. plugin_path = os.path.dirname(__file__)
  32. # create a QML component for the license dialog
  33. license_dialog_component_path = os.path.join(plugin_path, "resources", "qml", "InstallMissingPackagesDialog.qml")
  34. self._dialog = CuraApplication.getInstance().createQmlComponent(license_dialog_component_path, {"manager": self})
  35. self._dialog.show()
  36. def checkIfRestartNeeded(self) -> None:
  37. if self._dialog is None:
  38. return
  39. self._restart_needed = self._package_manager.hasPackagesToRemoveOrInstall
  40. self.showRestartChanged.emit()
  41. showRestartChanged = pyqtSignal()
  42. @pyqtProperty(bool, notify=showRestartChanged)
  43. def showRestartNotification(self) -> bool:
  44. return self._restart_needed
  45. @pyqtProperty(QObject)
  46. def model(self) -> MissingPackageList:
  47. return self._package_model
  48. @pyqtSlot()
  49. def showMissingMaterialsWarning(self) -> None:
  50. self._show_missing_materials_warning()