Skip to content

Commit 0309c5d

Browse files
committed
Settings: Move QMessageBox, get_settings() to app
(Remove the SettingsStore dependency on Qt.)
1 parent 707dea5 commit 0309c5d

File tree

2 files changed

+46
-25
lines changed

2 files changed

+46
-25
lines changed

src/classes/app.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __init__(self, *args, mode=None):
130130
pass
131131

132132
# Init settings
133-
self.settings = settings.SettingStore()
133+
self.settings = settings.SettingStore(parent=self)
134134
self.settings.load()
135135

136136
# Init and attach exception handler
@@ -273,6 +273,16 @@ def __init__(self, *args, mode=None):
273273
# Recover backup file (this can't happen until after the Main Window has completely loaded)
274274
self.window.RecoverBackup.emit()
275275

276+
def settings_load_error(self, filepath=None):
277+
"""Use QMessageBox to warn the user of a settings load issue"""
278+
_ = self._tr
279+
QMessageBox.warning(
280+
None,
281+
_("Settings Error"),
282+
_("Error loading settings file: %(file_path)s. Settings will be reset.")
283+
% {"file_path": filepath}
284+
)
285+
276286
def _tr(self, message):
277287
return self.translate("", message)
278288

@@ -291,7 +301,6 @@ def cleanup(self):
291301
log.error("Couldn't save user settings on exit.", exc_info=1)
292302

293303

294-
295304
# Log the session's end
296305
@atexit.register
297306
def onLogTheEnd():

src/classes/settings.py

+35-23
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,64 @@
1-
"""
1+
"""
22
@file
3-
@brief This file loads and saves settings
3+
@brief This file loads and saves settings
44
@author Noah Figg <[email protected]>
55
@author Jonathan Thomas <[email protected]>
66
@author Olivier Girard <[email protected]>
7-
7+
88
@section LICENSE
9-
9+
1010
Copyright (c) 2008-2018 OpenShot Studios, LLC
1111
(http://www.openshotstudios.com). This file is part of
1212
OpenShot Video Editor (http://www.openshot.org), an open-source project
1313
dedicated to delivering high quality video editing and animation solutions
1414
to the world.
15-
15+
1616
OpenShot Video Editor is free software: you can redistribute it and/or modify
1717
it under the terms of the GNU General Public License as published by
1818
the Free Software Foundation, either version 3 of the License, or
1919
(at your option) any later version.
20-
20+
2121
OpenShot Video Editor is distributed in the hope that it will be useful,
2222
but WITHOUT ANY WARRANTY; without even the implied warranty of
2323
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2424
GNU General Public License for more details.
25-
25+
2626
You should have received a copy of the GNU General Public License
2727
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2828
"""
2929

3030
# SettingStore - class which allows getting/storing of settings, loading and saving to json
3131
import os
3232

33-
from PyQt5.QtCore import QStandardPaths, QCoreApplication
34-
from PyQt5.QtWidgets import QMessageBox
35-
36-
from classes.logger import log
3733
from classes import info
34+
from classes.logger import log
3835
from classes.json_data import JsonDataStore
3936

4037

4138
def get_settings():
42-
""" Get the current QApplication's settings instance """
43-
return QCoreApplication.instance().settings
39+
""" Get the current application's settings instance """
40+
return SettingStore.get_app().settings
4441

4542

4643
class SettingStore(JsonDataStore):
4744
""" This class only allows setting pre-existing keys taken from default settings file, and merges user settings
4845
on load, assumes default OS dir."""
4946

50-
def __init__(self):
51-
JsonDataStore.__init__(self)
47+
@classmethod
48+
def save_app(cls, app_reference):
49+
cls._app = app_reference
50+
51+
@classmethod
52+
def get_app(cls):
53+
if hasattr(cls, "_app"):
54+
return cls._app
55+
return None
56+
57+
def __init__(self, parent=None):
58+
super().__init__()
59+
# Also keep track of our parent, if defined
60+
if parent:
61+
SettingStore.save_app(parent)
5262
# Set the data type name for logging clarity (base class functions use this variable)
5363
self.data_type = "user settings"
5464
self.settings_filename = "openshot.settings"
@@ -72,8 +82,11 @@ def set(self, key, value):
7282
if key in user_values:
7383
user_values[key]["value"] = value
7484
else:
75-
log.warn("{} key '{}' not valid. The following are valid: {}".format(self.data_type, key,
76-
list(self._data.keys())))
85+
log.warn(
86+
"{} key '{}' not valid. The following are valid: {}".format(
87+
self.data_type, key,
88+
list(self._data.keys()),
89+
))
7790

7891
def load(self):
7992
""" Load user settings file from disk, merging with allowed settings in default settings file.
@@ -90,17 +103,16 @@ def load(self):
90103

91104
# Load user settings (if found)
92105
if os.path.exists(os.fsencode(file_path)):
93-
94106
# Will raise exception to caller on failure to read
95107
try:
96108
user_settings = self.read_from_file(file_path)
97109
except Exception as ex:
98-
log.error("Error loading settings file: %s" % ex)
99-
100-
_ = QCoreApplication.instance()._tr
101-
QMessageBox.warning(None, _("Settings Error"),
102-
_("Error loading settings file: %(file_path)s. Settings will be reset.") % { "file_path": file_path})
110+
log.error("Error loading settings file: %s", ex)
103111
user_settings = {}
112+
app = SettingStore.get_app()
113+
if app:
114+
# We have a parent, ask to show a message box
115+
app.settings_load_error(file_path)
104116

105117
# Merge default and user settings, excluding settings not in default, Save settings
106118
self._data = self.merge_settings(default_settings, user_settings)

0 commit comments

Comments
 (0)