<reponame>yxdragon/camera-ctrl
import wx, numpy as np
from threading import Thread
from sciwx.canvas import MCanvas
from sciwx.widgets import ParaDialog

from .camera import RandomCamera, USBCamera

devices = {'Random Noise': RandomCamera, 'USB Camera': USBCamera}

class CameraPanel ( wx.Panel ):

    def __init__( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 600, 600 ), style = wx.TAB_TRAVERSAL, name = wx.EmptyString ):
        wx.Panel.__init__ ( self, parent, id = id, pos = pos, size = size, style = style, name = name )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.tool_bar = wx.ToolBar( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TB_HORIZONTAL|wx.TB_HORZ_TEXT )
        self.m_staticText1 = wx.StaticText( self.tool_bar, wx.ID_ANY, u"Camera: ", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText1.Wrap( -1 )

        self.tool_bar.AddControl( self.m_staticText1 )
        self.com_devices = wx.ComboBox( self.tool_bar, wx.ID_ANY, u"Combo!", wx.DefaultPosition, wx.DefaultSize, [], 0 )
        self.tool_bar.AddControl( self.com_devices )
        self.tol_open = self.tool_bar.AddTool( wx.ID_ANY, u"Open", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tol_start = self.tool_bar.AddTool( wx.ID_ANY, u"Start", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tol_stop = self.tool_bar.AddTool( wx.ID_ANY, u"Stop", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tol_close = self.tool_bar.AddTool( wx.ID_ANY, u"Close", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tool_bar.AddSeparator()

        self.spn_cache = wx.SpinCtrl( self.tool_bar, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.SP_ARROW_KEYS, 1, 1024, 0 )
        self.tool_bar.AddControl( self.spn_cache )

        self.btn_config = wx.Button( self.tool_bar, wx.ID_ANY, u"Config", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.tool_bar.AddControl( self.btn_config )
        self.tool_bar.Realize()

        bSizer1.Add( self.tool_bar, 0, wx.EXPAND, 5 )

        self.canvas = MCanvas( self )
        bSizer1.Add( self.canvas, 1, wx.EXPAND |wx.ALL, 0 )
        self.canvas.set_img(np.zeros((512, 512), np.uint8))

        self.com_devices.SetItems(sorted(devices))
        self.com_devices.Select(0)

        self.SetSizer( bSizer1 )
        self.Layout()

        # Connect Events
        self.Bind( wx.EVT_TOOL, self.on_open, id = self.tol_open.GetId() )
        self.Bind( wx.EVT_TOOL, self.on_start, id = self.tol_start.GetId() )
        self.Bind( wx.EVT_TOOL, self.on_stop, id = self.tol_stop.GetId() )
        self.Bind( wx.EVT_TOOL, self.on_close, id = self.tol_close.GetId() )
        self.btn_config.Bind( wx.EVT_BUTTON, self.on_config )

        self.status = False
        self.camera = RandomCamera()
        
    def on_open(self, event):
        self.camera = devices[self.com_devices.GetValue()]()
        if not self.camera.view is None:
            dialog = ParaDialog(self, self.camera.name)
            view, para = self.camera.view, self.camera.para.copy()
            dialog.init_view(view, para, False)
            if dialog.show():
                self.camera.para.update(para)
                self.camera.open()
        else: self.camera.open()

    def on_stop(self, event):
        self.status = False

    def on_close(self, event):
        self.camera.close()
        self.camera = RandomCamera()

    def on_start(self, event):
        self.status = True
        td = Thread(target=self.hold, args=())
        td.setDaemon(True)
        td.start()

    def on_config(self, event):
        dialog = ParaDialog(self, self.camera.name)
        view, para = self.camera.view, self.camera.para.copy()
        dialog.init_view(view, para, False)
        if dialog.show(): self.camera.para.update(para)
        
    def hold(self):
        while self.status:
            img = self.camera.frame()
            if img.shape != self.canvas.image.img.shape:
                wx.CallAfter(self.canvas.set_img, img)
            else:
                ips = self.canvas.image
                cache = self.spn_cache.GetValue()
                ips.imgs = (ips.imgs + [img])[-cache:]
                ips.cur = len(ips.imgs) - 1
                wx.CallAfter(self.canvas.update)
        
    def __del__( self ):
        pass


import wx, numpy as np
from threading import Thread
from sciwx.canvas import MCanvas
from sciwx.widgets import ParaDialog

from .camera import RandomCamera, USBCamera

devices = {'Random Noise': RandomCamera, 'USB Camera': USBCamera}

class CameraPanel ( wx.Panel ):

    def __init__( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 600, 600 ), style = wx.TAB_TRAVERSAL, name = wx.EmptyString ):
        wx.Panel.__init__ ( self, parent, id = id, pos = pos, size = size, style = style, name = name )

        bSizer1 = wx.BoxSizer( wx.VERTICAL )

        self.tool_bar = wx.ToolBar( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TB_HORIZONTAL|wx.TB_HORZ_TEXT )
        self.m_staticText1 = wx.StaticText( self.tool_bar, wx.ID_ANY, u"Camera: ", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText1.Wrap( -1 )

        self.tool_bar.AddControl( self.m_staticText1 )
        self.com_devices = wx.ComboBox( self.tool_bar, wx.ID_ANY, u"Combo!", wx.DefaultPosition, wx.DefaultSize, [], 0 )
        self.tool_bar.AddControl( self.com_devices )
        self.tol_open = self.tool_bar.AddTool( wx.ID_ANY, u"Open", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tol_start = self.tool_bar.AddTool( wx.ID_ANY, u"Start", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tol_stop = self.tool_bar.AddTool( wx.ID_ANY, u"Stop", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tol_close = self.tool_bar.AddTool( wx.ID_ANY, u"Close", wx.Bitmap('./next.png'), wx.NullBitmap, wx.ITEM_NORMAL, wx.EmptyString, wx.EmptyString, None )

        self.tool_bar.AddSeparator()

        self.spn_cache = wx.SpinCtrl( self.tool_bar, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.SP_ARROW_KEYS, 1, 1024, 0 )
        self.tool_bar.AddControl( self.spn_cache )

        self.btn_config = wx.Button( self.tool_bar, wx.ID_ANY, u"Config", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.tool_bar.AddControl( self.btn_config )
        self.tool_bar.Realize()

        bSizer1.Add( self.tool_bar, 0, wx.EXPAND, 5 )

        self.canvas = MCanvas( self )
        bSizer1.Add( self.canvas, 1, wx.EXPAND |wx.ALL, 0 )
        self.canvas.set_img(np.zeros((512, 512), np.uint8))

        self.com_devices.SetItems(sorted(devices))
        self.com_devices.Select(0)

        self.SetSizer( bSizer1 )
        self.Layout()

        # Connect Events
        self.Bind( wx.EVT_TOOL, self.on_open, id = self.tol_open.GetId() )
        self.Bind( wx.EVT_TOOL, self.on_start, id = self.tol_start.GetId() )
        self.Bind( wx.EVT_TOOL, self.on_stop, id = self.tol_stop.GetId() )
        self.Bind( wx.EVT_TOOL, self.on_close, id = self.tol_close.GetId() )
        self.btn_config.Bind( wx.EVT_BUTTON, self.on_config )

        self.status = False
        self.camera = RandomCamera()
        
    def on_open(self, event):
        self.camera = devices[self.com_devices.GetValue()]()
        if not self.camera.view is None:
            dialog = ParaDialog(self, self.camera.name)
            view, para = self.camera.view, self.camera.para.copy()
            dialog.init_view(view, para, False)
            if dialog.show():
                self.camera.para.update(para)
                self.camera.open()
        else: self.camera.open()

    def on_stop(self, event):
        self.status = False

    def on_close(self, event):
        self.camera.close()
        self.camera = RandomCamera()

    def on_start(self, event):
        self.status = True
        td = Thread(target=self.hold, args=())
        td.setDaemon(True)
        td.start()

    def on_config(self, event):
        dialog = ParaDialog(self, self.camera.name)
        view, para = self.camera.view, self.camera.para.copy()
        dialog.init_view(view, para, False)
        if dialog.show(): self.camera.para.update(para)
        
    def hold(self):
        while self.status:
            img = self.camera.frame()
            if img.shape != self.canvas.image.img.shape:
                wx.CallAfter(self.canvas.set_img, img)
            else:
                ips = self.canvas.image
                cache = self.spn_cache.GetValue()
                ips.imgs = (ips.imgs + [img])[-cache:]
                ips.cur = len(ips.imgs) - 1
                wx.CallAfter(self.canvas.update)
        
    def __del__( self ):
        pass
# Connect Events