From 99bd9ddca88dfe2707bbd4f8a8a802241198f5df Mon Sep 17 00:00:00 2001 From: karxi Date: Sat, 19 Nov 2016 14:55:00 -0500 Subject: [PATCH] Initial attempt at moving away from pygame. --- TODO.mkdn | 1 + generic.py | 4 ++- pesterchum.py | 68 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/TODO.mkdn b/TODO.mkdn index b03e297..c35128d 100644 --- a/TODO.mkdn +++ b/TODO.mkdn @@ -38,6 +38,7 @@ Features * Also: Overhaul settings storage. Bring it more in line with the system Textsub used (if feeling masochistic), but simpler. * Overhaul debugging * Make a console to display debug info without requiring us to run from console +* Debug generic.py's CaseInsensitiveDict/replace it with mine Bugs ---- diff --git a/generic.py b/generic.py index 8ff08be..212f266 100644 --- a/generic.py +++ b/generic.py @@ -128,8 +128,10 @@ class MovingWindow(QtGui.QFrame): self.moving = None class NoneSound(object): + def __init__(self, *args, **kwargs): + pass def play(self): pass - def set_volume(self, v): pass + def setVolume(self, v): pass class WMButton(QtGui.QPushButton): def __init__(self, icon, parent=None): diff --git a/pesterchum.py b/pesterchum.py index 78a0c7b..6cb8748 100644 --- a/pesterchum.py +++ b/pesterchum.py @@ -40,7 +40,7 @@ else: minor = int(vnum[vnum.find(".")+1:]) if not ((major > 4) or (major == 4 and minor >= 6)): print "ERROR: Pesterchum requires Qt version >= 4.6" - print "You currently have version " + vnum + ". Please upgrade Qt" + print "You currently have version " + vnum + ". Please upgrade Qt." exit() import ostools @@ -1626,34 +1626,56 @@ class PesterWindow(MovingWindow): self.mychumcolor.setText("") # sounds - if not pygame or not pygame.mixer: + self._setup_sounds() + self.setVolume(self.config.volume()) + + def _setup_sounds(self, soundclass=None): + """Set up the event sounds for later use.""" + # Set up the sounds we're using. + + if soundclass is None: + if (pygame and pygame.mixer): + # We have pygame, so we may as well use it. + soundclass = pygame.mixer.Sound + elif QtGui.QSound.isAvailable(): + # We don't have pygame; try to use QSound. + soundclass = QtGui.QSound + else: + # We don't have any options...just use fillers. + soundclass = NoneSound + + # Use the class we chose to build the sound set. + try: + # karxi: These all seem to be created using local paths. + # Note that if QSound.isAvailable is False, we could still try + # to play QSound objects - it'll just fail silently. + self.alarm = soundclass(self.theme["main/sounds/alertsound"]) + self.memosound = soundclass(self.theme["main/sounds/memosound"]) + self.namesound = soundclass("themes/namealarm.wav") + self.ceasesound = soundclass(self.theme["main/sounds/ceasesound"]) + self.honksound = soundclass("themes/honk.wav") + except Exception as err: + print "Warning: Error loading sounds!" self.alarm = NoneSound() self.memosound = NoneSound() self.namesound = NoneSound() self.ceasesound = NoneSound() self.honksound = NoneSound() - else: - try: - self.alarm = pygame.mixer.Sound(theme["main/sounds/alertsound"]) - self.memosound = pygame.mixer.Sound(theme["main/sounds/memosound"]) - self.namesound = pygame.mixer.Sound("themes/namealarm.wav") - self.ceasesound = pygame.mixer.Sound(theme["main/sounds/ceasesound"]) - self.honksound = pygame.mixer.Sound("themes/honk.wav") - except Exception, e: - self.alarm = NoneSound() - self.memosound = NoneSound() - self.namesound = NoneSound() - self.ceasesound = NoneSound() - self.honksound = NoneSound() - self.setVolume(self.config.volume()) - + def setVolume(self, vol): + # TODO: Find a way to make this usable. + vol = vol/100.0 - self.alarm.set_volume(vol) - self.memosound.set_volume(vol) - self.namesound.set_volume(vol) - self.ceasesound.set_volume(vol) - self.honksound.set_volume(vol) + + # TODO: Make these into an actual (stored) /dict/ later, for easier + # iteration. + sounds = [self.alarm, self.memosound, self.namesound, self.ceasesound, + self.honksound] + for sound in sounds: + if pygame and pygame.mixer: + sound.set_volume(vol) + else: + sound.setVolume(vol) def changeTheme(self, theme): # check theme @@ -2754,6 +2776,8 @@ class MainProgram(QtCore.QObject): pygame.mixer.init() except pygame.error, e: print "Warning: No sound! %s" % (e) + elif not QtGui.QSound.isAvailable(): + print "Warning: No sound! (No pygame/QSound)" else: print "Warning: No sound!" self.widget = PesterWindow(options, app=self.app)