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:
|
2023-03-09 20:26:57 +00:00
|
|
|
# warning-ignore:return_value_discarded
|
|
|
|
config.connect("file_changed", self, "populate_entries")
|
2023-03-04 13:31:27 +00:00
|
|
|
populate_entries()
|
|
|
|
|
|
|
|
|
|
|
|
func populate_entries() -> void:
|
|
|
|
clear()
|
|
|
|
var _root := create_item()
|
|
|
|
var entries_names := config.timesheet.entries_names
|
|
|
|
for entry_name in entries_names:
|
2023-03-09 20:26:57 +00:00
|
|
|
# warning-ignore:return_value_discarded
|
2023-03-04 13:31:27 +00:00
|
|
|
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 ""
|
2023-03-05 11:43:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
func find_item(root: TreeItem, item_name: String, or_create: bool) -> 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
|
|
|
|
if or_create:
|
2023-03-09 20:26:57 +00:00
|
|
|
var child := create_item(root)
|
2023-03-03 21:12:54 +00:00
|
|
|
child.set_text(COL.TEXT, item_name)
|
|
|
|
child.set_text(COL.TIME, Consts.NO_TIME)
|
|
|
|
return child
|
|
|
|
return null
|