Skip to content

Commit

Permalink
play tone in preferences widget
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Jul 23, 2024
1 parent bdd22cc commit 8547cd8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 20 deletions.
12 changes: 12 additions & 0 deletions qtribu/gui/dlg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###############
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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(
Expand Down
40 changes: 40 additions & 0 deletions qtribu/gui/dlg_settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,46 @@
</item>
<item row="9" column="1">
<widget class="QComboBox" name="cbb_ring_tone">
<item>
<property name="text">
<string>beep_1</string>
</property>
</item>
<item>
<property name="text">
<string>beep_2</string>
</property>
</item>
<item>
<property name="text">
<string>beep_3</string>
</property>
</item>
<item>
<property name="text">
<string>beep_4</string>
</property>
</item>
<item>
<property name="text">
<string>beep_5</string>
</property>
</item>
<item>
<property name="text">
<string>beep_6</string>
</property>
</item>
<item>
<property name="text">
<string>beep_7</string>
</property>
</item>
<item>
<property name="text">
<string>horn</string>
</property>
</item>
<item>
<property name="text">
<string>dog</string>
Expand Down
22 changes: 5 additions & 17 deletions qtribu/gui/wdg_qchat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# standard
import json
import os
from datetime import datetime
from functools import partial
from pathlib import Path
Expand All @@ -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,
Expand All @@ -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 = "---"
Expand Down Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions qtribu/toolbelt/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions qtribu/utils.py
Original file line number Diff line number Diff line change
@@ -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:
"""
Expand Down

0 comments on commit 8547cd8

Please sign in to comment.