Preferences, singletons
Create a Preference dialog for fonts and audio output devices Turn Preferences and SoundOff into singletons. No matter how many times you request a new one, the same instance is returned. Stop using singals on the parent() to access other instances, such as sound and Preferences.
This commit is contained in:
95
lib/preferences.py
Normal file
95
lib/preferences.py
Normal file
@@ -0,0 +1,95 @@
|
||||
import json
|
||||
import os
|
||||
from PyQt6.QtCore import Qt, pyqtSlot
|
||||
from PyQt6.QtWidgets import QDialog, QListWidgetItem, QAbstractItemView
|
||||
from PyQt6.QtMultimedia import QMediaDevices
|
||||
from ui.Preferences import Ui_Dialog
|
||||
|
||||
class Preferences(QDialog, Ui_Dialog):
|
||||
_instance = None
|
||||
def __new__(cls):
|
||||
if cls._instance:
|
||||
return cls._instance
|
||||
cls._instance = super(Preferences, cls).__new__(cls)
|
||||
return cls._instance
|
||||
@pyqtSlot(int)
|
||||
def done(self,r):
|
||||
self.hide()
|
||||
super().done(r)
|
||||
return
|
||||
@pyqtSlot()
|
||||
def exec(self):
|
||||
self.show()
|
||||
super().exec()
|
||||
return
|
||||
@pyqtSlot()
|
||||
def open(self):
|
||||
self.show()
|
||||
super().open()
|
||||
return
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(Preferences, self).__init__(*args, **kwargs)
|
||||
self.setupUi(self)
|
||||
self.hide()
|
||||
#
|
||||
# Overrides
|
||||
#
|
||||
self.alertList.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
|
||||
self.playerList.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
|
||||
self.readerCombo.setEditable(False)
|
||||
self.phoneticsCombo.setEditable(False)
|
||||
#
|
||||
# End OverRides
|
||||
#
|
||||
for index, output in enumerate(QMediaDevices.audioOutputs()):
|
||||
identifier = output.id().data().decode("utf-8")
|
||||
description = output.description()
|
||||
self.alertList.addItem(description)
|
||||
self.playerList.addItem(description)
|
||||
self.setCurrent()
|
||||
return
|
||||
|
||||
def setCurrent(self):
|
||||
if os.path.exists('preferences.json'):
|
||||
with open('preferences.json','r') as f:
|
||||
self.preferences = json.load(f)
|
||||
else:
|
||||
self.preferences = {
|
||||
'readerFont': 'OpenDyslexic',
|
||||
'phoneticFont': 'Gentium',
|
||||
'alertOutputs': [ 'default' ],
|
||||
'playerOutputs': [ 'Feed for virtual microphone' ]
|
||||
}
|
||||
for output in self.preferences['alertOutputs']:
|
||||
if output == 'default':
|
||||
output = QMediaDevices.defaultAudioOutput().description()
|
||||
for item in self.alertList.findItems(output,Qt.MatchFlag.MatchExactly):
|
||||
item.setSelected(True)
|
||||
for output in self.preferences['playerOutputs']:
|
||||
if output == 'default':
|
||||
output = QMediaDevices.defaultAudioOutput().description()
|
||||
for item in self.playerList.findItems(output,Qt.MatchFlag.MatchExactly ):
|
||||
item.setSelected(True)
|
||||
index = self.readerCombo.findText(self.preferences['readerFont'])
|
||||
if index >= 0:
|
||||
self.readerCombo.setCurrentIndex(index)
|
||||
index = self.phoneticsCombo.findText(self.preferences['phoneticFont'])
|
||||
if index >= 0:
|
||||
self.phoneticsCombo.setCurrentIndex(index)
|
||||
return
|
||||
|
||||
def get(self, name:str =None):
|
||||
if not name:
|
||||
return self.preferences
|
||||
return self.preferences[name]
|
||||
|
||||
def accept(self):
|
||||
self.preferences['readerFont'] = self.readerCombo.currentFont().family()
|
||||
self.preferences['phoneticFont'] =self.phoneticsCombo.currentFont().family()
|
||||
self.preferences['alertOutputs'] = [ x.data(Qt.ItemDataRole.DisplayRole) for x in self.alertList.selectedItems() ]
|
||||
self.preferences['playerOutputs'] = [ x.data(Qt.ItemDataRole.DisplayRole) for x in self.playerList.selectedItems() ]
|
||||
with open('preferences.json','w') as f:
|
||||
json.dump(self.preferences,f,indent=2)
|
||||
super().accept()
|
||||
return
|
||||
Reference in New Issue
Block a user