From ef15d4696af4e24d51e46502bfd0855870f7c7da Mon Sep 17 00:00:00 2001 From: Xananax Date: Sat, 4 Mar 2023 17:31:27 +0400 Subject: [PATCH] refactor --- scripts/config_manager.gd | 60 ++++++++++++++---- ui/Main.gd | 88 +++++--------------------- ui/Main.tscn | 112 +-------------------------------- ui/settings.gd | 59 ++++++++++++++++++ ui/settings.tscn | 113 ++++++++++++++++++++++++++++++++++ ui/time_entries_items_tree.gd | 20 +++++- 6 files changed, 254 insertions(+), 198 deletions(-) create mode 100644 ui/settings.gd create mode 100644 ui/settings.tscn diff --git a/scripts/config_manager.gd b/scripts/config_manager.gd index 896a6e7..6cbfad8 100644 --- a/scripts/config_manager.gd +++ b/scripts/config_manager.gd @@ -5,23 +5,58 @@ const CONFIG_PATH := "user://settings.cfg" var _config := ConfigFile.new() +var timesheet: TimeSheet: + get: + if timesheet == null: + timesheet = TimeSheet.restore(current_file) + return timesheet + +signal file_changed var current_file: String = "": - set(value): - current_file = value - _config.set_value("MAIN", "file", value) - save() + set = set_current_file, + get = get_current_file + + +func set_current_file(value: String) -> void: + timesheet = TimeSheet.restore(value) + if timesheet == null: + return + current_file = value + _config.set_value("MAIN", "file", value) + file_changed.emit() + save() + + +func get_current_file() -> String: + var _default_path := OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, true).path_join("mouse_timer.csv") + return _config.get_value("MAIN", "file", _default_path) + + +var theme: Theme: get: - var _default_path := OS.get_system_dir(OS.SYSTEM_DIR_DOCUMENTS, true).path_join("mouse_timer.csv") - return _config.get_value("MAIN", "file", _default_path) + if theme == null: + theme = ResourceLoader.load(theme_path, "Theme") + return theme +signal theme_changed var theme_path: String = "": - set(value): - theme_path = value - _config.set_value("MAIN", "theme", value) - save() - get: - return _config.get_value("MAIN", "theme", preload("res://assets/default_theme.theme").resource_path) + set = set_theme_path, + get = get_theme_path + + +func set_theme_path(value: String) -> void: + var new_theme: Theme = ResourceLoader.load(value, "Theme") + if new_theme != null: + theme = new_theme + theme_path = value + _config.set_value("MAIN", "theme", value) + theme_changed.emit() + save() + + +func get_theme_path() -> String: + return _config.get_value("MAIN", "theme", preload("res://assets/default_theme.theme").resource_path) var last_task_name: String = "": @@ -42,7 +77,6 @@ var sound_fx_on: bool = true: return _config.get_value("MAIN", "sound_fx", true) - func _init() -> void: _config.load(CONFIG_PATH) diff --git a/ui/Main.gd b/ui/Main.gd index edda131..60931ea 100644 --- a/ui/Main.gd +++ b/ui/Main.gd @@ -10,64 +10,32 @@ extends Control @onready var settings_button: Button = %SettingsButton @onready var previous_tasks_window: Window = %PreviousTasksWindow @onready var settings_window: Window = %SettingsWindow -@onready var file_path_file_dialog: FileDialog = %FilePathFileDialog -@onready var file_path_line_edit: LineEdit = %FilePathLineEdit -@onready var file_path_button: Button = %FilePathButton -@onready var theme_path_file_dialog: FileDialog = %ThemePathFileDialog -@onready var theme_path_button: Button = %ThemePathButton -@onready var sound_check_box: CheckBox = %SoundCheckBox -@onready var attributions_rich_text_label: RichTextLabel = %AttributionsRichTextLabel @onready var audio_stream_player: AudioStreamPlayer = %AudioStreamPlayer -@onready var open_data_dir_button: Button = %OpenDataDirButton -var timesheet: TimeSheet var config: ConfigManager = preload("res://config_manager.tres") -func _init() -> void: - - if config.theme_path: - var new_theme: Theme = ResourceLoader.load(config.theme_path, "Theme") - if new_theme != null: - theme = new_theme - - func _ready() -> void: + config.file_changed.connect(set_initial_state) + config.theme_changed.connect( + func theme_changed() -> void: + theme = config.them + ) + get_tree().set_auto_accept_quit(false) - var _root := time_entries_items_tree.create_item() - file_path_button.pressed.connect( - file_path_file_dialog.popup_centered - ) - - file_path_file_dialog.file_selected.connect(set_current_file) - file_path_line_edit.text_submitted.connect(set_current_file) - - theme_path_button.pressed.connect( - theme_path_file_dialog.popup_centered - ) - theme_path_file_dialog.file_selected.connect( - func theme_selected(theme_path: String) -> void: - var new_theme: Theme = ResourceLoader.load(theme_path, "Theme") - if new_theme != null: - theme = new_theme - config.theme_path = theme_path - ) - - theme_path_file_dialog.hide() - file_path_file_dialog.hide() previous_tasks_window.hide() settings_window.hide() timer.timeout.connect( func on_timer_timeout() -> void: - timesheet.update() + config.timesheet.update() - time_label.text = timesheet.get_period() + time_label.text = config.timesheet.get_period() - var total_elapsed: int = timesheet.get_total_elapsed_seconds() + var total_elapsed: int = config.timesheet.get_total_elapsed_seconds() time_entries_items_tree.set_time_elapsed(total_elapsed) ) @@ -75,13 +43,13 @@ func _ready() -> void: start_button.toggle_mode = true start_button.toggled.connect( func start(is_on: bool) -> void: - if sound_check_box.button_pressed: + if config.sound_fx_on: audio_stream_player.play() if is_on: - timesheet.start_entry(task_name_line_edit.text) + config.timesheet.start_entry(task_name_line_edit.text) set_button_as_started() else: - timesheet.close_entry() + config.timesheet.close_entry() set_button_as_stopped() ) @@ -117,24 +85,11 @@ func _ready() -> void: settings_button.set_pressed_no_signal(false) settings_window.hide() ) - - sound_check_box.button_pressed = config.sound_fx_on - sound_check_box.toggled.connect( - func sound_toggle(is_on: bool) -> void: - config.sound_fx_on = is_on - ) - open_data_dir_button.pressed.connect( - OS.shell_open.bind(OS.get_user_data_dir()) - ) - - attributions_rich_text_label.meta_clicked.connect(OS.shell_open) - - set_current_file(config.current_file) func set_button_as_started() -> void: - time_entries_items_tree.set_current_item(timesheet.current_entry.name) + time_entries_items_tree.set_current_item(config.timesheet.current_entry.name) start_button.tooltip_text = tr(Consts.STOP) start_button.theme_type_variation = Consts.THEME_OVERRIDE_STOP timer.start() @@ -147,23 +102,10 @@ func set_button_as_stopped() -> void: timer.stop() -## Changes the data file path, and loads the new path -func set_current_file(new_current_file: String) -> void: - timesheet = TimeSheet.restore(new_current_file) - if timesheet == null: - return - config.current_file = new_current_file - - for entry_name in timesheet.entries_names: - time_entries_items_tree.append_name_to_tree(entry_name, timesheet.entries_names[entry_name]) - - if timesheet.current_entry != null and timesheet.current_entry.closed == false: +func set_initial_state() -> void: + if config.timesheet.current_entry != null and config.timesheet.current_entry.closed == false: start_button.set_pressed_no_signal(true) set_button_as_started() - file_path_file_dialog.current_path = config.current_file - file_path_file_dialog.current_dir = config.current_file.get_base_dir() - file_path_line_edit.text = config.current_file - func _notification(what: int) -> void: diff --git a/ui/Main.tscn b/ui/Main.tscn index d196419..1d97618 100644 --- a/ui/Main.tscn +++ b/ui/Main.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=7 format=3 uid="uid://bmlciwscreowf"] +[gd_scene load_steps=8 format=3 uid="uid://bmlciwscreowf"] [ext_resource type="Theme" uid="uid://bd8ancgbfsvmd" path="res://assets/default_theme.theme" id="1_2s8h2"] [ext_resource type="Script" path="res://ui/Main.gd" id="2_sl5q6"] [ext_resource type="Script" path="res://ui/time_entries_items_tree.gd" id="3_oxqux"] +[ext_resource type="PackedScene" uid="uid://b07v41toqw355" path="res://ui/settings.tscn" id="4_4fa2j"] [ext_resource type="AudioStream" uid="uid://cdsbhoidgyx70" path="res://assets/pop.ogg" id="4_6ajaq"] [sub_resource type="InputEventKey" id="InputEventKey_guuii"] @@ -84,23 +85,6 @@ shortcut = SubResource("Shortcut_irhvi") [node name="Timer" type="Timer" parent="Main"] unique_name_in_owner = true -[node name="FilePathFileDialog" type="FileDialog" parent="Main"] -unique_name_in_owner = true -title = "Pick Tracker File" -size = Vector2i(800, 600) -ok_button_text = "Save" -access = 2 -filters = PackedStringArray("*.csv ; Comma Separated Files") - -[node name="ThemePathFileDialog" type="FileDialog" parent="Main"] -unique_name_in_owner = true -title = "Open a File" -size = Vector2i(800, 600) -ok_button_text = "Open" -file_mode = 0 -access = 2 -filters = PackedStringArray("*.theme ; Theme Files") - [node name="PreviousTasksWindow" type="Window" parent="Main"] unique_name_in_owner = true title = "Tasks" @@ -141,97 +125,7 @@ size = Vector2i(300, 300) visible = false always_on_top = true -[node name="PanelContainer" type="PanelContainer" parent="Main/SettingsWindow"] -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2 -size_flags_horizontal = 3 -size_flags_vertical = 3 -theme_type_variation = &"background" - -[node name="MarginContainer" type="MarginContainer" parent="Main/SettingsWindow/PanelContainer"] -layout_mode = 2 -size_flags_horizontal = 3 -size_flags_vertical = 3 -theme_override_constants/margin_left = 20 -theme_override_constants/margin_top = 20 -theme_override_constants/margin_right = 20 -theme_override_constants/margin_bottom = 20 - -[node name="VBoxContainer" type="VBoxContainer" parent="Main/SettingsWindow/PanelContainer/MarginContainer"] -layout_mode = 2 -size_flags_horizontal = 3 -size_flags_vertical = 3 -theme_override_constants/separation = 10 - -[node name="HBoxContainer" type="HBoxContainer" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer"] -layout_mode = 2 - -[node name="Label" type="Label" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] -layout_mode = 2 -text = "File Path" - -[node name="FilePathLineEdit" type="LineEdit" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] -unique_name_in_owner = true -layout_mode = 2 -size_flags_horizontal = 3 -caret_blink = true -caret_blink_interval = 0.5 - -[node name="FilePathButton" type="Button" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"] -unique_name_in_owner = true -layout_mode = 2 -text = "..." - -[node name="HBoxContainer2" type="HBoxContainer" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer"] -layout_mode = 2 - -[node name="Label" type="Label" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer2"] -layout_mode = 2 -text = "Alternative theme -" - -[node name="ThemePathButton" type="Button" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer2"] -unique_name_in_owner = true -layout_mode = 2 -text = "load" - -[node name="HBoxContainer3" type="HBoxContainer" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer"] -layout_mode = 2 - -[node name="SoundCheckBox" type="CheckBox" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer/HBoxContainer3"] -unique_name_in_owner = true -layout_mode = 2 -button_pressed = true -text = "Sounds" - -[node name="OpenDataDirButton" type="Button" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer"] -unique_name_in_owner = true -layout_mode = 2 -text = "open data dir" - -[node name="AttributionsRichTextLabel" type="RichTextLabel" parent="Main/SettingsWindow/PanelContainer/MarginContainer/VBoxContainer"] -unique_name_in_owner = true -layout_mode = 2 -size_flags_vertical = 3 -theme_type_variation = &"small_text" -bbcode_enabled = true -text = "Font: Cairo, Designed by Mohamed Gaber, Accademia di Belle Arti di Urbino - -Sound: [url]https://opengameart.org/content/bubbles-pop[/url] - -This game uses Godot Engine, available under the following license: - - Copyright (c) 2014-present Godot Engine contributors. Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. - - Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -" +[node name="Settings" parent="Main/SettingsWindow" instance=ExtResource("4_4fa2j")] [node name="AudioStreamPlayer" type="AudioStreamPlayer" parent="."] unique_name_in_owner = true diff --git a/ui/settings.gd b/ui/settings.gd new file mode 100644 index 0000000..04461ba --- /dev/null +++ b/ui/settings.gd @@ -0,0 +1,59 @@ +extends PanelContainer + + +var config: ConfigManager = preload("res://config_manager.tres") + + +@onready var file_path_file_dialog: FileDialog = %FilePathFileDialog +@onready var file_path_line_edit: LineEdit = %FilePathLineEdit +@onready var file_path_button: Button = %FilePathButton +@onready var theme_path_file_dialog: FileDialog = %ThemePathFileDialog +@onready var theme_path_button: Button = %ThemePathButton +@onready var sound_check_box: CheckBox = %SoundCheckBox +@onready var attributions_rich_text_label: RichTextLabel = %AttributionsRichTextLabel +@onready var open_data_dir_button: Button = %OpenDataDirButton + + +func _ready() -> void: + + config.theme_changed.connect( + func set_current_theme() -> void: + theme = config.theme + ) + + config.file_changed.connect( + func set_current_file() -> void: + file_path_file_dialog.current_path = config.current_file + file_path_file_dialog.current_dir = config.current_file.get_base_dir() + file_path_line_edit.text = config.current_file + ) + + file_path_button.pressed.connect( + file_path_file_dialog.popup_centered + ) + + file_path_file_dialog.file_selected.connect(config.set_current_file) + file_path_line_edit.text_submitted.connect(config.set_current_file) + + theme_path_button.pressed.connect( + theme_path_file_dialog.popup_centered + ) + theme_path_file_dialog.file_selected.connect(config.set_theme_path) + + theme_path_file_dialog.hide() + file_path_file_dialog.hide() + + sound_check_box.button_pressed = config.sound_fx_on + sound_check_box.toggled.connect( + func sound_toggle(is_on: bool) -> void: + config.sound_fx_on = is_on + ) + + attributions_rich_text_label.meta_clicked.connect(OS.shell_open) + + open_data_dir_button.pressed.connect( + OS.shell_open.bind(OS.get_user_data_dir()) + ) + + + diff --git a/ui/settings.tscn b/ui/settings.tscn new file mode 100644 index 0000000..e97fc1c --- /dev/null +++ b/ui/settings.tscn @@ -0,0 +1,113 @@ +[gd_scene load_steps=2 format=3 uid="uid://b07v41toqw355"] + +[ext_resource type="Script" path="res://ui/settings.gd" id="1_cmilf"] + +[node name="Settings" type="PanelContainer"] +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +theme_type_variation = &"background" +script = ExtResource("1_cmilf") + +[node name="MarginContainer" type="MarginContainer" parent="."] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +theme_override_constants/margin_left = 20 +theme_override_constants/margin_top = 20 +theme_override_constants/margin_right = 20 +theme_override_constants/margin_bottom = 20 + +[node name="VBoxContainer" type="VBoxContainer" parent="MarginContainer"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +theme_override_constants/separation = 10 + +[node name="HBoxContainer" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 + +[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/HBoxContainer"] +layout_mode = 2 +text = "File Path" + +[node name="FilePathLineEdit" type="LineEdit" parent="MarginContainer/VBoxContainer/HBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +caret_blink = true +caret_blink_interval = 0.5 + +[node name="FilePathButton" type="Button" parent="MarginContainer/VBoxContainer/HBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +text = "..." + +[node name="HBoxContainer2" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 + +[node name="Label" type="Label" parent="MarginContainer/VBoxContainer/HBoxContainer2"] +layout_mode = 2 +text = "Alternative theme +" + +[node name="ThemePathButton" type="Button" parent="MarginContainer/VBoxContainer/HBoxContainer2"] +unique_name_in_owner = true +layout_mode = 2 +text = "load" + +[node name="HBoxContainer3" type="HBoxContainer" parent="MarginContainer/VBoxContainer"] +layout_mode = 2 + +[node name="SoundCheckBox" type="CheckBox" parent="MarginContainer/VBoxContainer/HBoxContainer3"] +unique_name_in_owner = true +layout_mode = 2 +button_pressed = true +text = "Sounds" + +[node name="OpenDataDirButton" type="Button" parent="MarginContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +text = "open data dir" + +[node name="AttributionsRichTextLabel" type="RichTextLabel" parent="MarginContainer/VBoxContainer"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_vertical = 3 +theme_type_variation = &"small_text" +bbcode_enabled = true +text = "Font: Cairo, Designed by Mohamed Gaber, Accademia di Belle Arti di Urbino + +Sound: [url]https://opengameart.org/content/bubbles-pop[/url] + +This game uses Godot Engine, available under the following license: + + Copyright (c) 2014-present Godot Engine contributors. Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +" + +[node name="FilePathFileDialog" type="FileDialog" parent="."] +unique_name_in_owner = true +title = "Pick Tracker File" +size = Vector2i(800, 600) +ok_button_text = "Save" +access = 2 +filters = PackedStringArray("*.csv ; Comma Separated Files") + +[node name="ThemePathFileDialog" type="FileDialog" parent="."] +unique_name_in_owner = true +title = "Open a File" +size = Vector2i(800, 600) +ok_button_text = "Open" +file_mode = 0 +access = 2 +filters = PackedStringArray("*.theme ; Theme Files") diff --git a/ui/time_entries_items_tree.gd b/ui/time_entries_items_tree.gd index c05f3c7..1a06266 100644 --- a/ui/time_entries_items_tree.gd +++ b/ui/time_entries_items_tree.gd @@ -5,8 +5,22 @@ enum COL{ 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)) @@ -27,14 +41,14 @@ func get_current_text() -> String: ## 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: +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) - if not item.get_metadata(COL.TIME): - item.set_metadata(COL.TIME, total_time) + item.set_text(COL.TIME, TimeEntry.time_to_period(total_elapsed)) + item.set_metadata(COL.TIME, total_elapsed) scroll_to_item(item) return item