PreviewStage.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. class PreviewStage(CuraStage):
  10. """Displays a preview of what you're about to print.
  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. """
  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. def onStageSelected(self) -> None:
  21. """When selecting the stage, remember which was the previous view so that
  22. we can revert to that view when we go out of the stage later.
  23. """
  24. self._previously_active_view = self._application.getController().getActiveView()
  25. def onStageDeselected(self) -> None:
  26. """Called when going to a different stage (away from the Preview Stage).
  27. When going to a different stage, the view should be reverted to what it
  28. was before. Normally, that just reverts it to solid view.
  29. """
  30. if self._previously_active_view is not None:
  31. self._application.getController().setActiveView(self._previously_active_view.getPluginId())
  32. self._previously_active_view = None
  33. def _engineCreated(self) -> None:
  34. """Delayed load of the QML files.
  35. We need to make sure that the QML engine is running before we can load
  36. these.
  37. """
  38. plugin_path = self._application.getPluginRegistry().getPluginPath(self.getPluginId())
  39. if plugin_path is not None:
  40. menu_component_path = os.path.join(plugin_path, "PreviewMenu.qml")
  41. main_component_path = os.path.join(plugin_path, "PreviewMain.qml")
  42. self.addDisplayComponent("menu", menu_component_path)
  43. self.addDisplayComponent("main", main_component_path)