﻿#================================================================
#================================================================
# API-AIO(WDM)
# カウンタサンプル
# 設定UIファイル
#                                                 CONTEC Co.,Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# SettingWindowUI クラス
#========================================
class SettingWindowUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # 親ウィンドウの設定
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # 設定ウィンドウの作成
        #----------------------------------------
        self.setting_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # モーダルダイアログの設定
        #----------------------------------------
        self.setting_window.grab_set()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.setting_window.title('カウンタ設定')
        self.setting_window.geometry("480x430")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 比較カウント設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_compare_setting = tk.LabelFrame(self.setting_window, text="  比較カウント設定")
        self.groupbox_compare_setting.place(x=15, y=10, width=450, height=180)
        #--------------------
        # 比較カウント設定のCheckbuttonを作成
        #--------------------
        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)
        #--------------------
        # 設定データ数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_setting, text='設定データ数 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 設定データ数のComboboxを作成
        #--------------------
        self.combobox_compare_data_num = ttk.Combobox(self.groupbox_compare_setting)
        self.combobox_compare_data_num.place(x=115, y=5, width=120, height=20)
        self.combobox_compare_data_num['value'] = ('1', '2', '3', '4', '5')
        #--------------------
        # 繰り返し設定のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_setting, text='繰り返し設定 : ', anchor='nw').place(x=15, y=30, width=90, height=19)
        #--------------------
        # 繰り返し設定のComboboxを作成
        #--------------------
        self.combobox_compare_repeat = ttk.Combobox(self.groupbox_compare_setting)
        self.combobox_compare_repeat.place(x=115, y=30, width=120, height=20)
        self.combobox_compare_repeat['value'] = ('繰り返し設定なし', '繰り返し設定あり')
        #--------------------
        # 比較カウント値のLabelFrameを作成
        #--------------------
        self.groupbox_compare_value = tk.LabelFrame(self.groupbox_compare_setting, text="比較カウント値")
        self.groupbox_compare_value.place(x=275, y=5, width=160, height=150)
        #--------------------
        # 比較カウント値0のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_value, text='0 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 比較カウント値0のEntryを作成
        #--------------------
        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)
        #--------------------
        # 比較カウント値1のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_value, text='1 : ', anchor='nw').place(x=15, y=30, width=90, height=19)
        #--------------------
        # 比較カウント値1のEntryを作成
        #--------------------
        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)
        #--------------------
        # 比較カウント値2のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_value, text='2 : ', anchor='nw').place(x=15, y=55, width=90, height=19)
        #--------------------
        # 比較カウント値2のEntryを作成
        #--------------------
        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)
        #--------------------
        # 比較カウント値3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_value, text='3 : ', anchor='nw').place(x=15, y=80, width=90, height=19)
        #--------------------
        # 比較カウント値3のEntryを作成
        #--------------------
        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)
        #--------------------
        # 比較カウント値4のLabelを作成
        #--------------------
        tk.Label(self.groupbox_compare_value, text='4 : ', anchor='nw').place(x=15, y=105, width=90, height=19)
        #--------------------
        # 比較カウント値4のEntryを作成
        #--------------------
        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)
        #----------------------------------------
        # 比較一致プリセット設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_preset_setting = tk.LabelFrame(self.setting_window, text="  比較一致プリセット設定")
        self.groupbox_preset_setting.place(x=15, y=195, width=450, height=180)
        #--------------------
        # 比較一致プリセット設定のCheckbuttonを作成
        #--------------------
        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)
        #--------------------
        # 設定データ数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_setting, text='設定データ数 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 設定データ数のComboboxを作成
        #--------------------
        self.combobox_preset_data_num = ttk.Combobox(self.groupbox_preset_setting)
        self.combobox_preset_data_num.place(x=115, y=5, width=120, height=20)
        self.combobox_preset_data_num['value'] = ('1', '2', '3', '4', '5')
        #--------------------
        # 繰り返し設定のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_setting, text='繰り返し設定 : ', anchor='nw').place(x=15, y=30, width=90, height=19)
        #--------------------
        # 繰り返し設定のComboboxを作成
        #--------------------
        self.combobox_preset_repeat = ttk.Combobox(self.groupbox_preset_setting)
        self.combobox_preset_repeat.place(x=115, y=30, width=120, height=20)
        self.combobox_preset_repeat['value'] = ('繰り返し設定なし', '繰り返し設定あり')
        #--------------------
        # プリセット値のLabelFrameを作成
        #--------------------
        self.groupbox_preset_value = tk.LabelFrame(self.groupbox_preset_setting, text="プリセット値")
        self.groupbox_preset_value.place(x=275, y=5, width=160, height=150)
        #--------------------
        # プリセット値0のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_value, text='0 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # プリセット値0のEntryを作成
        #--------------------
        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)
        #--------------------
        # プリセット値1のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_value, text='1 : ', anchor='nw').place(x=15, y=30, width=90, height=19)
        #--------------------
        # プリセット値1のEntryを作成
        #--------------------
        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)
        #--------------------
        # プリセット値2のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_value, text='2 : ', anchor='nw').place(x=15, y=55, width=90, height=19)
        #--------------------
        # プリセット値2のEntryを作成
        #--------------------
        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)
        #--------------------
        # プリセット値3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_value, text='3 : ', anchor='nw').place(x=15, y=80, width=90, height=19)
        #--------------------
        # プリセット値3のEntryを作成
        #--------------------
        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)
        #--------------------
        # プリセット値4のLabelを作成
        #--------------------
        tk.Label(self.groupbox_preset_value, text='4 : ', anchor='nw').place(x=15, y=105, width=90, height=19)
        #--------------------
        # プリセット値4のEntryを作成
        #--------------------
        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)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_ok = tk.Button(self.setting_window, text='OK')
        self.button_ok.place(x=230, y=390, width=110, height=19)
        #--------------------
        # CancelのButtonを作成
        #--------------------
        self.button_cancel = tk.Button(self.setting_window, text='Cancel')
        self.button_cancel.place(x=350, y=390, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_compare_load,
                       command_compare_data_num,
                       command_preset_load,
                       command_preset_data_num,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # 「比較カウント設定」チェックボタンのコールバック関数を設定
        #----------------------------------------
        self.checkbutton_compare_load.configure(command=command_compare_load)
        #----------------------------------------
        # 比較カウント「設定データ数」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_compare_data_num.bind('<<ComboboxSelected>>', command_compare_data_num)
        #----------------------------------------
        # 「比較一致プリセット設定」チェックボタンのコールバック関数を設定
        #----------------------------------------
        self.checkbutton_preset_load.configure(command=command_preset_load)
        #----------------------------------------
        # 比較一致プリセット「設定データ数」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_preset_data_num.bind('<<ComboboxSelected>>', command_preset_data_num)
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_ok.configure(command=command_ok)
        #----------------------------------------
        # 「Cancel」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cancel.configure(command=command_cancel)

    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.setting_window)