From 431f042390ad36297a5ec986772c77da23b7fb67 Mon Sep 17 00:00:00 2001 From: dam Date: Thu, 6 Jan 2022 15:42:44 +0000 Subject: Remove date field and reduce usage of dictionary instances. --- logic/database.gd | 17 +++++++------ logic/database_entry.gd | 20 +++++++++++---- logic/stage.gd | 67 ++++++++++++++++++++++++------------------------- 3 files changed, 58 insertions(+), 46 deletions(-) diff --git a/logic/database.gd b/logic/database.gd index 551b9bf..044681f 100644 --- a/logic/database.gd +++ b/logic/database.gd @@ -77,6 +77,10 @@ func clear_selection(): unselect_all() +func scroll_to_selected(): + v_scroll_bar.value = float(selected_idx)/float(db.size()) * v_scroll_bar.max_value - (v_scroll_bar.page * 0.5) + + func delete_action(): if selected_idx < 0: return @@ -104,12 +108,11 @@ func add_action(): stage.set_stage(staged) -func scroll_down(): - v_scroll_bar.value = v_scroll_bar.max_value - - func save_stage(entry: Dictionary): - entry = DatabaseEntry.instance_entry(entry) # @DAM Maybe we could not be creating endless dictionaries? + if DatabaseEntry.is_valid_entry(entry) == false: + printerr("INVALID ENTRY RECEIVED") + return # @DAM Deal with this. + var next_selected_idx: int if staged_idx >= 0: db[staged_idx] = entry @@ -117,13 +120,12 @@ func save_stage(entry: Dictionary): else: db.append(entry) add_item(get_entry_view(entry)) - call_deferred("scroll_down") next_selected_idx = db.size() - 1 select(next_selected_idx) emit_signal("item_selected", next_selected_idx) # Calling "select" does not trigger the "item_selected" signal. set_item_text(selected_idx, get_entry_view(db[selected_idx])) - v_scroll_bar.value = float(selected_idx)/float(db.size()) * v_scroll_bar.max_value - (v_scroll_bar.page * 0.5) + call_deferred("scroll_to_selected") store_database() @@ -171,6 +173,7 @@ func store_database(file_path: String = DATABASE_FILE_PATH): file.store_csv_line(header) var entry := PoolStringArray() for it in db: + # @DAM This approach depends on the order the dictionary fields are created. file.store_csv_line(it.values()) file.close() diff --git a/logic/database_entry.gd b/logic/database_entry.gd index 19f2255..496b752 100644 --- a/logic/database_entry.gd +++ b/logic/database_entry.gd @@ -6,11 +6,10 @@ const DATE_FORMAT: String = "%04d-%02d-%02d" const ENTRY_PROTOTYPE: Dictionary = { "process_id": "", "surgery_id": "", - "place": "", - "date": "", "date_year": 0, "date_month": 0, "date_day": 0, + "place": "", "anesthesia": "", "first_assistant": "", "type": "", @@ -27,14 +26,13 @@ static func instance_entry(params: Dictionary = {}) -> Dictionary: var new_entry := ENTRY_PROTOTYPE.duplicate(true) new_entry.process_id = params.get("process_id", "") new_entry.surgery_id = params.get("surgery_id", "") - new_entry.place = params.get("place", "") var today = OS.get_date() new_entry.date_year = params.get("date_year", today.year) new_entry.date_month = params.get("date_month", today.month) new_entry.date_day = params.get("date_day", today.day) - new_entry.date = params.get("date", get_entry_date(new_entry)) # @DAM We should store only one version of the date. + new_entry.place = params.get("place", "") new_entry.anesthesia = params.get("anesthesia", "") new_entry.first_assistant = params.get("first_assistant", "") new_entry.type = params.get("type", "") @@ -48,6 +46,19 @@ static func instance_entry(params: Dictionary = {}) -> Dictionary: return new_entry +static func is_valid_entry(entry: Dictionary) -> bool: + var is_valid: bool + + is_valid = entry.has_all(ENTRY_PROTOTYPE.keys()) && ENTRY_PROTOTYPE.keys().size() == entry.keys().size() + + for it in ENTRY_PROTOTYPE.keys(): + if typeof(ENTRY_PROTOTYPE[it]) != typeof(entry[it]): + is_valid = false + break + + return is_valid + + static func get_entry_date(entry: Dictionary) -> String: return DATE_FORMAT % [entry.date_year, entry.date_month, entry.date_day] @@ -56,7 +67,6 @@ static func set_entry_date(entry: Dictionary, date: String): date = date.strip_edges().replace(" ", DATE_SEPARATOR).replace("/", DATE_SEPARATOR).replace("\\", DATE_SEPARATOR) var year_month_idx := date.find(DATE_SEPARATOR) var month_day_idx := date.find(DATE_SEPARATOR, year_month_idx + 1) - entry.date = date entry.date_year = int(date.substr(0, year_month_idx)) entry.date_month = int(date.substr(year_month_idx + 1, month_day_idx - year_month_idx - 1)) entry.date_day = int(date.substr(month_day_idx + 1)) diff --git a/logic/stage.gd b/logic/stage.gd index 511068e..f6f6e47 100644 --- a/logic/stage.gd +++ b/logic/stage.gd @@ -7,9 +7,10 @@ signal discard # () const FILTERS_FILE_PATH: String = "user://filters.csv" const POINTER_VELOCITY_DECAYING_FACTOR: float = 2.5 +var staged_entry := {} +var filters := {} var is_pointer_dragging := false var pointer_drag_velocity := 0.0 -var filters := {} onready var process_id := get_node("controls/process_id") as LineEdit onready var surgery_id := get_node("controls/surgery_id") as LineEdit @@ -79,42 +80,40 @@ func discard_action(): func set_stage(entry: Dictionary): - process_id.text = entry.process_id - surgery_id.text = entry.surgery_id - date.set_date(entry.date_year, entry.date_month, entry.date_day) - place.text = entry.place - anesthesia.text = entry.anesthesia - first_assistant.text = entry.first_assistant - type.text = entry.type - sub_type.text = entry.sub_type - sub_sub_type.text = entry.sub_sub_type - pathology.text = entry.pathology - intervention.text = entry.intervention - is_urgency.pressed = entry.is_urgency - notes.text = entry.notes - self.scroll_vertical = 0 # @DAM TODO + 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: - # @DAM Simplify all this... avoid creating multiple entries/dictionaries. - var entry: Dictionary = { - "process_id": process_id.text, - "surgery_id": surgery_id.text, - "date_year": date.get_year(), - "date_month": date.get_month(), - "date_day": date.get_day(), - "place": place.text, - "anesthesia": anesthesia.text, - "first_assistant": first_assistant.text, - "type": type.text, - "sub_type": sub_type.text, - "sub_sub_type": sub_sub_type.text, - "pathology": pathology.text, - "intervention": intervention.text, - "is_urgency": is_urgency.pressed, - "notes": notes.text, - } - return entry + 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 _notification(what: int): -- cgit v1.2.3