pyfast_and_pyside2_custom_window.py source

This example demonstrates how to use FAST in an existing PySide2 application.

This example demonstrates how to use FAST in an existing PySide2 application.

Image
from PySide2.QtWidgets import *
from PySide2.QtOpenGL import QGLWidget
from PySide2.QtCore import Slot
import PySide2.QtSvg # Must import this before fast due to conflicting symbols
from shiboken2 import wrapInstance
import fast
import threading
import sys

#fast.Reporter.setGlobalReportMethod(fast.Reporter.COUT)

# Create a simple window widget with pyside2
class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle('pyFAST + PySide2')

        # Create button
        self.button = QPushButton("Restart FAST pipeline")

        # Create FAST view
        self.view = fast.View()
        self.installEventFilter(wrapInstance(int(self.view.asQGLWidget()), QGLWidget))
        self.view.set2DMode()

        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(wrapInstance(int(self.view.asQGLWidget()), QGLWidget))
        layout.addWidget(self.button)
        self.setLayout(layout)

        # Connect button click event
        self.button.clicked.connect(self.restartPipeline)

        self.resize(512, 512)

    @Slot()
    def restartPipeline(self):
        # Create FAST computation thread
        # This is needed to run computations smoothly in the background
        # The computation thread must live in the object to avoid being destroyed when this function is done.
        self.computationThread = fast.ComputationThread.create()
        self.computationThread.addView(self.view)

        # Setup a FAST pipeline
        streamer = fast.ImageFileStreamer \
            .create(fast.Config.getTestDataPath() + '/US/Heart/ApicalFourChamber/US-2D_#.mhd')

        renderer = fast.ImageRenderer.create() \
            .connect(streamer)

        self.view.removeAllRenderers()
        self.view.addRenderer(renderer)
        self.view.reinitialize()
        self.computationThread.start()


if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)

    # Create and show the window
    window = Window()
    window.show()

    # Run the main Qt loop
    sys.exit(app.exec_())