﻿#================================================================
#================================================================
# API-DIO(WDM) (BusMaster)
# ジェネレーティングサンプル
# 条件設定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("480x290")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # ジェネレーティング条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_generating_condition = tk.LabelFrame(self.condition_window, text="ジェネレーティング条件")
        self.groupbox_generating_condition.place(x=15, y=15, width=450, height=120)
        #--------------------
        # スタート条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='スタート条件 : ', anchor='nw').place(x=5, y=10, width=90, height=19)
        #--------------------
        # スタート条件のComboboxを作成
        #--------------------
        self.combobox_start_trg = ttk.Combobox(self.groupbox_generating_condition)
        self.combobox_start_trg.place(x=100, y=10, width=140, height=20)
        self.combobox_start_trg['value'] = ('Software Start', 'External UP', 'External Down')
        self.combobox_start_trg['state'] = 'readonly'
        #--------------------
        # クロック条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='クロック条件 : ', anchor='nw').place(x=5, y=40, width=90, height=19)
        #--------------------
        # クロック条件のComboboxを作成
        #--------------------
        self.combobox_clock_trg = ttk.Combobox(self.groupbox_generating_condition)
        self.combobox_clock_trg.place(x=100, y=40, width=140, height=20)
        self.combobox_clock_trg['value'] = ('Internal Clock', 'External Clock', 'Hand Shake')
        self.combobox_clock_trg['state'] = 'readonly'
        #--------------------
        # 内部クロックのLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='内部クロック : ', anchor='nw').place(x=250, y=40, width=90, height=19)
        #--------------------
        # 内部クロックのEntryを作成
        #--------------------
        self.textbox_internal_clock_var = tk.StringVar(self.groupbox_generating_condition)
        self.textbox_internal_clock = tk.Entry(self.groupbox_generating_condition, textvariable=self.textbox_internal_clock_var, justify=tk.RIGHT)
        self.textbox_internal_clock.place(x=350, y=40, width=60, height=19)
        #--------------------
        # 内部クロック単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='ns', anchor='nw').place(x=415, y=40, width=30, height=19)
        #--------------------
        # ストップ条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='ストップ条件 : ', anchor='nw').place(x=5, y=70, width=90, height=19)
        #--------------------
        # ストップ条件のComboboxを作成
        #--------------------
        self.combobox_stop_trg = ttk.Combobox(self.groupbox_generating_condition)
        self.combobox_stop_trg.place(x=100, y=70, width=140, height=20)
        self.combobox_stop_trg['value'] = ('Software Stop', 'External UP', 'External Down', 'Number')
        self.combobox_stop_trg['state'] = 'readonly'
        #--------------------
        # 停止個数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_generating_condition, text='停止個数 : ', anchor='nw').place(x=250, y=70, width=90, height=19)
        #--------------------
        # 停止個数のEntryを作成
        #--------------------
        self.textbox_stop_num_var = tk.StringVar(self.groupbox_generating_condition)
        self.textbox_stop_num = tk.Entry(self.groupbox_generating_condition, textvariable=self.textbox_stop_num_var, justify=tk.RIGHT)
        self.textbox_stop_num.place(x=350, y=70, width=60, height=19)
        #----------------------------------------
        # バッファ条件設定のLabelFrameを作成
        #----------------------------------------
        self.groupbox_buf_condition = tk.LabelFrame(self.condition_window, text="バッファ条件設定")
        self.groupbox_buf_condition.place(x=15, y=140, width=450, height=55)
        #--------------------
        # 転送条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_buf_condition, text='転送条件 : ', anchor='nw').place(x=20, y=10, width=90, height=19)
        #--------------------
        # 転送条件のRadiobuttonを作成
        #--------------------
        self.radio_is_ring_var = tk.IntVar()
        tk.Radiobutton(self.groupbox_buf_condition, text='一回転送', variable=self.radio_is_ring_var, value=0).place(x=100, y=10, width=90, height=19)
        tk.Radiobutton(self.groupbox_buf_condition, text='無限回転送', variable=self.radio_is_ring_var, value=1).place(x=200, y=10, width=90, height=19)
        #----------------------------------------
        # 転送個数イベント通知のLabelFrameを作成
        #----------------------------------------
        self.groupbox_num_event = tk.LabelFrame(self.condition_window, text="転送個数イベント通知")
        self.groupbox_num_event.place(x=15, y=200, width=450, height=55)
        #--------------------
        # データ毎に通知のEntryを作成
        #--------------------
        self.textbox_num_event_var = tk.StringVar(self.groupbox_num_event)
        tk.Entry(self.groupbox_num_event, textvariable=self.textbox_num_event_var, justify=tk.RIGHT).place(x=30, y=10, width=90, height=19)
        #--------------------
        # データ毎に通知のLabelを作成
        #--------------------
        tk.Label(self.groupbox_num_event, text='データ毎に通知', anchor='nw').place(x=130, y=10, width=90, height=19)
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_ok = tk.Button(self.condition_window, text='OK')
        self.button_ok.place(x=230, y=260, width=110, height=19)
        #--------------------
        # ｷｬﾝｾﾙのButtonを作成
        #--------------------
        self.button_cancel = tk.Button(self.condition_window, text='ｷｬﾝｾﾙ')
        self.button_cancel.place(x=350, y=260, width=110, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_clock_trg,
                       command_stop_trg,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # 「クロック条件」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_clock_trg.bind('<<ComboboxSelected>>', command_clock_trg)
        #----------------------------------------
        # 「ストップ条件」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_stop_trg.bind('<<ComboboxSelected>>', command_stop_trg)
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_ok.configure(command=command_ok)
        #----------------------------------------
        # 「ｷｬﾝｾﾙ」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cancel.configure(command=command_cancel)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)