Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI #231

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

GUI #231

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added dwi_ml/GUI/__init__.py
Empty file.
Empty file.
73 changes: 73 additions & 0 deletions dwi_ml/GUI/args_management/argparse_equivalent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
import logging

store_options = ['store_true', 'store_false']


def format_arg(argname, help, metavar=None, type=str, default=None,
action=None, const=None, nargs=None, dest=None, choices=None):

if action is not None and action != 'store_true':
raise NotImplementedError("NOT READY")

# Format metavar
optional = True if argname[0] == '-' else False
if metavar is None and optional and action not in store_options:
# .upper() to put in capital like real argparser, but I don't like that
metavar = argname.replace('-', '')

# Modifying metavar if 'nargs' is set.
if metavar is not None and nargs is not None:
if isinstance(nargs, int):
metavar = (metavar + ' ') * nargs
elif nargs == '?': # i.e. either default or one value. Uses const.
metavar = '[' + metavar + ']'
elif nargs == '*': # Between 0 and inf nb.
metavar = '[' + metavar + '... ]'
elif nargs == '+': # Between 1 and inf nb.
metavar = metavar + ' [' + metavar + '... ]'

return {'help': help, 'metavar': metavar, 'type': type, 'default': default,
'const': const, 'nargs': nargs, 'dest': dest, 'choices': choices,
'action': action}


class ArgparserForGui:
def __init__(self, description=None):
logging.debug(" GUI.args_management.argparse_equivalent: "
"Initializing ArgparserForGui")
self.description = description
self.required_args_dict = {}
self.optional_args_dict = {}
self.exclusive_groups = [] # type: List[MutuallyExclusiveGroup]

# Should never be changed if self is created with add_argument_group.
# But we rely on the fact that the scripts are used through the real
# argparser. No need to test.
self.groups_of_args = [] # type: List[ArgparserForGui]

def add_argument(self, argname, **kwargs):
optional = True if argname[0] == '-' else False
if optional:
self.optional_args_dict[argname] = format_arg(argname, **kwargs)
else:
self.required_args_dict[argname] = format_arg(argname, **kwargs)

def add_argument_group(self, desc):
g = ArgparserForGui(desc)
self.groups_of_args.append(g)
return g

def add_mutually_exclusive_group(self, required=False):
g = MutuallyExclusiveGroup(required)
self.exclusive_groups.append(g)
return g


class MutuallyExclusiveGroup:
def __init__(self, required):
self.required = required
self.arguments_list = {}

def add_argument(self, argname, **kwargs):
self.arguments_list[argname] = format_arg(argname, **kwargs)
86 changes: 86 additions & 0 deletions dwi_ml/GUI/args_management/args_to_gui_all_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
import logging

import dearpygui.dearpygui as dpg

from dwi_ml.GUI.args_management.argparse_equivalent import ArgparserForGui
from dwi_ml.GUI.args_management.args_to_gui_one_arg import \
add_one_arg_and_help_to_gui
from dwi_ml.GUI.styles.my_styles import get_my_fonts_dictionary


def add_args_to_gui(gui_parser: ArgparserForGui, known_file_dialogs=None):
"""
Adds argparsing groups of arguments to the GUI

Parameters
----------
gui_parser: ArgparserForGui

known_file_dialogs: dict of str
keys are arg names that should be shown as file dialogs.
values are a list: [str, [str]]:
First str = one of ['unique_file', 'unique_folder']
(or eventually multi-selection, not implemented yet)
List of str = accepted extensions (for files only).
ex: { 'out_filename': ['unique_file', ['.txt']]}
"""
logging.debug(" GUI.args_management.args_to_gui_all_args: adding args "
"to GUI.")
my_fonts = get_my_fonts_dictionary()

# 1. Required args:
title = dpg.add_text('\n Required arguments:')
dpg.bind_item_font(title, my_fonts['group_title'])
for argname, argoptions in gui_parser.required_args_dict.items():
add_one_arg_and_help_to_gui(argname, argoptions, known_file_dialogs)

# 2. Grouped options
# At first, we had the idea to add them into a closable section.
# We didn't like it that much. Removed.
for sub_parser in gui_parser.groups_of_args:
# There should not be required args in groups.
assert len(sub_parser.required_args_dict.keys()) == 0

# There should not be a subgroup in a group
assert len(sub_parser.groups_of_args) == 0

title = dpg.add_text('\n' + sub_parser.description + ':')
dpg.bind_item_font(title, my_fonts['group_title'])

_add_options_group_to_gui(sub_parser, known_file_dialogs)

# 3. Non-grouped args ("Other options")
title = dpg.add_text('\n Other options:')
dpg.bind_item_font(title, my_fonts['group_title'])
_add_options_group_to_gui(gui_parser, known_file_dialogs)


def _add_options_group_to_gui(gui_parser, known_file_dialogs):
"""
Adds arguments under the group's title.
"""
# 2. "normal" optional args:
for argname, argoptions in gui_parser.optional_args_dict.items():
add_one_arg_and_help_to_gui(argname, argoptions, known_file_dialogs)

# 2. Mutually exclusive args.
# (They will always be listed last, contrary to Argparser, but it doesn't
# really matter)
for group in gui_parser.exclusive_groups:
exclusive_list = list(group.arguments_list.keys())

if group.required:
raise NotImplementedError("TODO")
with dpg.tree_node(label="Select maximum one", default_open=True,
indent=0):
for argname, params in group.arguments_list.items():
if argname[0] != '-':
raise NotImplementedError(
"Implementation error. Please contact our developers "
"with this information: Parameter {} is in an "
"exclusive group, so NOT required, but its name does "
"not start with '-'".format(argname))
add_one_arg_and_help_to_gui(
argname, params, known_file_dialogs,
exclusive_list=exclusive_list)
Loading
Loading