#================================================================
#================================================================
# API-DIO(WDM)
# パルス計測サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# PulseMeasurementUI クラス
#========================================
class PulseMeasurementUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('パルス計測')
        self.root.minsize(738,520)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        self.root.option_add("*Font", ("MS Gothic", 9))
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_init = tk.LabelFrame(self.root, text="<初期化処理>")
        self.groupbox_init.place(x=15, y=10, width=713, height=52)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 :', anchor='nw').place(x=5, y=5, width=75, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.groupbox_init)
        self.textbox_device_name_var.set("DIO000")
        tk.Entry(self.groupbox_init, textvariable=self.textbox_device_name_var).place(x=85, y=5, width=90, height=19)
        #--------------------
        # DioInitのButtonを作成
        #--------------------
        self.button_dio_init = tk.Button(self.groupbox_init, text='DioInit')
        self.button_dio_init.place(x=510, y=5, width=180, height=24)
        #----------------------------------------
        # 共通設定値のLabelFrameを作成
        #----------------------------------------
        self.groupbox_common_settings = tk.LabelFrame(self.root, text="<共通設定値>")
        self.groupbox_common_settings.place(x=15, y=65, width=713, height=52)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_common_settings, text='チャネル数 :', anchor='nw').place(x=5, y=5, width=75, height=19)
        #--------------------
        # チャネル数のComboboxを作成
        #--------------------
        self.combobox_channel_num = ttk.Combobox(self.groupbox_common_settings)
        self.combobox_channel_num.place(x=85, y=5, width=90, height=19)
        self.combobox_channel_num['value'] = ('1', '2', '3', '4')
        self.combobox_channel_num.current(3)
        self.combobox_channel_num['state'] = 'readonly'
        #----------------------------------------
        # パルス計測設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_pulse_measurement_settings = tk.LabelFrame(self.root, text="<パルス計測設定>")
        self.groupbox_pulse_measurement_settings.place(x=15, y=120, width=713, height=87)
        #--------------------
        # Ch0～3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 0', anchor='nw').place(x=115, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 1', anchor='nw').place(x=205, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 2', anchor='nw').place(x=295, y=-2, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_settings, text='Ch 3', anchor='nw').place(x=385, y=-2, width=30, height=19)
        #--------------------
        # パルス計測論理のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_settings, text='計測時間 :', anchor='nw').place(x=5, y=17, width=75, height=19)
        #--------------------
        # Ch0～3パルス計測論理のComboboxを作成
        #--------------------
        self.combobox_ch0_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch0_pulse_measurement_logic.place(x=85, y=17, width=80, height=19)
        self.combobox_ch0_pulse_measurement_logic['value'] = ('OFF時間', 'ON時間')
        self.combobox_ch0_pulse_measurement_logic.current(0)
        self.combobox_ch0_pulse_measurement_logic['state'] = 'readonly'
        self.combobox_ch1_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch1_pulse_measurement_logic.place(x=175, y=17, width=80, height=19)
        self.combobox_ch1_pulse_measurement_logic['value'] = ('OFF時間', 'ON時間')
        self.combobox_ch1_pulse_measurement_logic.current(0)
        self.combobox_ch1_pulse_measurement_logic['state'] = 'readonly'
        self.combobox_ch2_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch2_pulse_measurement_logic.place(x=265, y=17, width=80, height=19)
        self.combobox_ch2_pulse_measurement_logic['value'] = ('OFF時間', 'ON時間')
        self.combobox_ch2_pulse_measurement_logic.current(0)
        self.combobox_ch2_pulse_measurement_logic['state'] = 'readonly'
        self.combobox_ch3_pulse_measurement_logic = ttk.Combobox(self.groupbox_pulse_measurement_settings)
        self.combobox_ch3_pulse_measurement_logic.place(x=355, y=17, width=80, height=19)
        self.combobox_ch3_pulse_measurement_logic['value'] = ('OFF時間', 'ON時間')
        self.combobox_ch3_pulse_measurement_logic.current(0)
        self.combobox_ch3_pulse_measurement_logic['state'] = 'readonly'
        #--------------------
        # パルス計測周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_settings, text='計測周期 :', anchor='nw').place(x=5, y=42, width=75, height=19)
        #--------------------
        # Ch0～3パルス計測周期のEntryを作成
        #--------------------
        self.textbox_ch0_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch0_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch0_pulse_measurement_period_var, justify=tk.RIGHT).place(x=85, y=42, width=80, height=19)
        self.textbox_ch1_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch1_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch1_pulse_measurement_period_var, justify=tk.RIGHT).place(x=175, y=42, width=80, height=19)
        self.textbox_ch2_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch2_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch2_pulse_measurement_period_var, justify=tk.RIGHT).place(x=265, y=42, width=80, height=19)
        self.textbox_ch3_pulse_measurement_period_var = tk.StringVar(self.groupbox_pulse_measurement_settings)
        self.textbox_ch3_pulse_measurement_period_var.set('0')
        tk.Entry(self.groupbox_pulse_measurement_settings, textvariable=self.textbox_ch3_pulse_measurement_period_var, justify=tk.RIGHT).place(x=355, y=42, width=80, height=19)
        #--------------------
        # DioSetPulseMeasurementSettingのButtonを作成
        #--------------------
        self.button_dio_set_pulse_measurement_setting = tk.Button(self.groupbox_pulse_measurement_settings, text='DioSetPulseMeasurementSetting')
        self.button_dio_set_pulse_measurement_setting.place(x=510, y=22, width=180, height=24)
        #----------------------------------------
        # パルス計測動作のLabelFrameを作成
        #----------------------------------------
        self.groupbox_pulse_measurement_operation = tk.LabelFrame(self.root, text="<パルス計測動作>")
        self.groupbox_pulse_measurement_operation.place(x=15, y=210, width=713, height=172)
        #--------------------
        # Ch0～3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 0', anchor='nw').place(x=115, y=8, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 1', anchor='nw').place(x=205, y=8, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 2', anchor='nw').place(x=295, y=8, width=30, height=19)
        tk.Label(self.groupbox_pulse_measurement_operation, text='Ch 3', anchor='nw').place(x=385, y=8, width=30, height=19)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='ステータス :', anchor='nw').place(x=5, y=27, width=75, height=19)
        #--------------------
        # Ch0～3ステータスのEntryを作成
        #--------------------
        self.textbox_ch0_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch0_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch0_pulse_measurement_status_var, justify=tk.RIGHT).place(x=85, y=27, width=80, height=19)
        self.textbox_ch1_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch1_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch1_pulse_measurement_status_var, justify=tk.RIGHT).place(x=175, y=27, width=80, height=19)
        self.textbox_ch2_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch2_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch2_pulse_measurement_status_var, justify=tk.RIGHT).place(x=265, y=27, width=80, height=19)
        self.textbox_ch3_pulse_measurement_status_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch3_pulse_measurement_status_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch3_pulse_measurement_status_var, justify=tk.RIGHT).place(x=355, y=27, width=80, height=19)
        #--------------------
        # [Hex]のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='[Hex]', anchor='nw').place(x=450, y=27, width=85, height=19)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='エラー :', anchor='nw').place(x=5, y=52, width=75, height=19)
        #--------------------
        # Ch0～3エラーのEntryを作成
        #--------------------
        self.textbox_ch0_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch0_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch0_pulse_measurement_error_var, justify=tk.RIGHT).place(x=85, y=52, width=80, height=19)
        self.textbox_ch1_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch1_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch1_pulse_measurement_error_var, justify=tk.RIGHT).place(x=175, y=52, width=80, height=19)
        self.textbox_ch2_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch2_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch2_pulse_measurement_error_var, justify=tk.RIGHT).place(x=265, y=52, width=80, height=19)
        self.textbox_ch3_pulse_measurement_error_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch3_pulse_measurement_error_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch3_pulse_measurement_error_var, justify=tk.RIGHT).place(x=355, y=52, width=80, height=19)
        #--------------------
        # [Hex]のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='[Hex]', anchor='nw').place(x=450, y=52, width=85, height=19)
        #--------------------
        # パルス計測時間のLabelを作成
        #--------------------
        tk.Label(self.groupbox_pulse_measurement_operation, text='計測時間 :', anchor='nw').place(x=5, y=77, width=75, height=19)
        #--------------------
        # Ch0～3パルス計測時間のEntryを作成
        #--------------------
        self.textbox_ch0_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch0_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch0_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=85, y=77, width=80, height=19)
        self.textbox_ch1_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch1_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch1_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=175, y=77, width=80, height=19)
        self.textbox_ch2_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch2_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch2_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=265, y=77, width=80, height=19)
        self.textbox_ch3_pulse_measurement_time_value_var = tk.StringVar(self.groupbox_pulse_measurement_operation)
        self.textbox_ch3_pulse_measurement_time_value_var.set('')
        tk.Entry(self.groupbox_pulse_measurement_operation, textvariable=self.textbox_ch3_pulse_measurement_time_value_var, justify=tk.RIGHT).place(x=355, y=77, width=80, height=19)
        #--------------------
        # Ch0～3パルス計測時間単位のComboboxを作成
        #--------------------
        self.combobox_ch0_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch0_pulse_measurement_time_unit.place(x=85, y=102, width=80, height=19)
        self.combobox_ch0_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch0_pulse_measurement_time_unit.current(3)
        self.combobox_ch0_pulse_measurement_time_unit['state'] = 'readonly'
        self.combobox_ch1_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch1_pulse_measurement_time_unit.place(x=175, y=102, width=80, height=19)
        self.combobox_ch1_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch1_pulse_measurement_time_unit.current(3)
        self.combobox_ch1_pulse_measurement_time_unit['state'] = 'readonly'
        self.combobox_ch2_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch2_pulse_measurement_time_unit.place(x=265, y=102, width=80, height=19)
        self.combobox_ch2_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch2_pulse_measurement_time_unit.current(3)
        self.combobox_ch2_pulse_measurement_time_unit['state'] = 'readonly'
        self.combobox_ch3_pulse_measurement_time_unit = ttk.Combobox(self.groupbox_pulse_measurement_operation)
        self.combobox_ch3_pulse_measurement_time_unit.place(x=355, y=102, width=80, height=19)
        self.combobox_ch3_pulse_measurement_time_unit['value'] = ('sec', 'msec', 'usec', 'nsec')
        self.combobox_ch3_pulse_measurement_time_unit.current(3)
        self.combobox_ch3_pulse_measurement_time_unit['state'] = 'readonly'
        #--------------------
        # DioStartPulseMeasurementのButtonを作成
        #--------------------
        self.button_dio_start_pulse_measurement = tk.Button(self.groupbox_pulse_measurement_operation, text='DioStartPulseMeasurement')
        self.button_dio_start_pulse_measurement.place(x=510, y=5, width=180, height=24)
        #--------------------
        # DioReadPulseMeasurementDataのButtonを作成
        #--------------------
        self.button_dio_read_pulse_measurement_data = tk.Button(self.groupbox_pulse_measurement_operation, text='DioReadPulseMeasurementData')
        self.button_dio_read_pulse_measurement_data.place(x=510, y=65, width=180, height=24)
        #--------------------
        # DioStopPulseMeasurementのButtonを作成
        #--------------------
        self.button_dio_stop_pulse_measurement = tk.Button(self.groupbox_pulse_measurement_operation, text='DioStopPulseMeasurement')
        self.button_dio_stop_pulse_measurement.place(x=510, y=125, width=180, height=24)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="<終了処理>")
        self.groupbox_exit.place(x=15, y=385, width=713, height=52)
        #--------------------
        # DioExitのButtonを作成
        #--------------------
        self.button_dio_exit = tk.Button(self.groupbox_exit, text='DioExit')
        self.button_dio_exit.place(x=510, y=5, width=180, height=24)
        #----------------------------------------
        # 戻り値のLabelを作成
        #----------------------------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=22, y=445, width=75, height=19)
        #----------------------------------------
        # 戻り値のEntryを作成
        #----------------------------------------
        self.textbox_return_string_var = tk.StringVar(self.root)
        self.textbox_return_string_var.set('')
        tk.Entry(self.root, textvariable=self.textbox_return_string_var).place(x=105, y=445, width=350, height=19)
        #----------------------------------------
        # 閉じるのButtonを作成
        #----------------------------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=527, y=470, width=180, height=24)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_dio_init,
                       command_dio_set_pulse_measurement_setting,
                       command_dio_start_pulse_measurement,
                       command_dio_read_pulse_measurement_data,
                       command_dio_stop_pulse_measurement,
                       command_dio_exit,
                       command_close):
        #----------------------------------------
        # 「DioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_init.configure(command=command_dio_init)
        #----------------------------------------
        # 「DioSetPulseMeasurementSetting」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_set_pulse_measurement_setting.configure(command=command_dio_set_pulse_measurement_setting)
        #----------------------------------------
        # 「DioStartPulseMeasurement」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_start_pulse_measurement.configure(command=command_dio_start_pulse_measurement)
        #----------------------------------------
        # 「DioReadPulseMeasurementData」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_read_pulse_measurement_data.configure(command=command_dio_read_pulse_measurement_data)
        #----------------------------------------
        # 「DioStopPulseMeasurement」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_stop_pulse_measurement.configure(command=command_dio_stop_pulse_measurement)
        #----------------------------------------
        # 「DioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_dio_exit.configure(command=command_dio_exit)
        #----------------------------------------
        # 「閉じる」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_close.configure(command=command_close)

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

    #========================================
    # メインイベントループを実行
    #========================================
    def run(self):
        self.root.mainloop()