#================================================================
#================================================================
# API-AIO(WDM)
# アナログ簡易出力サンプル
# main UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# AoSimpleUI クラス
#========================================
class AoSimpleUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self):
        #----------------------------------------
        # メイン画面の作成開始
        #----------------------------------------
        self.root = tk.Tk()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.root.title('アナログ簡易出力')
        self.root.minsize(740,470)
        #----------------------------------------
        # コントロールのフォントを設定
        #----------------------------------------
        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=710, height=50)
        #--------------------
        # デバイス名のLabelを作成
        #--------------------
        tk.Label(self.groupbox_init, text='デバイス名 : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # デバイス名のEntryを作成
        #--------------------
        self.textbox_device_name_var = tk.StringVar(self.groupbox_init)
        self.textbox_device_name_var.set("AIO000")
        tk.Entry(self.groupbox_init, textvariable=self.textbox_device_name_var).place(x=125, y=5, width=130, height=19)
        #--------------------
        # AioInitのButtonを作成
        #--------------------
        self.button_aio_init = tk.Button(self.groupbox_init, text='AioInit')
        self.button_aio_init.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # レンジ設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_range_settings = tk.LabelFrame(self.root, text="レンジ設定")
        self.groupbox_range_settings.place(x=15, y=65, width=710, height=50)
        #--------------------
        # レンジのLabelを作成
        #--------------------
        tk.Label(self.groupbox_range_settings, text='レンジ : ', anchor='nw').place(x=15, y=5, width=75, height=19)
        #--------------------
        # レンジのComboboxを作成
        #--------------------
        self.combobox_range = ttk.Combobox(self.groupbox_range_settings)
        self.combobox_range.place(x=125, y=5, width=130, height=19)
        self.combobox_range['value'] = ('-10 - +10V', '-5 - +5V', '-2.5 - +2.5V', '-1.25 - +1.25V', '0 - +10V', '0 - +5V', '0 - +2.5V', '0 - +1.25V', '0 - 20mA', '4 - 20mA', '1 - +5V')
        self.combobox_range.current(0)
        self.combobox_range['state'] = 'readonly'
        #--------------------
        # AioSetAoRangeAllのButtonを作成
        #--------------------
        self.button_aio_set_ao_range_all = tk.Button(self.groupbox_range_settings, text='AioSetAoRangeAll')
        self.button_aio_set_ao_range_all.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 指定チャネル簡易出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_single_ao_ex = tk.LabelFrame(self.root, text="指定チャネル簡易出力")
        self.groupbox_single_ao_ex.place(x=15, y=125, width=710, height=50)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_single_ao_ex, text='チャネル番号 : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_channel_no_var = tk.StringVar(self.groupbox_single_ao_ex)
        self.textbox_channel_no_var.set("0")
        tk.Entry(self.groupbox_single_ao_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_no_var).place(x=125, y=5, width=130, height=19)
        #--------------------
        # 出力データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_single_ao_ex, text='出力データ : ', anchor='nw').place(x=270, y=5, width=75, height=19)
        #--------------------
        # 出力データのEntryを作成
        #--------------------
        self.textbox_single_ao_data_var = tk.StringVar(self.groupbox_single_ao_ex)
        self.textbox_single_ao_data_var.set("0.0")
        tk.Entry(self.groupbox_single_ao_ex, justify=tk.RIGHT, textvariable=self.textbox_single_ao_data_var).place(x=355, y=5, width=130, height=19)
        #--------------------
        # AioSingleAoExのButtonを作成
        #--------------------
        self.button_aio_single_ao_ex = tk.Button(self.groupbox_single_ao_ex, text='AioSingleAoEx')
        self.button_aio_single_ao_ex.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 複数チャネル簡易出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_multi_ao_ex = tk.LabelFrame(self.root, text="複数チャネル簡易出力")
        self.groupbox_multi_ao_ex.place(x=15, y=180, width=710, height=150)
        #--------------------
        # チャネル数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex, text='チャネル数 : ', anchor='nw').place(x=15, y=10, width=85, height=19)
        #--------------------
        # チャネル数のEntryを作成
        #--------------------
        self.textbox_channel_num_var = tk.StringVar(self.groupbox_multi_ao_ex)
        self.textbox_channel_num_var.set("1")
        tk.Entry(self.groupbox_multi_ao_ex, justify=tk.RIGHT, textvariable=self.textbox_channel_num_var).place(x=125, y=10, width=130, height=19)
        #--------------------
        # 出力データのLabelFrameを作成
        #--------------------
        self.groupbox_multi_ao_ex_data = tk.LabelFrame(self.groupbox_multi_ao_ex, text="出力データ")
        self.groupbox_multi_ao_ex_data.place(x=265, y=0, width=240, height=125)
        #--------------------
        # CH0データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH0 : ', anchor='nw').place(x=15, y=5, width=85, height=19)
        #--------------------
        # CH1データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH1 : ', anchor='nw').place(x=15, y=30, width=85, height=19)
        #--------------------
        # CH2データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH2 : ', anchor='nw').place(x=15, y=55, width=85, height=19)
        #--------------------
        # CH3データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_multi_ao_ex_data, text='CH3 : ', anchor='nw').place(x=15, y=80, width=85, height=19)
        #--------------------
        # CH0データのEntryを作成
        #--------------------
        self.textbox_multi_ao_ex_data1_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data1_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data1_var).place(x=88, y=5, width=130, height=19)
        #--------------------
        # CH1データのEntryを作成
        #--------------------
        self.textbox_multi_ao_ex_data2_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data2_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data2_var).place(x=88, y=30, width=130, height=19)
        #--------------------
        # CH2データのEntryを作成
        #--------------------
        self.textbox_multi_ao_ex_data3_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data3_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data3_var).place(x=88, y=55, width=130, height=19)
        #--------------------
        # CH3データのEntryを作成
        #--------------------
        self.textbox_multi_ao_ex_data4_var = tk.StringVar(self.groupbox_multi_ao_ex_data)
        self.textbox_multi_ao_ex_data4_var.set("0.0")
        tk.Entry(self.groupbox_multi_ao_ex_data, justify=tk.RIGHT, textvariable=self.textbox_multi_ao_ex_data4_var).place(x=88, y=80, width=130, height=19)
        #--------------------
        # AioMultiAoExのButtonを作成
        #--------------------
        self.button_aio_multi_ao_ex = tk.Button(self.groupbox_multi_ao_ex, text='AioMultiAoEx')
        self.button_aio_multi_ao_ex.place(x=550, y=10, width=150, height=19)
        #----------------------------------------
        # 終了処理のLabelFrameを作成
        #----------------------------------------
        self.groupbox_exit = tk.LabelFrame(self.root, text="終了処理")
        self.groupbox_exit.place(x=15, y=335, width=710, height=50)
        #--------------------
        # AioExitのButtonを作成
        #--------------------
        self.button_aio_exit = tk.Button(self.groupbox_exit, text='AioExit')
        self.button_aio_exit.place(x=550, y=5, width=150, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 戻り値のLabelを作成
        #--------------------
        tk.Label(self.root, text='戻り値 : ', anchor='nw').place(x=33, y=400, width=160, 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=142, y=400, width=330, height=19)
        #--------------------
        # 閉じるのButtonを作成
        #--------------------
        self.button_close = tk.Button(self.root, text='閉じる')
        self.button_close.place(x=567, y=425, width=150, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_aio_init,
                       command_aio_set_ao_range_all,
                       command_aio_single_ao_ex,
                       command_aio_multi_ao_ex,
                       command_aio_exit,
                       command_close):
        #----------------------------------------
        # 「AioInit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_init.configure(command=command_aio_init)
        #----------------------------------------
        # 「AioSetAoRangeAll」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_set_ao_range_all.configure(command=command_aio_set_ao_range_all)
        #----------------------------------------
        # 「AioSingleAoEx」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_single_ao_ex.configure(command=command_aio_single_ao_ex)
        #----------------------------------------
        # 「AioMultiAoEx」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_multi_ao_ex.configure(command=command_aio_multi_ao_ex)
        #----------------------------------------
        # 「AioExit」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_aio_exit.configure(command=command_aio_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()