aboutsummaryrefslogtreecommitdiff
path: root/logic/stage.gd
blob: 0c2f97be6f43d42c3bdb0d8430272fa28ba78e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
extends TouchVerticalContainer
class_name Stage

signal save		# (database_entry: Dictionary)
signal discard	# ()

const OPTION_SETS_FILE_PATH: String = "user://option_sets.json"
const OPTION_SETS_NOT_AVAILABLE: String = "--"
const OPTION_SETS_TREE_STRUCTURE := {
	"place": null,
	"anesthesia": null,
	"first_assistant": null,
	"type": {
		"sub_type": {
			"sub_sub_type": null,
			"pathology": null,
			"intervention": null,
		}
	}
}

var option_sets := {
	"thing": {},
	"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
onready 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
}

func _init():
	exclude_controls = ["date_picker", "save", "discard"]
	load_option_sets()


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.
	# @DAM TEST defining he option_sets_map onready
#	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 is_sub_dictionary(dictionary: Dictionary, key: String) -> bool:
	return dictionary.has(key) && dictionary[key] is Dictionary


func get_option_sets(field: String):
	var options: Array
	
	match field:
		"sub_type":
			if option_sets["type"].get(type.text) != null:
				options = option_sets["type"][type.text][field].keys()
		"sub_sub_type", "pathology", "intervention":
			if option_sets["type"].get(type.text) != null && option_sets["type"][type.text]["sub_type"].get(sub_type.text) != null:
				options = option_sets["type"][type.text]["sub_type"][sub_type.text][field].keys()
		_:
			options = option_sets[field].keys()
	
	if options.size() == 0:
		options.append(OPTION_SETS_NOT_AVAILABLE)
	
	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(get_option_sets(field))


func auto_selected(index: int, field: String):
	var field_input: LineEdit = self[field]
	field_input.text = get_option_sets(field)[index]
	field_input.caret_position = field_input.text.length()


func save_action():
	self.visible = false
	var staged_entry = get_stage()
	gather_new_option_sets(staged_entry)
	emit_signal("save", staged_entry)


func discard_action():
	self.visible = false
	emit_signal("discard")


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


func get_stage() -> Dictionary:
	var entry := {
		"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


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()
	file.close()
	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'.")

	# @DAM Only do sanitize_option_sets_dict if file_path is no the default one.
	sanitize_option_sets_dict(OPTION_SETS_TREE_STRUCTURE, option_sets)
#	if file_path != OPTION_SETS_FILE_PATH:
#		sanitize_option_sets()


func sanitize_option_sets_dict(blueprint: Dictionary, test: Dictionary):
	for key in blueprint:
		if test.get(key) == null:
			test[key] = {}
		if blueprint[key] != null:
			for sub_value in test[key]:
				if test[key][sub_value] == null:
					test[key][sub_value] = {}
				sanitize_option_sets_dict(blueprint[key], test[key][sub_value])


func gather_new_option_sets(entry: Dictionary):
	pass
	# @DAM TODO WIP
#	WIP WIP WIP
	if entry["type"] != OPTION_SETS_NOT_AVAILABLE:
		if option_sets["type"].has(entry["type"]) == false:
			
#		option_sets["type"][entry["type"]] = {}


func sanitize_option_sets():
	for key in OPTION_SETS_TREE_STRUCTURE:
		if option_sets.get(key) == null:
			option_sets[key] = {}
	
	for type_key in option_sets["type"]:
		var type_value := option_sets["type"][type_key] as Dictionary
		if type_value.get("sub_type") == null:
			type_value["sub_type"] = {}
		else:
			for sub_type_key in type_value["sub_type"]:
				var sub_type_value := type_value["sub_type"][sub_type_key] as Dictionary
				if sub_type_value.get("sub_sub_type") == null:
					sub_type_value["sub_sub_type"] = {}
				if sub_type_value.get("pathology") == null:
					sub_type_value["pathology"] = {}
				if sub_type_value.get("intervention") == null:
					sub_type_value["intervention"] = {}


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")