﻿#================================================================
#================================================================
# API-AIO(WDM)
# Infinite generating using user buffer sample
# Condition setting UI file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# ConditionWindowUI class
#========================================
class ConditionWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set the parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the condition setting window
        #----------------------------------------
        self.condition_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set the modal dialog
        #----------------------------------------
        self.condition_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.condition_window.title('Generating Condition')
        self.condition_window.geometry("810x270")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Start Condition
        #----------------------------------------
        self.groupbox_start_condition = tk.LabelFrame(self.condition_window, text="Start Condition")
        self.groupbox_start_condition.place(x=15, y=10, width=780, height=50)
        #--------------------
        # Create Label for Start Condition
        #--------------------
        tk.Label(self.groupbox_start_condition, text='Start Condition : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Combobox for Start Condition
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_start_condition)
        self.combobox_start_condition.place(x=120, y=5, width=200, height=19)
        self.combobox_start_condition['value'] = ('Software', 'External trigger rising edge', 'External trigger falling edge')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #----------------------------------------
        # Create LabelFrame for Clock Condition
        #----------------------------------------
        self.groupbox_clock_condition = tk.LabelFrame(self.condition_window, text="Clock Condition")
        self.groupbox_clock_condition.place(x=15, y=65, width=780, height=100)
        #--------------------
        # Create Label for Clock Type
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='Clock Type : ', anchor='nw').place(x=5, y=5, width=90, height=19)
        #--------------------
        # Create Combobox for Clock Type
        #--------------------
        self.combobox_clock_type = ttk.Combobox(self.groupbox_clock_condition)
        self.combobox_clock_type.place(x=120, y=5, width=200, height=19)
        self.combobox_clock_type['value'] = ('Internal clock', 'External clock')
        self.combobox_clock_type.current(0)
        self.combobox_clock_type['state'] = 'readonly'
        #--------------------
        # Create Label for Generating Clock Period
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='Generating Clock Period : ', anchor='nw').place(x=330, y=5, width=200, height=19)
        #--------------------
        # Create Entry for Generating Clock Period
        #--------------------
        self.textbox_generating_clock_var = tk.StringVar(self.groupbox_clock_condition)
        self.textbox_generating_clock_var.set("1000")
        self.textbox_generating_clock = tk.Entry(self.groupbox_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_generating_clock_var)
        self.textbox_generating_clock.place(x=530, y=5, width=190, height=19)
        #--------------------
        # Create Label for Generating Clock Period Unit
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='[usec]', anchor='nw').place(x=720, y=5, width=40, height=19)
        #--------------------
        # Create Label for Timing of External Clock Input
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='Timing of External Clock Input : ', anchor='nw').place(x=330, y=30, width=200, height=19)
        #--------------------
        # Create Combobox for Timing of External Clock Input
        #--------------------
        self.combobox_clock_edge = ttk.Combobox(self.groupbox_clock_condition)
        self.combobox_clock_edge.place(x=530, y=30, width=190, height=19)
        self.combobox_clock_edge['value'] = ('External input falling edge', 'External input rising edge')
        self.combobox_clock_edge.current(0)
        self.combobox_clock_edge['state'] = 'readonly'
        #--------------------
        # Create Label for Transfer Size
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='Transfer Size : ', anchor='nw').place(x=330, y=55, width=200, height=19)
        #--------------------
        # Create Entry for Transfer Size
        #--------------------
        self.textbox_transfer_size_var = tk.StringVar(self.groupbox_clock_condition)
        self.textbox_transfer_size_var.set("0")
        self.textbox_transfer_size = tk.Entry(self.groupbox_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_transfer_size_var)
        self.textbox_transfer_size.place(x=530, y=55, width=190, height=19)
        #--------------------
        # Create Label for Transfer Size Unit
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='[packet]', anchor='nw').place(x=720, y=55, width=50, height=19)
        #----------------------------------------
        # Create LabelFrame for Stop Condition
        #----------------------------------------
        self.groupbox_stop_condition = tk.LabelFrame(self.condition_window, text="Stop Condition")
        self.groupbox_stop_condition.place(x=15, y=170, width=780, height=50)
        #--------------------
        # Create Label for Stop Condition
        #--------------------
        tk.Label(self.groupbox_stop_condition, text='Stop Condition : ', anchor='nw').place(x=5, y=5, width=100, height=19)
        #--------------------
        # Create Combobox for Stop Condition
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_stop_condition)
        self.combobox_stop_condition.place(x=120, y=5, width=200, height=19)
        self.combobox_stop_condition['value'] = ('External trigger rising edge', 'External trigger falling edge')
        self.combobox_stop_condition.current(0)
        self.combobox_stop_condition['state'] = 'readonly'
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_ok = tk.Button(self.condition_window, text='OK')
        self.button_ok.place(x=580, y=235, width=80, height=19)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_cancel = tk.Button(self.condition_window, text='Cancel')
        self.button_cancel.place(x=680, y=235, width=80, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_clock_type,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # Set the callback function for the [Clock Type] button
        #----------------------------------------
        self.combobox_clock_type.bind('<<ComboboxSelected>>', command_clock_type)
        #----------------------------------------
        # Set the callback function for the [OK] button
        #----------------------------------------
        self.button_ok.configure(command=command_ok)
        #----------------------------------------
        # Set the callback function for the [Cancel] button
        #----------------------------------------
        self.button_cancel.configure(command=command_cancel)

    #========================================
    # Display the error in a message box
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)