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


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

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 初期化処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_device_name = tk.LabelFrame(self.root, text="デバイス名")
        self.groupbox_device_name.place(x=10, y=10, width=390, height=40)
        #--------------------
        # マスターのLabelを作成
        #--------------------
        tk.Label(self.groupbox_device_name, text='マスター : ', anchor='nw').place(x=10, y=0, width=70, height=20)
        #--------------------
        # マスターのEntryを作成
        #--------------------
        self.textbox_master_device_name_var= tk.StringVar(self.root)
        self.textbox_master_device_name_var.set("CNT000")
        tk.Entry(self.groupbox_device_name, textvariable=self.textbox_master_device_name_var).place(x=80, y=0, width=100, height=20)
        #--------------------
        # スレーブのLabelを作成
        #--------------------
        tk.Label(self.groupbox_device_name, text='スレーブ : ', anchor='nw').place(x=200, y=0, width=70, height=20)
        #--------------------
        # スレーブのEntryを作成
        #--------------------
        self.textbox_slave_device_name_var= tk.StringVar(self.root)
        self.textbox_slave_device_name_var.set("CNT001")
        tk.Entry(self.groupbox_device_name, textvariable=self.textbox_slave_device_name_var).place(x=270, y=0, width=100, height=20)
        #----------------------------------------
        # マスターのLabelFrameを作成
        #----------------------------------------
        self.groupbox_master = tk.LabelFrame(self.root, text="マスター")
        self.groupbox_master.place(x=10, y=50, width=390, height=130)
        #--------------------
        # カウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master, text='カウント : ', anchor='nw').place(x=10, y=0, width=80, height=20)
        #--------------------
        # カウントのEntryを作成
        #--------------------
        self.textbox_master_transfer_count_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_transfer_count_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_transfer_count_var, justify=tk.RIGHT).place(x=100, y=0, width=270, height=20)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master, text='ステータス : ', anchor='nw').place(x=10, y=30, width=80, height=20)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_master_status_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_status_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_status_var).place(x=100, y=30, width=270, height=20)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master, text='エラー : ', anchor='nw').place(x=10, y=60, width=80, height=20)
        #--------------------
        # エラーのEntryを作成
        #--------------------
        self.textbox_master_error_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_error_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_error_var).place(x=100, y=60, width=270, height=20)
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master, text='戻り値 : ', anchor='nw').place(x=10, y=90, width=80, height=20)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_master_return_string_var= tk.StringVar(self.groupbox_master)
        self.textbox_master_return_string_var.set("")
        tk.Entry(self.groupbox_master, textvariable=self.textbox_master_return_string_var).place(x=100, y=90, width=270, height=20)
        #----------------------------------------
        # スレーブのLabelFrameを作成
        #----------------------------------------
        self.groupbox_slave = tk.LabelFrame(self.root, text="スレーブ")
        self.groupbox_slave.place(x=10, y=180, width=390, height=130)
        #--------------------
        # カウントのLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave, text='カウント : ', anchor='nw').place(x=10, y=0, width=80, height=20)
        #--------------------
        # カウントのEntryを作成
        #--------------------
        self.textbox_slave_transfer_count_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_transfer_count_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_transfer_count_var, justify=tk.RIGHT).place(x=100, y=0, width=270, height=20)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave, text='ステータス : ', anchor='nw').place(x=10, y=30, width=80, height=20)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_slave_status_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_status_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_status_var).place(x=100, y=30, width=270, height=20)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave, text='エラー : ', anchor='nw').place(x=10, y=60, width=80, height=20)
        #--------------------
        # エラーのEntryを作成
        #--------------------
        self.textbox_slave_error_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_error_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_error_var).place(x=100, y=60, width=270, height=20)
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave, text='戻り値 : ', anchor='nw').place(x=10, y=90, width=80, height=20)
        #--------------------
        # 戻り値のEntryを作成
        #--------------------
        self.textbox_slave_return_string_var= tk.StringVar(self.groupbox_slave)
        self.textbox_slave_return_string_var.set("")
        tk.Entry(self.groupbox_slave, textvariable=self.textbox_slave_return_string_var).place(x=100, y=90, width=270, height=20)
        #----------------------------------------
        # マスターボタンのLabelFrameを作成
        #----------------------------------------
        self.groupbox_master_button = tk.LabelFrame(self.root, text="マスター")
        self.groupbox_master_button.place(x=410, y=10, width=145, height=300)
        #--------------------
        # CntInitのButtonを作成
        #--------------------
        self.button_m_cnt_init = tk.Button(self.groupbox_master_button, text='CntInit')
        self.button_m_cnt_init.place(x=5, y=10, width=130, height=20)
        #--------------------
        # モードのButtonを作成
        #--------------------
        self.button_m_cnt_mode = tk.Button(self.groupbox_master_button, text='モード...')
        self.button_m_cnt_mode.place(x=5, y=40, width=130, height=20)
        #--------------------
        # サンプリングモードのButtonを作成
        #--------------------
        self.button_m_cnt_condition = tk.Button(self.groupbox_master_button, text='サンプリングモード...')
        self.button_m_cnt_condition.place(x=5, y=70, width=130, height=20)
        #--------------------
        # 転送スタートのButtonを作成
        #--------------------
        self.button_m_cnt_start = tk.Button(self.groupbox_master_button, text='転送スタート')
        self.button_m_cnt_start.place(x=5, y=100, width=130, height=20)
        #--------------------
        # 転送ストップのButtonを作成
        #--------------------
        self.button_m_cnt_stop = tk.Button(self.groupbox_master_button, text='転送ストップ')
        self.button_m_cnt_stop.place(x=5, y=130, width=130, height=20)
        #--------------------
        # 取得データのButtonを作成
        #--------------------
        self.button_m_cnt_data = tk.Button(self.groupbox_master_button, text='取得データ...')
        self.button_m_cnt_data.place(x=5, y=160, width=130, height=20)
        #--------------------
        # 転送カウントのButtonを作成
        #--------------------
        self.button_m_cnt_count = tk.Button(self.groupbox_master_button, text='転送カウント')
        self.button_m_cnt_count.place(x=5, y=190, width=130, height=20)
        #--------------------
        # 転送ステータスのButtonを作成
        #--------------------
        self.button_m_cnt_status = tk.Button(self.groupbox_master_button, text='転送ステータス')
        self.button_m_cnt_status.place(x=5, y=220, width=130, height=20)
        #--------------------
        # CntExitのButtonを作成
        #--------------------
        self.button_m_cnt_exit = tk.Button(self.groupbox_master_button, text='CntExit')
        self.button_m_cnt_exit.place(x=5, y=250, width=130, height=20)
        #----------------------------------------
        # スレーブボタンのLabelFrameを作成
        #----------------------------------------
        self.groupbox_slave_button = tk.LabelFrame(self.root, text="スレーブ")
        self.groupbox_slave_button.place(x=565, y=10, width=145, height=300)
        #--------------------
        # CntInitのButtonを作成
        #--------------------
        self.button_s_cnt_init = tk.Button(self.groupbox_slave_button, text='CntInit')
        self.button_s_cnt_init.place(x=5, y=10, width=130, height=20)
        #--------------------
        # モードのButtonを作成
        #--------------------
        self.button_s_cnt_mode = tk.Button(self.groupbox_slave_button, text='モード...')
        self.button_s_cnt_mode.place(x=5, y=40, width=130, height=20)
        #--------------------
        # サンプリングモードのButtonを作成
        #--------------------
        self.button_s_cnt_condition = tk.Button(self.groupbox_slave_button, text='サンプリングモード...')
        self.button_s_cnt_condition.place(x=5, y=70, width=130, height=20)
        #--------------------
        # 転送スタートのButtonを作成
        #--------------------
        self.button_s_cnt_start = tk.Button(self.groupbox_slave_button, text='転送スタート')
        self.button_s_cnt_start.place(x=5, y=100, width=130, height=20)
        #--------------------
        # 転送ストップのButtonを作成
        #--------------------
        self.button_s_cnt_stop = tk.Button(self.groupbox_slave_button, text='転送ストップ')
        self.button_s_cnt_stop.place(x=5, y=130, width=130, height=20)
        #--------------------
        # 取得データのButtonを作成
        #--------------------
        self.button_s_cnt_data = tk.Button(self.groupbox_slave_button, text='取得データ...')
        self.button_s_cnt_data.place(x=5, y=160, width=130, height=20)
        #--------------------
        # 転送カウントのButtonを作成
        #--------------------
        self.button_s_cnt_count = tk.Button(self.groupbox_slave_button, text='転送カウント')
        self.button_s_cnt_count.place(x=5, y=190, width=130, height=20)
        #--------------------
        # 転送ステータスのButtonを作成
        #--------------------
        self.button_s_cnt_status = tk.Button(self.groupbox_slave_button, text='転送ステータス')
        self.button_s_cnt_status.place(x=5, y=220, width=130, height=20)
        #--------------------
        # CntExitのButtonを作成
        #--------------------
        self.button_s_cnt_exit = tk.Button(self.groupbox_slave_button, text='CntExit')
        self.button_s_cnt_exit.place(x=5, y=250, width=130, height=20)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=490, y=320, width=130, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_m_cnt_init,
                       command_m_cnt_mode,
                       command_m_cnt_condition,
                       command_m_cnt_start,
                       command_m_cnt_stop,
                       command_m_cnt_data,
                       command_m_cnt_count,
                       command_m_cnt_status,
                       command_m_cnt_exit,
                       command_s_cnt_init,
                       command_s_cnt_mode,
                       command_s_cnt_condition,
                       command_s_cnt_start,
                       command_s_cnt_stop,
                       command_s_cnt_data,
                       command_s_cnt_count,
                       command_s_cnt_status,
                       command_s_cnt_exit,
                       command_exit):
        #----------------------------------------
        # 「CntInit」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_init.configure(command=command_m_cnt_init)
        #----------------------------------------
        # 「モード設定...」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_mode.configure(command=command_m_cnt_mode)
        #----------------------------------------
        # 「条件設定...」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_condition.configure(command=command_m_cnt_condition)
        #----------------------------------------
        # 「転送スタート」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_start.configure(command=command_m_cnt_start)
        #----------------------------------------
        # 「転送ストップ」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_stop.configure(command=command_m_cnt_stop)
        #----------------------------------------
        # 「取得データ...」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_data.configure(command=command_m_cnt_data)
        #----------------------------------------
        # 「転送カウント」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_count.configure(command=command_m_cnt_count)
        #----------------------------------------
        # 「転送ステータス」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_status.configure(command=command_m_cnt_status)
        #----------------------------------------
        # 「CntExit」マスターボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_cnt_exit.configure(command=command_m_cnt_exit)
        #----------------------------------------
        # 「CntInit」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_init.configure(command=command_s_cnt_init)
        #----------------------------------------
        # 「モード設定...」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_mode.configure(command=command_s_cnt_mode)
        #----------------------------------------
        # 「条件設定...」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_condition.configure(command=command_s_cnt_condition)
        #----------------------------------------
        # 「転送スタート」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_start.configure(command=command_s_cnt_start)
        #----------------------------------------
        # 「転送ストップ」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_stop.configure(command=command_s_cnt_stop)
        #----------------------------------------
        # 「取得データ...」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_data.configure(command=command_s_cnt_data)
        #----------------------------------------
        # 「転送カウント」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_count.configure(command=command_s_cnt_count)
        #----------------------------------------
        # 「転送ステータス」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_status.configure(command=command_s_cnt_status)
        #----------------------------------------
        # 「CntExit」スレーブボタンのコールバック関数を設定
        #----------------------------------------
        self.button_s_cnt_exit.configure(command=command_s_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()