#================================================================
#================================================================
# API-CNT(WDM) for サンプリング機能搭載デバイス
# カウンタサンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import messagebox


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

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理
        #----------------------------------------
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.root, text='デバイス名 : ', anchor='nw').place(x=10, y=5, width=75, height=20)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        self.textbox_device_name_var= tk.StringVar(self.root)
        self.textbox_device_name_var.set("CNT000")
        tk.Entry(self.root, textvariable=self.textbox_device_name_var).place(x=100, y=5, width=100, height=20)
        #----------------------------------------
        # カウント値 / ステータスのLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_value_status = tk.LabelFrame(self.root, text="カウント値 / ステータス : ")
        self.groupbox_counter_value_status.place(x=10, y=30, width=280, height=110)
        #--------------------
        # CH0のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH0 : ', anchor='nw').place(x=10, y=5, width=50, height=20)
        #--------------------
        # CH0カウント値のEntryを作成
        #--------------------
        self.textbox_ch0_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch0_value_var.set('')
        self.textbox_ch0_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch0_value_var, justify=tk.RIGHT)
        self.textbox_ch0_value.place(x=70, y=5, width=80, height=20)
        #--------------------
        # CH0ステータスのEntryを作成
        #--------------------
        self.textbox_ch0_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch0_status_var.set('')
        self.textbox_ch0_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch0_status_var, justify=tk.RIGHT)
        self.textbox_ch0_status.place(x=170, y=5, width=80, height=20)
        #--------------------
        # CH1のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH1 : ', anchor='nw').place(x=10, y=25, width=71, height=20)
        #--------------------
        # CH1カウント値のEntryを作成
        #--------------------
        self.textbox_ch1_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch1_value_var.set('')
        self.textbox_ch1_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch1_value_var, justify=tk.RIGHT)
        self.textbox_ch1_value.place(x=70, y=25, width=80, height=20)
        #--------------------
        # CH1ステータスのEntryを作成
        #--------------------
        self.textbox_ch1_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch1_status_var.set('')
        self.textbox_ch1_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch1_status_var, justify=tk.RIGHT)
        self.textbox_ch1_status.place(x=170, y=25, width=80, height=20)
        #--------------------
        # CH2のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH2 : ', anchor='nw').place(x=10, y=45, width=50, height=20)
        #--------------------
        # CH2カウント値のEntryを作成
        #--------------------
        self.textbox_ch2_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch2_value_var.set('')
        self.textbox_ch2_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch2_value_var, justify=tk.RIGHT)
        self.textbox_ch2_value.place(x=70, y=45, width=80, height=20)
        #--------------------
        # CH2ステータスのEntryを作成
        #--------------------
        self.textbox_ch2_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch2_status_var.set('')
        self.textbox_ch2_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch2_status_var, justify=tk.RIGHT)
        self.textbox_ch2_status.place(x=170, y=45, width=80, height=20)
        #--------------------
        # CH3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_counter_value_status, text='CH3 : ', anchor='nw').place(x=10, y=65, width=50, height=20)
        #--------------------
        # CH3カウント値のEntryを作成
        #--------------------
        self.textbox_ch3_value_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch3_value_var.set('')
        self.textbox_ch3_value = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch3_value_var, justify=tk.RIGHT)
        self.textbox_ch3_value.place(x=70, y=65, width=80, height=20)
        #--------------------
        # CH3ステータスのEntryを作成
        #--------------------
        self.textbox_ch3_status_var = tk.StringVar(self.groupbox_counter_value_status)
        self.textbox_ch3_status_var.set('')
        self.textbox_ch3_status = tk.Entry(self.groupbox_counter_value_status, textvariable=self.textbox_ch3_status_var, justify=tk.RIGHT)
        self.textbox_ch3_status.place(x=170, y=65, width=80, height=20)
        #----------------------------------------
        # 通知のLabelFrameを作成
        #----------------------------------------
        self.groupbox_notification = tk.LabelFrame(self.root, text="通知 : ")
        self.groupbox_notification.place(x=10, y=145, width=280, height=110)
        #--------------------
        # 通知のLabelを作成
        #--------------------
        self.label_notification = tk.Label(self.groupbox_notification, text='Notification:', anchor='nw')
        self.label_notification.place(x=10, y=5, width=265, height=80)
        #----------------------------------------
        # 戻り値
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=10, y=260, width=80, height=20)
        #--------------------
        # 戻り値の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=260, width=185, height=20)
        #----------------------------------------
        # ボタン
        #----------------------------------------
        #--------------------
        # CntInitのButtonを作成
        #--------------------
        self.button_cnt_init = tk.Button(self.root, text='CntInit')
        self.button_cnt_init.place(x=305, y=15, width=100, height=20)
        #--------------------
        # モード設定のButtonを作成
        #--------------------
        self.button_cnt_mode = tk.Button(self.root, text='モード設定...')
        self.button_cnt_mode.place(x=305, y=42, width=100, height=20)
        #--------------------
        # 通知のButtonを作成
        #--------------------
        self.button_cnt_notify = tk.Button(self.root, text='通知...')
        self.button_cnt_notify.place(x=305, y=69, width=100, height=20)
        #--------------------
        # イベント設定のButtonを作成
        #--------------------
        self.button_cnt_event = tk.Button(self.root, text='イベント設定...')
        self.button_cnt_event.place(x=305, y=96, width=100, height=20)
        #--------------------
        # CntStartのButtonを作成
        #--------------------
        self.button_cnt_start = tk.Button(self.root, text='CntStart')
        self.button_cnt_start.place(x=305, y=123, width=100, height=20)
        #--------------------
        # CntPresetのButtonを作成
        #--------------------
        self.button_cnt_preset = tk.Button(self.root, text='CntPreset')
        self.button_cnt_preset.place(x=305, y=150, width=100, height=20)
        #--------------------
        # CntResetのButtonを作成
        #--------------------
        self.button_cnt_reset = tk.Button(self.root, text='CntReset')
        self.button_cnt_reset.place(x=305, y=177, width=100, height=20)
        #--------------------
        # CntStopのButtonを作成
        #--------------------
        self.button_cnt_stop = tk.Button(self.root, text='CntStop')
        self.button_cnt_stop.place(x=305, y=204, width=100, height=20)
        #--------------------
        # CntExitのButtonを作成
        #--------------------
        self.button_cnt_exit = tk.Button(self.root, text='CntExit')
        self.button_cnt_exit.place(x=305, y=231, width=100, height=20)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=305, y=260, width=100, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_cnt_init,
                       command_cnt_mode,
                       command_cnt_notify,
                       command_cnt_event,
                       command_cnt_start,
                       command_cnt_preset,
                       command_cnt_reset,
                       command_cnt_stop,
                       command_cnt_exit,
                       command_exit):
        #----------------------------------------
        # 「CntInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_init.configure(command=command_cnt_init)
        #----------------------------------------
        # 「モード設定...」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_mode.configure(command=command_cnt_mode)
        #----------------------------------------
        # 「通知...」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_notify.configure(command=command_cnt_notify)
        #----------------------------------------
        # 「イベント設定...」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_event.configure(command=command_cnt_event)
        #----------------------------------------
        # 「CntStartCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_start.configure(command=command_cnt_start)
        #----------------------------------------
        # 「CntPreset」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_preset.configure(command=command_cnt_preset)
        #----------------------------------------
        # 「CntReset」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_reset.configure(command=command_cnt_reset)
        #----------------------------------------
        # 「CntStopCount」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_stop.configure(command=command_cnt_stop)
        #----------------------------------------
        # 「CntExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_exit.configure(command=command_cnt_exit)
        #----------------------------------------
        # 「Exit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_exit.configure(command=command_exit)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.root)

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