Improve getting/setting of Settings

This commit is contained in:
Keith Edmunds 2024-06-16 08:16:24 +01:00
parent a46b9a3d6f
commit 21156d8fa1
5 changed files with 45 additions and 110 deletions

View File

@ -250,9 +250,9 @@ class TrackSelectDialog(QDialog):
self.track: Optional[Tracks] = None
self.signals = MusicMusterSignals()
record = Settings.get_int_settings(self.session, "dbdialog_width")
record = Settings.get_setting(self.session, "dbdialog_width")
width = record.f_int or 800
record = Settings.get_int_settings(self.session, "dbdialog_height")
record = Settings.get_setting(self.session, "dbdialog_height")
height = record.f_int or 600
self.resize(width, height)
@ -366,13 +366,13 @@ class TrackSelectDialog(QDialog):
if not event:
return
record = Settings.get_int_settings(self.session, "dbdialog_height")
if record.f_int != self.height():
record.update(self.session, {"f_int": self.height()})
record = Settings.get_setting(self.session, "dbdialog_height")
record.f_int = self.height()
record = Settings.get_int_settings(self.session, "dbdialog_width")
if record.f_int != self.width():
record.update(self.session, {"f_int": self.width()})
record = Settings.get_setting(self.session, "dbdialog_width")
record.f_int = self.width()
self.session.commit()
event.accept()

View File

@ -584,22 +584,8 @@ class Settings(dbtables.SettingsTable):
session.commit()
@classmethod
def all_as_dict(cls, session):
"""
Return all setting in a dictionary keyed by name
"""
result = {}
settings = session.scalars(select(cls)).all()
for setting in settings:
result[setting.name] = setting
return result
@classmethod
def get_int_settings(cls, session: Session, name: str) -> "Settings":
"""Get setting for an integer or return new setting record"""
def get_setting(cls, session: Session, name: str) -> "Settings":
"""Get existing setting or return new setting record"""
try:
return session.execute(select(cls).where(cls.name == name)).scalar_one()
@ -607,12 +593,6 @@ class Settings(dbtables.SettingsTable):
except NoResultFound:
return Settings(session, name)
def update(self, session: Session, data: dict) -> None:
for key, value in data.items():
assert hasattr(self, key)
setattr(self, key, value)
session.commit()
class Tracks(dbtables.TracksTable):
def __init__(

View File

@ -239,36 +239,6 @@ class Window(QMainWindow, Ui_MainWindow):
)
else:
with db.Session() as session:
settings = Settings.all_as_dict(session)
record = settings["mainwindow_height"]
if record.f_int != self.height():
record.update(session, {"f_int": self.height()})
record = settings["mainwindow_width"]
if record.f_int != self.width():
record.update(session, {"f_int": self.width()})
record = settings["mainwindow_x"]
if record.f_int != self.x():
record.update(session, {"f_int": self.x()})
record = settings["mainwindow_y"]
if record.f_int != self.y():
record.update(session, {"f_int": self.y()})
# Save splitter settings
splitter_sizes = self.splitter.sizes()
assert len(splitter_sizes) == 2
splitter_top, splitter_bottom = splitter_sizes
record = settings["splitter_top"]
if record.f_int != splitter_top:
record.update(session, {"f_int": splitter_top})
record = settings["splitter_bottom"]
if record.f_int != splitter_bottom:
record.update(session, {"f_int": splitter_bottom})
# Save tab number of open playlists
open_playlist_ids: dict[int, int] = {}
for idx in range(self.tabPlaylist.count()):
@ -280,9 +250,20 @@ class Window(QMainWindow, Ui_MainWindow):
log.debug(f"Set {playlist=} tab to {idx=}")
playlist.tab = idx
# Save current tab
record = settings["active_tab"]
record.update(session, {"f_int": self.tabPlaylist.currentIndex()})
# Save window attributes
splitter_top, splitter_bottom = self.splitter.sizes()
attributes_to_save = dict(
mainwindow_height=self.height(),
mainwindow_width=self.width(),
mainwindow_x=self.x(),
mainwindow_y=self.y(),
splitter_top=splitter_top,
splitter_bottom=splitter_bottom,
active_tab=self.tabPlaylist.currentIndex(),
)
for name, value in attributes_to_save.items():
record = Settings.get_setting(session, name)
record.f_int = value
session.commit()
@ -775,7 +756,7 @@ class Window(QMainWindow, Ui_MainWindow):
playlist_ids.append(playlist.id)
log.debug(f"load_last_playlists() loaded {playlist=}")
# Set active tab
record = Settings.get_int_settings(session, "active_tab")
record = Settings.get_setting(session, "active_tab")
if record.f_int is not None and record.f_int >= 0:
self.tabPlaylist.setCurrentIndex(record.f_int)
@ -1334,40 +1315,15 @@ class Window(QMainWindow, Ui_MainWindow):
"""Set size of window from database"""
with db.Session() as session:
settings = Settings.all_as_dict(session)
if "mainwindow_x" in settings:
record = settings["mainwindow_x"]
x = record.f_int or 1
else:
x = 100
if "mainwindow_y" in settings:
record = settings["mainwindow_y"]
y = record.f_int or 1
else:
y = 100
if "mainwindow_width" in settings:
record = settings["mainwindow_width"]
width = record.f_int or 1599
else:
width = 100
if "mainwindow_height" in settings:
record = settings["mainwindow_height"]
height = record.f_int or 981
else:
height = 100
x = Settings.get_setting(session, "mainwindow_x").f_int or 100
y = Settings.get_setting(session, "mainwindow_y").f_int or 100
width = Settings.get_setting(session, "mainwindow_width").f_int or 100
height = Settings.get_setting(session, "mainwindow_height").f_int or 100
splitter_top = Settings.get_setting(session, "splitter_top").f_int or 100
splitter_bottom = Settings.get_setting(session, "splitter_bottom").f_int or 100
self.setGeometry(x, y, width, height)
if "splitter_top" in settings:
record = settings["splitter_top"]
splitter_top = record.f_int or 256
else:
splitter_top = 100
if "splitter_bottom" in settings:
record = settings["splitter_bottom"]
splitter_bottom = record.f_int or 256
else:
splitter_bottom = 100
self.splitter.setSizes([splitter_top, splitter_bottom])
return
def set_selected_track_next(self) -> None:
"""
@ -1661,9 +1617,9 @@ class SelectPlaylistDialog(QDialog):
self.session = session
self.playlist = None
record = Settings.get_int_settings(self.session, "select_playlist_dialog_width")
record = Settings.get_setting(self.session, "select_playlist_dialog_width")
width = record.f_int or 800
record = Settings.get_int_settings(
record = Settings.get_setting(
self.session, "select_playlist_dialog_height"
)
height = record.f_int or 600
@ -1676,15 +1632,15 @@ class SelectPlaylistDialog(QDialog):
self.ui.lstPlaylists.addItem(p)
def __del__(self): # review
record = Settings.get_int_settings(
record = Settings.get_setting(
self.session, "select_playlist_dialog_height"
)
if record.f_int != self.height():
record.update(self.session, {"f_int": self.height()})
record.f_int = self.height()
record = Settings.get_int_settings(self.session, "select_playlist_dialog_width")
if record.f_int != self.width():
record.update(self.session, {"f_int": self.width()})
record = Settings.get_setting(self.session, "select_playlist_dialog_width")
record.f_int = self.width()
self.session.commit()
def list_doubleclick(self, entry): # review
self.playlist = entry.data(Qt.ItemDataRole.UserRole)

View File

@ -573,7 +573,7 @@ class PlaylistTab(QTableView):
with db.Session() as session:
attr_name = f"playlist_col_{column_number}_width"
record = Settings.get_int_settings(session, attr_name)
record = Settings.get_setting(session, attr_name)
record.f_int = self.columnWidth(column_number)
session.commit()
@ -900,7 +900,7 @@ class PlaylistTab(QTableView):
with db.Session() as session:
for column_number in range(header.count() - 1):
attr_name = f"playlist_col_{column_number}_width"
record = Settings.get_int_settings(session, attr_name)
record = Settings.get_setting(session, attr_name)
if record.f_int is not None:
self.setColumnWidth(column_number, record.f_int)
else:

View File

@ -41,10 +41,9 @@ class TestMMMisc(unittest.TestCase):
setting = Settings(session, SETTING_NAME)
# test repr
_ = str(setting)
setting.update(session, dict(f_int=VALUE))
_ = Settings.all_as_dict(session)
test = Settings.get_int_settings(session, SETTING_NAME)
setting.f_int = VALUE
test = Settings.get_setting(session, SETTING_NAME)
assert test.name == SETTING_NAME
assert test.f_int == VALUE
test_new = Settings.get_int_settings(session, NO_SUCH_SETTING)
test_new = Settings.get_setting(session, NO_SUCH_SETTING)
assert test_new.name == NO_SUCH_SETTING