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


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

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # サンプリング条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_sampling_condition = tk.LabelFrame(self.condition_window, text="サンプリング条件")
        self.groupbox_sampling_condition.place(x=10, y=10, width=480, height=140)
        #--------------------
        # スタート条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='スタート条件 : ', anchor='nw').place(x=5, y=5, width=90, height=20)
        #--------------------
        # スタート条件のComboboxを作成
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_sampling_condition)
        self.combobox_start_condition.place(x=100, y=5, width=200, height=20)
        self.combobox_start_condition['value'] = ('ソフトウェアスタート', '外部信号立ち上がり', '外部信号立ち下がり', 
                                                  'カウント一致CH0 比較レジスタ0', 'カウント一致CH1 比較レジスタ0', 
                                                  'カウント一致CH2 比較レジスタ0', 'カウント一致CH3 比較レジスタ0', 
                                                  'カウント一致CH0 比較レジスタ1', 'カウント一致CH1 比較レジスタ1', 
                                                  'カウント一致CH2 比較レジスタ1', 'カウント一致CH3 比較レジスタ1')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #--------------------
        # クロック条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='クロック条件 : ', anchor='nw').place(x=5, y=35, width=90, height=20)
        #--------------------
        # クロック条件のComboboxを作成
        #--------------------
        self.combobox_clock_condition = ttk.Combobox(self.groupbox_sampling_condition)
        self.combobox_clock_condition.place(x=100, y=35, 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_sampling_condition, text='ストップ条件 : ', anchor='nw').place(x=5, y=65, width=90, height=20)
        #--------------------
        # ストップ条件のComboboxを作成
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_sampling_condition)
        self.combobox_stop_condition.place(x=100, y=65, width=200, height=20)
        self.combobox_stop_condition['value'] = ('ソフトウェアスタート', '外部信号立ち上がり', '外部信号立ち下がり', '指定個数転送完了', 
                                                 'カウント一致CH0 比較レジスタ0', 'カウント一致CH1 比較レジスタ0', 
                                                 'カウント一致CH2 比較レジスタ0', 'カウント一致CH3 比較レジスタ0', 
                                                 'カウント一致CH0 比較レジスタ1', 'カウント一致CH1 比較レジスタ1', 
                                                 'カウント一致CH2 比較レジスタ1', 'カウント一致CH3 比較レジスタ1')
        self.combobox_stop_condition.current(0)
        self.combobox_stop_condition['state'] = 'readonly'
        #--------------------
        # 比較値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='比較値 : ', anchor='nw').place(x=305, y=5, width=85, height=20)
        #--------------------
        # 比較値のEntryを作成
        #--------------------
        self.textbox_start_compare_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_start_compare_value_var.set('100')
        self.textbox_start_compare_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_start_compare_value_var, justify=tk.RIGHT)
        self.textbox_start_compare_value.place(x=395, y=5, width=55, height=20)
        #--------------------
        # HexのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Hex', anchor='nw').place(x=450, y=5, width=20, height=20)
        #--------------------
        # 内部クロックのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='内部クロック : ', anchor='nw').place(x=305, y=35, width=85, height=20)
        #--------------------
        # 内部クロックのEntryを作成
        #--------------------
        self.textbox_internal_clock_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_internal_clock_value_var.set('100000')
        self.textbox_internal_clock_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_internal_clock_value_var, justify=tk.RIGHT)
        self.textbox_internal_clock_value.place(x=395, y=35, width=55, height=20)
        #--------------------
        # nsのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='ns', anchor='nw').place(x=450, y=35, width=20, height=20)
        #--------------------
        # 比較値のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='比較値 : ', anchor='nw').place(x=305, y=65, width=85, height=20)
        #--------------------
        # 比較値のEntryを作成
        #--------------------
        self.textbox_stop_compare_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_stop_compare_value_var.set('200')
        self.textbox_stop_compare_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_stop_compare_value_var, justify=tk.RIGHT)
        self.textbox_stop_compare_value.place(x=395, y=65, width=55, height=20)
        #--------------------
        # HexのLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='Hex', anchor='nw').place(x=450, y=65, width=20, height=20)
        #--------------------
        # 停止個数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_sampling_condition, text='停止個数 : ', anchor='nw').place(x=305, y=95, width=85, height=20)
        #--------------------
        # 停止個数のEntryを作成
        #--------------------
        self.textbox_stop_number_value_var = tk.StringVar(self.groupbox_sampling_condition)
        self.textbox_stop_number_value_var.set('1000')
        self.textbox_stop_number_value = tk.Entry(self.groupbox_sampling_condition, textvariable=self.textbox_stop_number_value_var, justify=tk.RIGHT)
        self.textbox_stop_number_value.place(x=395, y=95, width=55, height=20)
        #----------------------------------------
        # カウンタスタートモードのLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_start_mode = tk.LabelFrame(self.condition_window, text="カウンタスタートモード")
        self.groupbox_counter_start_mode.place(x=10, y=160, width=480, height=60)
        #--------------------
        # カウンタスタートモードのRadiobuttonを作成
        #--------------------
        self.radiobutton_counter_start_mode = tk.IntVar()
        self.radiobutton_counter_start_mode.set(1)
        self.radiobutton_counter_start_unrelated = tk.Radiobutton(self.groupbox_counter_start_mode, justify=tk.LEFT, text="サンプリングとは無関係にカウンタスタート", variable=self.radiobutton_counter_start_mode, value=1)
        self.radiobutton_counter_start_unrelated.place(x=5, y=0,height=20)
        self.radiobutton_counter_start_same_as = tk.Radiobutton(self.groupbox_counter_start_mode, justify=tk.LEFT, text="サンプリングと同時にカウンタスタート", variable=self.radiobutton_counter_start_mode, value=2)
        self.radiobutton_counter_start_same_as.place(x=5, y=20,height=20)
        #----------------------------------------
        # カウンタストップモードのLabelFrameを作成
        #----------------------------------------
        self.groupbox_counter_stop_mode = tk.LabelFrame(self.condition_window, text="カウンタストップモード")
        self.groupbox_counter_stop_mode.place(x=10, y=230, width=480, height=60)
        #--------------------
        # カウンタストップモードのRadiobuttonを作成
        #--------------------
        self.radiobutton_counter_stop_mode = tk.IntVar()
        self.radiobutton_counter_stop_mode.set(1)
        self.radiobutton_counter_stop_unrelated = tk.Radiobutton(self.groupbox_counter_stop_mode, justify=tk.LEFT, text="サンプリングとは無関係にカウンタストップ", variable=self.radiobutton_counter_stop_mode, value=1)
        self.radiobutton_counter_stop_unrelated.place(x=5, y=0,height=20)
        self.radiobutton_counter_stop_same_as = tk.Radiobutton(self.groupbox_counter_stop_mode, justify=tk.LEFT, text="サンプリングと同時にカウンタストップ", variable=self.radiobutton_counter_stop_mode, value=2)
        self.radiobutton_counter_stop_same_as.place(x=5, y=20,height=20)
        #----------------------------------------
        # 転送個数イベント通知のLabelFrameを作成
        #----------------------------------------
        self.groupbox_transfer_event = tk.LabelFrame(self.condition_window, text="転送個数イベント通知")
        self.groupbox_transfer_event.place(x=10, y=310, width=480, height=50)
        #--------------------
        # 転送個数イベント通知のEntryを作成
        #--------------------
        self.textbox_transfer_event_var = tk.StringVar(self.groupbox_sampling_condition)
        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=30, y=5, width=100, height=20)
        #--------------------
        # データ毎に通知のLabelを作成
        #--------------------
        tk.Label(self.groupbox_transfer_event, text='データ毎に通知', anchor='nw').place(x=135, y=5, width=100, height=20)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_condition_window_ok = tk.Button(self.condition_window, text='OK')
        self.button_condition_window_ok.place(x=340, y=370, width=70, height=20)
        #--------------------
        # キャンセルのButtonを作成
        #--------------------
        self.button_condition_window_cancel = tk.Button(self.condition_window, text='キャンセル')
        self.button_condition_window_cancel.place(x=420, y=370, width=70, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_select_start_condition,
                       command_select_clock_condition,
                       command_select_stop_condition,
                       command_condition_window_ok,
                       command_condition_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_condition_window_ok.configure(command=command_condition_window_ok)
        #----------------------------------------
        # 「キャンセル」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_condition_window_cancel.configure(command=command_condition_window_cancel)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)