89 def __updateLanguages(self): |
89 def __updateLanguages(self): |
90 """ |
90 """ |
91 Private slot to update the language combo boxes. |
91 Private slot to update the language combo boxes. |
92 """ |
92 """ |
93 self.__ensureTranslationEngineReady() |
93 self.__ensureTranslationEngineReady() |
94 |
94 if self.__translationEngine is not None: |
95 supportedCodes = self.__translationEngine.supportedLanguages() |
95 supportedCodes = self.__translationEngine.supportedLanguages() |
96 enabledCodes = self.__plugin.getPreferences("EnabledLanguages") |
96 enabledCodes = self.__plugin.getPreferences("EnabledLanguages") |
97 |
97 |
98 # 1. save current selections |
98 # 1. save current selections |
99 origLanguage = self.origLanguageComboBox.itemData( |
99 origLanguage = self.origLanguageComboBox.itemData( |
100 self.origLanguageComboBox.currentIndex()) |
100 self.origLanguageComboBox.currentIndex()) |
101 |
101 |
102 # 2. reload the original language combo box |
102 # 2. reload the original language combo box |
103 self.origLanguageComboBox.blockSignals(True) |
103 self.origLanguageComboBox.blockSignals(True) |
104 self.origLanguageComboBox.clear() |
104 self.origLanguageComboBox.clear() |
105 for code in enabledCodes: |
105 for code in enabledCodes: |
106 if code in supportedCodes: |
106 if code in supportedCodes: |
107 language = self.__languages.getLanguage(code) |
107 language = self.__languages.getLanguage(code) |
108 if language: |
108 if language: |
109 icon = self.__languages.getLanguageIcon(code) |
109 icon = self.__languages.getLanguageIcon(code) |
110 self.origLanguageComboBox.addItem(icon, language, code) |
110 self.origLanguageComboBox.addItem( |
111 self.origLanguageComboBox.model().sort(0) |
111 icon, language, code) |
112 origIndex = self.origLanguageComboBox.findData(origLanguage) |
112 self.origLanguageComboBox.model().sort(0) |
113 if origIndex == -1: |
113 origIndex = self.origLanguageComboBox.findData(origLanguage) |
114 origIndex = 0 |
114 if origIndex == -1: |
115 self.origLanguageComboBox.blockSignals(False) |
115 origIndex = 0 |
116 self.origLanguageComboBox.setCurrentIndex(origIndex) |
116 self.origLanguageComboBox.blockSignals(False) |
|
117 self.origLanguageComboBox.setCurrentIndex(origIndex) |
117 |
118 |
118 def __updateEngines(self): |
119 def __updateEngines(self): |
119 """ |
120 """ |
120 Private slot to update the engines combo box. |
121 Private slot to update the engines combo box. |
121 """ |
122 """ |
324 |
325 |
325 @param index current index |
326 @param index current index |
326 @type int |
327 @type int |
327 """ |
328 """ |
328 self.__ensureTranslationEngineReady() |
329 self.__ensureTranslationEngineReady() |
329 |
330 if self.__translationEngine is not None: |
330 self.__updateTranslateButton() |
331 self.__updateTranslateButton() |
331 self.__updatePronounceButtons() |
332 self.__updatePronounceButtons() |
332 |
333 |
333 self.__plugin.setPreferences( |
334 self.__plugin.setPreferences( |
334 "SelectedEngine", self.engineComboBox.itemData(index)) |
335 "SelectedEngine", self.engineComboBox.itemData(index)) |
335 |
336 |
336 def __updatePronounceButtons(self): |
337 def __updatePronounceButtons(self): |
337 """ |
338 """ |
338 Private slot to set the state of the pronounce buttons. |
339 Private slot to set the state of the pronounce buttons. |
339 """ |
340 """ |
376 if self.__translatorRequest is None: |
377 if self.__translatorRequest is None: |
377 from .TranslatorRequest import TranslatorRequest |
378 from .TranslatorRequest import TranslatorRequest |
378 self.__translatorRequest = TranslatorRequest(self) |
379 self.__translatorRequest = TranslatorRequest(self) |
379 |
380 |
380 self.__ensureTranslationEngineReady() |
381 self.__ensureTranslationEngineReady() |
381 |
382 if self.__translationEngine is None: |
382 result, ok = self.__translationEngine.getTranslation( |
383 return "", False |
383 self.__translatorRequest, text, originalLanguage, |
384 else: |
384 translationLanguage) |
385 result, ok = self.__translationEngine.getTranslation( |
385 |
386 self.__translatorRequest, text, originalLanguage, |
386 return result, ok |
387 translationLanguage) |
|
388 |
|
389 return result, ok |
387 |
390 |
388 def __pronounce(self, text, language): |
391 def __pronounce(self, text, language): |
389 """ |
392 """ |
390 Private method to pronounce the given text. |
393 Private method to pronounce the given text. |
391 |
394 |
406 |
409 |
407 if self.__mediaPlayer.state() == QMediaPlayer.PlayingState: |
410 if self.__mediaPlayer.state() == QMediaPlayer.PlayingState: |
408 return |
411 return |
409 |
412 |
410 self.__ensureTranslationEngineReady() |
413 self.__ensureTranslationEngineReady() |
411 |
414 if self.__translationEngine is not None: |
412 if not self.__translationEngine.hasTTS(): |
415 if not self.__translationEngine.hasTTS(): |
413 E5MessageBox.critical( |
416 E5MessageBox.critical( |
414 self, |
417 self, |
415 self.tr("Translation Error"), |
418 self.tr("Translation Error"), |
416 self.tr("The selected translation service does not support" |
419 self.tr("The selected translation service does not" |
417 " the Text-to-Speech function.")) |
420 " support the Text-to-Speech function.")) |
418 return |
421 return |
419 |
|
420 data, ok = self.__translationEngine.getTextToSpeechData( |
|
421 self.__translatorRequest, text, language) |
|
422 if ok: |
|
423 self.__mediaFile = QTemporaryFile(self) |
|
424 self.__mediaFile.open() |
|
425 self.__mediaFile.setAutoRemove(False) |
|
426 self.__mediaFile.write(data) |
|
427 |
422 |
428 self.__mediaPlayer.setMedia(QMediaContent(), self.__mediaFile) |
423 data, ok = self.__translationEngine.getTextToSpeechData( |
429 self.__mediaPlayer.play() |
424 self.__translatorRequest, text, language) |
430 else: |
425 if ok: |
431 E5MessageBox.critical( |
426 self.__mediaFile = QTemporaryFile(self) |
432 self, |
427 self.__mediaFile.open() |
433 self.tr("Translation Error"), |
428 self.__mediaFile.setAutoRemove(False) |
434 data) |
429 self.__mediaFile.write(data) |
|
430 |
|
431 self.__mediaPlayer.setMedia(QMediaContent(), self.__mediaFile) |
|
432 self.__mediaPlayer.play() |
|
433 else: |
|
434 E5MessageBox.critical( |
|
435 self, |
|
436 self.tr("Translation Error"), |
|
437 data) |
435 |
438 |
436 def __mediaPlayerStateChanged(self, state): |
439 def __mediaPlayerStateChanged(self, state): |
437 """ |
440 """ |
438 Private slot handling changes of the media player state. |
441 Private slot handling changes of the media player state. |
439 |
442 |