rat-times/ui/time_entries_items_tree.gd

70 lines
1.7 KiB
GDScript3
Raw Permalink Normal View History

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-03 21:12:54 +00:00
var current_item: TreeItem
2023-03-04 13:31:27 +00:00
func _ready() -> void:
config.file_changed.connect(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:
append_name_to_tree(entry_name, entries_names[entry_name])
2023-03-03 21:12:54 +00:00
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 ""
2023-03-03 21:12:54 +00:00
## Adds a new item to the tree, or returns the old item if it exists
2023-03-04 13:31:27 +00:00
func append_name_to_tree(task_name: String, total_elapsed := 0) -> TreeItem:
2023-03-03 21:12:54 +00:00
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)
2023-03-04 13:31:27 +00:00
item.set_text(COL.TIME, TimeEntry.time_to_period(total_elapsed))
item.set_metadata(COL.TIME, total_elapsed)
2023-03-03 21:12:54 +00:00
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