Improve getting/setting of Settings
This commit is contained in:
parent
a46b9a3d6f
commit
21156d8fa1
@ -250,9 +250,9 @@ class TrackSelectDialog(QDialog):
|
|||||||
self.track: Optional[Tracks] = None
|
self.track: Optional[Tracks] = None
|
||||||
self.signals = MusicMusterSignals()
|
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
|
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
|
height = record.f_int or 600
|
||||||
self.resize(width, height)
|
self.resize(width, height)
|
||||||
|
|
||||||
@ -366,13 +366,13 @@ class TrackSelectDialog(QDialog):
|
|||||||
if not event:
|
if not event:
|
||||||
return
|
return
|
||||||
|
|
||||||
record = Settings.get_int_settings(self.session, "dbdialog_height")
|
record = Settings.get_setting(self.session, "dbdialog_height")
|
||||||
if record.f_int != self.height():
|
record.f_int = self.height()
|
||||||
record.update(self.session, {"f_int": self.height()})
|
|
||||||
|
|
||||||
record = Settings.get_int_settings(self.session, "dbdialog_width")
|
record = Settings.get_setting(self.session, "dbdialog_width")
|
||||||
if record.f_int != self.width():
|
record.f_int = self.width()
|
||||||
record.update(self.session, {"f_int": self.width()})
|
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
event.accept()
|
event.accept()
|
||||||
|
|
||||||
|
|||||||
@ -584,22 +584,8 @@ class Settings(dbtables.SettingsTable):
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def all_as_dict(cls, session):
|
def get_setting(cls, session: Session, name: str) -> "Settings":
|
||||||
"""
|
"""Get existing setting or return new setting record"""
|
||||||
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"""
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return session.execute(select(cls).where(cls.name == name)).scalar_one()
|
return session.execute(select(cls).where(cls.name == name)).scalar_one()
|
||||||
@ -607,12 +593,6 @@ class Settings(dbtables.SettingsTable):
|
|||||||
except NoResultFound:
|
except NoResultFound:
|
||||||
return Settings(session, name)
|
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):
|
class Tracks(dbtables.TracksTable):
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|||||||
@ -239,36 +239,6 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
with db.Session() as session:
|
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
|
# Save tab number of open playlists
|
||||||
open_playlist_ids: dict[int, int] = {}
|
open_playlist_ids: dict[int, int] = {}
|
||||||
for idx in range(self.tabPlaylist.count()):
|
for idx in range(self.tabPlaylist.count()):
|
||||||
@ -280,9 +250,20 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
log.debug(f"Set {playlist=} tab to {idx=}")
|
log.debug(f"Set {playlist=} tab to {idx=}")
|
||||||
playlist.tab = idx
|
playlist.tab = idx
|
||||||
|
|
||||||
# Save current tab
|
# Save window attributes
|
||||||
record = settings["active_tab"]
|
splitter_top, splitter_bottom = self.splitter.sizes()
|
||||||
record.update(session, {"f_int": self.tabPlaylist.currentIndex()})
|
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()
|
session.commit()
|
||||||
|
|
||||||
@ -775,7 +756,7 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
playlist_ids.append(playlist.id)
|
playlist_ids.append(playlist.id)
|
||||||
log.debug(f"load_last_playlists() loaded {playlist=}")
|
log.debug(f"load_last_playlists() loaded {playlist=}")
|
||||||
# Set active tab
|
# 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:
|
if record.f_int is not None and record.f_int >= 0:
|
||||||
self.tabPlaylist.setCurrentIndex(record.f_int)
|
self.tabPlaylist.setCurrentIndex(record.f_int)
|
||||||
|
|
||||||
@ -1334,40 +1315,15 @@ class Window(QMainWindow, Ui_MainWindow):
|
|||||||
"""Set size of window from database"""
|
"""Set size of window from database"""
|
||||||
|
|
||||||
with db.Session() as session:
|
with db.Session() as session:
|
||||||
settings = Settings.all_as_dict(session)
|
x = Settings.get_setting(session, "mainwindow_x").f_int or 100
|
||||||
if "mainwindow_x" in settings:
|
y = Settings.get_setting(session, "mainwindow_y").f_int or 100
|
||||||
record = settings["mainwindow_x"]
|
width = Settings.get_setting(session, "mainwindow_width").f_int or 100
|
||||||
x = record.f_int or 1
|
height = Settings.get_setting(session, "mainwindow_height").f_int or 100
|
||||||
else:
|
splitter_top = Settings.get_setting(session, "splitter_top").f_int or 100
|
||||||
x = 100
|
splitter_bottom = Settings.get_setting(session, "splitter_bottom").f_int or 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
|
|
||||||
self.setGeometry(x, y, width, height)
|
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])
|
self.splitter.setSizes([splitter_top, splitter_bottom])
|
||||||
return
|
|
||||||
|
|
||||||
def set_selected_track_next(self) -> None:
|
def set_selected_track_next(self) -> None:
|
||||||
"""
|
"""
|
||||||
@ -1661,9 +1617,9 @@ class SelectPlaylistDialog(QDialog):
|
|||||||
self.session = session
|
self.session = session
|
||||||
self.playlist = None
|
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
|
width = record.f_int or 800
|
||||||
record = Settings.get_int_settings(
|
record = Settings.get_setting(
|
||||||
self.session, "select_playlist_dialog_height"
|
self.session, "select_playlist_dialog_height"
|
||||||
)
|
)
|
||||||
height = record.f_int or 600
|
height = record.f_int or 600
|
||||||
@ -1676,15 +1632,15 @@ class SelectPlaylistDialog(QDialog):
|
|||||||
self.ui.lstPlaylists.addItem(p)
|
self.ui.lstPlaylists.addItem(p)
|
||||||
|
|
||||||
def __del__(self): # review
|
def __del__(self): # review
|
||||||
record = Settings.get_int_settings(
|
record = Settings.get_setting(
|
||||||
self.session, "select_playlist_dialog_height"
|
self.session, "select_playlist_dialog_height"
|
||||||
)
|
)
|
||||||
if record.f_int != self.height():
|
record.f_int = self.height()
|
||||||
record.update(self.session, {"f_int": self.height()})
|
|
||||||
|
|
||||||
record = Settings.get_int_settings(self.session, "select_playlist_dialog_width")
|
record = Settings.get_setting(self.session, "select_playlist_dialog_width")
|
||||||
if record.f_int != self.width():
|
record.f_int = self.width()
|
||||||
record.update(self.session, {"f_int": self.width()})
|
|
||||||
|
self.session.commit()
|
||||||
|
|
||||||
def list_doubleclick(self, entry): # review
|
def list_doubleclick(self, entry): # review
|
||||||
self.playlist = entry.data(Qt.ItemDataRole.UserRole)
|
self.playlist = entry.data(Qt.ItemDataRole.UserRole)
|
||||||
|
|||||||
@ -573,7 +573,7 @@ class PlaylistTab(QTableView):
|
|||||||
|
|
||||||
with db.Session() as session:
|
with db.Session() as session:
|
||||||
attr_name = f"playlist_col_{column_number}_width"
|
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)
|
record.f_int = self.columnWidth(column_number)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
@ -900,7 +900,7 @@ class PlaylistTab(QTableView):
|
|||||||
with db.Session() as session:
|
with db.Session() as session:
|
||||||
for column_number in range(header.count() - 1):
|
for column_number in range(header.count() - 1):
|
||||||
attr_name = f"playlist_col_{column_number}_width"
|
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:
|
if record.f_int is not None:
|
||||||
self.setColumnWidth(column_number, record.f_int)
|
self.setColumnWidth(column_number, record.f_int)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -41,10 +41,9 @@ class TestMMMisc(unittest.TestCase):
|
|||||||
setting = Settings(session, SETTING_NAME)
|
setting = Settings(session, SETTING_NAME)
|
||||||
# test repr
|
# test repr
|
||||||
_ = str(setting)
|
_ = str(setting)
|
||||||
setting.update(session, dict(f_int=VALUE))
|
setting.f_int = VALUE
|
||||||
_ = Settings.all_as_dict(session)
|
test = Settings.get_setting(session, SETTING_NAME)
|
||||||
test = Settings.get_int_settings(session, SETTING_NAME)
|
|
||||||
assert test.name == SETTING_NAME
|
assert test.name == SETTING_NAME
|
||||||
assert test.f_int == VALUE
|
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
|
assert test_new.name == NO_SUCH_SETTING
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user