Доработка. Сохранение настроек.

This commit is contained in:
lepshiy
2025-03-07 09:52:21 +03:00
parent 88a50afe22
commit abbf83de25
3 changed files with 100 additions and 19 deletions

View File

@@ -89,6 +89,18 @@ func setting_tab():
$heard_table.set_header_text(HEADER_ROW_TEXT)
$heard_table.set_columns_min_size([600, 950])
$heard_table.set_columns_alignments(ALIGNMENT)
var settings_data = {} # Загружаем настройки из файла
var file = FileAccess.open(settings.settings_file, FileAccess.READ)
if file:
var read = file.get_as_text()
file.close()
var json_conv = JSON.new()
if json_conv.parse(read) == OK:
settings_data = json_conv.get_data()
else:
log.info("Не удалось прочитать JSON, используем настройки по умолчанию")
else:
log.info("Файл настроек не найден, используем настройки по умолчанию")
for i in SETTING_TABLE.size():
var setting_str = SETTING_TABLE[i]
var parameter = setting_str[0]
@@ -98,6 +110,14 @@ func setting_tab():
var label_instance = $scroll_set/table.get_node2(0, i)
label_instance.text = parameter
$scroll_set/table.set_node_editable(0, i, false)
var node_values = $scroll_set/table.get_node2(1, i).get_children()
if node_values.size() > 1:
var child = node_values[1]
if child is CheckButton:
if parameter in settings_data:
child.set_pressed(settings_data[parameter][0])
else:
child.set_pressed(false)
var commit_instance = $scroll_set/table.get_node2(1, 21)
var server_instance = $scroll_set/table.get_node2(1, 22)
commit_instance.text = '%s' % commit.VCS_HEAD
@@ -158,6 +178,19 @@ func _on_btn_save_settings_pressed():
json_data[parameter].append(true)
elif port == "false":
json_data[parameter].append(false)
else:
port = port.strip_edges()
if port.is_valid_int():
json_data[parameter].append(int(port))
else:
json_data[parameter].append(port)
else:
var children = $scroll_set/table.get_node2(1, i).get_children()
if len(children):
var child = children[1]
if parameter not in json_data:
json_data[parameter] = []
json_data[parameter].append(child.is_pressed())
var file = FileAccess.open(settings.settings_file, FileAccess.WRITE)
if file:
var json_conv = JSON.new()

View File

@@ -30,7 +30,7 @@ var settings_program: Dictionary
'уарэп-5п28': [ '5p28', '127.0.0.1', [50000], 'IP-адрес комплекса 5П-28'],
# Название: [ Протокол, Адрес, Скорость, ??, ??, Комментарий
'уарэп-щ3': ['mbcs', '/home/sasha/dev/tty1', [19200, 7, 'N', 8, 1], 'Параметры клиента Modbus Serial'],
'уарэп-щ3': ['mbcs', '/home/sasha/dev/tty1', [7, 19200, 'N', 8, 1], 'Параметры клиента Modbus Serial'],
# Название: [ Протокол, Имя порта, Скорость, Чётность, ??, Комментарий
'уарэп-спт25-1': ['uart', '/home/sasha/dev/tty0', [19200, false, false], 'Адрес прибора СПТ-25 (G1)'],
@@ -45,24 +45,42 @@ var settings_program: Dictionary
func _ready() -> void:
var file = FileAccess.open(settings_file, FileAccess.READ)
if file != null:
var read = file.get_as_text()
file.close()
var json_conv: = JSON.new()
if Error.OK == json_conv.parse(read):
var json_data = json_conv.get_data()
settings_program.merge(json_data)
for key in UnitProfiles.keys():
if json_data.has(key):
UnitProfiles[key][1] = json_data[key][0]
for i in len(json_data[key]) - 1:
if typeof(json_data[key][i+1]) == TYPE_STRING:
if json_data[key][i+1].is_valid_int():
UnitProfiles[key][2][i] = int(json_data[key][i+1])
else:
UnitProfiles[key][2][i] = json_data[key][i+1]
else:
UnitProfiles[key][2][i] = json_data[key][i+1]
if not file:
log.info('Не удалось открыть файл настроек %s, использованы настройики по умолчанию' % settings_file)
return
var read = file.get_as_text()
file.close()
var json_conv: = JSON.new()
if Error.OK != json_conv.parse(read):
log.info('Не удалось прочитать файл настроек %s, использованы настройики по умолчанию' % settings_file)
return
var json_data = json_conv.get_data()
settings_program.merge(json_data)
for key in UnitProfiles.keys():
if not json_data.has(key): continue
var temp_array = []
var set_from_file = true
if typeof(json_data[key][0]) == typeof(UnitProfiles[key][1]):
temp_array.append(json_data[key][0])
else:
set_from_file = false
if (len(json_data[key]) - 1) != len(UnitProfiles[key][2]):
log.info('Не удалось прочитать файл настроек %s, ошибка длинны массива' % settings_file)
continue
temp_array.append([])
for i in len(json_data[key]) - 1:
if tools.type_is_num(json_data[key][i+1]) == tools.type_is_num(UnitProfiles[key][2][i]):
temp_array[1].append(json_data[key][i+1])
elif typeof(json_data[key][i+1]) == typeof(UnitProfiles[key][2][i]):
temp_array[1].append(json_data[key][i+1])
else:
set_from_file = false
if set_from_file:
UnitProfiles[key][1] = temp_array[0]
UnitProfiles[key][2] = temp_array[1]
else:
log.info('Не удалось прочитать ключ %s из файла настроек, использованы настройики по умолчанию' % key)
log.info('Модуль настроек готов')

30
table/set_taggle.tscn Normal file
View File

@@ -0,0 +1,30 @@
[gd_scene load_steps=4 format=3 uid="uid://dgidt8lifm7xe"]
[ext_resource type="PackedScene" uid="uid://dspa16ufrpanp" path="res://table/ячейка-2.tscn" id="1_eib5d"]
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_t3fqw"]
[sub_resource type="Theme" id="Theme_odxqp"]
CheckButton/styles/focus = SubResource("StyleBoxEmpty_t3fqw")
[node name="Control" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="LineEdit" parent="." instance=ExtResource("1_eib5d")]
layout_mode = 0
offset_right = 85.0
offset_bottom = 27.0
editable = false
[node name="CheckButton" type="CheckButton" parent="."]
layout_mode = 0
offset_left = 21.0
offset_top = 1.0
offset_right = 65.0
offset_bottom = 25.0
theme = SubResource("Theme_odxqp")