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


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

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # サンプリング条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_slave_sampling_mode = tk.LabelFrame(self.s_sampling_mode_window, text="マスターサンプリング条件設定")
        self.groupbox_slave_sampling_mode.place(x=10, y=10, width=530, height=180)
        #--------------------
        # スタート条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave_sampling_mode, text='スタート条件 : ', anchor='nw').place(x=5, y=5, width=150, height=20)
        #--------------------
        # スタート条件のComboboxを作成
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_slave_sampling_mode)
        self.combobox_start_condition.place(x=160, y=5, width=200, height=20)
        self.combobox_start_condition['value'] = ('ExtSig1', 'ExtSig2', 'ExtSig3')
        self.combobox_start_condition.current(1)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # カウンタスタートモードのLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave_sampling_mode, text='カウンタスタートモード : ', anchor='nw').place(x=5, y=35, width=150, height=20)
        #--------------------
        # カウンタスタートモードのComboboxを作成
        #--------------------
        self.combobox_start_with = ttk.Combobox(self.groupbox_slave_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_slave_sampling_mode, text='クロック条件 : ', anchor='nw').place(x=5, y=65, width=150, height=20)
        #--------------------
        # クロック条件のComboboxを作成
        #--------------------
        self.combobox_clock_condition = ttk.Combobox(self.groupbox_slave_sampling_mode)
        self.combobox_clock_condition.place(x=160, y=65, width=200, height=20)
        self.combobox_clock_condition['value'] = ('ExtSig1', 'ExtSig2', 'ExtSig3')
        self.combobox_clock_condition.current(0)
        self.combobox_clock_condition['state'] = 'readonly'
        #--------------------
        # ストップ条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave_sampling_mode, text='ストップ条件 : ', anchor='nw').place(x=5, y=95, width=150, height=20)
        #--------------------
        # ストップ条件のComboboxを作成
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_slave_sampling_mode)
        self.combobox_stop_condition.place(x=160, y=95, width=200, height=20)
        self.combobox_stop_condition['value'] = ('ExtSig1', 'ExtSig2', 'ExtSig3', '指定個数転送完了')
        self.combobox_stop_condition.current(2)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # カウンタストップモードのLabelを作成
        #--------------------
        tk.Label(self.groupbox_slave_sampling_mode, text='カウンタストップモード : ', anchor='nw').place(x=5, y=125, width=150, height=20)
        #--------------------
        # カウンタストップモードのComboboxを作成
        #--------------------
        self.combobox_stop_with = ttk.Combobox(self.groupbox_slave_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_slave_sampling_mode, text='停止個数 : ', anchor='nw').place(x=365, y=95, width=85, height=20)
        #--------------------
        # 停止個数のEntryを作成
        #--------------------
        self.textbox_stop_number_value_var = tk.StringVar(self.groupbox_slave_sampling_mode)
        self.textbox_stop_number_value_var.set('1000')
        self.textbox_stop_number_value = tk.Entry(self.groupbox_slave_sampling_mode, textvariable=self.textbox_stop_number_value_var, justify=tk.RIGHT)
        self.textbox_stop_number_value.place(x=455, y=95, width=55, height=20)
        #----------------------------------------
        # 転送個数イベント通知のLabelFrameを作成
        #----------------------------------------
        self.groupbox_transfer_event = tk.LabelFrame(self.s_sampling_mode_window, text="転送個数イベント通知")
        self.groupbox_transfer_event.place(x=10, y=200, width=260, height=50)
        #--------------------
        # 転送個数イベント通知のEntryを作成
        #--------------------
        self.textbox_transfer_event_var = tk.StringVar(self.groupbox_slave_sampling_mode)
        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.s_sampling_mode_window, text="転送方式")
        self.groupbox_transfer_type.place(x=280, y=200, width=260, 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_s_sampling_mode_window_ok = tk.Button(self.s_sampling_mode_window, text='OK')
        self.button_s_sampling_mode_window_ok.place(x=320, y=270, width=70, height=20)
        #--------------------
        # キャンセルのButtonを作成
        #--------------------
        self.button_s_sampling_mode_window_cancel = tk.Button(self.s_sampling_mode_window, text='キャンセル')
        self.button_s_sampling_mode_window_cancel.place(x=400, y=270, width=70, height=20)

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