rat-times/ui/tasks_list.gd
2023-03-10 00:26:57 +04:00

86 lines
2.2 KiB
GDScript

class_name TimeEntriesItemsTree extends Tree
enum COL{
TEXT,
TIME
}
var config: ConfigManager = preload("res://config_manager.tres")
var current_item: TreeItem
func _ready() -> void:
# warning-ignore:return_value_discarded
config.connect("file_changed", self, "populate_entries")
populate_entries()
func populate_entries() -> void:
clear()
var _root := create_item()
var entries_names := config.timesheet.entries_names
for entry_name in entries_names:
# warning-ignore:return_value_discarded
append_name_to_tree(entry_name, entries_names[entry_name])
func set_time_elapsed(total_elapsed: int) -> void:
current_item.set_text(COL.TIME, TimeEntry.time_to_period(total_elapsed))
current_item.set_metadata(COL.TIME, total_elapsed)
func set_current_item(task_name: String) -> void:
current_item = append_name_to_tree(task_name, 0)
func get_current_text() -> String:
var item := get_selected()
if not item:
return ""
var resp = item.get_metadata(COL.TEXT)
if resp is String:
return resp
return ""
## Adds a new item to the tree, or returns the old item if it exists
func append_name_to_tree(task_name: String, total_elapsed := 0) -> TreeItem:
var item := get_root()
for item_name in task_name.split("/"):
item.collapsed = false
item = find_item(item, item_name, true)
item.set_metadata(COL.TEXT, task_name)
item.set_text(COL.TIME, TimeEntry.time_to_period(total_elapsed))
item.set_metadata(COL.TIME, total_elapsed)
scroll_to_item(item)
return item
## 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
## Finds an item in the tree by text
func find_item(root: TreeItem, item_name: String, or_create: bool) -> TreeItem:
for child in _get_tree_item_children(root):
if child.get_text(COL.TEXT) == item_name:
return child
if or_create:
var child := create_item(root)
child.set_text(COL.TEXT, item_name)
child.set_text(COL.TIME, Consts.NO_TIME)
return child
return null