from PyQt5 import Qt

from ris_widget import _ris_widget
from .. import scope_client

class LiveViewerWidget(Qt.QObject):
    # TODO might this better be a subclass of RisWidget?
    RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT = 1001

    @staticmethod
    def can_run(scope):
        return hasattr(scope, 'camera')

    def __init__(self, scope, scope_properties):
        self.ris_widget = _ris_widget.RisWidget()
        super().__init__(self.ris_widget)
        self.live_streamer = scope_client.LiveStreamer(scope, scope_properties, self.post_live_update)

    def event(self, e):
        # This is called by the main QT event loop to service the event posted in post_live_update().
        if e.type() == self.RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT:
            image, frame_no = self.live_streamer.get_image()
            self.ris_widget.showImage(image)
            return True
        return super().event(e)

    def post_live_update(self):
        # posting an event does not require calling thread to have an event loop,
        # unlike sending a signal
        Qt.QCoreApplication.postEvent(self, Qt.QEvent(self.RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT))

    def show(self):
        self.ris_widget.show()


#from PyQt5 import Qt
#
#from ris_widget import ris_widget
#from ris_widget import image as ris_widget_image
#from .. import scope_client
#
#class LiveViewerWidget(Qt.QObject):
#    # TODO might this better be a subclass of RisWidget?
#    RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT = 1001
#
#    @staticmethod
#    def can_run(scope):
#        return hasattr(scope, 'camera')
#
#    def __init__(self, scope, scope_properties):
#        self.ris_widget = ris_widget.RisWidget()
#        super().__init__(self.ris_widget)
#        self.live_streamer = scope_client.LiveStreamer(scope, scope_properties, self.post_live_update)
#
#    def event(self, e):
#        # This is called by the main QT event loop to service the event posted in post_live_update().
#        if e.type() == self.RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT:
#            image, frame_no = self.live_streamer.get_image()
#            rwimage = ris_widget_image.Image(image, is_twelve_bit=self.live_streamer.scope.camera.bit_depth=='12 Bit')
#            self.ris_widget.image = rwimage
#            return True
#        return super().event(e)
#
#    def post_live_update(self):
#        # posting an event does not require calling thread to have an event loop,
#        # unlike sending a signal
#        Qt.QCoreApplication.postEvent(self, Qt.QEvent(self.RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT))
#
#    def show(self):
#        self.ris_widget.show()

@@ -38,17 +38,16 @@
  import 
-_
 ris_widg
@@ -49,16 +49,65 @@
 _widget%0A
+from ris_widget import image as ris_widget_image%0A
 from .. 
@@ -431,17 +431,16 @@
 idget = 
-_
 ris_widg
@@ -857,16 +857,135 @@
 image()%0A
+            rwimage = ris_widget_image.Image(image, is_twelve_bit=self.live_streamer.scope.camera.bit_depth=='12 Bit')%0A
         
@@ -1008,24 +1008,23 @@
 get.
-showImage(
+image = rw
 image
-)
 %0A   
@@ -1383,1427 +1383,4 @@
 w()%0A
-%0A%0A#from PyQt5 import Qt%0A#%0A#from ris_widget import ris_widget%0A#from ris_widget import image as ris_widget_image%0A#from .. import scope_client%0A#%0A#class LiveViewerWidget(Qt.QObject):%0A#    # TODO might this better be a subclass of RisWidget?%0A#    RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT = 1001%0A#%0A#    @staticmethod%0A#    def can_run(scope):%0A#        return hasattr(scope, 'camera')%0A#%0A#    def __init__(self, scope, scope_properties):%0A#        self.ris_widget = ris_widget.RisWidget()%0A#        super().__init__(self.ris_widget)%0A#        self.live_streamer = scope_client.LiveStreamer(scope, scope_properties, self.post_live_update)%0A#%0A#    def event(self, e):%0A#        # This is called by the main QT event loop to service the event posted in post_live_update().%0A#        if e.type() == self.RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT:%0A#            image, frame_no = self.live_streamer.get_image()%0A#            rwimage = ris_widget_image.Image(image, is_twelve_bit=self.live_streamer.scope.camera.bit_depth=='12 Bit')%0A#            self.ris_widget.image = rwimage%0A#            return True%0A#        return super().event(e)%0A#%0A#    def post_live_update(self):%0A#        # posting an event does not require calling thread to have an event loop,%0A#        # unlike sending a signal%0A#        Qt.QCoreApplication.postEvent(self, Qt.QEvent(self.RW_LIVE_STREAM_BINDING_LIVE_UPDATE_EVENT))%0A#%0A#    def show(self):%0A#        self.ris_widget.show()%0A

Make live viewer use new RisWidget.