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


#========================================
# MSamplingModeWindowUI クラス
#========================================
class MSamplingModeWindowUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # 親ウィンドウの設定
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # マスターサンプリング条件設定ウィンドウの作成
        #----------------------------------------
        self.m_sampling_mode_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # モーダルダイアログの設定
        #----------------------------------------
        self.m_sampling_mode_window.grab_set()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.m_sampling_mode_window.title("マスターサンプリング条件設定")
        self.m_sampling_mode_window.geometry("650x300")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # サンプリング条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_master_sampling_mode = tk.LabelFrame(self.m_sampling_mode_window, text="マスターサンプリング条件設定")
        self.groupbox_master_sampling_mode.place(x=10, y=10, width=630, height=180)
        #--------------------
        # スタート条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='スタート条件 : ', anchor='nw').place(x=5, y=5, width=150, height=20)
        #--------------------
        # ->ExtSig2のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='->ExtSig2', anchor='nw').place(x=365, y=5, width=80, height=20)
        #--------------------
        # スタート条件のComboboxを作成
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_start_condition.place(x=160, y=5, width=200, height=20)
        self.combobox_start_condition['value'] = ('ソフトウェアスタート', '外部信号立ち上がり', '外部信号立ち下がり', 
                                                  'カウント一致CH0 比較レジスタ0', 'カウント一致CH1 比較レジスタ0', 
                                                  'カウント一致CH2 比較レジスタ0', 'カウント一致CH3 比較レジスタ0', 
                                                  'カウント一致CH4 比較レジスタ0', 'カウント一致CH5 比較レジスタ0', 
                                                  'カウント一致CH6 比較レジスタ0', 'カウント一致CH7 比較レジスタ0', 
                                                  'カウント一致CH0 比較レジスタ1', 'カウント一致CH1 比較レジスタ1', 
                                                  'カウント一致CH2 比較レジスタ1', 'カウント一致CH3 比較レジスタ1', 
                                                  'カウント一致CH4 比較レジスタ1', 'カウント一致CH5 比較レジスタ1', 
                                                  'カウント一致CH6 比較レジスタ1', 'カウント一致CH7 比較レジスタ1')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # カウンタスタートモードのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='カウンタスタートモード : ', anchor='nw').place(x=5, y=35, width=150, height=20)
        #--------------------
        # カウンタスタートモードのComboboxを作成
        #--------------------
        self.combobox_start_with = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_start_with.place(x=160, y=35, width=200, height=20)
        self.combobox_start_with['value'] = ('サンプリングとは無関係に開始', 'サンプリングと同時に開始')
        self.combobox_start_with.current(0)
        self.combobox_start_with['state'] = 'readonly'
        #--------------------
        # クロック条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='クロック条件 : ', anchor='nw').place(x=5, y=65, width=150, height=20)
        #--------------------
        # ->ExtSig1のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='->ExtSig1', anchor='nw').place(x=365, y=65, width=80, height=20)
        #--------------------
        # クロック条件のComboboxを作成
        #--------------------
        self.combobox_clock_condition = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_clock_condition.place(x=160, y=65, width=200, height=20)
        self.combobox_clock_condition['value'] = ('内部クロック', '外部クロック')
        self.combobox_clock_condition.current(0)
        self.combobox_clock_condition['state'] = 'readonly'
        #--------------------
        # ストップ条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='ストップ条件 : ', anchor='nw').place(x=5, y=95, width=150, height=20)
        #--------------------
        # ->ExtSig3のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='->ExtSig3', anchor='nw').place(x=365, y=95, width=80, height=20)
        #--------------------
        # ストップ条件のComboboxを作成
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_stop_condition.place(x=160, y=95, width=200, height=20)
        self.combobox_stop_condition['value'] = ('ソフトウェアスタート', '外部信号立ち上がり', '外部信号立ち下がり', '指定個数転送完了', 
                                                 'カウント一致CH0 比較レジスタ0', 'カウント一致CH1 比較レジスタ0', 
                                                 'カウント一致CH2 比較レジスタ0', 'カウント一致CH3 比較レジスタ0', 
                                                 'カウント一致CH4 比較レジスタ0', 'カウント一致CH5 比較レジスタ0', 
                                                 'カウント一致CH6 比較レジスタ0', 'カウント一致CH7 比較レジスタ0', 
                                                 'カウント一致CH0 比較レジスタ1', 'カウント一致CH1 比較レジスタ1', 
                                                 'カウント一致CH2 比較レジスタ1', 'カウント一致CH3 比較レジスタ1', 
                                                 'カウント一致CH4 比較レジスタ1', 'カウント一致CH5 比較レジスタ1', 
                                                 'カウント一致CH6 比較レジスタ1', 'カウント一致CH7 比較レジスタ1')
        self.combobox_stop_condition.current(0)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # カウンタストップモードのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='カウンタストップモード : ', anchor='nw').place(x=5, y=125, width=150, height=20)
        #--------------------
        # カウンタストップモードのComboboxを作成
        #--------------------
        self.combobox_stop_with = ttk.Combobox(self.groupbox_master_sampling_mode)
        self.combobox_stop_with.place(x=160, y=125, width=200, height=20)
        self.combobox_stop_with['value'] = ('サンプリングとは無関係に停止', 'サンプリングと同時に停止')
        self.combobox_stop_with.current(0)
        self.combobox_stop_with['state'] = 'readonly'
        #--------------------
        # 比較値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='比較値 : ', anchor='nw').place(x=455, y=5, width=85, height=20)
        #--------------------
        # 比較値のEntryを作成
        #--------------------
        self.textbox_start_compare_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_start_compare_value_var.set('100')
        self.textbox_start_compare_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_start_compare_value_var, justify=tk.RIGHT)
        self.textbox_start_compare_value.place(x=545, y=5, width=55, height=20)
        #--------------------
        # HexのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Hex', anchor='nw').place(x=600, y=5, width=20, height=20)
        #--------------------
        # 内部クロックのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='内部クロック : ', anchor='nw').place(x=455, y=65, width=85, height=20)
        #--------------------
        # 内部クロックのEntryを作成
        #--------------------
        self.textbox_internal_clock_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_internal_clock_value_var.set('1000')
        self.textbox_internal_clock_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_internal_clock_value_var, justify=tk.RIGHT)
        self.textbox_internal_clock_value.place(x=545, y=65, width=55, height=20)
        #--------------------
        # nsのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='ns', anchor='nw').place(x=600, y=65, width=20, height=20)
        #--------------------
        # 比較値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='比較値 : ', anchor='nw').place(x=455, y=95, width=85, height=20)
        #--------------------
        # 比較値のEntryを作成
        #--------------------
        self.textbox_stop_compare_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_stop_compare_value_var.set('200')
        self.textbox_stop_compare_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_stop_compare_value_var, justify=tk.RIGHT)
        self.textbox_stop_compare_value.place(x=545, y=95, width=55, height=20)
        #--------------------
        # HexのLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='Hex', anchor='nw').place(x=600, y=95, width=20, height=20)
        #--------------------
        # 停止個数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_master_sampling_mode, text='停止個数 : ', anchor='nw').place(x=455, y=125, width=85, height=20)
        #--------------------
        # 停止個数のEntryを作成
        #--------------------
        self.textbox_stop_number_value_var = tk.StringVar(self.groupbox_master_sampling_mode)
        self.textbox_stop_number_value_var.set('1000')
        self.textbox_stop_number_value = tk.Entry(self.groupbox_master_sampling_mode, textvariable=self.textbox_stop_number_value_var, justify=tk.RIGHT)
        self.textbox_stop_number_value.place(x=545, y=125, width=55, height=20)
        #----------------------------------------
        # 転送個数イベント通知のLabelFrameを作成
        #----------------------------------------
        self.groupbox_transfer_event = tk.LabelFrame(self.m_sampling_mode_window, text="転送個数イベント通知")
        self.groupbox_transfer_event.place(x=10, y=200, width=310, height=50)
        #--------------------
        # 転送個数イベント通知のEntryを作成
        #--------------------
        self.textbox_transfer_event_var = tk.StringVar(self.groupbox_transfer_event)
        self.textbox_transfer_event_var.set('500')
        self.textbox_transfer_event = tk.Entry(self.groupbox_transfer_event, textvariable=self.textbox_transfer_event_var, justify=tk.RIGHT)
        self.textbox_transfer_event.place(x=150, y=5, width=100, height=20)
        #--------------------
        # 個数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_transfer_event, text='個数 : ', anchor='nw').place(x=10, y=5, width=100, height=20)
        #----------------------------------------
        # 転送方式のLabelFrameを作成
        #----------------------------------------
        self.groupbox_transfer_type = tk.LabelFrame(self.m_sampling_mode_window, text="転送方式")
        self.groupbox_transfer_type.place(x=330, y=200, width=310, height=50)
        #--------------------
        # 転送方式のRadiobuttonを作成
        #--------------------
        self.radiobutton_transfer_type = tk.IntVar()
        self.radiobutton_transfer_type.set(1)
        self.radiobutton_transfer_once = tk.Radiobutton(self.groupbox_transfer_type, justify=tk.LEFT, text="1回転送", variable=self.radiobutton_transfer_type, value=1)
        self.radiobutton_transfer_once.place(x=5, y=5,height=20)
        self.radiobutton_transfer_infinite = tk.Radiobutton(self.groupbox_transfer_type, justify=tk.LEFT, text="無限回転送", variable=self.radiobutton_transfer_type, value=2)
        self.radiobutton_transfer_infinite.place(x=150, y=5,height=20)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_m_sampling_mode_window_ok = tk.Button(self.m_sampling_mode_window, text='OK')
        self.button_m_sampling_mode_window_ok.place(x=470, y=270, width=70, height=20)
        #--------------------
        # キャンセルのButtonを作成
        #--------------------
        self.button_m_sampling_mode_window_cancel = tk.Button(self.m_sampling_mode_window, text='キャンセル')
        self.button_m_sampling_mode_window_cancel.place(x=550, y=270, width=70, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_select_start_condition,
                       command_select_clock_condition,
                       command_select_stop_condition,
                       command_m_sampling_mode_window_ok,
                       command_m_sampling_mode_window_cancel):
        #----------------------------------------
        # スタート条件のコンボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_start_condition.bind("<<ComboboxSelected>>", command_select_start_condition)
        #----------------------------------------
        # クロック条件のコンボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_clock_condition.bind("<<ComboboxSelected>>", command_select_clock_condition)
        #----------------------------------------
        # ストップ条件のコンボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_stop_condition.bind("<<ComboboxSelected>>", command_select_stop_condition)
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_sampling_mode_window_ok.configure(command=command_m_sampling_mode_window_ok)
        #----------------------------------------
        # 「キャンセル」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_m_sampling_mode_window_cancel.configure(command=command_m_sampling_mode_window_cancel)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.m_sampling_mode_window)