Skip to content

Commit 5e6ef7a

Browse files
author
arch
committed
make settings persistent (#12)
1 parent 451acb7 commit 5e6ef7a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

funscript_editor/config/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dialog_settings.json

funscript_editor/ui/settings_dialog.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
""" Settings Dialog for the Funscript Generator """
2+
import json
3+
import os
24

35
import funscript_editor.ui.settings_view as settings_view
46

57
from funscript_editor.utils.config import PROJECTION
8+
from funscript_editor.definitions import CONFIG_DIR
69

710
from PyQt5 import QtWidgets, QtCore, QtGui
811

@@ -22,18 +25,75 @@ def __init__(self, settings: dict, include_vr: bool = True):
2225
self.ui.setupUi(self.form)
2326
self.form.setWindowTitle("Settings")
2427
self.settings = settings
28+
self.settings_file = os.path.join(CONFIG_DIR, "dialog_settings.json")
2529
self.__setup_ui_bindings()
2630
self.__setup_combo_boxes()
31+
self.__setup_dialog_elements()
32+
self.__load_settings()
33+
2734

2835
#: apply settings event
2936
applySettings = QtCore.pyqtSignal()
3037

3138

39+
def __setup_dialog_elements(self):
40+
self.dialog_elements = {
41+
'videoType': {
42+
'instance': self.ui.videoTypeComboBox,
43+
'type': 'combobox'
44+
},
45+
'trackingMetric': {
46+
'instance': self.ui.trackingMetricComboBox,
47+
'type': 'combobox'
48+
},
49+
'trackingMethod': {
50+
'instance': self.ui.trackingMethodComboBox,
51+
'type': 'combobox'
52+
},
53+
'numberOfTracker': {
54+
'instance': self.ui.numberOfTrackerComboBox,
55+
'type': 'combobox'
56+
}
57+
}
58+
59+
3260
def show(self):
3361
""" Show settings dialog """
3462
self.form.show()
3563

3664

65+
def __load_settings(self):
66+
if not os.path.exists(self.settings_file):
67+
return
68+
69+
with open(self.settings_file, "r") as f:
70+
settings = json.load(f)
71+
for key in self.dialog_elements.keys():
72+
if key not in settings.keys():
73+
continue
74+
75+
if self.dialog_elements[key]['type'] == 'combobox':
76+
index = self.dialog_elements[key]['instance'].findText(str(settings[key]), QtCore.Qt.MatchFixedString)
77+
if index >= 0:
78+
self.dialog_elements[key]['instance'].setCurrentIndex(index)
79+
else:
80+
print("ERROR: Setting not found", str(settings[key]))
81+
else:
82+
raise NotImplementedError(self.dialog_elements[key]['type'] + "type is not implemented")
83+
84+
85+
def __save_settings(self):
86+
settings = {}
87+
for key in self.dialog_elements.keys():
88+
if self.dialog_elements[key]['type'] == 'combobox':
89+
settings[key] = self.dialog_elements[key]['instance'].currentText()
90+
else:
91+
raise NotImplementedError(self.dialog_elements[key]['type'] + "type is not implemented")
92+
93+
with open(self.settings_file, "w") as f:
94+
json.dump(settings, f)
95+
96+
3797
def __setup_ui_bindings(self):
3898
self.ui.okButton.clicked.connect(self.__apply)
3999
self.ui.videoTypeComboBox.currentTextChanged.connect(
@@ -73,6 +133,7 @@ def __set_tracking_metric(self, value):
73133

74134

75135
def __apply(self):
136+
self.__save_settings()
76137
self.form.hide()
77138
self.applySettings.emit()
78139

0 commit comments

Comments
 (0)