2023-03-03 21:12:54 +00:00
|
|
|
class_name TimeEntriesItemsTree extends Tree
|
|
|
|
|
|
|
|
enum COL{
|
|
|
|
TEXT,
|
|
|
|
TIME
|
|
|
|
}
|
|
|
|
|
2023-03-04 13:31:27 +00:00
|
|
|
var config: ConfigManager = preload("res://config_manager.tres")
|
|
|
|
|
2023-03-09 23:25:50 +00:00
|
|
|
var _timer := Timer.new()
|
|
|
|
|
2023-03-03 21:12:54 +00:00
|
|
|
|
2023-03-04 13:31:27 +00:00
|
|
|
func _ready() -> void:
|
2023-03-09 23:25:50 +00:00
|
|
|
hide_root = true
|
|
|
|
add_child(_timer)
|
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
connect("button_pressed", self, "_on_button_pressed")
|
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
_timer.connect("timeout", self, "_on_timer_timeout")
|
2023-03-09 20:26:57 +00:00
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
config.connect("file_changed", self, "populate_entries")
|
2023-03-09 23:25:50 +00:00
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
config.connect("time_sheet_reloaded", self, "populate_entries")
|
2023-03-04 13:31:27 +00:00
|
|
|
populate_entries()
|
|
|
|
|
|
|
|
|
|
|
|
func populate_entries() -> void:
|
|
|
|
clear()
|
2023-03-09 23:25:50 +00:00
|
|
|
var tree_items_root := create_item()
|
|
|
|
var item_entries_tree := config.timesheet.tree
|
|
|
|
_populate_from_entry(tree_items_root, item_entries_tree)
|
|
|
|
_timer.start()
|
|
|
|
#for item in entries:
|
2023-03-09 20:26:57 +00:00
|
|
|
# warning-ignore:return_value_discarded
|
2023-03-09 23:25:50 +00:00
|
|
|
# set_entry(entry)
|
2023-03-04 13:31:27 +00:00
|
|
|
|
2023-03-03 21:12:54 +00:00
|
|
|
|
2023-03-09 23:25:50 +00:00
|
|
|
# warning-ignore:unused_argument
|
2023-03-03 21:12:54 +00:00
|
|
|
func set_time_elapsed(total_elapsed: int) -> void:
|
2023-03-09 23:25:50 +00:00
|
|
|
#current_item.set_text(COL.TIME, TimeEntry.time_to_period(total_elapsed))
|
|
|
|
#current_item.set_metadata(COL.TIME, total_elapsed)
|
|
|
|
pass
|
2023-03-03 21:12:54 +00:00
|
|
|
|
|
|
|
|
2023-03-09 23:25:50 +00:00
|
|
|
func set_current_item(_task_name: String) -> void:
|
|
|
|
#current_item = append_name_to_tree(task_name, 0)
|
|
|
|
pass
|
2023-03-03 21:12:54 +00:00
|
|
|
|
|
|
|
|
2023-03-09 23:25:50 +00:00
|
|
|
func _on_timer_timeout() -> void:
|
|
|
|
config.timesheet.update()
|
2023-03-03 21:12:54 +00:00
|
|
|
|
2023-03-09 23:25:50 +00:00
|
|
|
|
|
|
|
func _populate_from_entry(tree_item_root: TreeItem, time_entry_item_root: TimeEntryTreeItem):
|
|
|
|
var children := time_entry_item_root.children
|
|
|
|
for time_entry_name in children:
|
|
|
|
var time_entry_item: TimeEntryTreeItem = children[time_entry_name]
|
|
|
|
var item := find_or_create_item(tree_item_root, time_entry_name)
|
|
|
|
item.set_metadata(COL.TEXT, time_entry_item)
|
|
|
|
item.set_text(COL.TIME, time_entry_item.get_period())
|
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
time_entry_item.connect("end_time_updated", self, "_on_time_entry_changed_update_item", [time_entry_item, item])
|
|
|
|
_populate_from_entry(item, time_entry_item)
|
|
|
|
var entries = time_entry_item_root.time_entries
|
|
|
|
for entry_item in entries:
|
|
|
|
var time_entry_item := entry_item as TimeEntryTreeItem
|
|
|
|
var item := create_item(tree_item_root)
|
|
|
|
var time_entry := time_entry_item.time_entry
|
|
|
|
item.set_text(COL.TEXT, time_entry.start_time.to_string())
|
|
|
|
item.set_metadata(COL.TEXT, time_entry_item)
|
|
|
|
item.set_text(COL.TIME, time_entry_item.get_period())
|
|
|
|
if time_entry.is_closed == false:
|
|
|
|
var texture := preload("res://assets/stop_small.svg")
|
|
|
|
item.add_button(COL.TIME, texture)
|
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
time_entry_item.connect("end_time_updated", self, "_on_time_entry_changed_update_item", [time_entry_item, item])
|
2023-03-03 21:12:54 +00:00
|
|
|
|
|
|
|
|
2023-03-09 23:25:50 +00:00
|
|
|
func _on_time_entry_changed_update_item(time_entry_item: TimeEntryTreeItem, item: TreeItem) -> void:
|
|
|
|
item.set_text(COL.TIME, time_entry_item.get_period())
|
|
|
|
|
|
|
|
|
|
|
|
func _on_button_pressed(item: TreeItem, _column: int, _id: int) -> void:
|
|
|
|
var time_entry_tree_item: TimeEntryTreeItem = item.get_metadata(COL.TEXT)
|
|
|
|
if time_entry_tree_item == null:
|
|
|
|
return
|
|
|
|
var time_entry := time_entry_tree_item.time_entry
|
|
|
|
if time_entry == null:
|
|
|
|
return
|
|
|
|
if time_entry.is_closed:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
time_entry.close()
|
|
|
|
item.erase_button(COL.TIME, 0)
|
2023-03-03 21:12:54 +00:00
|
|
|
|
|
|
|
|
2023-03-09 20:26:57 +00:00
|
|
|
## Unecessary in Godot 4, can bre replaced with get_children()
|
|
|
|
static func _get_tree_item_children(item: TreeItem):
|
|
|
|
var children = []
|
|
|
|
var child = item.get_children()
|
|
|
|
if child == null:
|
|
|
|
return children
|
|
|
|
children.append(child)
|
|
|
|
child = child.get_next()
|
|
|
|
while child != null:
|
|
|
|
children.append(child)
|
|
|
|
child = child.get_next()
|
|
|
|
return children
|
|
|
|
|
|
|
|
|
2023-03-03 21:12:54 +00:00
|
|
|
## Finds an item in the tree by text
|
2023-03-09 23:25:50 +00:00
|
|
|
func find_item(root: TreeItem, item_name: String) -> TreeItem:
|
2023-03-09 20:26:57 +00:00
|
|
|
for child in _get_tree_item_children(root):
|
2023-03-03 21:12:54 +00:00
|
|
|
if child.get_text(COL.TEXT) == item_name:
|
|
|
|
return child
|
|
|
|
return null
|
2023-03-09 23:25:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
func find_or_create_item(root: TreeItem, item_name: String) -> TreeItem:
|
|
|
|
var child := find_item(root, item_name)
|
|
|
|
if child != null:
|
|
|
|
return child
|
|
|
|
child = create_item(root)
|
|
|
|
child.set_text(COL.TEXT, item_name)
|
|
|
|
return child
|