diff options
Diffstat (limited to 'logic')
| -rw-r--r-- | logic/database.gd | 1 | ||||
| -rw-r--r-- | logic/stage.gd | 95 |
2 files changed, 48 insertions, 48 deletions
diff --git a/logic/database.gd b/logic/database.gd index b76fda5..d88b710 100644 --- a/logic/database.gd +++ b/logic/database.gd @@ -143,6 +143,7 @@ func load_database(file_path: String = DATABASE_FILE_PATH): _: entry[field_name] = field_value db.append(entry) + file.close() func store_database(file_path: String = DATABASE_FILE_PATH): diff --git a/logic/stage.gd b/logic/stage.gd index 3c3d59c..4c1521c 100644 --- a/logic/stage.gd +++ b/logic/stage.gd @@ -4,7 +4,7 @@ class_name Stage signal save # (database_entry: Dictionary) signal discard # () -const OPTION_SETS_FILE_PATH: String = "user://option_sets.csv" +const OPTION_SETS_FILE_PATH: String = "user://option_sets.json" var staged_entry := {} var option_sets := { @@ -120,6 +120,8 @@ onready var discard_button := get_node("controls/discard") as Button func _init(): exclude_controls = ["date_picker", "save", "discard"] + load_option_sets() + store_option_sets() # @DAM Only for testing. func _ready(): @@ -132,20 +134,46 @@ func _ready(): it.connect("focus_entered", it, "set_cursor_position", [99999999]) # @DAM Use MAX_INT it.connect("focus_exited", it, "deselect") - - var auto_place := place.get_node("auto") as Button - auto_place.connect("pressed", self, "auto_populate", ["place"]) + # Map option sets buttons. + var option_sets_map := { + "place": place, + "anesthesia": anesthesia, + "first_assistant": first_assistant, + "type": type, + "sub_type": sub_type, + "sub_sub_type": sub_sub_type, + "pathology": pathology, + "intervention": intervention + } + for key in option_sets_map: + var button := option_sets_map[key].get_node("auto") as Button + button.connect("pressed", self, "auto_populate", [key]) + + +func get_option_sets(field: String): + var options: Array + # @DAM WIP Improve match to check if dictionaries being accessed are not null. + match field: + "sub_type": + options = option_sets["type"][type.text][field].keys() + "sub_sub_type", "pathology", "intervention": + options = option_sets["type"][type.text]["sub_type"][sub_type.text][field].keys() + _: + options = option_sets[field].keys() + return options func auto_populate(field: String): var stage_options = get_node("/root/main/popup_list") as Popup stage_options.connect("item_selected", self, "auto_selected", [field], CONNECT_ONESHOT) - stage_options.popup_options(option_sets[field].keys()) + # @DAM WIP Check if we can use directly the output of the get_option_sets(field). + stage_options.popup_options(get_option_sets(field)) func auto_selected(index: int, field: String): var field_input: LineEdit = self[field] - field_input.text = option_sets[field].keys()[index] + # @DAM WIP Check if we can use the index directly on the output of the get_option_sets(field). + field_input.text = get_option_sets(field)[index] field_input.caret_position = field_input.text.length() @@ -199,54 +227,25 @@ func get_stage() -> Dictionary: func load_option_sets(file_path: String = OPTION_SETS_FILE_PATH): var file := File.new() file.open(file_path, File.READ_WRITE) - var headers: PoolStringArray - var is_first_line := true - while file.get_position() < file.get_len(): - var csv_entry := file.get_csv_line() - if is_first_line: - is_first_line = false - headers = csv_entry - continue -# var entry = DatabaseEntry.instance_entry() -# for idx in headers.size(): -# var field_name := headers[idx] -# var field_value := csv_entry[idx] -# match field_name: -# "date_year", "date_month", "date_day": -# entry[field_name] = int(field_value) -# "is_urgency": -# entry[field_name] = true if field_value.strip_edges().to_lower() == "true" else false -# _: -# entry[field_name] = field_value -# option_sets.append(entry) + var file_content = file.get_as_text() + var parse_result = JSON.parse(file_content) + if parse_result.error == OK && typeof(parse_result.result) == TYPE_DICTIONARY: + option_sets = parse_result.result + else: + option_sets = {} + push_error("Failed to parse option sets file: '%s'.") + file.close() func store_option_sets(file_path: String = OPTION_SETS_FILE_PATH): - var list: Array - var entry := DatabaseEntry.instance_entry() - if option_sets.has("type"): - for type in option_sets["type"].keys(): - entry.type = type - if option_sets["type"][type].has("sub_type"): - for sub_type in option_sets["type"][type]["sub_type"]: - entry.sub_type = sub_type - if option_sets["type"][type]["sub_type"].has("sub_sub_type"): - for sub_sub_type in option_sets["type"][type]["sub_type"][sub_type]["sub_sub_type"].keys(): - entry.sub_sub_type = sub_sub_type - list.append(entry.duplicate()) - else: - pass - else: - pass - - -# if option_sets.has("type"): -# - pass # @DAM TODO + var file := File.new() + file.open(file_path, File.WRITE) + file.store_string(JSON.print(option_sets, "" if file_path == OPTION_SETS_FILE_PATH else "\t")) + file.close() func clear_option_sets(save_changes: bool = false): - pass # @DAM TODO + option_sets = {} if save_changes: store_option_sets() |
