extends TouchVerticalContainer class_name Stage signal save # (database_entry: Dictionary) signal discard # () const OPTION_SETS_FILE_PATH: String = "user://option_sets.json" var staged_entry := {} var option_sets := { "place": { "P0": null, "P1": null, "P2": null, "xpto_01": null, "xpto_02": null, "xpto_03": null, "xpto_04": null, "xpto_05": null, "xpto_06": null, "xpto_07": null, "xpto_08": null, "xpto_09": null, "xpto_10": null, "xpto_11": null, "xpto_12": null, "xpto_13": null, "xpto_14": null, "xpto_15": null, "xpto_16": null, "xpto_17": null, "xpto_18": null, "xpto_19": null, "xpto_20": null, "xpto_21": null, "xpto_22": null, "xpto_23": null, "xpto_24": null, "xpto_25": null, "xpto_26": null, "xpto_27": null, "xpto_28": null, "xpto_29": null, }, "first_assistant": { "FA0": null, "FA1": null, "FA2": null, }, "anesthesia": { "AN0": null, "AN1": null, "AN2": null, }, "type": { "A": { "sub_type": { "aA": { "sub_sub_type": { "aaA": null, "aaB": null, "aaC": null, }, "pathology": { "aaP0": null, "aaP1": null, "aaP2": null, }, "intervention": { "aaI0": null, "aaI1": null, "aaI2": null, }, }, "aB": { "sub_sub_type": { "abA": null, "abB": null, "abC": null, }, "pathology": { "abP0": null, "abP1": null, "abP2": null, }, "intervention": { "abI0": null, "abI1": null, "abI2": null, }, }, }, }, "B": { "sub_type": { "bA": null, "bB": null, }, }, "C": null, }, } onready var process_id := get_node("controls/process_id") as LineEdit onready var surgery_id := get_node("controls/surgery_id") as LineEdit onready var date := get_node("controls/date_picker") as DatePicker onready var place := get_node("controls/place") as LineEdit onready var anesthesia := get_node("controls/anesthesia") as LineEdit onready var first_assistant := get_node("controls/first_assistant") as LineEdit onready var type := get_node("controls/type") as LineEdit onready var sub_type := get_node("controls/sub_type") as LineEdit onready var sub_sub_type := get_node("controls/sub_sub_type") as LineEdit onready var pathology := get_node("controls/pathology") as LineEdit onready var intervention := get_node("controls/intervention") as LineEdit onready var is_urgency := get_node("controls/is_urgency") as Button onready var notes := get_node("controls/notes") as LineEdit onready var save_button := get_node("controls/save") as Button 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(): save_button.connect("pressed", self, "save_action") discard_button.connect("pressed", self, "discard_action") for it in get_node("controls").get_children(): it = it as Control if it is LineEdit: it.connect("focus_entered", it, "set_cursor_position", [99999999]) # @DAM Use MAX_INT it.connect("focus_exited", it, "deselect") # 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) # @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] # @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() func save_action(): self.visible = false emit_signal("save", get_stage()) func discard_action(): self.visible = false emit_signal("discard") func set_stage(entry: Dictionary): staged_entry = entry.duplicate(true) process_id.text = staged_entry.process_id surgery_id.text = staged_entry.surgery_id date.set_date(staged_entry.date_year, staged_entry.date_month, staged_entry.date_day) place.text = staged_entry.place anesthesia.text = staged_entry.anesthesia first_assistant.text = staged_entry.first_assistant type.text = staged_entry.type sub_type.text = staged_entry.sub_type sub_sub_type.text = staged_entry.sub_sub_type pathology.text = staged_entry.pathology intervention.text = staged_entry.intervention is_urgency.pressed = staged_entry.is_urgency notes.text = staged_entry.notes self.scroll_vertical = 0 func get_stage() -> Dictionary: staged_entry.process_id = process_id.text staged_entry.surgery_id = surgery_id.text staged_entry.date_year = date.get_year() staged_entry.date_month = date.get_month() staged_entry.date_day = date.get_day() staged_entry.place = place.text staged_entry.anesthesia = anesthesia.text staged_entry.first_assistant= first_assistant.text staged_entry.type = type.text staged_entry.sub_type = sub_type.text staged_entry.sub_sub_type = sub_sub_type.text staged_entry.pathology = pathology.text staged_entry.intervention = intervention.text staged_entry.is_urgency = is_urgency.pressed staged_entry.notes = notes.text return staged_entry func load_option_sets(file_path: String = OPTION_SETS_FILE_PATH): var file := File.new() file.open(file_path, File.READ_WRITE) 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 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): option_sets = {} if save_changes: store_option_sets() func pointer_input_on_click_handler(pointer: PointerInputSensor.PointerInputData): .pointer_input_on_click_handler(pointer) var target: Control = pointer.target.get_parent() var button: Button if target is Button: button = target elif target.get_node("auto") is Button: button = target.get_node("auto") if button != null && button.get_global_rect().has_point(pointer.current_position): if button is CheckBox || button is CheckButton: button.pressed = !button.pressed button.emit_signal("button_down") button.emit_signal("pressed") button.emit_signal("button_up")