#================================================================
#================================================================
# API-CNT(WDM)
# カウンタサンプル
# カウンタモード設定UIファイル
#                                                CONTEC Co., Ltd.
#================================================================
#================================================================
#----------------------------------------
# インポートモジュール
#----------------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


#========================================
# ModeWindowUI クラス
#========================================
class ModeWindowUI:
    #========================================
    # 初期化関数
    #========================================
    def __init__(self, parent):
        #----------------------------------------
        # 親ウィンドウの設定
        #----------------------------------------
        self.parent = parent
        #----------------------------------------
        # カウンタモード設定ウィンドウの作成
        #----------------------------------------
        self.mode_window = tk.Toplevel(self.parent.ui.root)
        #----------------------------------------
        # モーダルダイアログの設定
        #----------------------------------------
        self.mode_window.grab_set()
        #----------------------------------------
        # タイトル
        #----------------------------------------
        self.mode_window.title("カウンタモード設定")
        self.mode_window.geometry("400x310")
        #----------------------------------------
        # UIの作成を開始
        #----------------------------------------
        self._create_ui()

    #========================================
    # UIの作成関数
    #========================================
    def _create_ui(self):
        #----------------------------------------
        # カウンタモードのLabelFrameを作成
        #----------------------------------------
        self.groupbox_mode_setting = tk.LabelFrame(self.mode_window, text="カウンタモード")
        self.groupbox_mode_setting.place(x=10, y=10, width=380, height=240)
        #--------------------
        # Z相モードのLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Z相モード : ', anchor='nw').place(x=15, y=15, width=90, height=20)
        #--------------------
        # Z相モードのComboboxを作成
        #--------------------
        self.combobox_z_phase_mode = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_z_phase_mode.place(x=140, y=15, width=200, height=20)
        self.combobox_z_phase_mode['value'] = ('未使用', '1回', '毎回')
        self.combobox_z_phase_mode.current(0)
        self.combobox_z_phase_mode['state'] = 'readonly'
        #--------------------
        # Z相論理のLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='Z相論理 : ', anchor='nw').place(x=15, y=40, width=90, height=20)
        #--------------------
        # Z相論理のComboboxを作成
        #--------------------
        self.combobox_z_logic = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_z_logic.place(x=140, y=40, width=200, height=20)
        self.combobox_z_logic['value'] = ('正論理', '負論理')
        self.combobox_z_logic.current(1)
        self.combobox_z_logic['state'] = 'readonly'
        #--------------------
        # 信号源のLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='信号源 : ', anchor='nw').place(x=15, y=65, width=90, height=20)
        #--------------------
        # 信号源のComboboxを作成
        #--------------------
        self.combobox_signal_source = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_signal_source.place(x=140, y=65, width=200, height=20)
        self.combobox_signal_source['value'] = ('フォトカプラ絶縁', 'TTL', '差動')
        self.combobox_signal_source.current(2)
        self.combobox_signal_source['state'] = 'readonly'
        #--------------------
        # カウント方向のLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='カウント方向 : ', anchor='nw').place(x=15, y=90, width=90, height=20)
        #--------------------
        # カウント方向のComboboxを作成
        #--------------------
        self.combobox_count_direction = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_count_direction.place(x=140, y=90, width=200, height=20)
        self.combobox_count_direction['value'] = ('ダウンカウント', 'アップカウント')
        self.combobox_count_direction.current(1)
        self.combobox_count_direction['state'] = 'readonly'
        #--------------------
        # デジタルフィルタのLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='デジタルフィルタ : ', anchor='nw').place(x=15, y=115, width=150, height=20)
        #--------------------
        # デジタルフィルタのComboboxを作成
        #--------------------
        self.combobox_filter = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_filter.place(x=140, y=115, width=200, height=20)
        self.combobox_filter['value'] = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
        self.combobox_filter.current(0)
        self.combobox_filter['state'] = 'readonly'
        #--------------------
        # 相数のLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='相数 : ', anchor='nw').place(x=15, y=140, width=90, height=20)
        #--------------------
        # 相数のComboboxを作成
        #--------------------
        self.combobox_phase = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_phase.place(x=140, y=140, width=200, height=20)
        self.combobox_phase['value'] = ('単相', '2相', 'ゲートコントロール')
        self.combobox_phase.current(1)
        self.combobox_phase['state'] = 'readonly'
        #--------------------
        # 逓倍のLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='逓倍 : ', anchor='nw').place(x=15, y=165, width=90, height=20)
        #--------------------
        # 逓倍のComboboxを作成
        #--------------------
        self.combobox_mul = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_mul.place(x=140, y=165, width=200, height=20)
        self.combobox_mul['value'] = ('1逓倍', '2逓倍', '4逓倍')
        self.combobox_mul.current(1)
        self.combobox_mul['state'] = 'readonly'
        #--------------------
        # 同期クリアのLabelを作成
        #--------------------
        tk.Label(self.groupbox_mode_setting, text='同期クリア : ', anchor='nw').place(x=15, y=190, width=90, height=20)
        #--------------------
        # 同期クリアのComboboxを作成
        #--------------------
        self.combobox_sync_clear = ttk.Combobox(self.groupbox_mode_setting)
        self.combobox_sync_clear.place(x=140, y=190, width=200, height=20)
        self.combobox_sync_clear['value'] = ('非同期クリア', '同期クリア')
        self.combobox_sync_clear.current(1)
        self.combobox_sync_clear['state'] = 'readonly'
        #----------------------------------------
        # 終了
        #----------------------------------------
        #--------------------
        # 全てのチャネルにおいて同一の設定となりますのLabelを作成
        #--------------------
        tk.Label(self.mode_window, text='全てのチャネルにおいて同一の設定となります。', anchor='nw').place(x=20, y=250, width=300, height=20)
        #--------------------
        # OKのButtonを作成
        #--------------------
        self.button_mode_window_ok = tk.Button(self.mode_window, text='OK')
        self.button_mode_window_ok.place(x=220, y=275, width=70, height=20)
        #--------------------
        # キャンセルのButtonを作成
        #--------------------
        self.button_mode_window_cancel = tk.Button(self.mode_window, text='キャンセル')
        self.button_mode_window_cancel.place(x=300, y=275, width=70, height=20)

    #========================================
    # UIコントロールのコールバック関数を設定する関数
    #========================================
    def setup_commands(self,
                       command_mode_window_ok,
                       command_mode_window_cancel):
        #----------------------------------------
        # 「OK」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_mode_window_ok.configure(command=command_mode_window_ok)
        #----------------------------------------
        # 「キャンセル」ボタンのコールバック関数を設定
        #----------------------------------------
        self.button_mode_window_cancel.configure(command=command_mode_window_cancel)
 
    #========================================
    # メッセージボックスにエラーを表示
    #========================================
    def show_error(self, caption, error):
        messagebox.showerror(caption, error, parent=self.mode_window)