﻿#================================================================
#================================================================
# API-AIO(WDM)
# デバイスバッファを使用した無限ジェネレーティング
# 条件設定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("770x245")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # 開始条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_start_condition = tk.LabelFrame(self.condition_window, text="開始条件")
        self.groupbox_start_condition.place(x=15, y=10, width=740, height=50)
        #--------------------
        # 開始条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_start_condition, text='開始条件 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 開始条件のComboboxを作成
        #--------------------
        self.combobox_start_condition = ttk.Combobox(self.groupbox_start_condition)
        self.combobox_start_condition.place(x=130, y=5, width=140, height=19)
        self.combobox_start_condition['value'] = ('ソフトウェア', '外部トリガ立ち上がり', '外部トリガ立ち下がり')
        self.combobox_start_condition.current(0)
        self.combobox_start_condition['state'] = 'readonly'
        #----------------------------------------
        # クロック条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_clock_condition = tk.LabelFrame(self.condition_window, text="クロック条件")
        self.groupbox_clock_condition.place(x=15, y=65, width=740, height=75)
        #--------------------
        # クロック種類のLabelを作成
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='クロック種類 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # クロック種類のComboboxを作成
        #--------------------
        self.combobox_clock_type = ttk.Combobox(self.groupbox_clock_condition)
        self.combobox_clock_type.place(x=130, y=5, width=140, height=19)
        self.combobox_clock_type['value'] = ('内部クロック', '外部クロック')
        self.combobox_clock_type.current(0)
        self.combobox_clock_type['state'] = 'readonly'
        #--------------------
        # ジェネレーティングクロック周期のLabelを作成
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='ジェネレーティングクロック周期 : ', anchor='nw').place(x=300, y=5, width=200, height=19)
        #--------------------
        # ジェネレーティングクロック周期のEntryを作成
        #--------------------
        self.textbox_generating_clock_var = tk.StringVar(self.groupbox_clock_condition)
        self.textbox_generating_clock_var.set("1000")
        self.textbox_generating_clock = tk.Entry(self.groupbox_clock_condition, justify=tk.RIGHT, textvariable=self.textbox_generating_clock_var)
        self.textbox_generating_clock.place(x=520, y=5, width=140, height=19)
        #--------------------
        # ジェネレーティングクロック周期単位のLabelを作成
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='[μsec]', anchor='nw').place(x=665, y=5, width=50, height=19)
        #--------------------
        # 外部クロック入力タイミングのLabelを作成
        #--------------------
        tk.Label(self.groupbox_clock_condition, text='外部クロック入力タイミング : ', anchor='nw').place(x=300, y=30, width=200, height=19)
        #--------------------
        # 外部クロック入力タイミングのComboboxを作成
        #--------------------
        self.combobox_clock_edge = ttk.Combobox(self.groupbox_clock_condition)
        self.combobox_clock_edge.place(x=520, y=30, width=140, height=19)
        self.combobox_clock_edge['value'] = ('立ち下がりエッジ', '立ち上がりエッジ')
        self.combobox_clock_edge.current(0)
        self.combobox_clock_edge['state'] = 'readonly'
        #----------------------------------------
        # 停止条件のLabelFrameを作成
        #----------------------------------------
        self.groupbox_stop_condition = tk.LabelFrame(self.condition_window, text="停止条件")
        self.groupbox_stop_condition.place(x=15, y=145, width=740, height=50)
        #--------------------
        # 停止条件のLabelを作成
        #--------------------
        tk.Label(self.groupbox_stop_condition, text='停止条件 : ', anchor='nw').place(x=15, y=5, width=90, height=19)
        #--------------------
        # 停止条件のComboboxを作成
        #--------------------
        self.combobox_stop_condition = ttk.Combobox(self.groupbox_stop_condition)
        self.combobox_stop_condition.place(x=130, y=5, width=140, height=19)
        self.combobox_stop_condition['value'] = ('外部トリガ立ち上がり', '外部トリガ立ち下がり')
        self.combobox_stop_condition.current(0)
        self.combobox_stop_condition['state'] = 'readonly'
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_ok = tk.Button(self.condition_window, text='OK')
        self.button_ok.place(x=540, y=210, width=80, height=19)
        #--------------------
        # CancelのButtonを作成
        #--------------------
        self.button_cancel = tk.Button(self.condition_window, text='Cancel')
        self.button_cancel.place(x=640, y=210, width=80, height=19)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_clock_type,
                       command_ok,
                       command_cancel):
        #----------------------------------------
        # 「クロック種類」コンボボックスのコールバック関数を設定
        #----------------------------------------
        self.combobox_clock_type.bind('<<ComboboxSelected>>', command_clock_type)
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_ok.configure(command=command_ok)
        #----------------------------------------
        # 「Cancel」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_cancel.configure(command=command_cancel)

    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.condition_window)