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


#========================================
# InfiniteUI クラス
#========================================
class InfiniteUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('無限サンプリング')
        self.root.minsize(480,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=15, y=20, 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=110, y=20, width=100, height=20)
        #----------------------------------------
        # 転送カウント、ステータス、エラー、戻り値
        #----------------------------------------
        #--------------------
        # 転送カウントのLabelを作成
        #--------------------
        tk.Label(self.root, text='転送カウント : ', anchor='nw').place(x=15, y=170, width=90, height=20)
        #--------------------
        # 転送カウントのEntryを作成
        #--------------------
        self.textbox_transfer_count_var= tk.StringVar(self.root)
        self.textbox_transfer_count_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_transfer_count_var, justify=tk.RIGHT).place(x=110, y=170, width=230, height=20)
        #--------------------
        # ステータスのLabelを作成
        #--------------------
        tk.Label(self.root, text='ステータス : ', anchor='nw').place(x=15, y=200, width=80, height=20)
        #--------------------
        # ステータスのEntryを作成
        #--------------------
        self.textbox_status_var= tk.StringVar(self.root)
        self.textbox_status_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_status_var).place(x=110, y=200, width=230, height=20)
        #--------------------
        # エラーのLabelを作成
        #--------------------
        tk.Label(self.root, text='エラー : ', anchor='nw').place(x=15, y=230, width=80, height=20)
        #--------------------
        # エラーのEntryを作成
        #--------------------
        self.textbox_error_var= tk.StringVar(self.root)
        self.textbox_error_var.set("")
        tk.Entry(self.root, textvariable=self.textbox_error_var).place(x=110, y=230, width=230, height=20)
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=15, 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=110, y=260, width=230, height=20)
        #----------------------------------------
        # ボタン
        #----------------------------------------
        #--------------------
        # CntInitのButtonを作成
        #--------------------
        self.button_cnt_init = tk.Button(self.root, text='CntInit')
        self.button_cnt_init.place(x=360, y=15, width=100, height=20)
        #--------------------
        # モード設定のButtonを作成
        #--------------------
        self.button_cnt_mode = tk.Button(self.root, text='モード設定...')
        self.button_cnt_mode.place(x=360, y=42, width=100, height=20)
        #--------------------
        # 条件設定のButtonを作成
        #--------------------
        self.button_cnt_condition = tk.Button(self.root, text='条件設定...')
        self.button_cnt_condition.place(x=360, y=69, width=100, height=20)
        #--------------------
        # 転送スタートのButtonを作成
        #--------------------
        self.button_cnt_start = tk.Button(self.root, text='転送スタート')
        self.button_cnt_start.place(x=360, y=96, width=100, height=20)
        #--------------------
        # 転送ストップのButtonを作成
        #--------------------
        self.button_cnt_stop = tk.Button(self.root, text='転送ストップ')
        self.button_cnt_stop.place(x=360, y=123, width=100, height=20)
        #--------------------
        # 取得データのButtonを作成
        #--------------------
        self.button_cnt_data = tk.Button(self.root, text='取得データ...')
        self.button_cnt_data.place(x=360, y=150, width=100, height=20)
        #--------------------
        # 転送カウントのButtonを作成
        #--------------------
        self.button_cnt_count = tk.Button(self.root, text='転送カウント')
        self.button_cnt_count.place(x=360, y=177, width=100, height=20)
        #--------------------
        # 転送ステータスのButtonを作成
        #--------------------
        self.button_cnt_status = tk.Button(self.root, text='転送ステータス')
        self.button_cnt_status.place(x=360, y=204, width=100, height=20)
        #--------------------
        # CntExitのButtonを作成
        #--------------------
        self.button_cnt_exit = tk.Button(self.root, text='CntExit')
        self.button_cnt_exit.place(x=360, y=231, width=100, height=20)
        #--------------------
        # ExitのButtonを作成
        #--------------------
        self.button_exit = tk.Button(self.root, text='Exit')
        self.button_exit.place(x=360, y=258, width=100, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_cnt_init,
                       command_cnt_mode,
                       command_cnt_condition,
                       command_cnt_start,
                       command_cnt_stop,
                       command_cnt_data,
                       command_cnt_count,
                       command_cnt_status,
                       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_condition.configure(command=command_cnt_condition)
        #----------------------------------------
        # 「転送スタート」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_start.configure(command=command_cnt_start)
        #----------------------------------------
        # 「転送ストップ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_stop.configure(command=command_cnt_stop)
        #----------------------------------------
        # 「取得データ...」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_data.configure(command=command_cnt_data)
        #----------------------------------------
        # 「転送カウント」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_count.configure(command=command_cnt_count)
        #----------------------------------------
        # 「転送ステータス」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cnt_status.configure(command=command_cnt_status)
        #----------------------------------------
        # 「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()