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


#========================================
# EventWindowUI クラス
#========================================
class EventWindowUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # 親ウィンドウの設定
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # ハードウェアイベント設定ウィンドウの作成
        #----------------------------------------
        self.event_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # モーダルダイアログの設定
        #----------------------------------------
        self.event_window.grab_set()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.event_window.title("ハードウェアイベント設定")
        self.event_window.geometry("700x280")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # イベント信号入力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_input_event = tk.LabelFrame(self.event_window, text="イベント信号入力")
        self.groupbox_input_event.place(x=10, y=10, width=240, height=260)
        #--------------------
        # タイプのLabelを作成
        #--------------------
        tk.Label(self.groupbox_input_event, text='タイプ : ', anchor='nw').place(x=5, y=5, width=50, height=20)
        #--------------------
        # タイプのComboboxを作成
        #--------------------
        self.combobox_input_type = ttk.Combobox(self.groupbox_input_event)
        self.combobox_input_type.place(x=58, y=5, width=150, height=20)
        self.combobox_input_type['value'] = ('汎用入力', 'カウンタ 開始/停止', 'カウンタ プリセット', 'カウンタ クリア')
        self.combobox_input_type.current(0)
        self.combobox_input_type['state'] = 'readonly'
        #--------------------
        # RF0 (クリア/プリセット/開始)のLabelFrameを作成
        #--------------------
        self.groupbox_input_event_rf0 = tk.LabelFrame(self.groupbox_input_event, text="RF0 (クリア/プリセット/開始)")
        self.groupbox_input_event_rf0.place(x=5, y=30, width=227, height=80)
        #--------------------
        # RF0 (クリア/プリセット/開始)のComboboxを作成
        #--------------------
        self.combobox_input_event_rf0 = ttk.Combobox(self.groupbox_input_event_rf0)
        self.combobox_input_event_rf0.place(x=15, y=5, width=185, height=20)
        self.combobox_input_event_rf0['value'] = ('立ち上がり', '立ち下り', 'なし')
        self.combobox_input_event_rf0.current(2)
        self.combobox_input_event_rf0['state'] = 'readonly'
        #--------------------
        # プリセット レジスタのLabelを作成
        #--------------------
        tk.Label(self.groupbox_input_event_rf0, text='プリセット レジスタ : ', anchor='nw').place(x=5, y=30, width=130, height=20)
        #--------------------
        # プリセット レジスタのEntryを作成
        #--------------------
        self.textbox_input_preset_register_value_var = tk.StringVar(self.groupbox_input_event_rf0)
        self.textbox_input_preset_register_value_var.set('1000')
        self.textbox_input_preset_register_value = tk.Entry(self.groupbox_input_event_rf0, textvariable=self.textbox_input_preset_register_value_var, justify=tk.RIGHT)
        self.textbox_input_preset_register_value.place(x=140, y=30, width=55, height=20)
        #--------------------
        # HexのLabelを作成
        #--------------------
        tk.Label(self.groupbox_input_event_rf0, text='Hex', anchor='nw').place(x=195, y=30, width=25, height=20)
        #--------------------
        # RF1 (停止)のLabelFrameを作成
        #--------------------
        self.groupbox_input_event_rf1 = tk.LabelFrame(self.groupbox_input_event, text="RF1 (停止)")
        self.groupbox_input_event_rf1.place(x=5, y=120, width=227, height=50)
        #--------------------
        # RF1 (停止)のComboboxを作成
        #--------------------
        self.combobox_input_event_rf1 = ttk.Combobox(self.groupbox_input_event_rf1)
        self.combobox_input_event_rf1.place(x=15, y=5, width=185, height=20)
        self.combobox_input_event_rf1['value'] = ('立ち上がり', '立ち下り', 'なし')
        self.combobox_input_event_rf1.current(2)
        self.combobox_input_event_rf1['state'] = 'readonly'
        #----------------------------------------
        # イベント信号出力のLabelFrameを作成
        #----------------------------------------
        self.groupbox_output_event = tk.LabelFrame(self.event_window, text="イベント信号出力")
        self.groupbox_output_event.place(x=255, y=10, width=240, height=260)
        #--------------------
        # タイプのLabelFrameを作成
        #--------------------
        self.groupbox_output_event_type = tk.LabelFrame(self.groupbox_output_event, text="タイプ : ")
        self.groupbox_output_event_type.place(x=5, y=5, width=227, height=140)
        #--------------------
        # イベント信号出力タイプのRadiobuttonを作成
        #--------------------
        self.radiobutton_output_event_type_var = tk.IntVar()
        self.radiobutton_output_event_type_var.set(1)
        self.radiobutton_general_output = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="汎用出力", variable=self.radiobutton_output_event_type_var, value=1)
        self.radiobutton_general_output.place(x=5, y=0,height=20)
        self.radiobutton_count_match_reg0 = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="カウント一致 REG0", variable=self.radiobutton_output_event_type_var, value=2)
        self.radiobutton_count_match_reg0.place(x=5, y=20, height=20)
        self.radiobutton_count_match_reg1 = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="カウント一致 REG1", variable=self.radiobutton_output_event_type_var, value=3)
        self.radiobutton_count_match_reg1.place(x=5, y=40, height=20)
        self.radiobutton_abnormal_input_error = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="異常入力エラー", variable=self.radiobutton_output_event_type_var, value=4)
        self.radiobutton_abnormal_input_error.place(x=5, y=60, height=20)
        self.radiobutton_filter_error = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="デジタルフィルタエラー", variable=self.radiobutton_output_event_type_var, value=5)
        self.radiobutton_filter_error.place(x=5, y=80, height=20)
        self.radiobutton_alarm_error = tk.Radiobutton(self.groupbox_output_event_type, justify=tk.LEFT, text="アラームエラー", variable=self.radiobutton_output_event_type_var, value=6)
        self.radiobutton_alarm_error.place(x=5, y=100, height=20)
        #--------------------
        # パルス幅のLabelを作成
        #--------------------
        tk.Label(self.groupbox_output_event, text='パルス幅(係数) : ', anchor='nw').place(x=5, y=150, width=100, height=20)
        #--------------------
        # パルス幅のComboboxを作成
        #--------------------
        self.combobox_output_pulse_width = ttk.Combobox(self.groupbox_output_event)
        self.combobox_output_pulse_width.place(x=110, y=150, width=70, height=20)
        self.combobox_output_pulse_width['value'] = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
        self.combobox_output_pulse_width.current(0)
        self.combobox_output_pulse_width['state'] = 'readonly'
        #--------------------
        # HexのLabelを作成
        #--------------------
        tk.Label(self.groupbox_output_event, text='Hex', anchor='nw').place(x=180, y=150, width=25, height=20)
        #--------------------
        # 汎用出力のLabelFrameを作成
        #--------------------
        self.groupbox_general_output = tk.LabelFrame(self.groupbox_output_event, text="汎用出力")
        self.groupbox_general_output.place(x=5, y=175, width=227, height=65)
        #--------------------
        # チャネル番号のLabelを作成
        #--------------------
        tk.Label(self.groupbox_general_output, text='チャネル番号 : ', anchor='nw').place(x=5, y=0, height=20)
        #--------------------
        # チャネル番号のEntryを作成
        #--------------------
        self.textbox_output_preset_register_value_ch_var = tk.StringVar(self.groupbox_general_output)
        self.textbox_output_preset_register_value_ch_var.set('0')
        self.textbox_output_preset_register_value_ch = tk.Entry(self.groupbox_general_output, textvariable=self.textbox_output_preset_register_value_ch_var, justify=tk.RIGHT)
        self.textbox_output_preset_register_value_ch.place(x=95, y=0, width=40, height=20)
        #--------------------
        # データのLabelを作成
        #--------------------
        tk.Label(self.groupbox_general_output, text='データ : ', anchor='nw').place(x=5, y=25, height=20)
        #--------------------
        # 信号出力のEntryを作成
        #--------------------
        self.textbox_output_preset_register_value_data_var = tk.StringVar(self.groupbox_general_output)
        self.textbox_output_preset_register_value_data_var.set('1')
        self.textbox_output_preset_register_value_data = tk.Entry(self.groupbox_general_output, textvariable=self.textbox_output_preset_register_value_data_var, justify=tk.RIGHT)
        self.textbox_output_preset_register_value_data.place(x=95, y=25, width=40, height=20)
        #--------------------
        # 信号出力のButtonを作成
        #--------------------
        self.button_signal_output = tk.Button(self.groupbox_general_output, text='信号出力')
        self.button_signal_output.place(x=145, y=15, width=70, height=20)
        #----------------------------------------
        # カウント一致イベントのLabelFrameを作成
        #----------------------------------------
        self.groupbox_count_match_event = tk.LabelFrame(self.event_window, text="カウント一致イベント")
        self.groupbox_count_match_event.place(x=500, y=10, width=190, height=70)
        #--------------------
        # タイプのLabelを作成
        #--------------------
        tk.Label(self.groupbox_count_match_event, text='タイプ : ', anchor='nw').place(x=15, y=15, height=20)
        #--------------------
        # タイプのComboboxを作成
        #--------------------
        self.combobox_count_match_type = ttk.Combobox(self.groupbox_count_match_event)
        self.combobox_count_match_type.place(x=68, y=15, width=100, height=20)
        self.combobox_count_match_type['value'] = ('なし', 'プリセット', 'クリア')
        self.combobox_count_match_type.current(0)
        self.combobox_count_match_type['state'] = 'readonly'
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 全てのチャネルにおいて同一の設定となりますのLabelを作成
        #--------------------
        tk.Label(self.event_window, justify=tk.LEFT, text='全てのチャネルにおいて同一の\n設定となります。', anchor='nw').place(x=500, y=90, height=40)
        #--------------------
        # カウント一致レジスタの設定は通知設定 ダイアログで行いますのLabelを作成
        #--------------------
        tk.Label(self.event_window, justify=tk.LEFT, text='カウント一致レジスタの設定は\n通知設定 ダイアログで行います。', anchor='nw').place(x=500, y=150, height=40)
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_event_window_ok = tk.Button(self.event_window, text='OK')
        self.button_event_window_ok.place(x=520, y=250, width=70, height=20)
        #--------------------
        # キャンセルのButtonを作成
        #--------------------
        self.button_event_window_cancel = tk.Button(self.event_window, text='キャンセル')
        self.button_event_window_cancel.place(x=600, y=250, width=70, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_select_count_match_type,
                       command_select_input_type,
                       command_general_output,
                       command_count_match_reg0,
                       command_count_match_reg1,
                       command_abnormal_input_error,
                       command_filter_error,
                       command_alarm_error,
                       command_signal_output,
                       command_event_window_ok,
                       command_event_window_cancel):
        #----------------------------------------
        # カウントマッチタイプのコンボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_count_match_type.bind("<<ComboboxSelected>>", command_select_count_match_type)
        #----------------------------------------
        # 入力タイプのコンボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_input_type.bind("<<ComboboxSelected>>", command_select_input_type)
        #----------------------------------------
        # 「汎用出力」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_general_output.configure(command=command_general_output)
        #----------------------------------------
        # 「カウント一致 REG0」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_count_match_reg0.configure(command=command_count_match_reg0)
        #----------------------------------------
        # 「カウント一致 REG1」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_count_match_reg1.configure(command=command_count_match_reg1)
        #----------------------------------------
        # 「異常入力エラー」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_abnormal_input_error.configure(command=command_abnormal_input_error)
        #----------------------------------------
        # 「デジタルフィルタエラー」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_filter_error.configure(command=command_filter_error)
        #----------------------------------------
        # 「アラームエラー」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.radiobutton_alarm_error.configure(command=command_alarm_error)
        #----------------------------------------
        # 「信号出力」ラジオボタンのコールバック関数を設定
        #----------------------------------------
        self.button_signal_output.configure(command=command_signal_output)
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_event_window_ok.configure(command=command_event_window_ok)
        #----------------------------------------
        # 「キャンセル」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_event_window_cancel.configure(command=command_event_window_cancel)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.event_window)