67 lines
1.7 KiB
GDScript
67 lines
1.7 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:
|
|
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])
|
|
|
|
|
|
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_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
|
|
|
|
|
|
## 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
|