diff --git a/qtribu/gui/dlg_settings.py b/qtribu/gui/dlg_settings.py index 5961d5e0..ca4a5580 100644 --- a/qtribu/gui/dlg_settings.py +++ b/qtribu/gui/dlg_settings.py @@ -27,6 +27,7 @@ from qtribu.toolbelt import PlgLogger, PlgOptionsManager from qtribu.toolbelt.commons import open_url_in_browser from qtribu.toolbelt.preferences import PlgSettingsStructure +from qtribu.utils import play_resource_sound # ############################################################################ # ########## Globals ############### @@ -85,6 +86,9 @@ def __init__(self, parent=None): # load previously saved settings self.load_settings() + # play sound on ringtone changed + self.cbb_ring_tone.currentIndexChanged.connect(self.on_ring_tone_changed) + def apply(self): """Called to permanently apply the settings shown in the options page (e.g. \ save them to QgsSettings objects). This is usually called when the options \ @@ -158,6 +162,14 @@ def show_instance_rules(self) -> None: ), ) + def on_ring_tone_changed(self) -> None: + """ + Action called when ringtone value is changed + """ + play_resource_sound( + self.cbb_ring_tone.currentText(), self.hsl_sound_volume.value() + ) + def reset_read_history(self): """Set latest_content_guid to None.""" new_settings = PlgSettingsStructure( diff --git a/qtribu/gui/dlg_settings.ui b/qtribu/gui/dlg_settings.ui index 0f991473..8e3cbccd 100644 --- a/qtribu/gui/dlg_settings.ui +++ b/qtribu/gui/dlg_settings.ui @@ -349,6 +349,46 @@ + + + beep_1 + + + + + beep_2 + + + + + beep_3 + + + + + beep_4 + + + + + beep_5 + + + + + beep_6 + + + + + beep_7 + + + + + horn + + dog diff --git a/qtribu/gui/wdg_qchat.py b/qtribu/gui/wdg_qchat.py index 6436f48b..ae1ca5a4 100644 --- a/qtribu/gui/wdg_qchat.py +++ b/qtribu/gui/wdg_qchat.py @@ -1,6 +1,5 @@ # standard import json -import os from datetime import datetime from functools import partial from pathlib import Path @@ -16,7 +15,6 @@ from qgis.PyQt.QtGui import QIcon from qgis.PyQt.QtWidgets import QMessageBox, QTreeWidgetItem, QWidget -from qtribu.__about__ import DIR_PLUGIN_ROOT from qtribu.constants import ( CHEATCODE_10OCLOCK, CHEATCODE_DIZZY, @@ -29,7 +27,7 @@ # plugin from qtribu.toolbelt import PlgLogger, PlgOptionsManager from qtribu.toolbelt.preferences import PlgSettingsStructure -from qtribu.utils import play_sound +from qtribu.utils import play_resource_sound # -- GLOBALS -- MARKER_VALUE = "---" @@ -308,7 +306,9 @@ def on_ws_message_received(self, message: str) -> None: 0, self.add_message_to_treeview(self.current_room, message) ) if self.settings.qchat_play_sounds: - self._play_sound(self.settings.qchat_ring_tone) + play_resource_sound( + self.settings.qchat_ring_tone, self.settings.qchat_sound_volume + ) def on_clear_chat_button_clicked(self) -> None: """ @@ -381,18 +381,6 @@ def check_cheatcode(self, message: dict[str, str]) -> bool: # play sounds if self.settings.qchat_play_sounds: if msg in [CHEATCODE_DONTCRYBABY, CHEATCODE_IAMAROBOT, CHEATCODE_10OCLOCK]: - self._play_sound(msg) + play_resource_sound(msg, self.settings.qchat_sound_volume) return True return False - - def _play_sound(self, file_name: str) -> None: - """ - Play a sound inside QGIS - The file_name param must be the name (without extension) of a .ogg audio file inside resources/sounds folder - """ - file_path = str(DIR_PLUGIN_ROOT / f"resources/sounds/{file_name}.ogg") - if not os.path.exists(file_path): - err_msg = f"File '{file_name}.wav' not found in resources/sounds folder" - self.log(message=err_msg, log_level=Qgis.Critical) - raise FileNotFoundError(err_msg) - play_sound(file_path, self.settings.qchat_sound_volume) diff --git a/qtribu/toolbelt/preferences.py b/qtribu/toolbelt/preferences.py index 3cd0964c..c1cbb244 100644 --- a/qtribu/toolbelt/preferences.py +++ b/qtribu/toolbelt/preferences.py @@ -35,9 +35,9 @@ class PlgSettingsStructure: qchat_instance_uri: str = "https://qchat.guilhemallaman.net" qchat_nickname: str = "J.D." qchat_activate_cheatcode: bool = True - qchat_play_sounds: bool = False - qchat_sound_volume: int = 50 - qchat_ring_tone: str = "dog" + qchat_play_sounds: bool = True + qchat_sound_volume: int = 33 + qchat_ring_tone: str = "beep_1" # usage browser: int = 1 diff --git a/qtribu/utils.py b/qtribu/utils.py index 62b2ae53..cb08356c 100644 --- a/qtribu/utils.py +++ b/qtribu/utils.py @@ -1,7 +1,24 @@ +import os + from PyQt5 import QtMultimedia # noqa QGS103 from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QSound # noqa QGS103 from qgis.PyQt.QtCore import QUrl +from qtribu.__about__ import DIR_PLUGIN_ROOT + + +def play_resource_sound(resource: str, volume: int) -> None: + """ + Play a sound inside QGIS + The file_name param must be the name (without extension) of a .ogg audio file inside resources/sounds folder + """ + file_path = str(DIR_PLUGIN_ROOT / f"resources/sounds/{resource}.ogg") + if not os.path.exists(file_path): + raise FileNotFoundError( + f"File '{resource}.wav' not found in resources/sounds folder" + ) + play_sound(file_path, volume) + def play_sound(file: str, volume: int) -> None: """