Skip to content
Snippets Groups Projects
Select Git revision
  • 35424baf0ac5b0dd75fae1c56717ce4f45c7cfbc
  • main default protected
2 results

dynamic_ui.py

Blame
  • dynamic_ui.py 9.57 KiB
    import dearpygui.dearpygui as dpg
    
    from utils import change_height, get_current_ui_type, get_config, get_config_file_path, does_tab_bar_child_exist, get_max_n_emission_point, set_main_window_value
    from style_constants import *
    from config import CfgVar
    
    def check_every_child_checkbox(s, a):
        parent = dpg.get_item_parent(s)
    
        for child in dpg.get_item_children(parent, 1):
            t = dpg.get_item_type(child)
            if t == 'mvAppItemType::mvGroup':
                dpg.set_value(dpg.get_item_children(child, 1)[1], a)
            if t == 'mvAppItemType::mvCollapsingHeader':
                checkbox = dpg.get_item_children(child, 1)[0]
                dpg.set_value(checkbox, a)
                check_every_child_checkbox(checkbox, a)
    
    def folder_checkbox_clb(s, a, u):
        check_every_child_checkbox(s, a)
    
    def construct_model_output_structure_ui(filepaths, folders):
        dpg.add_collapsing_header(label='output', parent='model_output_child_window', tag='output_header', indent=0, default_open=True)
        dpg.add_checkbox(label='select all', default_value=False, parent='output_header', tag='output', callback=folder_checkbox_clb)
    
        for path in folders:
            folders = path.split('/')
            parent = 'output_header'
            for folder in folders:
                if not dpg.does_item_exist(f'{folder}_header'):
                    with dpg.collapsing_header(label=folder, parent=parent, tag=f'{folder}_header', indent=INDENT):
                        dpg.add_checkbox(label='select all', default_value=False, tag=folder, callback=folder_checkbox_clb)
    
                parent = f'{folder}_header'
    
        for filepath in filepaths:
            if '/' not in filepath:
                parent = 'output_header'
            else:
                parent = filepath.split('/')[-2] + '_header'
            with dpg.group(horizontal=True, parent=parent, indent=INDENT, xoffset=XOFFSET):
                dpg.add_text(filepath.split('/')[-1], tag=f'{filepath}.text')
                dpg.add_checkbox(default_value=False, tag=filepath)
    
    def open_folder_dialog_clb(s, a, u):
        dpg.show_item('set_folder_dialog')
        dpg.set_item_user_data('set_folder_dialog', u)
    
    def change_tab_bar_height_clb(s, a, u):
        change_height(a)
    
    def construct_config_ui(config_file_path, config):
        if get_current_ui_type() == TABS_UI:
            construct_config_ui_tabs(config_file_path, config)
        else:
            construct_config_ui_collapsing_headers(config_file_path, config)
    
    def add_item_to_config_ui(full_path, before=None):
        config_file_path = get_config_file_path()
        config = get_config()
        if get_current_ui_type() == TABS_UI:
            namespaces = full_path.split('.')
            parent = config_file_path
            for level in range(len(namespaces) - 1):
                path = config_file_path + '.' + '.'.join(namespaces[:level+1])
    
                tab_bar_exists, tab_bar_uuid = does_tab_bar_child_exist(parent)
    
                if not tab_bar_exists:
                    tab_bar_uuid = dpg.add_tab_bar(parent=parent, callback=change_tab_bar_height_clb)
                    dpg.set_item_user_data(parent, dpg.get_item_user_data(parent) + 4 * SPACE + FONT_SIZE)
    
                if not dpg.does_item_exist(path):
                    tab_uuid = dpg.add_tab(label=namespaces[level], parent=tab_bar_uuid)
                    dpg.add_child_window(parent=tab_uuid, tag=path, border=True, autosize_y=False, autosize_x=True)
                    dpg.set_item_user_data(path, 2 * SPACE)
    
                parent = path
        else:
            namespaces = full_path.split('.')
            parent = config_file_path
            for level in range(len(namespaces) - 1):
                path = config_file_path + '.' + '.'.join(namespaces[:level+1])
    
                if not dpg.does_item_exist(path):
                    if before:
                        dpg.add_collapsing_header(label=namespaces[level], parent=parent, tag=path, indent=INDENT, before=before)
                    else:
                        dpg.add_collapsing_header(label=namespaces[level], parent=parent, tag=path, indent=INDENT)
    
                parent = path
    
    
        if not dpg.does_item_exist(config_file_path + '.' + full_path):
            create_widget(config_file_path, config, full_path)
    
    def add_emission_point_and_update_ui(x, y):
        emission_point = {
            'value': CfgVar(key='value', value=28481176.531511, value_type='DOUBLE', comment=''),
            'begin': CfgVar(key='begin', value=25200.0, value_type='DOUBLE', comment='[s]'),
            'xpos': CfgVar(key='xpos', value=x, value_type='DOUBLE', comment='[m]'),
            'ypos': CfgVar(key='ypos', value=y, value_type='DOUBLE', comment='[m]'),
            'zpos': CfgVar(key='zpos', value=60.0, value_type='DOUBLE', comment='[m]'),
            'sx': CfgVar(key='sx', value=20.0, value_type='DOUBLE', comment='[m]'),
            'sy': CfgVar(key='sy', value=20.0, value_type='DOUBLE', comment='[m]'),
            'sz': CfgVar(key='sz', value=10.0, value_type='DOUBLE', comment='[m]')
        }
        
        prefix = f'passive_tracers.tracer_{get_max_n_emission_point() + 1}.point_emission'
    
        config = get_config()
    
        for key in emission_point:
            full_path = '.'.join([prefix, key])
            config[full_path] = emission_point[key]
            add_item_to_config_ui(full_path)
    
        set_main_window_value('config', config)
    
    def construct_config_ui_collapsing_headers(config_file_path, config):
        dpg.add_collapsing_header(label=config_file_path, parent='main', tag=config_file_path, indent=0, default_open=True)
    
        for full_path in config:
            namespaces = full_path.split('.')
            parent = config_file_path
            for level in range(len(namespaces) - 1):
                path = config_file_path + '.' + '.'.join(namespaces[:level+1])
    
                if not dpg.does_item_exist(path):
                    dpg.add_collapsing_header(label=namespaces[level], parent=parent, tag=path, indent=INDENT)
    
                parent = path
    
            create_widget(config_file_path, config, full_path)
    
    def construct_config_ui_tabs(config_file_path, config):
        tab_bar_exists, tab_bar_uuid = does_tab_bar_child_exist('main')
        if not tab_bar_exists:
            tab_bar_uuid = dpg.add_tab_bar(parent='main')
        tab_uuid = dpg.add_tab(label=config_file_path, parent=tab_bar_uuid)
        dpg.add_child_window(parent=tab_uuid, tag=config_file_path, border=True, autosize_y=False, autosize_x=True)
        dpg.set_item_user_data(config_file_path, 2 * SPACE)
    
        for full_path in config:
            namespaces = full_path.split('.')
            parent = config_file_path
            for level in range(len(namespaces) - 1):
                path = config_file_path + '.' + '.'.join(namespaces[:level+1])
    
                tab_bar_exists, tab_bar_uuid = does_tab_bar_child_exist(parent)
    
                if not tab_bar_exists:
                    tab_bar_uuid = dpg.add_tab_bar(parent=parent, callback=change_tab_bar_height_clb)
                    dpg.set_item_user_data(parent, dpg.get_item_user_data(parent) + 4 * SPACE + FONT_SIZE)
    
                if not dpg.does_item_exist(path):
                    tab_uuid = dpg.add_tab(label=namespaces[level], parent=tab_bar_uuid)
                    dpg.add_child_window(parent=tab_uuid, tag=path, border=True, autosize_y=False, autosize_x=True)
                    dpg.set_item_user_data(path, 2 * SPACE)
    
                parent = path
    
            create_widget(config_file_path, config, full_path)
    
    def create_widget(config_file_path, config, full_path):
        tag = config_file_path + '.' + full_path
        parent =  '.'.join([config_file_path] + full_path.split('.')[:-1])
    
        value = config[full_path].value
        value_type = config[full_path].value_type
        key = config[full_path].key
        comment = config[full_path].comment
    
        if dpg.get_value('use_tabs_ui') and parent != config_file_path and full_path != key:
            dpg.set_item_user_data(parent, dpg.get_item_user_data(parent) + 2 * SPACE + FONT_SIZE + SPACE)
    
        indent = INDENT
        if dpg.get_value('use_tabs_ui'):
            indent = 0
    
        with dpg.group(horizontal=True, parent=parent, indent=indent, xoffset=XOFFSET):
            if not comment:
                dpg.add_text(key, tag=f'{tag}.text')
                if value_type == 'BOOLEAN':
                    dpg.add_checkbox(default_value=value, tag=tag)
                elif value_type == 'DOUBLE':
                    dpg.add_input_double(default_value=value, tag=tag, format='%.10f', width=WIDGET_WIDTH)
                elif value_type == 'INT':
                    dpg.add_input_int(default_value=value, tag=tag, width=WIDGET_WIDTH)
                elif 'dir' in key.lower() or 'file' in key.lower():
                    with dpg.group(horizontal=True):
                        dpg.add_input_text(default_value=value, tag=tag, width=WIDGET_WIDTH)
                        dpg.add_button(label='...', callback=open_folder_dialog_clb, user_data=tag)
                else:
                    dpg.add_input_text(default_value=value, tag=tag, width=WIDGET_WIDTH)
            else:
                dpg.add_text(key, tag=f'{tag}.text')
                with dpg.group(horizontal=True, xoffset=WIDGET_WIDTH + 100):
                    if value_type == 'BOOLEAN':
                        dpg.add_checkbox(default_value=value, tag=tag)
                    elif value_type == 'DOUBLE':
                        dpg.add_input_double(default_value=value, tag=tag, format='%.10f', width=WIDGET_WIDTH)
                    elif value_type == 'INT':
                        dpg.add_input_int(default_value=value, tag=tag, width=WIDGET_WIDTH)
                    elif 'dir' in key.lower() or 'file' in key.lower():
                        with dpg.group(horizontal=True):
                            dpg.add_input_text(default_value=value, tag=tag, width=WIDGET_WIDTH - 3 * SPACE - dpg.get_text_size('...')[0])
                            dpg.add_button(label='...', callback=open_folder_dialog_clb, user_data=tag)
                    else:
                        dpg.add_input_text(default_value=value, tag=tag, width=WIDGET_WIDTH)
                    dpg.add_text(comment, color=(255, 255, 255, 150))