PreviewStage.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import os.path
  4. from UM.Qt.QtApplication import QtApplication
  5. from cura.Stages.CuraStage import CuraStage
  6. from typing import TYPE_CHECKING, Optional
  7. if TYPE_CHECKING:
  8. from UM.View.View import View
  9. ## Displays a preview of what you're about to print.
  10. #
  11. # The Python component of this stage just loads PreviewMain.qml for display
  12. # when the stage is selected, and makes sure that it reverts to the previous
  13. # view when the previous stage is activated.
  14. class PreviewStage(CuraStage):
  15. def __init__(self, application: QtApplication, parent = None) -> None:
  16. super().__init__(parent)
  17. self._application = application
  18. self._application.engineCreatedSignal.connect(self._engineCreated)
  19. self._previously_active_view = None # type: Optional[View]
  20. ## When selecting the stage, remember which was the previous view so that
  21. # we can revert to that view when we go out of the stage later.
  22. def onStageSelected(self) -> None:
  23. self._previously_active_view = self._application.getController().getActiveView()
  24. ## Called when going to a different stage (away from the Preview Stage).
  25. #
  26. # When going to a different stage, the view should be reverted to what it
  27. # was before. Normally, that just reverts it to solid view.
  28. def onStageDeselected(self) -> None:
  29. if self._previously_active_view is not None:
  30. self._application.getController().setActiveView(self._previously_active_view.getPluginId())
  31. self._previously_active_view = None
  32. ## Delayed load of the QML files.
  33. #
  34. # We need to make sure that the QML engine is running before we can load
  35. # these.
  36. def _engineCreated(self) -> None:
  37. plugin_path = self._application.getPluginRegistry().getPluginPath(self.getPluginId())
  38. if plugin_path is not None:
  39. menu_component_path = os.path.join(plugin_path, "PreviewMenu.qml")
  40. main_component_path = os.path.join(plugin_path, "PreviewMain.qml")
  41. self.addDisplayComponent("menu", menu_component_path)
  42. self.addDisplayComponent("main", main_component_path)