rat-times/ui/time_entries_items_tree.gd
2023-03-04 01:12:54 +04:00

53 lines
1.3 KiB
GDScript

class_name TimeEntriesItemsTree extends Tree
enum COL{
TEXT,
TIME
}
var current_item: TreeItem
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: String = item.get_metadata(COL.TEXT)
return resp
## Adds a new item to the tree, or returns the old item if it exists
func append_name_to_tree(task_name: String, total_time := 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)
if not item.get_metadata(COL.TIME):
item.set_metadata(COL.TIME, total_time)
scroll_to_item(item)
return item
## Finds an item in the tree by text
func find_item(root: TreeItem, item_name: String, or_create: bool) -> TreeItem:
for child in root.get_children():
if child.get_text(COL.TEXT) == item_name:
return child
if or_create:
var child := root.create_child()
child.set_text(COL.TEXT, item_name)
child.set_text(COL.TIME, Consts.NO_TIME)
return child
return null