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
|
extends TouchItemList
class_name Database
const DATABASE_FILE_PATH: String = "user://database.json"
const DATABASE_FILE_VERSION: int = 1
var db: Array
var selected_idx: int
var staged_idx: int
onready var confirm_action := get_node("/root/main/confirm_action") as ConfirmationDialog
onready var stage := get_node("/root/main/stage") as Stage
onready var delete_button := get_node("actions/delete") as Button
onready var edit_button := get_node("actions/edit") as Button
onready var add_button := get_node("actions/add") as Button
func _init():
selected_idx = -1
staged_idx = -1
load_database()
func _ready():
self.connect("item_selected", self, "item_selected")
self.connect("nothing_selected", self, "clear_selection")
delete_button.connect("pressed", self, "delete_action")
edit_button.connect("pressed", self, "edit_action")
add_button.connect("pressed", self, "add_action")
stage.connect("save", self, "save_stage")
stage.connect("discard", self, "discard_stage")
for it in db:
self.add_item(get_entry_view(it))
clear_selection()
func _notification(what: int):
# @DAM This code should be moved into the main.gd which should check which node was currently
# active and above, and send the signal there.
if visible == false || has_focus() == false:
return
if what == MainLoop.NOTIFICATION_WM_GO_BACK_REQUEST:
if selected_idx >= 0:
clear_selection()
else:
get_tree().quit()
func get_entry_view(database_entry: Dictionary) -> String:
return "%6s | %6s | %s" % [database_entry.process_id, database_entry.surgery_id, DatabaseEntry.get_entry_date(database_entry)]
func item_selected(index: int):
selected_idx = index
fade_action_buttons(1.0)
func clear_selection():
selected_idx = -1
unselect_all()
fade_action_buttons(0.25)
func fade_action_buttons(target_alpha: float):
get_node("actions/delete").modulate.a = target_alpha
get_node("actions/edit").modulate.a = target_alpha
func delete_action():
if selected_idx < 0:
return
confirm_action.dialog_text = "Do you want to delete entry with process ID '%s' from the database?" % db[selected_idx].process_id
confirm_action.connect("confirmed", self, "delete_action_confirmed", [], CONNECT_ONESHOT)
confirm_action.show_modal(true)
func delete_action_confirmed():
db.remove(selected_idx)
self.remove_item(selected_idx)
selected_idx = -1
save_database()
clear_selection()
func edit_action():
if selected_idx < 0:
return
staged_idx = selected_idx
self.visible = false
stage.visible = true
var staged := (db[staged_idx] as Dictionary).duplicate(true)
stage.set_stage(staged)
func add_action():
self.visible = false
stage.visible = true
var staged := DatabaseEntry.instance_entry()
stage.set_stage(staged)
func save_stage(entry: Dictionary):
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
next_selected_idx = staged_idx
else:
db.append(entry)
self.add_item(get_entry_view(entry))
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]))
ensure_current_is_visible()
save_database()
staged_idx = -1
self.visible = true
grab_focus()
func discard_stage():
staged_idx = -1
self.visible = true
grab_focus()
func load_database(file_path: String = DATABASE_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:
push_error("Failed to parse database file: '%s'.")
return
if parse_result.result["version"] != DATABASE_FILE_VERSION:
push_error("Invalid database file version '%s', expected '%s'." % DATABASE_FILE_VERSION)
return
if typeof(parse_result.result["database"]) != TYPE_ARRAY:
push_error("Failed to load database file contents.")
return
db = parse_result.result["database"]
# The JSON specification does not define integer or float types, but only a number type.
# Therefore, converting a Variant to JSON text will convert all numerical values to float types.
# Thus, we cast all integer values once we load them.
for it in db:
it.date_year = int(it.date_year)
it.date_month = int(it.date_month)
it.date_day = int(it.date_day)
func save_database(file_path: String = DATABASE_FILE_PATH):
var database_file := {
"version": DATABASE_FILE_VERSION,
"database": db,
}
var indentation_char := "" if file_path == DATABASE_FILE_PATH else "\t"
var file_content := JSON.print(database_file, indentation_char)
var file := File.new()
file.open(file_path, File.WRITE)
file.store_string(file_content)
file.close()
static func import_database(file_path: String) -> Array:
var result: Array
var file := File.new()
file.open(file_path, File.READ_WRITE)
var headers := file.get_csv_line()
while file.get_position() < file.get_len():
var csv_entry := file.get_csv_line()
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":
DatabaseEntry.set_entry_date(entry, field_value)
"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
_:
if DatabaseEntry.ENTRY_PROTOTYPE.has(field_name):
entry[field_name] = field_value
if DatabaseEntry.is_valid_entry(entry):
result.append(entry)
file.close()
return result
static func export_database(file_path: String, database: Array = db):
var file := File.new()
file.open(file_path, File.WRITE)
var header := PoolStringArray(DatabaseEntry.ENTRY_PROTOTYPE.keys())
file.store_csv_line(header)
for it in database:
# @DAM This approach depends on the order the dictionary fields are created.
file.store_csv_line(it.values())
file.close()
func clear_database():
clear_selection()
self.clear()
db.resize(0)
func fake_database():
clear_database()
for idx in range(500):
var today := OS.get_date(true)
var date_year = today.year + int(float(idx) / 30.0 / 12)
var date_month = 1 + int(float(idx) / 30.0) % 12
var date_day = 1 + (idx % 30)
var fake_entry = DatabaseEntry.instance_entry({
"process_id": "%06d" % idx,
"surgery_id": "s%05d" % idx,
"date_year": date_year,
"date_month": date_month,
"date_day": date_day,
})
db.append(fake_entry)
self.add_item(get_entry_view(fake_entry))
|