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