config reloading after save, working - database/table reload accounted for too yay

This commit is contained in:
tsi-billypom 2025-03-27 15:32:03 -04:00
parent b761aa3956
commit 2d997052e2
2 changed files with 42 additions and 26 deletions

View File

@ -1,6 +1,7 @@
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QDialog, QDialog,
QListWidgetItem,
QVBoxLayout, QVBoxLayout,
QLabel, QLabel,
QLineEdit, QLineEdit,
@ -10,7 +11,7 @@ from PyQt5.QtWidgets import (
QWidget, QWidget,
QDial, QDial,
) )
from logging import info from logging import debug
from PyQt5.QtGui import QFont from PyQt5.QtGui import QFont
from configparser import ConfigParser from configparser import ConfigParser
from pathlib import Path from pathlib import Path
@ -18,10 +19,11 @@ from appdirs import user_config_dir
class PreferencesWindow(QDialog): class PreferencesWindow(QDialog):
def __init__(self, reloadConfigSignal): def __init__(self, reloadConfigSignal, reloadDatabaseSignal):
super(PreferencesWindow, self).__init__() super(PreferencesWindow, self).__init__()
# Config # Config
self.reloadConfigSignal = reloadConfigSignal self.reloadConfigSignal = reloadConfigSignal
self.reloadDatabaseSignal = reloadDatabaseSignal
self.setWindowTitle("Preferences") self.setWindowTitle("Preferences")
self.setMinimumSize(800, 800) self.setMinimumSize(800, 800)
self.cfg_file = ( self.cfg_file = (
@ -31,6 +33,8 @@ class PreferencesWindow(QDialog):
self.config = ConfigParser() self.config = ConfigParser()
self.config.read(self.cfg_file) self.config.read(self.cfg_file)
self.current_category = "" self.current_category = ""
# # Labels & input fields
self.input_fields = {}
# Widgets # Widgets
self.content_area = QWidget() self.content_area = QWidget()
@ -49,12 +53,12 @@ class PreferencesWindow(QDialog):
for category in self.config.sections(): for category in self.config.sections():
nav_pane.addItem(category) nav_pane.addItem(category)
nav_pane.itemClicked.connect(self.on_nav_item_clicked) nav_pane.itemClicked.connect(self.on_nav_item_clicked)
nav_pane.setCurrentRow(0)
first_category = self.config.sections()[0]
self.on_nav_item_clicked(first_category)
# # Labels & input fields # Pretend to click on 1st category in nav pane
self.input_fields = {} nav_pane.setCurrentRow(0)
first_category = nav_pane.item(0)
assert first_category is not None
self.on_nav_item_clicked(first_category)
# Add widgets to the layout # Add widgets to the layout
main_layout.addWidget(nav_pane) main_layout.addWidget(nav_pane)
@ -63,20 +67,23 @@ class PreferencesWindow(QDialog):
# Set the layout # Set the layout
self.setLayout(main_layout) self.setLayout(main_layout)
def on_nav_item_clicked(self, item): def on_nav_item_clicked(self, item: QListWidgetItem):
self.current_category = item
self.clear_layout(self.content_layout) self.clear_layout(self.content_layout)
self.input_fields = {} self.input_fields = {}
if isinstance(item, str): if isinstance(item, str):
self.current_category = item self.current_category_str = item
else: else:
self.current_category = item.text() self.current_category_str = item.text()
category_label = QLabel(f"{self.current_category}") # Labels
category_label = QLabel(f"{self.current_category_str}")
category_label.setFont(QFont("Sans", weight=QFont.Bold)) category_label.setFont(QFont("Sans", weight=QFont.Bold))
category_label.setStyleSheet("text-transform:uppercase;") category_label.setStyleSheet("text-transform:uppercase;")
self.content_layout.addWidget(category_label) self.content_layout.addWidget(category_label)
for key in self.config[self.current_category]: # Input fields
for key in self.config[self.current_category_str]:
label = QLabel(key) label = QLabel(key)
input_field = QLineEdit(self.config[self.current_category][key]) input_field = QLineEdit(self.config[self.current_category_str][key])
self.content_layout.addWidget(label) self.content_layout.addWidget(label)
self.content_layout.addWidget(input_field) self.content_layout.addWidget(input_field)
self.input_fields[key] = input_field self.input_fields[key] = input_field
@ -93,18 +100,22 @@ class PreferencesWindow(QDialog):
child.widget().deleteLater() child.widget().deleteLater()
def save_preferences(self): def save_preferences(self):
# FIXME: this isnt working? at least not for database """Save preferences, reload the config, then reload the database"""
# Upcate the config fields # Upcate the config fields
for key in self.input_fields: try:
for category in self.config.sections(): for key in self.input_fields:
if key in self.config[category]: for category in self.config.sections():
self.config[self.current_category][key] = self.input_fields[ if key in self.config[category]:
key value = self.input_fields[key].text()
].text() self.config[self.current_category_str][key] = value
# Write the config file # Write the config file
with open(self.cfg_file, "w") as configfile: with open(self.cfg_file, "w") as configfile:
self.config.write(configfile) self.config.write(configfile)
self.reloadConfigSignal.emit() self.reloadConfigSignal.emit()
if self.current_category_str == "db":
self.reloadDatabaseSignal.emit()
except Exception as e:
debug(e)

View File

@ -130,6 +130,7 @@ class Worker(QRunnable):
class ApplicationWindow(QMainWindow, Ui_MainWindow): class ApplicationWindow(QMainWindow, Ui_MainWindow):
playlistCreatedSignal = pyqtSignal() playlistCreatedSignal = pyqtSignal()
reloadConfigSignal = pyqtSignal() reloadConfigSignal = pyqtSignal()
reloadDatabaseSignal = pyqtSignal()
def __init__(self, clipboard): def __init__(self, clipboard):
super(ApplicationWindow, self).__init__() super(ApplicationWindow, self).__init__()
@ -252,6 +253,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
/ "config.ini" / "config.ini"
) )
self.config.read(cfg_file) self.config.read(cfg_file)
debug("CONFIG LOADED")
def get_thread_pool(self) -> QThreadPool: def get_thread_pool(self) -> QThreadPool:
"""Returns the threadpool instance""" """Returns the threadpool instance"""
@ -514,8 +516,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow):
def open_preferences(self) -> None: def open_preferences(self) -> None:
"""Opens the preferences window""" """Opens the preferences window"""
preferences_window = PreferencesWindow(self.reloadConfigSignal) preferences_window = PreferencesWindow(
preferences_window.reloadConfigSignal.connect(self.load_config) self.reloadConfigSignal, self.reloadDatabaseSignal
)
preferences_window.reloadConfigSignal.connect(self.load_config) # type: ignore
preferences_window.reloadDatabaseSignal.connect(self.tableView.load_music_table) # type: ignore
preferences_window.exec_() # Display the preferences window modally preferences_window.exec_() # Display the preferences window modally
# Quick Actions # Quick Actions