﻿#================================================================
#================================================================
# API-DIO(WDM) (BusMaster)
# Generating 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('Set Generating Conditions')
        self.condition_window.geometry("480x290")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Generation Conditions
        #----------------------------------------
        self.groupbox_generating_condition = tk.LabelFrame(self.condition_window, text="Generation Conditions")
        self.groupbox_generating_condition.place(x=15, y=15, width=450, height=120)
        #--------------------
        # Create Label for Start Condition
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='Start Condition : ', anchor='nw').place(x=5, y=10, width=120, height=19)
        #--------------------
        # Create Combobox for Start Condition
        #--------------------
        self.combobox_start_trg = ttk.Combobox(self.groupbox_generating_condition)
        self.combobox_start_trg.place(x=115, y=10, width=110,  height=20)
        self.combobox_start_trg['value'] = ('Software Start', 'External UP', 'External Down')
        self.combobox_start_trg['state'] = 'readonly'
        #--------------------
        # Create Label for Clock Condition
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='Clock Condition : ', anchor='nw').place(x=5, y=40, width=120, height=19)
        #--------------------
        # Create Combobox for Clock Condition
        #--------------------
        self.combobox_clock_trg = ttk.Combobox(self.groupbox_generating_condition)
        self.combobox_clock_trg.place(x=115, y=40, width=110,  height=20)
        self.combobox_clock_trg['value'] = ('Internal Clock', 'External Clock', 'Hand Shake')
        self.combobox_clock_trg['state'] = 'readonly'
        #--------------------
        # Create Label for Internal Clock
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='Internal Clock : ', anchor='nw').place(x=240, y=40, width=120, height=19)
        #--------------------
        # Create Entry for Internal Clock
        #--------------------
        self.textbox_internal_clock_var = tk.StringVar(self.groupbox_generating_condition)
        self.textbox_internal_clock = tk.Entry(self.groupbox_generating_condition, textvariable=self.textbox_internal_clock_var, justify=tk.RIGHT)
        self.textbox_internal_clock.place(x=350, y=40, width=60, height=19)
        #--------------------
        # Create Label for Internal Clock Unit
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='ns', anchor='nw').place(x=415, y=40, width=30, height=19)
        #--------------------
        # Create Label for Stop Condition
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='Stop Condition : ', anchor='nw').place(x=5, y=70, width=120, height=19)
        #--------------------
        # Create Combobox for Stop Condition
        #--------------------
        self.combobox_stop_trg = ttk.Combobox(self.groupbox_generating_condition)
        self.combobox_stop_trg.place(x=115, y=70, width=110,  height=20)
        self.combobox_stop_trg['value'] = ('Software Stop', 'External UP', 'External Down', 'Number')
        self.combobox_stop_trg['state'] = 'readonly'
        #--------------------
        # Create Label for Stop Number
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='Stop Number : ', anchor='nw').place(x=240, y=70, width=120, height=19)
        #--------------------
        # Create Entry for Stop Number
        #--------------------
        self.textbox_stop_num_var = tk.StringVar(self.groupbox_generating_condition)
        self.textbox_stop_num = tk.Entry(self.groupbox_generating_condition, textvariable=self.textbox_stop_num_var, justify=tk.RIGHT)
        self.textbox_stop_num.place(x=350, y=70, width=60, height=19)
        #----------------------------------------
        # Create LabelFrame for Set Buffer Condition
        #----------------------------------------
        self.groupbox_buf_condition = tk.LabelFrame(self.condition_window, text="Set Buffer Condition")
        self.groupbox_buf_condition.place(x=15, y=140, width=450, height=55)
        #--------------------
        # Create Label for Buffer Write
        #--------------------
        tk.Label(self.groupbox_buf_condition, text='Buffer Write : ', anchor='nw').place(x=5, y=10, width=120, height=19)
        #--------------------
        # Create Radiobutton for Buffer Write
        #--------------------
        self.radio_is_ring_var = tk.IntVar()
        tk.Radiobutton(self.groupbox_buf_condition, text='Write Once', variable=self.radio_is_ring_var, value=0).place(x=115, y=10, width=90, height=19)
        tk.Radiobutton(self.groupbox_buf_condition, text='Write Ring', variable=self.radio_is_ring_var, value=1).place(x=200, y=10, width=90, height=19)
        #----------------------------------------
        # Create LabelFrame for Notification
        #----------------------------------------
        self.groupbox_num_event = tk.LabelFrame(self.condition_window, text="Notification")
        self.groupbox_num_event.place(x=15, y=200, width=450, height=55)
        #--------------------
        # Create Label for Number of Data
        #--------------------
        tk.Label(self.groupbox_num_event, text='Number of Data : ', anchor='nw').place(x=5, y=10, width=120, height=19)
        #--------------------
        # Create Entry for Number of Data
        #--------------------
        self.textbox_num_event_var = tk.StringVar(self.groupbox_num_event)
        tk.Entry(self.groupbox_num_event, textvariable=self.textbox_num_event_var, justify=tk.RIGHT).place(x=115, y=10, width=110, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_ok = tk.Button(self.condition_window, text='OK')
        self.button_ok.place(x=230, y=260, width=110, height=19)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_cancel = tk.Button(self.condition_window, text='Cancel')
        self.button_cancel.place(x=350, y=260, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_clock_trg,
                       command_stop_trg,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # Set the callback function for the [Clock Condition] combobox
        #----------------------------------------
        self.combobox_clock_trg.bind('<<ComboboxSelected>>', command_clock_trg)
        #----------------------------------------
        # Set the callback function for the [Stop Condition] combobox
        #----------------------------------------
        self.combobox_stop_trg.bind('<<ComboboxSelected>>', command_stop_trg)
        #----------------------------------------
        # 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)