mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Move TTS UI logic into its own module
This commit is contained in:
parent
eaccc523af
commit
1fdef796ba
45
src/calibre/gui2/viewer/tts.py
Normal file
45
src/calibre/gui2/viewer/tts.py
Normal file
@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
from PyQt5.Qt import QObject, pyqtSignal
|
||||
from calibre.gui2 import error_dialog
|
||||
|
||||
|
||||
class TTS(QObject):
|
||||
|
||||
dispatch_on_main_thread_signal = pyqtSignal(object)
|
||||
|
||||
def __init__(self, parent=None):
|
||||
QObject.__init__(self, parent)
|
||||
self._tts_client = None
|
||||
self.dispatch_on_main_thread_signal.connect(self.dispatch_on_main_thread)
|
||||
|
||||
def dispatch_on_main_thread(self, func):
|
||||
try:
|
||||
func()
|
||||
except Exception:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
@property
|
||||
def tts_client(self):
|
||||
if self._tts_client is None:
|
||||
from calibre.gui2.tts.implementation import Client
|
||||
self._tts_client = Client(self.dispatch_on_main_thread_signal.emit)
|
||||
return self._tts_client
|
||||
|
||||
def shutdown(self):
|
||||
if self._tts_client is not None:
|
||||
self._tts_client.shutdown()
|
||||
self._tts_client = None
|
||||
|
||||
def speak_simple_text(self, text):
|
||||
from calibre.gui2.tts.errors import TTSSystemUnavailable
|
||||
try:
|
||||
self.tts_client.speak_simple_text(text)
|
||||
except TTSSystemUnavailable as err:
|
||||
return error_dialog(self.parent(), _('Text-to-Speech unavailable'), str(err), show=True)
|
||||
|
||||
def action(self, action, data):
|
||||
pass
|
@ -25,6 +25,7 @@ from calibre.ebooks.metadata.book.base import field_metadata
|
||||
from calibre.ebooks.oeb.polish.utils import guess_type
|
||||
from calibre.gui2 import choose_images, error_dialog, safe_open_url
|
||||
from calibre.gui2.viewer.config import viewer_config_dir, vprefs
|
||||
from calibre.gui2.viewer.tts import TTS
|
||||
from calibre.gui2.webengine import (
|
||||
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
|
||||
secure_webengine, to_js
|
||||
@ -465,18 +466,16 @@ class WebView(RestartingWebEngineView):
|
||||
paged_mode_changed = pyqtSignal()
|
||||
standalone_misc_settings_changed = pyqtSignal(object)
|
||||
view_created = pyqtSignal(object)
|
||||
dispatch_on_main_thread_signal = pyqtSignal(object)
|
||||
|
||||
def __init__(self, parent=None):
|
||||
self._host_widget = None
|
||||
self._tts_client = None
|
||||
self.callback_id_counter = count()
|
||||
self.callback_map = {}
|
||||
self.current_cfi = self.current_content_file = None
|
||||
RestartingWebEngineView.__init__(self, parent)
|
||||
self.tts = TTS(self)
|
||||
self.dead_renderer_error_shown = False
|
||||
self.render_process_failed.connect(self.render_process_died)
|
||||
self.dispatch_on_main_thread_signal.connect(self.dispatch_on_main_thread)
|
||||
w = QApplication.instance().desktop().availableGeometry(self).width()
|
||||
QApplication.instance().palette_changed.connect(self.palette_changed)
|
||||
self.show_home_page_on_ready = True
|
||||
@ -519,8 +518,8 @@ class WebView(RestartingWebEngineView):
|
||||
self.bridge.close_prep_finished.connect(self.close_prep_finished)
|
||||
self.bridge.highlights_changed.connect(self.highlights_changed)
|
||||
self.bridge.open_url.connect(safe_open_url)
|
||||
self.bridge.speak_simple_text.connect(self.speak_simple_text)
|
||||
self.bridge.tts.connect(self.tts_action)
|
||||
self.bridge.speak_simple_text.connect(self.tts.speak_simple_text)
|
||||
self.bridge.tts.connect(self.tts.action)
|
||||
self.bridge.export_shortcut_map.connect(self.set_shortcut_map)
|
||||
self.shortcut_map = {}
|
||||
self.bridge.report_cfi.connect(self.call_callback)
|
||||
@ -534,34 +533,8 @@ class WebView(RestartingWebEngineView):
|
||||
self.inspector = Inspector(parent.inspector_dock.toggleViewAction(), self)
|
||||
parent.inspector_dock.setWidget(self.inspector)
|
||||
|
||||
@property
|
||||
def tts_client(self):
|
||||
if self._tts_client is None:
|
||||
from calibre.gui2.tts.implementation import Client
|
||||
self._tts_client = Client(self.dispatch_on_main_thread_signal.emit)
|
||||
return self._tts_client
|
||||
|
||||
def dispatch_on_main_thread(self, func):
|
||||
try:
|
||||
func()
|
||||
except Exception:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
def speak_simple_text(self, text):
|
||||
from calibre.gui2.tts.errors import TTSSystemUnavailable
|
||||
try:
|
||||
self.tts_client.speak_simple_text(text)
|
||||
except TTSSystemUnavailable as err:
|
||||
return error_dialog(self, _('Text-to-Speech unavailable'), str(err), show=True)
|
||||
|
||||
def tts_action(self, action, data):
|
||||
pass
|
||||
|
||||
def shutdown(self):
|
||||
if self._tts_client is not None:
|
||||
self._tts_client.shutdown()
|
||||
self._tts_client = None
|
||||
self.tts.shutdown()
|
||||
|
||||
def set_shortcut_map(self, smap):
|
||||
self.shortcut_map = smap
|
||||
|
Loading…
x
Reference in New Issue
Block a user