# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Delta_Window.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
from concurrent import futures
from concurrent.futures import thread
from tkinter.constants import S
import cv2

from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import QFileDialog, QTableWidgetItem,QHeaderView,QLineEdit,QItemDelegate,QVBoxLayout,QTableWidget,QWidget
from PyQt5.QtGui import QPixmap, QImage, QColor,QPainter, QPen,QDoubleValidator
from PyQt5.QtCore import pyqtSignal, pyqtSlot,QRect, Qt,QObject,QThread
from matplotlib.colors import cnames
from PyQt5.QtCore import QSize, qVersion, PYQT_VERSION_STR
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    QPushButton,
    QStyleFactory,
    QLabel,
    QProxyStyle,
    QStyle,
    QCommonStyle,
)

#from numpy.core.shape_base import stack
from ui import Ui_MainWindow
import pandas as pd
import sys
import numpy as np
import json

import threading
import time
import concurrent.futures
from datetime import datetime
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import socket
from Camera_Manager import webcam
from WindowQt2CV import Display
from Algorithm import findPosition
class ProxyStyle(QProxyStyle):
    def drawControl(self, control, option, painter, widget):
        if control == QStyle.CE_PushButtonLabel:
            QCommonStyle.drawControl(self, control, option, painter, widget)
        else:
            super().drawControl(control, option, painter, widget)

class VideoThread(QThread):
    change_pixmap_signal = pyqtSignal(np.ndarray)

    def run(self):
        # capture from web cam
        cap = cv2.VideoCapture(0)
        while True:
            ret, cv_img = cap.read()
            if ret:
                self.change_pixmap_signal.emit(cv_img)


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    new_image_signal = pyqtSignal(np.ndarray)

 
    
   #region Setup
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self._display = Display(640,480)
        self.btn_open_camera.clicked.connect(self.On_webcam)
        self.btn_open_Inspec_Size.clicked.connect(self.On_Algorithms)
        self.btn_Stop_Inspec.clicked.connect(self.On_Stop_Algorithms)
        self.disply_width = 640
        self.display_height = 480
        self.Algorithm = False
    
       
        
    def On_webcam(self):
        # self._cam = webcam()
        # self._cam.change_pixmap_signal.connect(self.update_image)
        # self._cam.run()
        self._cam = webcam()
        # connect its signal to the update_image slot
        self._cam.change_pixmap_signal.connect(self.UpdateImg)
        # start the thread
        """can not use like this self._cam.run()"""
        self._cam.start()
        
    @pyqtSlot(np.ndarray)
    def UpdateImg(self,Image):
        Image = self.Algorithms(Image) 
        self._display.run(Image)
        self.Main_display.setPixmap(self._display.get_img())

    def On_Algorithms(self):
        self.Algorithm = True
        self.findobj = findPosition()
        self.btn_open_Inspec_Size.setEnabled(False)
    def Algorithms(self,Image):
        if self.Algorithm == True:
            Low = self.Spin_low.value()
            High = self.Spin_high.value()
            Image,Size = self.findobj._findPosition(Image,int(Low),int(High))
            self.label_3.setText("{:.2f}".format(Size))
            Max = self.Spin_size_Max.value()
            Min = self.Spin_size_min.value()
            if Size < Max and Size > Min:
                self.Status_size.setEnabled(False)
            else:
                self.Status_size.setEnabled(True)
        return Image

    def On_Stop_Algorithms(self):
        self.Algorithm = False
        self.btn_open_Inspec_Size.setEnabled(True)



  
    
        
     



if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setStyle(QStyleFactory.create("fusion"))
    proxy = ProxyStyle(app.style())
    app.setStyle(proxy)
    test = MainWindow()
    test.show()
    sys.exit(app.exec_())
    

