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:
@@ -2,4 +2,3 @@ from .books import Book
|
||||
from .person import PersonDialog
|
||||
from .read import EditDialog
|
||||
from .session import SessionDialog
|
||||
from .sounds import SoundOff
|
||||
|
||||
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
|
||||
28
lib/read.py
28
lib/read.py
@@ -18,7 +18,6 @@ from PyQt6.QtCore import (
|
||||
from PyQt6.QtGui import (
|
||||
QBrush,
|
||||
QColor,
|
||||
QFont,
|
||||
QKeyEvent,
|
||||
QMouseEvent,
|
||||
QPainter,
|
||||
@@ -35,21 +34,32 @@ from PyQt6.QtWidgets import QDialog, QPushButton
|
||||
|
||||
from main import query_error
|
||||
from ui.EditDialog import Ui_Dialog
|
||||
|
||||
from lib.preferences import Preferences
|
||||
from lib.sounds import SoundOff
|
||||
|
||||
class EditDialog(QDialog, Ui_Dialog):
|
||||
playSound = pyqtSignal(str)
|
||||
block: int
|
||||
paragraphs = True
|
||||
sessionSignal = pyqtSignal()
|
||||
displayedWord = pyqtSignal(int)
|
||||
newParagraph = pyqtSignal(int, int)
|
||||
|
||||
|
||||
def __init__(self, parent, session, person_id: int) -> None:
|
||||
self.session = session
|
||||
super(EditDialog, self).__init__(parent)
|
||||
print(self.parent())
|
||||
self.person_id = person_id
|
||||
self.preferences = Preferences().get()
|
||||
self.sound = SoundOff()
|
||||
styleSheet = QResource(":/display.css").data().decode("utf-8")
|
||||
print(styleSheet)
|
||||
styleSheet = styleSheet.replace(
|
||||
'{readerFont}',self.preferences['readerFont']
|
||||
)
|
||||
styleSheet = styleSheet.replace(
|
||||
'{phoneticFont}',self.preferences['phoneticFont']
|
||||
)
|
||||
print(styleSheet)
|
||||
self.setupUi(self)
|
||||
#
|
||||
# Override UI
|
||||
@@ -75,8 +85,6 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
self.scrollBtn.clicked.connect(self.scrollAction)
|
||||
self.nextBtn.clicked.connect(self.nextAction)
|
||||
self.prevBtn.clicked.connect(self.prevAction)
|
||||
# self.sessionSignal.connect(self.session.timerAction)
|
||||
# self.sessionBtn.clicked.connect(self.sessionAction)
|
||||
self.sessionBtn.clicked.connect(self.session.timerAction)
|
||||
self.paraEdit.verticalScrollBar().valueChanged.connect(self.scrollSlot)
|
||||
self.defEdit.selectionChanged.connect(self.recursiveDef)
|
||||
@@ -85,6 +93,8 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
#
|
||||
self.displayedWord.connect(self.session.addWord)
|
||||
self.newParagraph.connect(self.session.addBlock)
|
||||
self.playSound.connect(self.sound.playSound)
|
||||
#self.alert.connect(self.sound.alert)
|
||||
return
|
||||
|
||||
#
|
||||
@@ -161,14 +171,14 @@ class EditDialog(QDialog, Ui_Dialog):
|
||||
self.phonetics = data["phonetics"]
|
||||
else:
|
||||
self.phonetics = None
|
||||
print("Checking for phonetics")
|
||||
if not self.phonetics:
|
||||
return
|
||||
print("Looking for audio")
|
||||
print("Searching for audio file")
|
||||
for entry in self.phonetics:
|
||||
if len(entry["audio"]) > 0:
|
||||
# self.parent().playAlert.emit()
|
||||
self.parent().playSound.emit(entry["audio"])
|
||||
print(f"playing {entry['audio']}")
|
||||
self.playSound.emit(entry["audio"])
|
||||
return
|
||||
|
||||
@pyqtSlot()
|
||||
|
||||
@@ -114,7 +114,7 @@ class SessionDialog(QDialog, Ui_Dialog):
|
||||
query = QSqlQuery()
|
||||
query.prepare(
|
||||
"UPDATE sessions "
|
||||
"SET start=:start , SET stop=:stop, SET notes=:notes "
|
||||
"SET start=:start, stop=:stop, notes=:notes "
|
||||
"WHERE sesion_id = :session_id"
|
||||
)
|
||||
query.bindValue(":session_id", self.session_id)
|
||||
|
||||
@@ -11,6 +11,13 @@ from PyQt6.QtMultimedia import (
|
||||
|
||||
|
||||
class SoundOff(QObject):
|
||||
_instance = None
|
||||
def __new__(cls):
|
||||
if cls._instance:
|
||||
return cls._instance
|
||||
cls._instance = super(SoundOff, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
#
|
||||
@@ -95,7 +102,6 @@ class SoundOff(QObject):
|
||||
self.localPlayer.play()
|
||||
if not self.virtualDevice:
|
||||
return
|
||||
return
|
||||
self.virtualPlayer.setSource(src)
|
||||
self.virtualPlayer.setPosition(0)
|
||||
if not self.virtualPlayer.audioOutput():
|
||||
|
||||
Reference in New Issue
Block a user