mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-27 19:24:08 +00:00
make ArticleView::hasSound() async
This commit is contained in:
parent
dc19f417db
commit
95be606a9e
|
@ -2,6 +2,7 @@
|
|||
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
||||
|
||||
#include "articleview.hh"
|
||||
#include "QtCore/qvariant.h"
|
||||
#include "folding.hh"
|
||||
#include "fulltextsearch.hh"
|
||||
#include "gddebug.hh"
|
||||
|
@ -829,6 +830,7 @@ bool ArticleView::handleF3( QObject * /*obj*/, QEvent * ev )
|
|||
|
||||
bool ArticleView::eventFilter( QObject * obj, QEvent * ev )
|
||||
{
|
||||
|
||||
if( ev->type() == QEvent::Gesture )
|
||||
{
|
||||
Gestures::GestureResult result;
|
||||
|
@ -1644,12 +1646,14 @@ void ArticleView::forward()
|
|||
ui.definition->forward();
|
||||
}
|
||||
|
||||
bool ArticleView::hasSound()
|
||||
void ArticleView::hasSound(const std::function< void (bool) > &callback)
|
||||
{
|
||||
QVariant v = runJavaScriptSync( ui.definition->page(),"gdAudioLinks.first" );
|
||||
ui.definition->page()->runJavaScript("gdAudioLinks.first", [callback](const QVariant &v) {
|
||||
bool has = false;
|
||||
if ( v.type() == QVariant::String )
|
||||
return !v.toString().isEmpty();
|
||||
return false;
|
||||
has = !v.toString().isEmpty();
|
||||
callback(has);
|
||||
});
|
||||
}
|
||||
|
||||
//use webengine javascript to playsound
|
||||
|
@ -1661,10 +1665,14 @@ void ArticleView::playSound()
|
|||
" } "
|
||||
" return link;})(); ";
|
||||
|
||||
QString soundScript = runJavaScriptSync(ui.definition->page(), variable);
|
||||
ui.definition->page()->runJavaScript(variable,[this](const QVariant & result){
|
||||
if (result.type() == QVariant::String) {
|
||||
QString soundScript = result.toString();
|
||||
if (!soundScript.isEmpty())
|
||||
openLink(QUrl::fromEncoded(soundScript.toUtf8()), ui.definition->url());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// use eventloop to turn the async callback to sync execution.
|
||||
QString ArticleView::toHtml()
|
||||
|
|
|
@ -179,7 +179,7 @@ public:
|
|||
{ ui.definition->reload(); }
|
||||
|
||||
/// Returns true if there's an audio reference on the page, false otherwise.
|
||||
bool hasSound();
|
||||
void hasSound(const std::function< void (bool has) > &callback);
|
||||
|
||||
/// Plays the first audio reference on the page, if any.
|
||||
void playSound();
|
||||
|
|
|
@ -2033,10 +2033,14 @@ void MainWindow::updateBackForwardButtons()
|
|||
|
||||
void MainWindow::updatePronounceAvailability()
|
||||
{
|
||||
bool pronounceEnabled = ui.tabWidget->count() > 0 &&
|
||||
getCurrentArticleView()->hasSound();
|
||||
|
||||
navPronounce->setEnabled( pronounceEnabled );
|
||||
if (ui.tabWidget->count() > 0) {
|
||||
getCurrentArticleView()->hasSound([this](bool has) {
|
||||
navPronounce->setEnabled( has );
|
||||
});
|
||||
}
|
||||
else {
|
||||
navPronounce->setEnabled( false );
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::editDictionaries( unsigned editDictionaryGroup )
|
||||
|
|
|
@ -1141,7 +1141,11 @@ void ScanPopup::altModePoll()
|
|||
|
||||
void ScanPopup::pageLoaded( ArticleView * )
|
||||
{
|
||||
ui.pronounceButton->setVisible( definition->hasSound() );
|
||||
|
||||
|
||||
definition->hasSound([this](bool has){
|
||||
ui.pronounceButton->setVisible( has );
|
||||
});
|
||||
|
||||
updateBackForwardButtons();
|
||||
|
||||
|
|
Loading…
Reference in a new issue