﻿#================================================================
#================================================================
# API-AIO(WDM)
# Counter sample
# Counter setting UI file
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# Import modules
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# SettingWindowUI class
#========================================
class SettingWindowUI:
    #========================================
    # Initialization function
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # Set parent window
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # Create the settings window
        #----------------------------------------
        self.setting_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # Set modal dialog
        #----------------------------------------
        self.setting_window.grab_set()
        #----------------------------------------
        # Title
        #----------------------------------------
        self.setting_window.title('Counter Settings')
        self.setting_window.geometry("480x430")
        #----------------------------------------
        # Start creating the UI
        #----------------------------------------
        self._create_ui()

    #========================================
    # UI creation function
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # Create LabelFrame for Comparison Count Set
        #----------------------------------------
        self.groupbox_compare_setting = tk.LabelFrame(self.setting_window, text="  Comparison Count Set")
        self.groupbox_compare_setting.place(x=15, y=10, width=450, height=180)
        #--------------------
        # Create Checkbutton for Comparison Count Set
        #--------------------
        self.checkbutton_compare_load_var = tk.IntVar(self.setting_window)
        self.checkbutton_compare_load = tk.Checkbutton(self.setting_window, variable=self.checkbutton_compare_load_var)
        self.checkbutton_compare_load.place(x=18, y=7, width=16, height=19)
        #--------------------
        # Create Label for Number of Setting Data
        #--------------------
        tk.Label(self.groupbox_compare_setting, text='Number of Setting Data : ', anchor='nw').place(x=15, y=5, width=150, height=19)
        #--------------------
        # Create Combobox for Number of Setting Data
        #--------------------
        self.combobox_compare_data_num = ttk.Combobox(self.groupbox_compare_setting)
        self.combobox_compare_data_num.place(x=170, y=5, width=90, height=20)
        self.combobox_compare_data_num['value'] = ('1', '2', '3', '4', '5')
        #--------------------
        # Create Label for Repeat Setting
        #--------------------
        tk.Label(self.groupbox_compare_setting, text='Repeat Setting : ', anchor='nw').place(x=15, y=30, width=100, height=19)
        #--------------------
        # Create Combobox for Repeat Setting
        #--------------------
        self.combobox_compare_repeat = ttk.Combobox(self.groupbox_compare_setting)
        self.combobox_compare_repeat.place(x=170, y=30, width=90, height=20)
        self.combobox_compare_repeat['value'] = ('Not repeat', 'Repeat')
        #--------------------
        # Create LabelFrame for Comparison Count Data
        #--------------------
        self.groupbox_compare_value = tk.LabelFrame(self.groupbox_compare_setting, text="Comparison Count Data")
        self.groupbox_compare_value.place(x=275, y=5, width=160, height=150)
        #--------------------
        # Create Label for Comparison Count Data 0
        #--------------------
        tk.Label(self.groupbox_compare_value, text='0 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Comparison Count Data 0
        #--------------------
        self.textbox_compare_data0_var = tk.StringVar(self.groupbox_compare_value)
        self.textbox_compare_data0 = tk.Entry(self.groupbox_compare_value, textvariable=self.textbox_compare_data0_var, justify=tk.RIGHT)
        self.textbox_compare_data0.place(x=50, y=5, width=95, height=19)
        #--------------------
        # Create Label for Comparison Count Data 1
        #--------------------
        tk.Label(self.groupbox_compare_value, text='1 : ', anchor='nw').place(x=15, y=30, width=90, height=19)
        #--------------------
        # Create Entry for Comparison Count Data 1
        #--------------------
        self.textbox_compare_data1_var = tk.StringVar(self.groupbox_compare_value)
        self.textbox_compare_data1 = tk.Entry(self.groupbox_compare_value, textvariable=self.textbox_compare_data1_var, justify=tk.RIGHT)
        self.textbox_compare_data1.place(x=50, y=30, width=95, height=19)
        #--------------------
        # Create Label for Comparison Count Data 2
        #--------------------
        tk.Label(self.groupbox_compare_value, text='2 : ', anchor='nw').place(x=15, y=55, width=90, height=19)
        #--------------------
        # Create Entry for Comparison Count Data 2
        #--------------------
        self.textbox_compare_data2_var = tk.StringVar(self.groupbox_compare_value)
        self.textbox_compare_data2 = tk.Entry(self.groupbox_compare_value, textvariable=self.textbox_compare_data2_var, justify=tk.RIGHT)
        self.textbox_compare_data2.place(x=50, y=55, width=95, height=19)
        #--------------------
        # Create Label for Comparison Count Data 3
        #--------------------
        tk.Label(self.groupbox_compare_value, text='3 : ', anchor='nw').place(x=15, y=80, width=90, height=19)
        #--------------------
        # Create Entry for Comparison Count Data 3
        #--------------------
        self.textbox_compare_data3_var = tk.StringVar(self.groupbox_compare_value)
        self.textbox_compare_data3 = tk.Entry(self.groupbox_compare_value, textvariable=self.textbox_compare_data3_var, justify=tk.RIGHT)
        self.textbox_compare_data3.place(x=50, y=80, width=95, height=19)
        #--------------------
        # Create Label for Comparison Count Data 4
        #--------------------
        tk.Label(self.groupbox_compare_value, text='4 : ', anchor='nw').place(x=15, y=105, width=90, height=19)
        #--------------------
        # Create Entry for Comparison Count Data 4
        #--------------------
        self.textbox_compare_data4_var = tk.StringVar(self.groupbox_compare_value)
        self.textbox_compare_data4 = tk.Entry(self.groupbox_compare_value, textvariable=self.textbox_compare_data4_var, justify=tk.RIGHT)
        self.textbox_compare_data4.place(x=50, y=105, width=95, height=19)
        #----------------------------------------
        # Create LabelFrame for Comparison Match Preset Set
        #----------------------------------------
        self.groupbox_preset_setting = tk.LabelFrame(self.setting_window, text="  Comparison Match Preset Set")
        self.groupbox_preset_setting.place(x=15, y=195, width=450, height=180)
        #--------------------
        # Create Checkbutton for Comparison Match Preset Set
        #--------------------
        self.checkbutton_preset_load_var = tk.IntVar(self.setting_window)
        self.checkbutton_preset_load = tk.Checkbutton(self.setting_window, variable=self.checkbutton_preset_load_var)
        self.checkbutton_preset_load.place(x=18, y=192, width=16, height=19)
        #--------------------
        # Create Label for Number of Setting Data
        #--------------------
        tk.Label(self.groupbox_preset_setting, text='Number of Setting Data : ', anchor='nw').place(x=15, y=5, width=150, height=19)
        #--------------------
        # Create Combobox for Number of Setting Data
        #--------------------
        self.combobox_preset_data_num = ttk.Combobox(self.groupbox_preset_setting)
        self.combobox_preset_data_num.place(x=170, y=5, width=90, height=20)
        self.combobox_preset_data_num['value'] = ('1', '2', '3', '4', '5')
        #--------------------
        # Create Label for Repeat Setting
        #--------------------
        tk.Label(self.groupbox_preset_setting, text='Repeat Setting : ', anchor='nw').place(x=15, y=30, width=100, height=19)
        #--------------------
        # Create Combobox for Repeat Setting
        #--------------------
        self.combobox_preset_repeat = ttk.Combobox(self.groupbox_preset_setting)
        self.combobox_preset_repeat.place(x=170, y=30, width=90, height=20)
        self.combobox_preset_repeat['value'] = ('Not repeat', 'Repeat')
        #--------------------
        # Create LabelFrame for Preset Data
        #--------------------
        self.groupbox_preset_value = tk.LabelFrame(self.groupbox_preset_setting, text="Preset Data")
        self.groupbox_preset_value.place(x=275, y=5, width=160, height=150)
        #--------------------
        # Create Label for Preset Data 0
        #--------------------
        tk.Label(self.groupbox_preset_value, text='0 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # Create Entry for Preset Data 0
        #--------------------
        self.textbox_preset_data0_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_preset_data0 = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_preset_data0_var, justify=tk.RIGHT)
        self.textbox_preset_data0.place(x=50, y=5, width=95, height=19)
        #--------------------
        # Create Label for Preset Data 1
        #--------------------
        tk.Label(self.groupbox_preset_value, text='1 : ', anchor='nw').place(x=15, y=30, width=90, height=19)
        #--------------------
        # Create Entry for Preset Data 1
        #--------------------
        self.textbox_preset_data1_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_preset_data1 = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_preset_data1_var, justify=tk.RIGHT)
        self.textbox_preset_data1.place(x=50, y=30, width=95, height=19)
        #--------------------
        # Create Label for Preset Data 2
        #--------------------
        tk.Label(self.groupbox_preset_value, text='2 : ', anchor='nw').place(x=15, y=55, width=90, height=19)
        #--------------------
        # Create Entry for Preset Data 2
        #--------------------
        self.textbox_preset_data2_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_preset_data2 = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_preset_data2_var, justify=tk.RIGHT)
        self.textbox_preset_data2.place(x=50, y=55, width=95, height=19)
        #--------------------
        # Create Label for Preset Data 3
        #--------------------
        tk.Label(self.groupbox_preset_value, text='3 : ', anchor='nw').place(x=15, y=80, width=90, height=19)
        #--------------------
        # Create Entry for Preset Data 3
        #--------------------
        self.textbox_preset_data3_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_preset_data3 = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_preset_data3_var, justify=tk.RIGHT)
        self.textbox_preset_data3.place(x=50, y=80, width=95, height=19)
        #--------------------
        # Create Label for Preset Data 4
        #--------------------
        tk.Label(self.groupbox_preset_value, text='4 : ', anchor='nw').place(x=15, y=105, width=90, height=19)
        #--------------------
        # Create Entry for Preset Data 4
        #--------------------
        self.textbox_preset_data4_var = tk.StringVar(self.groupbox_preset_value)
        self.textbox_preset_data4 = tk.Entry(self.groupbox_preset_value, textvariable=self.textbox_preset_data4_var, justify=tk.RIGHT)
        self.textbox_preset_data4.place(x=50, y=105, width=95, height=19)
        #----------------------------------------
        # Exit
        #----------------------------------------
        #--------------------
        # Create Button for OK
        #--------------------
        self.button_ok = tk.Button(self.setting_window, text='OK')
        self.button_ok.place(x=230, y=390, width=110, height=19)
        #--------------------
        # Create Button for Cancel
        #--------------------
        self.button_cancel = tk.Button(self.setting_window, text='Cancel')
        self.button_cancel.place(x=350, y=390, width=110, height=19)

    #========================================
    # Function that sets the callback function for the UI controls
    #========================================
    def setup_commands(self,
                       command_compare_load,
                       command_compare_data_num,
                       command_preset_load,
                       command_preset_data_num,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # Set the callback function for the [Comparison Count Set] checkbutton
        #----------------------------------------
        self.checkbutton_compare_load.configure(command=command_compare_load)
        #----------------------------------------
        # Set the callback function for the Comparison Count [Number of Setting Data] button
        #----------------------------------------
        self.combobox_compare_data_num.bind('<<ComboboxSelected>>', command_compare_data_num)
        #----------------------------------------
        # Set the callback function for the [Comparison Match Preset Set] checkbutton
        #----------------------------------------
        self.checkbutton_preset_load.configure(command=command_preset_load)
        #----------------------------------------
        # Set the callback function for the Comparison Match Preset [Number of Setting Data] button
        #----------------------------------------
        self.combobox_preset_data_num.bind('<<ComboboxSelected>>', command_preset_data_num)
        #----------------------------------------
        # 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.setting_window)