goldendict-ng/src/ui/articleview.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

2407 lines
75 KiB
C++
Raw Normal View History

2012-02-20 21:47:14 +00:00
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "articleview.hh"
#include "dict/programs.hh"
#include "folding.hh"
#include "gddebug.hh"
#include "gestures.hh"
#include "globalbroadcaster.hh"
#include "speechclient.hh"
#include "utils.hh"
#include "webmultimediadownload.hh"
#include "wildcard.hh"
#include "wstring_qt.hh"
#include <QBuffer>
#include <QClipboard>
#include <QCryptographicHash>
#include <QDebug>
#include <QDesktopServices>
#include <QFileDialog>
#include <QKeyEvent>
#include <QMenu>
#include <QMessageBox>
#include <QRegularExpression>
#include <QVariant>
#include <QWebChannel>
2021-07-06 13:01:50 +00:00
#include <QWebEngineHistory>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>
#include <QWebEngineSettings>
#include <map>
#include <QApplication>
#include <QRandomGenerator>
2022-02-27 05:17:37 +00:00
#if ( QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) && QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
2021-12-28 13:59:49 +00:00
#include <QWebEngineContextMenuData>
2022-02-27 05:17:37 +00:00
#endif
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
2022-02-27 14:42:40 +00:00
#include <QtCore5Compat/QRegExp>
2022-02-27 05:17:37 +00:00
#include <QWebEngineContextMenuRequest>
#include <QWebEngineFindTextResult>
#include <utility>
2022-02-27 05:17:37 +00:00
#endif
#ifdef Q_OS_WIN32
#include <windows.h>
#include <QPainter>
#endif
using std::map;
using std::list;
namespace {
char const * const scrollToPrefix = "gdfrom-";
bool isScrollTo( QString const & id )
{
return id.startsWith( scrollToPrefix );
}
QString dictionaryIdFromScrollTo( QString const & scrollTo )
{
Q_ASSERT( isScrollTo( scrollTo ) );
constexpr int scrollToPrefixLength = 7;
return scrollTo.mid( scrollToPrefixLength );
}
QString searchStatusMessageNoMatches()
{
return ArticleView::tr( "Phrase not found" );
}
QString searchStatusMessage( int activeMatch, int matchCount )
{
Q_ASSERT( matchCount > 0 );
Q_ASSERT( activeMatch > 0 );
Q_ASSERT( activeMatch <= matchCount );
return ArticleView::tr( "%1 of %2 matches" ).arg( activeMatch ).arg( matchCount );
}
} // unnamed namespace
QString ArticleView::scrollToFromDictionaryId( QString const & dictionaryId )
{
Q_ASSERT( !isScrollTo( dictionaryId ) );
return scrollToPrefix + dictionaryId;
}
ArticleView::ArticleView( QWidget * parent,
ArticleNetworkAccessManager & nm,
AudioPlayerPtr const & audioPlayer_,
std::vector< sptr< Dictionary::Class > > const & allDictionaries_,
Instances::Groups const & groups_,
bool popupView_,
Config::Class const & cfg_,
QAction & openSearchAction_,
QLineEdit const * translateLine_,
QAction * dictionaryBarToggled_,
GroupComboBox const * groupComboBox_ ):
QWidget( parent ),
articleNetMgr( nm ),
audioPlayer( audioPlayer_ ),
allDictionaries( allDictionaries_ ),
groups( groups_ ),
popupView( popupView_ ),
cfg( cfg_ ),
pasteAction( this ),
articleUpAction( this ),
articleDownAction( this ),
goBackAction( this ),
goForwardAction( this ),
selectCurrentArticleAction( this ),
copyAsTextAction( this ),
inspectAction( this ),
2014-04-16 16:18:28 +00:00
openSearchAction( openSearchAction_ ),
2009-05-16 11:14:43 +00:00
searchIsOpened( false ),
dictionaryBarToggled( dictionaryBarToggled_ ),
groupComboBox( groupComboBox_ ),
2023-05-13 00:44:17 +00:00
translateLine( translateLine_ )
{
if ( groupComboBox_ )
currentGroupId = groupComboBox_->getCurrentGroup();
// setup GUI
webview = new ArticleWebView( this );
ftsSearchPanel = new FtsSearchPanel( this );
searchPanel = new SearchPanel( this );
// Layout
auto * mainLayout = new QVBoxLayout( this );
mainLayout->addWidget( webview );
mainLayout->addWidget( ftsSearchPanel );
mainLayout->addWidget( searchPanel );
webview->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
ftsSearchPanel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
searchPanel->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Minimum );
mainLayout->setContentsMargins( 0, 0, 0, 0 );
// end UI setup
connect( searchPanel->previous, &QPushButton::clicked, this, &ArticleView::on_searchPrevious_clicked );
connect( searchPanel->next, &QPushButton::clicked, this, &ArticleView::on_searchNext_clicked );
connect( searchPanel->close, &QPushButton::clicked, this, &ArticleView::on_searchCloseButton_clicked );
connect( searchPanel->caseSensitive, &QPushButton::clicked, this, &ArticleView::on_searchCaseSensitive_clicked );
connect( searchPanel->highlightAll, &QPushButton::clicked, this, &ArticleView::on_highlightAllButton_clicked );
connect( searchPanel->lineEdit, &QLineEdit::textEdited, this, &ArticleView::on_searchText_textEdited );
connect( searchPanel->lineEdit, &QLineEdit::returnPressed, this, &ArticleView::on_searchText_returnPressed );
connect( ftsSearchPanel->next, &QPushButton::clicked, this, &ArticleView::on_ftsSearchNext_clicked );
connect( ftsSearchPanel->previous, &QPushButton::clicked, this, &ArticleView::on_ftsSearchPrevious_clicked );
//
webview->setUp( const_cast< Config::Class * >( &cfg ) );
goBackAction.setShortcut( QKeySequence( "Alt+Left" ) );
webview->addAction( &goBackAction );
connect( &goBackAction, &QAction::triggered, this, &ArticleView::back );
goForwardAction.setShortcut( QKeySequence( "Alt+Right" ) );
webview->addAction( &goForwardAction );
connect( &goForwardAction, &QAction::triggered, this, &ArticleView::forward );
webview->pageAction( QWebEnginePage::Copy )->setShortcut( QKeySequence::Copy );
webview->addAction( webview->pageAction( QWebEnginePage::Copy ) );
QAction * selectAll = webview->pageAction( QWebEnginePage::SelectAll );
2021-08-21 01:41:40 +00:00
selectAll->setShortcut( QKeySequence::SelectAll );
selectAll->setShortcutContext( Qt::WidgetWithChildrenShortcut );
webview->addAction( selectAll );
webview->setContextMenuPolicy( Qt::CustomContextMenu );
connect( webview, &QWebEngineView::loadFinished, this, &ArticleView::loadFinished );
connect( webview, &ArticleWebView::linkClicked, this, &ArticleView::linkClicked );
connect( webview->page(), &QWebEnginePage::titleChanged, this, &ArticleView::handleTitleChanged );
connect( webview->page(), &QWebEnginePage::urlChanged, this, &ArticleView::handleUrlChanged );
connect( webview, &QWidget::customContextMenuRequested, this, &ArticleView::contextMenuRequested );
connect( webview->page(), &QWebEnginePage::linkHovered, this, &ArticleView::linkHovered );
connect( webview, &ArticleWebView::doubleClicked, this, &ArticleView::doubleClicked );
pasteAction.setShortcut( QKeySequence::Paste );
webview->addAction( &pasteAction );
connect( &pasteAction, &QAction::triggered, this, &ArticleView::pasteTriggered );
articleUpAction.setShortcut( QKeySequence( "Alt+Up" ) );
webview->addAction( &articleUpAction );
connect( &articleUpAction, &QAction::triggered, this, &ArticleView::moveOneArticleUp );
articleDownAction.setShortcut( QKeySequence( "Alt+Down" ) );
webview->addAction( &articleDownAction );
connect( &articleDownAction, &QAction::triggered, this, &ArticleView::moveOneArticleDown );
webview->addAction( &openSearchAction );
connect( &openSearchAction, &QAction::triggered, this, &ArticleView::openSearch );
2009-05-16 11:14:43 +00:00
selectCurrentArticleAction.setShortcut( QKeySequence( "Ctrl+Shift+A" ) );
selectCurrentArticleAction.setText( tr( "Select Current Article" ) );
webview->addAction( &selectCurrentArticleAction );
connect( &selectCurrentArticleAction, &QAction::triggered, this, &ArticleView::selectCurrentArticle );
copyAsTextAction.setShortcut( QKeySequence( "Ctrl+Shift+C" ) );
copyAsTextAction.setText( tr( "Copy as text" ) );
webview->addAction( &copyAsTextAction );
connect( &copyAsTextAction, &QAction::triggered, this, &ArticleView::copyAsText );
inspectAction.setShortcut( QKeySequence( Qt::Key_F12 ) );
inspectAction.setText( tr( "Inspect" ) );
webview->addAction( &inspectAction );
2021-11-30 03:40:57 +00:00
connect( &inspectAction, &QAction::triggered, this, &ArticleView::inspectElement );
webview->installEventFilter( this );
searchPanel->installEventFilter( this );
ftsSearchPanel->installEventFilter( this );
QWebEngineSettings * settings = webview->settings();
settings->setUnknownUrlSchemePolicy( QWebEngineSettings::UnknownUrlSchemePolicy::DisallowUnknownUrlSchemes );
#if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
settings->defaultSettings()->setAttribute( QWebEngineSettings::LocalContentCanAccessRemoteUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::LocalContentCanAccessFileUrls, true );
settings->defaultSettings()->setAttribute( QWebEngineSettings::ErrorPageEnabled, false );
settings->defaultSettings()->setAttribute( QWebEngineSettings::PluginsEnabled, true );
2022-04-26 12:21:45 +00:00
settings->defaultSettings()->setAttribute( QWebEngineSettings::PlaybackRequiresUserGesture, false );
settings->defaultSettings()->setAttribute( QWebEngineSettings::JavascriptCanAccessClipboard, true );
2022-05-24 13:40:53 +00:00
settings->defaultSettings()->setAttribute( QWebEngineSettings::PrintElementBackgrounds, false );
#else
settings->setAttribute( QWebEngineSettings::LocalContentCanAccessRemoteUrls, true );
settings->setAttribute( QWebEngineSettings::LocalContentCanAccessFileUrls, true );
settings->setAttribute( QWebEngineSettings::ErrorPageEnabled, false );
settings->setAttribute( QWebEngineSettings::PluginsEnabled, true );
settings->setAttribute( QWebEngineSettings::PlaybackRequiresUserGesture, false );
2022-04-26 12:21:45 +00:00
settings->setAttribute( QWebEngineSettings::JavascriptCanAccessClipboard, true );
2022-05-24 13:40:53 +00:00
settings->setAttribute( QWebEngineSettings::PrintElementBackgrounds, false );
#endif
auto html = articleNetMgr.getHtml( ResourceType::UNTITLE );
webview->setHtml( QString::fromStdString( html ) );
2022-08-23 11:09:38 +00:00
expandOptionalParts = cfg.preferences.alwaysExpandOptionalParts;
webview->grabGesture( Gestures::GDPinchGestureType );
webview->grabGesture( Gestures::GDSwipeGestureType );
// Variable name for store current selection range
rangeVarName = QString( "sr_%1" ).arg( QString::number( (quint64)this, 16 ) );
connect( GlobalBroadcaster::instance(), &GlobalBroadcaster::dictionaryChanges, this, &ArticleView::setActiveDictIds );
connect( GlobalBroadcaster::instance(), &GlobalBroadcaster::dictionaryClear, this, &ArticleView::dictionaryClear );
channel = new QWebChannel( webview->page() );
agent = new ArticleViewAgent( this );
attachWebChannelToHtml();
ankiConnector = new AnkiConnector( this, cfg );
connect( ankiConnector, &AnkiConnector::errorText, this, [ this ]( QString const & errorText ) {
emit statusBarMessage( errorText );
} );
// Set up an Anki action if Anki integration is enabled in settings.
if ( cfg.preferences.ankiConnectServer.enabled ) {
sendToAnkiAction.setShortcut( QKeySequence( "Ctrl+Shift+N" ) );
webview->addAction( &sendToAnkiAction );
connect( &sendToAnkiAction, &QAction::triggered, this, &ArticleView::handleAnkiAction );
}
}
// explicitly report the minimum size, to avoid
// sidebar widgets' improper resize during restore
QSize ArticleView::minimumSizeHint() const
{
return searchPanel->minimumSizeHint();
}
void ArticleView::setCurrentGroupId( unsigned currentGrgId )
{
currentGroupId = currentGrgId;
}
unsigned ArticleView::getCurrentGroupId()
{
return currentGroupId;
}
ArticleView::~ArticleView()
{
cleanupTemp();
audioPlayer->stop();
//channel->deregisterObject(this);
webview->ungrabGesture( Gestures::GDPinchGestureType );
webview->ungrabGesture( Gestures::GDSwipeGestureType );
}
void ArticleView::showDefinition( QString const & word,
unsigned group,
QString const & scrollTo,
2015-10-28 19:56:58 +00:00
Contexts const & contexts_ )
{
GlobalBroadcaster::instance()->pronounce_engine.reset();
currentWord = word.trimmed();
if ( currentWord.isEmpty() )
return;
historyMode = false;
currentActiveDictIds.clear();
// first, let's stop the player
audioPlayer->stop();
QUrl req;
2015-10-28 19:56:58 +00:00
Contexts contexts( contexts_ );
req.setScheme( "gdlookup" );
req.setHost( "localhost" );
Utils::Url::addQueryItem( req, "word", word );
Utils::Url::addQueryItem( req, "group", QString::number( group ) );
if ( cfg.preferences.ignoreDiacritics )
Utils::Url::addQueryItem( req, "ignore_diacritics", "1" );
if ( scrollTo.size() )
Utils::Url::addQueryItem( req, "scrollto", scrollTo );
if ( delayedHighlightText.size() ) {
Utils::Url::addQueryItem( req, "regexp", delayedHighlightText );
delayedHighlightText.clear();
}
if ( Contexts::Iterator pos = contexts.find( "gdanchor" ); pos != contexts.end() ) {
Utils::Url::addQueryItem( req, "gdanchor", contexts[ "gdanchor" ] );
2015-10-28 19:56:58 +00:00
contexts.erase( pos );
}
if ( contexts.size() ) {
QBuffer buf;
buf.open( QIODevice::WriteOnly );
QDataStream stream( &buf );
stream << contexts;
buf.close();
Utils::Url::addQueryItem( req, "contexts", QString::fromLatin1( buf.buffer().toBase64() ) );
}
QString mutedDicts = getMutedForGroup( group );
if ( mutedDicts.size() )
Utils::Url::addQueryItem( req, "muted", mutedDicts );
// Any search opened is probably irrelevant now
closeSearch();
//QApplication::setOverrideCursor( Qt::WaitCursor );
webview->setCursor( Qt::WaitCursor );
load( req );
// Update headwords history
emit sendWordToHistory( word );
}
void ArticleView::showDefinition( QString const & word,
QStringList const & dictIDs,
QRegExp const & searchRegExp,
unsigned group,
bool ignoreDiacritics )
2014-04-16 16:18:28 +00:00
{
if ( dictIDs.isEmpty() )
return;
currentWord = word.trimmed();
if ( currentWord.isEmpty() )
return;
historyMode = false;
//clear founded dicts.
currentActiveDictIds.clear();
2014-04-16 16:18:28 +00:00
// first, let's stop the player
audioPlayer->stop();
2014-04-16 16:18:28 +00:00
QUrl req;
req.setScheme( "gdlookup" );
req.setHost( "localhost" );
Utils::Url::addQueryItem( req, "word", word );
Utils::Url::addQueryItem( req, "dictionaries", dictIDs.join( "," ) );
Utils::Url::addQueryItem( req, "regexp", searchRegExp.pattern() );
2022-03-20 11:27:35 +00:00
if ( searchRegExp.caseSensitivity() == Qt::CaseSensitive )
Utils::Url::addQueryItem( req, "matchcase", "1" );
2022-03-20 11:27:35 +00:00
if ( searchRegExp.patternSyntax() == QRegExp::WildcardUnix )
Utils::Url::addQueryItem( req, "wildcards", "1" );
Utils::Url::addQueryItem( req, "group", QString::number( group ) );
if ( ignoreDiacritics )
Utils::Url::addQueryItem( req, "ignore_diacritics", "1" );
2014-04-16 16:18:28 +00:00
// Any search opened is probably irrelevant now
closeSearch();
// Clear highlight all button selection
searchPanel->highlightAll->setChecked( false );
webview->setCursor( Qt::WaitCursor );
2014-04-16 16:18:28 +00:00
load( req );
2014-04-16 16:18:28 +00:00
// Update headwords history
emit sendWordToHistory( word );
2014-04-16 16:18:28 +00:00
}
void ArticleView::sendToAnki( QString const & word, QString const & dict_definition, QString const & sentence )
{
ankiConnector->sendToAnki( word, dict_definition, sentence );
}
void ArticleView::showAnticipation()
{
webview->setHtml( "" );
webview->setCursor( Qt::WaitCursor );
}
void ArticleView::inspectElement()
{
emit inspectSignal( webview->page() );
}
void ArticleView::loadFinished( bool result )
{
setZoomFactor( cfg.preferences.zoomFactor );
QUrl url = webview->url();
qDebug() << "article view loaded url:" << url.url().left( 200 ) << result;
if ( url.url() == "about:blank" ) {
return;
}
if ( !result ) {
qWarning() << "article loaded unsuccessful";
return;
}
Add "Automatically scroll to target article" option When a user clicks on a link in a dictionary or requests translation of a word by double-clicking or translates selection via the context menu, at first the article from the highest-priority dictionary is at the top. Then, after approximately one second, the article from the dictionary, out of which the translation was requested, becomes current and the view scrolls down to this article placing it on top, hiding articles from the dictionaries above it. Such application behavior is inconvenient in some workflows so that the user manually navigates to the top dictionary translation when this automatic scrolling happens. For example: a user has English->Russian dictionaries and English->English dictionaries. The English->Russian dictionaries are higher up in the dictionary order because they provide easier/faster to understand translations. Some rare words and phrases are missing from the English->Russian dictionaries however. Thus the user occasionally reads the English explanation of a word/phrase. When the user double-clicks on a word or follows a link in the English->English dictionary article, she would rather see translations from the preferable English->Russian dictionaries. The new option allows to disable automatic scrolling and ensure that articles from higher-priority dictionaries are visible. The option doesn't affect backward/forward navigation via arrow buttons or Alt+Arrow shortcuts: these still scroll to the stored vertical position among articles. This remaining automatic scrolling happens much faster, is not a problem for the described use case and hopefully for other use cases.
2019-01-11 13:50:40 +00:00
if ( cfg.preferences.autoScrollToTargetArticle ) {
QString const scrollTo = Utils::Url::queryItemValue( url, "scrollto" );
if ( isScrollTo( scrollTo ) ) {
setCurrentArticle( scrollTo, true );
}
2023-07-27 00:29:51 +00:00
else {
setActiveArticleId( "" );
}
}
2023-07-27 00:29:51 +00:00
else {
//clear current active dictionary id;
2023-07-27 00:29:51 +00:00
setActiveArticleId( "" );
}
webview->unsetCursor();
// Expand collapsed article if only one loaded
webview->page()->runJavaScript( QString( "gdCheckArticlesNumber();" ) );
if ( !Utils::Url::queryItemValue( url, "gdanchor" ).isEmpty() ) {
2023-04-09 03:48:21 +00:00
const QString anchor = QUrl::fromPercentEncoding( Utils::Url::encodedQueryItemValue( url, "gdanchor" ) );
2015-10-28 19:56:58 +00:00
// Find GD anchor on page
url.clear();
url.setFragment( anchor );
webview->page()->runJavaScript(
QString( "window.location.hash = \"%1\"" ).arg( QString::fromUtf8( url.toEncoded() ) ) );
2015-10-28 19:56:58 +00:00
}
//the click audio url such as gdau://xxxx ,webview also emit a pageLoaded signal but with the result is false.need future investigation.
//the audio link click ,no need to emit pageLoaded signal
if ( result ) {
emit pageLoaded( this );
}
if ( Utils::Url::hasQueryItem( webview->url(), "regexp" ) ) {
highlightFTSResults();
}
}
void ArticleView::handleTitleChanged( QString const & title )
{
2016-04-01 13:38:07 +00:00
if ( !title.isEmpty() ) // Qt 5.x WebKit raise signal titleChanges(QString()) while navigation within page
emit titleChanged( this, title );
}
void ArticleView::handleUrlChanged( QUrl const & url )
{
QIcon icon;
if ( unsigned group = getGroup( url ) ) {
// Find the group's instance corresponding to the fragment value
2023-05-13 00:44:17 +00:00
for ( auto const & g : groups ) {
if ( g.id == group ) {
// Found it
2023-05-13 00:44:17 +00:00
icon = g.makeIcon();
break;
}
2023-05-13 00:44:17 +00:00
}
}
emit iconChanged( this, icon );
}
unsigned ArticleView::getGroup( QUrl const & url )
{
if ( url.scheme() == "gdlookup" && Utils::Url::hasQueryItem( url, "group" ) )
return Utils::Url::queryItemValue( url, "group" ).toUInt();
return 0;
}
QStringList ArticleView::getArticlesList()
2021-12-13 14:45:16 +00:00
{
return currentActiveDictIds;
}
QString ArticleView::getActiveArticleId()
{
return activeDictId;
}
void ArticleView::setActiveArticleId( QString const & dictId )
{
this->activeDictId = dictId;
}
QString ArticleView::getCurrentArticle()
{
const QString dictId = getActiveArticleId();
return scrollToFromDictionaryId( dictId );
}
void ArticleView::jumpToDictionary( QString const & id, bool force )
{
// jump only if neceessary, or when forced
if ( const QString targetArticle = scrollToFromDictionaryId( id ); force || targetArticle != getCurrentArticle() ) {
setCurrentArticle( targetArticle, true );
}
}
bool ArticleView::setCurrentArticle( QString const & id, bool moveToIt )
{
if ( !isScrollTo( id ) )
return false; // Incorrect id
if ( !webview->isVisible() )
return false; // No action on background page, scrollIntoView there don't work
2021-10-05 01:23:30 +00:00
if ( moveToIt ) {
2022-02-25 14:48:43 +00:00
QString dictId = id.mid( 7 );
if ( dictId.isEmpty() )
2022-05-24 23:57:43 +00:00
return false;
QString script =
QString(
"var elem=document.getElementById('%1'); "
"if(elem!=undefined){elem.scrollIntoView(true);} if(typeof gdMakeArticleActive !='undefined') gdMakeArticleActive('%2',true);" )
.arg( id, dictId );
2022-02-25 14:48:43 +00:00
onJsActiveArticleChanged( id );
webview->page()->runJavaScript( script );
2022-02-25 14:48:43 +00:00
setActiveArticleId( dictId );
2021-10-05 01:23:30 +00:00
}
return true;
}
void ArticleView::selectCurrentArticle()
{
webview->page()->runJavaScript(
QString(
"gdSelectArticle( '%1' );var elem=document.getElementById('%2'); if(elem!=undefined){elem.scrollIntoView(true);}" )
.arg( getActiveArticleId(), getCurrentArticle() ) );
}
2022-03-30 15:08:24 +00:00
void ArticleView::isFramedArticle( QString const & ca, const std::function< void( bool ) > & callback )
{
2022-03-30 15:08:24 +00:00
if ( ca.isEmpty() )
callback( false );
webview->page()->runJavaScript( QString( "!!document.getElementById('gdexpandframe-%1');" ).arg( ca.mid( 7 ) ),
[ callback ]( const QVariant & res ) {
callback( res.toBool() );
} );
}
void ArticleView::tryMangleWebsiteClickedUrl( QUrl & url, Contexts & contexts )
{
// Don't try mangling audio urls, even if they are from the framed websites
if ( !url.isValid() )
return;
if ( ( url.scheme() == "http" || url.scheme() == "https" ) && !Utils::Url::isWebAudioUrl( url ) ) {
// Maybe a link inside a website was clicked?
QString ca = getCurrentArticle();
2022-07-08 14:13:40 +00:00
isFramedArticle( ca, []( bool framed ) {} );
}
}
void ArticleView::load( QUrl const & url )
{
webview->load( url );
}
void ArticleView::cleanupTemp()
{
2023-05-13 00:44:17 +00:00
auto it = desktopOpenedTempFiles.begin();
while ( it != desktopOpenedTempFiles.end() ) {
if ( QFile::remove( *it ) )
it = desktopOpenedTempFiles.erase( it );
else
++it;
}
}
bool ArticleView::handleF3( QObject * /*obj*/, QEvent * ev )
{
if ( ev->type() == QEvent::ShortcutOverride || ev->type() == QEvent::KeyPress ) {
QKeyEvent * ke = static_cast< QKeyEvent * >( ev );
if ( ke->key() == Qt::Key_F3 && isSearchOpened() ) {
if ( !ke->modifiers() ) {
if ( ev->type() == QEvent::KeyPress )
on_searchNext_clicked();
ev->accept();
return true;
}
if ( ke->modifiers() == Qt::ShiftModifier ) {
if ( ev->type() == QEvent::KeyPress )
on_searchPrevious_clicked();
ev->accept();
return true;
}
}
if ( ke->key() == Qt::Key_F3 && ftsSearchIsOpened ) {
if ( !ke->modifiers() ) {
if ( ev->type() == QEvent::KeyPress )
on_ftsSearchNext_clicked();
ev->accept();
return true;
}
if ( ke->modifiers() == Qt::ShiftModifier ) {
if ( ev->type() == QEvent::KeyPress )
on_ftsSearchPrevious_clicked();
ev->accept();
return true;
}
}
}
return false;
}
bool ArticleView::eventFilter( QObject * obj, QEvent * ev )
{
#ifdef Q_OS_MAC
2022-03-27 14:22:42 +00:00
2022-03-30 15:10:37 +00:00
if ( ev->type() == QEvent::NativeGesture ) {
qDebug() << "it's a Native Gesture!";
// handle Qt::ZoomNativeGesture Qt::SmartZoomNativeGesture here
// ignore swipe left/right.
// QWebEngine can handle Qt::SmartZoomNativeGesture.
}
#else
if ( ev->type() == QEvent::Gesture ) {
Gestures::GestureResult result;
QPoint pt;
bool handled = Gestures::handleGestureEvent( obj, ev, result, pt );
if ( handled ) {
if ( result == Gestures::ZOOM_IN )
emit zoomIn();
else if ( result == Gestures::ZOOM_OUT )
emit zoomOut();
else if ( result == Gestures::SWIPE_LEFT )
back();
else if ( result == Gestures::SWIPE_RIGHT )
forward();
else if ( result == Gestures::SWIPE_UP || result == Gestures::SWIPE_DOWN ) {
int delta = result == Gestures::SWIPE_UP ? -120 : 120;
QWidget * widget = static_cast< QWidget * >( obj );
QPoint angleDelta( 0, delta );
QPoint pixelDetal;
QWidget * child = widget->childAt( widget->mapFromGlobal( pt ) );
if ( child ) {
QWheelEvent whev( child->mapFromGlobal( pt ),
pt,
pixelDetal,
angleDelta,
Qt::NoButton,
Qt::NoModifier,
Qt::NoScrollPhase,
false );
qApp->sendEvent( child, &whev );
}
else {
QWheelEvent whev( widget->mapFromGlobal( pt ),
pt,
pixelDetal,
angleDelta,
Qt::NoButton,
Qt::NoModifier,
Qt::NoScrollPhase,
false );
qApp->sendEvent( widget, &whev );
}
}
}
return handled;
}
#endif
if ( ev->type() == QEvent::MouseMove ) {
if ( Gestures::isFewTouchPointsPresented() ) {
ev->accept();
return true;
}
}
if ( handleF3( obj, ev ) ) {
return true;
}
if ( obj == webview ) {
if ( ev->type() == QEvent::KeyPress ) {
auto keyEvent = static_cast< QKeyEvent * >( ev );
if ( keyEvent->modifiers() & ( Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier ) )
return false; // A non-typing modifier is pressed
if ( Utils::ignoreKeyEvent( keyEvent ) || keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter )
return false; // Those key have other uses than to start typing
QString text = keyEvent->text();
if ( text.size() ) {
emit typingEvent( text );
return true;
}
}
2022-02-17 16:39:24 +00:00
else if ( ev->type() == QEvent::Wheel ) {
QWheelEvent * pe = static_cast< QWheelEvent * >( ev );
if ( pe->modifiers().testFlag( Qt::ControlModifier ) ) {
if ( pe->angleDelta().y() > 0 ) {
zoomIn();
}
else {
zoomOut();
}
}
}
}
else
return QWidget::eventFilter( obj, ev );
return false;
}
QString ArticleView::getMutedForGroup( unsigned group )
{
if ( dictionaryBarToggled && dictionaryBarToggled->isChecked() ) {
// Dictionary bar is active -- mute the muted dictionaries
Instances::Group const * groupInstance = groups.findGroup( group );
// Find muted dictionaries for current group
Config::Group const * grp = cfg.getGroup( group );
Config::MutedDictionaries const * mutedDictionaries;
if ( group == Instances::Group::AllGroupId )
mutedDictionaries = popupView ? &cfg.popupMutedDictionaries : &cfg.mutedDictionaries;
else
2023-05-13 00:44:17 +00:00
mutedDictionaries = grp ? ( popupView ? &grp->popupMutedDictionaries : &grp->mutedDictionaries ) : nullptr;
if ( !mutedDictionaries )
2023-09-02 03:45:47 +00:00
return {};
QStringList mutedDicts;
if ( groupInstance ) {
for ( const auto & dictionarie : groupInstance->dictionaries ) {
QString id = QString::fromStdString( dictionarie->getId() );
if ( mutedDictionaries->contains( id ) )
mutedDicts.append( id );
}
}
if ( !mutedDicts.empty() )
return mutedDicts.join( "," );
}
2023-09-02 03:45:47 +00:00
return {};
}
2022-01-01 10:19:11 +00:00
QStringList ArticleView::getMutedDictionaries( unsigned group )
{
if ( dictionaryBarToggled && dictionaryBarToggled->isChecked() ) {
// Dictionary bar is active -- mute the muted dictionaries
Instances::Group const * groupInstance = groups.findGroup( group );
// Find muted dictionaries for current group
Config::Group const * grp = cfg.getGroup( group );
Config::MutedDictionaries const * mutedDictionaries;
if ( group == Instances::Group::AllGroupId )
mutedDictionaries = popupView ? &cfg.popupMutedDictionaries : &cfg.mutedDictionaries;
else
2023-05-13 00:44:17 +00:00
mutedDictionaries = grp ? ( popupView ? &grp->popupMutedDictionaries : &grp->mutedDictionaries ) : nullptr;
2022-01-01 10:19:11 +00:00
if ( !mutedDictionaries )
2023-09-02 03:45:47 +00:00
return {};
2022-01-01 10:19:11 +00:00
QStringList mutedDicts;
if ( groupInstance ) {
for ( const auto & dictionarie : groupInstance->dictionaries ) {
QString id = QString::fromStdString( dictionarie->getId() );
2022-01-01 10:19:11 +00:00
if ( mutedDictionaries->contains( id ) )
mutedDicts.append( id );
}
}
return mutedDicts;
}
2023-09-02 03:45:47 +00:00
return {};
2021-10-03 11:28:26 +00:00
}
2021-07-06 13:01:50 +00:00
void ArticleView::linkHovered( const QString & link )
{
QString msg;
QUrl url( link );
if ( url.scheme() == "bres" ) {
msg = tr( "Resource" );
}
else if ( url.scheme() == "gdau" || Utils::Url::isAudioUrl( url ) ) {
msg = tr( "Audio" );
}
2013-04-24 16:01:44 +00:00
else if ( url.scheme() == "gdtts" ) {
msg = tr( "TTS Voice" );
}
2013-06-22 16:36:25 +00:00
else if ( url.scheme() == "gdvideo" ) {
if ( url.path().isEmpty() ) {
msg = tr( "Video" );
}
else {
QString path = url.path();
if ( path.startsWith( '/' ) ) {
path = path.mid( 1 );
}
msg = tr( "Video: %1" ).arg( path );
}
}
else if ( url.scheme() == "gdlookup" || url.scheme().compare( "bword" ) == 0 ) {
QString def = url.path();
if ( def.startsWith( "/" ) ) {
def = def.mid( 1 );
}
if ( Utils::Url::hasQueryItem( url, "dict" ) ) {
// Link to other dictionary
QString dictName( Utils::Url::queryItemValue( url, "dict" ) );
if ( !dictName.isEmpty() )
msg = tr( "Definition from dictionary \"%1\": %2" ).arg( dictName, def );
}
if ( msg.isEmpty() ) {
if ( def.isEmpty() && url.hasFragment() )
msg = '#' + url.fragment(); // this must be a citation, footnote or backlink
else
msg = tr( "Definition: %1" ).arg( def );
}
}
else {
msg = link;
}
emit statusBarMessage( msg );
}
void ArticleView::attachWebChannelToHtml()
{
// set the web channel to be used by the page
// see http://doc.qt.io/qt-5/qwebenginepage.html#setWebChannel
webview->page()->setWebChannel( channel, QWebEngineScript::MainWorld );
2021-07-06 13:01:50 +00:00
// register QObjects to be exposed to JavaScript
channel->registerObject( QStringLiteral( "articleview" ), agent );
}
void ArticleView::linkClicked( QUrl const & url_ )
{
Qt::KeyboardModifiers kmod = QApplication::keyboardModifiers();
// Lock jump on links while Alt key is pressed
if ( kmod & Qt::AltModifier )
return;
QUrl url( url_ );
Contexts contexts;
tryMangleWebsiteClickedUrl( url, contexts );
if ( !popupView && ( webview->isMidButtonPressed() || ( kmod & ( Qt::ControlModifier | Qt::ShiftModifier ) ) )
&& !isAudioLink( url ) ) {
// Mid button or Control/Shift is currently pressed - open the link in new tab
webview->resetMidButtonPressed();
emit openLinkInNewTab( url, webview->url(), getCurrentArticle(), contexts );
}
else
openLink( url, webview->url(), getCurrentArticle(), contexts );
}
void ArticleView::linkClickedInHtml( QUrl const & url_ )
{
webview->linkClickedInHtml( url_ );
if ( !url_.isEmpty() ) {
linkClicked( url_ );
}
}
void ArticleView::makeAnkiCardFromArticle( QString const & article_id )
{
auto const js_code = QString( R"EOF(document.getElementById("gdarticlefrom-%1").innerText)EOF" ).arg( article_id );
webview->page()->runJavaScript( js_code, [ this ]( const QVariant & article_text ) {
sendToAnki( webview->title(), article_text.toString(), translateLine->text() );
} );
}
void ArticleView::openLink( QUrl const & url, QUrl const & ref, QString const & scrollTo, Contexts const & contexts_ )
{
audioPlayer->stop();
qDebug() << "open link url:" << url;
2022-06-07 00:05:49 +00:00
auto [ valid, word ] = Utils::Url::getQueryWord( url );
if ( valid && word.isEmpty() ) {
//if valid=true and word is empty,the url must be a invalid gdlookup url.
//else if valid=false,the url should be external urls.
return;
}
2015-10-28 19:56:58 +00:00
Contexts contexts( contexts_ );
if ( url.scheme().compare( "ankisearch" ) == 0 ) {
ankiConnector->ankiSearch( url.path() );
return;
}
else if ( url.scheme().compare( "ankicard" ) == 0 ) {
// If article id is set in path and selection is empty, use text from the current article.
// Otherwise, grab currently selected text and use it as the definition.
if ( !url.path().isEmpty() && webview->selectedText().isEmpty() ) {
makeAnkiCardFromArticle( url.path() );
}
else {
sendToAnki( webview->title(), webview->selectedText(), translateLine->text() );
}
qDebug() << "requested to make Anki card.";
return;
}
else if ( url.scheme().compare( "bword" ) == 0 || url.scheme().compare( "entry" ) == 0 ) {
if ( Utils::Url::hasQueryItem( ref, "dictionaries" ) ) {
QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ).split( ",", Qt::SkipEmptyParts );
showDefinition( word, dictsList, QRegExp(), getGroup( ref ), false );
}
else
showDefinition( word, getGroup( ref ), scrollTo, contexts );
}
else if ( url.scheme() == "gdlookup" ) // Plain html links inherit gdlookup scheme
{
if ( url.hasFragment() ) {
webview->page()->runJavaScript(
QString( "window.location = \"%1\"" ).arg( QString::fromUtf8( url.toEncoded() ) ) );
}
else {
if ( Utils::Url::hasQueryItem( ref, "dictionaries" ) ) {
// Specific dictionary group from full-text search
QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ).split( ",", Qt::SkipEmptyParts );
showDefinition( url.path().mid( 1 ), dictsList, QRegExp(), getGroup( ref ), false );
return;
}
if ( Utils::Url::hasQueryItem( url, "dictionaries" ) ) {
// Specific dictionary group from full-text search
QStringList dictsList = Utils::Url::queryItemValue( url, "dictionaries" ).split( ",", Qt::SkipEmptyParts );
showDefinition( word, dictsList, QRegExp(), getGroup( url ), false );
return;
}
QString newScrollTo( scrollTo );
if ( Utils::Url::hasQueryItem( url, "dict" ) ) {
// Link to other dictionary
QString dictName( Utils::Url::queryItemValue( url, "dict" ) );
for ( const auto & allDictionarie : allDictionaries ) {
if ( dictName.compare( QString::fromUtf8( allDictionarie->getName().c_str() ) ) == 0 ) {
newScrollTo = scrollToFromDictionaryId( QString::fromUtf8( allDictionarie->getId().c_str() ) );
break;
}
}
}
2015-10-28 19:56:58 +00:00
if ( Utils::Url::hasQueryItem( url, "gdanchor" ) )
contexts[ "gdanchor" ] = Utils::Url::queryItemValue( url, "gdanchor" );
2015-10-28 19:56:58 +00:00
showDefinition( word, getGroup( ref ), newScrollTo, contexts );
}
}
2013-06-22 16:36:25 +00:00
else if ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo"
|| Utils::Url::isAudioUrl( url ) ) {
// Download it
// Clear any pending ones
resourceDownloadRequests.clear();
resourceDownloadUrl = url;
if ( Utils::Url::isWebAudioUrl( url ) ) {
sptr< Dictionary::DataRequest > req = std::make_shared< Dictionary::WebMultimediaDownload >( url, articleNetMgr );
resourceDownloadRequests.push_back( req );
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
else if ( url.scheme() == "gdau" && url.host() == "search" ) {
// Since searches should be limited to current group, we just do them
// here ourselves since otherwise we'd need to pass group id to netmgr
// and it should've been having knowledge of the current groups, too.
unsigned currentGroup = getGroup( ref );
std::vector< sptr< Dictionary::Class > > const * activeDicts = nullptr;
2023-09-02 03:45:47 +00:00
if ( !groups.empty() ) {
for ( const auto & group : groups )
if ( group.id == currentGroup ) {
activeDicts = &( group.dictionaries );
break;
}
}
else
activeDicts = &allDictionaries;
if ( activeDicts ) {
unsigned preferred = UINT_MAX;
if ( url.hasFragment() ) {
// Find sound in the preferred dictionary
QString preferredName = Utils::Url::fragment( url );
try {
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
if ( preferredName.compare( QString::fromUtf8( ( *activeDicts )[ x ]->getName().c_str() ) ) == 0 ) {
preferred = x;
sptr< Dictionary::DataRequest > req =
( *activeDicts )[ x ]->getResource( url.path().mid( 1 ).toUtf8().data() );
resourceDownloadRequests.push_back( req );
if ( !req->isFinished() ) {
// Queued loading
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
else {
// Immediate loading
if ( req->dataSize() > 0 ) {
// Resource already found, stop next search
resourceDownloadFinished();
return;
}
}
break;
}
}
}
catch ( std::exception & e ) {
emit statusBarMessage( tr( "ERROR: %1" ).arg( e.what() ), 10000, QPixmap( ":/icons/error.svg" ) );
}
}
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
try {
if ( x == preferred )
continue;
sptr< Dictionary::DataRequest > req =
( *activeDicts )[ x ]->getResource( url.path().mid( 1 ).toUtf8().data() );
2017-04-27 20:55:53 +00:00
resourceDownloadRequests.push_back( req );
if ( !req->isFinished() ) {
// Queued loading
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
else {
// Immediate loading
if ( req->dataSize() > 0 ) {
// Resource already found, stop next search
break;
}
}
}
catch ( std::exception & e ) {
emit statusBarMessage( tr( "ERROR: %1" ).arg( e.what() ), 10000, QPixmap( ":/icons/error.svg" ) );
}
}
2017-04-27 20:55:53 +00:00
}
}
else {
// Normal resource download
QString contentType;
sptr< Dictionary::DataRequest > req = articleNetMgr.getResource( url, contentType );
if ( !req.get() ) {
2024-01-10 01:57:31 +00:00
qDebug() << "request failed: " << url;
// Request failed, fail
}
else if ( req->isFinished() && req->dataSize() >= 0 ) {
// Have data ready, handle it
resourceDownloadRequests.push_back( req );
resourceDownloadFinished();
return;
}
else if ( !req->isFinished() ) {
// Queue to be handled when done
resourceDownloadRequests.push_back( req );
connect( req.get(), &Dictionary::Request::finished, this, &ArticleView::resourceDownloadFinished );
}
}
if ( resourceDownloadRequests.empty() ) // No requests were queued
{
2024-01-10 01:57:31 +00:00
qDebug() << tr( "The referenced resource doesn't exist." );
return;
}
else
resourceDownloadFinished(); // Check any requests finished already
}
else if ( url.scheme() == "gdprg" ) {
// Program. Run it.
QString id( url.host() );
for ( const auto & program : cfg.programs ) {
if ( program.id == id ) {
// Found the corresponding program.
Programs::RunInstance * req = new Programs::RunInstance;
connect( req, &Programs::RunInstance::finished, req, &QObject::deleteLater );
QString error;
// Delete the request if it fails to start
if ( !req->start( program, url.path().mid( 1 ), error ) ) {
delete req;
2013-02-01 12:36:01 +00:00
QMessageBox::critical( this, "GoldenDict", error );
}
return;
}
}
// Still here? No such program exists.
QMessageBox::critical( this, "GoldenDict", tr( "The referenced audio program doesn't exist." ) );
}
else if ( url.scheme() == "gdtts" ) {
// Text to speech
QString md5Id = Utils::Url::queryItemValue( url, "engine" );
QString text( url.path().mid( 1 ) );
for ( const auto & voiceEngine : cfg.voiceEngines ) {
QString itemMd5Id =
QString( QCryptographicHash::hash( voiceEngine.name.toUtf8(), QCryptographicHash::Md5 ).toHex() );
if ( itemMd5Id == md5Id ) {
SpeechClient * speechClient = new SpeechClient( voiceEngine, this );
connect( speechClient, SIGNAL( finished() ), speechClient, SLOT( deleteLater() ) );
2013-04-24 16:01:44 +00:00
speechClient->tell( text );
break;
}
}
}
2023-09-02 03:45:47 +00:00
else if ( Utils::isExternalLink( url ) ) {
// Use the system handler for the conventional external links
QDesktopServices::openUrl( url );
}
}
2017-04-27 20:55:53 +00:00
ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QString & fileName )
{
return saveResource( url, webview->url(), fileName );
}
2017-04-27 20:55:53 +00:00
ResourceToSaveHandler * ArticleView::saveResource( const QUrl & url, const QUrl & ref, const QString & fileName )
{
2017-04-27 20:55:53 +00:00
ResourceToSaveHandler * handler = new ResourceToSaveHandler( this, fileName );
sptr< Dictionary::DataRequest > req;
if ( url.scheme() == "bres" || url.scheme() == "gico" || url.scheme() == "gdau" || url.scheme() == "gdvideo" ) {
if ( url.host() == "search" ) {
// Since searches should be limited to current group, we just do them
// here ourselves since otherwise we'd need to pass group id to netmgr
// and it should've been having knowledge of the current groups, too.
unsigned currentGroup = getGroup( ref );
std::vector< sptr< Dictionary::Class > > const * activeDicts = nullptr;
if ( groups.size() ) {
for ( const auto & group : groups )
if ( group.id == currentGroup ) {
activeDicts = &( group.dictionaries );
break;
}
}
else
activeDicts = &allDictionaries;
if ( activeDicts ) {
unsigned preferred = UINT_MAX;
if ( url.hasFragment() && url.scheme() == "gdau" ) {
// Find sound in the preferred dictionary
QString preferredName = Utils::Url::fragment( url );
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
try {
if ( preferredName.compare( QString::fromUtf8( ( *activeDicts )[ x ]->getName().c_str() ) ) == 0 ) {
preferred = x;
sptr< Dictionary::DataRequest > data_request =
( *activeDicts )[ x ]->getResource( url.path().mid( 1 ).toUtf8().data() );
handler->addRequest( data_request );
if ( data_request->isFinished() && data_request->dataSize() > 0 ) {
handler->downloadFinished();
return handler;
}
break;
}
}
catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n",
e.what(),
( *activeDicts )[ x ]->getName().c_str() );
}
}
}
for ( unsigned x = 0; x < activeDicts->size(); ++x ) {
try {
if ( x == preferred )
continue;
req = ( *activeDicts )[ x ]->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
2017-04-27 20:55:53 +00:00
handler->addRequest( req );
if ( req->isFinished() && req->dataSize() > 0 ) {
// Resource already found, stop next search
break;
}
}
catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n",
e.what(),
( *activeDicts )[ x ]->getName().c_str() );
}
}
}
}
else {
// Normal resource download
QString contentType;
req = articleNetMgr.getResource( url, contentType );
2013-10-18 14:32:58 +00:00
if ( req.get() ) {
2017-04-27 20:55:53 +00:00
handler->addRequest( req );
2013-10-18 14:32:58 +00:00
}
}
}
else {
req = std::make_shared< Dictionary::WebMultimediaDownload >( url, articleNetMgr );
2017-04-27 20:55:53 +00:00
handler->addRequest( req );
}
2017-04-27 20:55:53 +00:00
if ( handler->isEmpty() ) // No requests were queued
{
emit statusBarMessage( tr( "ERROR: %1" ).arg( tr( "The referenced resource doesn't exist." ) ),
10000,
QPixmap( ":/icons/error.svg" ) );
}
2017-04-27 20:55:53 +00:00
// Check already finished downloads
handler->downloadFinished();
return handler;
}
void ArticleView::updateMutedContents()
{
QUrl currentUrl = webview->url();
if ( currentUrl.scheme() != "gdlookup" )
return; // Weird url -- do nothing
unsigned group = getGroup( currentUrl );
if ( !group )
return; // No group in url -- do nothing
QString mutedDicts = getMutedForGroup( group );
if ( Utils::Url::queryItemValue( currentUrl, "muted" ) != mutedDicts ) {
// The list has changed -- update the url
Utils::Url::removeQueryItem( currentUrl, "muted" );
if ( mutedDicts.size() )
Utils::Url::addQueryItem( currentUrl, "muted", mutedDicts );
load( currentUrl );
//QApplication::setOverrideCursor( Qt::WaitCursor );
webview->setCursor( Qt::WaitCursor );
}
}
bool ArticleView::canGoBack()
{
return webview->history()->canGoBack();
}
bool ArticleView::canGoForward()
{
return webview->history()->canGoForward();
}
void ArticleView::setSelectionBySingleClick( bool set )
{
webview->setSelectionBySingleClick( set );
}
void ArticleView::setDelayedHighlightText( QString const & text )
{
delayedHighlightText = text;
}
void ArticleView::back()
{
// Don't allow navigating back to page 0, which is usually the initial
// empty page
if ( canGoBack() ) {
currentActiveDictIds.clear();
historyMode = true;
webview->back();
}
}
void ArticleView::forward()
{
currentActiveDictIds.clear();
historyMode = true;
webview->forward();
}
void ArticleView::handleAnkiAction()
{
// React to the "send *word* to anki" action.
// If selected text is empty, use the whole article as the definition.
if ( webview->selectedText().isEmpty() ) {
makeAnkiCardFromArticle( getActiveArticleId() );
}
else {
sendToAnki( webview->title(), webview->selectedText(), translateLine->text() );
}
}
void ArticleView::reload()
{
webview->reload();
}
2022-03-30 15:08:24 +00:00
void ArticleView::hasSound( const std::function< void( bool ) > & callback )
{
webview->page()->runJavaScript( R"(if(typeof(gdAudioLinks)!="undefined") gdAudioLinks.first)",
[ callback ]( const QVariant & v ) {
bool has = false;
if ( v.type() == QVariant::String )
has = !v.toString().isEmpty();
callback( has );
} );
}
2021-12-13 14:45:16 +00:00
//use webengine javascript to playsound
void ArticleView::playSound()
{
QString variable = R"( (function(){ var link=gdAudioMap.get(gdAudioLinks.current);
if(link==undefined){
link=gdAudioLinks.first;
}
return link;})(); )";
2022-01-31 00:42:36 +00:00
webview->page()->runJavaScript( variable, [ this ]( const QVariant & result ) {
if ( result.type() == QVariant::String ) {
QString soundScript = result.toString();
if ( !soundScript.isEmpty() )
openLink( QUrl::fromEncoded( soundScript.toUtf8() ), webview->url() );
}
} );
}
void ArticleView::stopSound()
{
audioPlayer->stop();
}
2022-03-30 15:08:24 +00:00
void ArticleView::toHtml( const std::function< void( QString & ) > & callback )
2009-05-01 11:17:29 +00:00
{
webview->page()->toHtml( [ = ]( const QString & content ) {
QString html = content;
callback( html );
} );
2009-05-01 11:17:29 +00:00
}
void ArticleView::setHtml( const QString & content, const QUrl & baseUrl )
{
webview->page()->setHtml( content, baseUrl );
2021-08-21 01:41:40 +00:00
}
void ArticleView::setContent( const QByteArray & data, const QString & mimeType, const QUrl & baseUrl )
2009-05-01 11:17:29 +00:00
{
webview->page()->setContent( data, mimeType, baseUrl );
2009-05-01 11:17:29 +00:00
}
QString ArticleView::getTitle()
{
return webview->page()->title();
}
QString ArticleView::getWord() const
{
return currentWord;
}
2009-05-01 12:20:33 +00:00
void ArticleView::print( QPrinter * printer ) const
{
2022-03-12 10:12:17 +00:00
QEventLoop loop;
bool result;
auto printPreview = [ & ]( bool success ) {
result = success;
loop.quit();
};
2022-03-29 12:34:41 +00:00
#if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
webview->page()->print( printer, std::move( printPreview ) );
2022-03-29 12:34:41 +00:00
#else
connect( webview, &QWebEngineView::printFinished, &loop, std::move( printPreview ) );
webview->print( printer );
2022-03-29 12:34:41 +00:00
#endif
2022-03-12 10:12:17 +00:00
loop.exec();
if ( !result ) {
qDebug() << "print failed";
}
2009-05-01 12:20:33 +00:00
}
void ArticleView::contextMenuRequested( QPoint const & pos )
{
// Is that a link? Is there a selection?
QWebEnginePage * r = webview->page();
QMenu menu( this );
QAction * followLink = nullptr;
QAction * followLinkExternal = nullptr;
QAction * followLinkNewTab = nullptr;
QAction * lookupSelection = nullptr;
QAction * lookupSelectionGr = nullptr;
QAction * lookupSelectionNewTab = nullptr;
QAction * lookupSelectionNewTabGr = nullptr;
QAction * maxDictionaryRefsAction = nullptr;
QAction * addWordToHistoryAction = nullptr;
QAction * addHeaderToHistoryAction = nullptr;
QAction * sendWordToInputLineAction = nullptr;
QAction * saveImageAction = nullptr;
QAction * openImageAction = nullptr;
QAction * saveSoundAction = nullptr;
QAction * saveBookmark = nullptr;
2022-02-27 05:17:37 +00:00
#if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
const QWebEngineContextMenuData * menuData = &( r->contextMenuData() );
#else
QWebEngineContextMenuRequest * menuData = webview->lastContextMenuRequest();
2022-02-27 05:17:37 +00:00
#endif
QUrl targetUrl( menuData->linkUrl() );
Contexts contexts;
tryMangleWebsiteClickedUrl( targetUrl, contexts );
2021-12-29 12:09:29 +00:00
if ( !targetUrl.isEmpty() ) {
2023-09-02 03:45:47 +00:00
if ( !Utils::isExternalLink( targetUrl ) ) {
followLink = new QAction( tr( "Op&en Link" ), &menu );
menu.addAction( followLink );
if ( !popupView && !isAudioLink( targetUrl ) ) {
followLinkNewTab = new QAction( QIcon( ":/icons/addtab.svg" ), tr( "Open Link in New &Tab" ), &menu );
menu.addAction( followLinkNewTab );
}
}
2023-09-02 03:45:47 +00:00
if ( Utils::isExternalLink( targetUrl ) ) {
followLinkExternal = new QAction( tr( "Open Link in &External Browser" ), &menu );
menu.addAction( followLinkExternal );
menu.addAction( webview->pageAction( QWebEnginePage::CopyLinkToClipboard ) );
}
}
2022-01-02 08:30:16 +00:00
QUrl imageUrl;
2022-02-27 05:17:37 +00:00
#if ( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
if ( !popupView && menuData->mediaType() == QWebEngineContextMenuData::MediaTypeImage )
#else
if ( !popupView && menuData->mediaType() == QWebEngineContextMenuRequest::MediaType::MediaTypeImage )
#endif
2022-01-02 08:30:16 +00:00
{
imageUrl = menuData->mediaUrl();
if ( !imageUrl.isEmpty() ) {
menu.addAction( webview->pageAction( QWebEnginePage::CopyImageToClipboard ) );
saveImageAction = new QAction( tr( "Save &image..." ), &menu );
menu.addAction( saveImageAction );
openImageAction = new QAction( tr( "Open image in system viewer..." ), &menu );
menu.addAction( openImageAction );
}
2022-01-02 08:30:16 +00:00
}
if ( !popupView && isAudioLink( targetUrl ) ) {
2022-01-02 08:30:16 +00:00
saveSoundAction = new QAction( tr( "Save s&ound..." ), &menu );
menu.addAction( saveSoundAction );
}
QString const selectedText = webview->selectedText();
QString text = Utils::trimNonChar( selectedText );
if ( text.size() && text.size() < 60 ) {
// We don't prompt for selections larger or equal to 60 chars, since
// it ruins the menu and it's hardly a single word anyway.
lookupSelection = new QAction( tr( "&Look up \"%1\"" ).arg( text ), &menu );
menu.addAction( lookupSelection );
if ( !popupView ) {
lookupSelectionNewTab =
new QAction( QIcon( ":/icons/addtab.svg" ), tr( "Look up \"%1\" in &New Tab" ).arg( text ), &menu );
menu.addAction( lookupSelectionNewTab );
sendWordToInputLineAction = new QAction( tr( "Send \"%1\" to input line" ).arg( text ), &menu );
menu.addAction( sendWordToInputLineAction );
}
addWordToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).arg( text ), &menu );
menu.addAction( addWordToHistoryAction );
Instances::Group const * altGroup =
( currentGroupId != getGroup( webview->url() ) ) ? groups.findGroup( currentGroupId ) : nullptr;
if ( altGroup ) {
QIcon icon = altGroup->icon.size() ? QIcon( ":/flags/" + altGroup->icon ) : QIcon();
2009-05-24 16:40:53 +00:00
lookupSelectionGr = new QAction( icon, tr( "Look up \"%1\" in %2" ).arg( text ).arg( altGroup->name ), &menu );
menu.addAction( lookupSelectionGr );
if ( !popupView ) {
lookupSelectionNewTabGr =
new QAction( QIcon( ":/icons/addtab.svg" ),
tr( "Look up \"%1\" in %2 in &New Tab" ).arg( text ).arg( altGroup->name ),
&menu );
menu.addAction( lookupSelectionNewTabGr );
}
}
}
if ( text.size() ) {
// avoid too long in the menu ,use left 30 characters.
saveBookmark = new QAction( tr( "Save &Bookmark \"%1...\"" ).arg( text.left( 30 ) ), &menu );
menu.addAction( saveBookmark );
}
// Add anki menu (if enabled)
// If there is no selected text, it will extract text from the current article.
if ( cfg.preferences.ankiConnectServer.enabled ) {
menu.addAction( &sendToAnkiAction );
sendToAnkiAction.setText( webview->selectedText().isEmpty() ? tr( "&Send Current Article to Anki" ) :
tr( "&Send selected text to Anki" ) );
}
if ( text.isEmpty() && !cfg.preferences.storeHistory ) {
QString txt = webview->title();
if ( txt.size() > 60 )
txt = txt.left( 60 ) + "...";
addHeaderToHistoryAction = new QAction( tr( "&Add \"%1\" to history" ).arg( txt ), &menu );
menu.addAction( addHeaderToHistoryAction );
}
if ( selectedText.size() ) {
menu.addAction( webview->pageAction( QWebEnginePage::Copy ) );
menu.addAction( &copyAsTextAction );
}
else {
menu.addAction( &selectCurrentArticleAction );
menu.addAction( webview->pageAction( QWebEnginePage::SelectAll ) );
}
map< QAction *, QString > tableOfContents;
// Add table of contents
QStringList ids = getArticlesList();
if ( !menu.isEmpty() && ids.size() )
menu.addSeparator();
unsigned refsAdded = 0;
bool maxDictionaryRefsReached = false;
for ( QStringList::const_iterator i = ids.constBegin(); i != ids.constEnd(); ++i, ++refsAdded ) {
// Find this dictionary
for ( unsigned x = allDictionaries.size(); x--; ) {
if ( allDictionaries[ x ]->getId() == i->toUtf8().data() ) {
QAction * action = nullptr;
if ( refsAdded == cfg.preferences.maxDictionaryRefsInContextMenu ) {
// Enough! Or the menu would become too large.
maxDictionaryRefsAction = new QAction( ".........", &menu );
action = maxDictionaryRefsAction;
maxDictionaryRefsReached = true;
}
else {
action = new QAction( allDictionaries[ x ]->getIcon(),
QString::fromUtf8( allDictionaries[ x ]->getName().c_str() ),
&menu );
2018-07-07 09:33:15 +00:00
// Force icons in menu on all platforms,
// since without them it will be much harder
// to find things.
action->setIconVisibleInMenu( true );
}
menu.addAction( action );
tableOfContents[ action ] = *i;
break;
}
}
if ( maxDictionaryRefsReached )
break;
}
menu.addSeparator();
if ( !popupView || cfg.pinPopupWindow )
menu.addAction( &inspectAction );
if ( !menu.isEmpty() ) {
connect( this, &ArticleView::closePopupMenu, &menu, &QWidget::close );
QAction * result = menu.exec( webview->mapToGlobal( pos ) );
if ( !result )
return;
if ( result == followLink )
openLink( targetUrl, webview->url(), getCurrentArticle(), contexts );
else if ( result == followLinkExternal )
2021-12-29 12:09:29 +00:00
QDesktopServices::openUrl( targetUrl );
else if ( result == lookupSelection )
showDefinition( text, getGroup( webview->url() ), getCurrentArticle() );
else if ( result == saveBookmark ) {
emit saveBookmarkSignal( text.left( 60 ) );
}
else if ( result == &sendToAnkiAction ) {
// This action is handled by a slot.
return;
}
else if ( result == lookupSelectionGr && currentGroupId )
showDefinition( selectedText, currentGroupId, QString() );
else if ( result == addWordToHistoryAction )
emit forceAddWordToHistory( selectedText );
if ( result == addHeaderToHistoryAction )
emit forceAddWordToHistory( webview->title() );
else if ( result == sendWordToInputLineAction )
emit sendWordToInputLine( selectedText );
else if ( !popupView && result == followLinkNewTab )
emit openLinkInNewTab( targetUrl, webview->url(), getCurrentArticle(), contexts );
else if ( !popupView && result == lookupSelectionNewTab )
emit showDefinitionInNewTab( selectedText, getGroup( webview->url() ), getCurrentArticle(), Contexts() );
else if ( !popupView && result == lookupSelectionNewTabGr && currentGroupId )
emit showDefinitionInNewTab( selectedText, currentGroupId, QString(), Contexts() );
else if ( result == saveImageAction || result == saveSoundAction ) {
QUrl url = ( result == saveImageAction ) ? imageUrl : targetUrl;
QString savePath;
QString fileName;
if ( cfg.resourceSavePath.isEmpty() )
savePath = QDir::homePath();
else {
savePath = QDir::fromNativeSeparators( cfg.resourceSavePath );
if ( !QDir( savePath ).exists() )
savePath = QDir::homePath();
}
QString name = Utils::Url::path( url ).section( '/', -1 );
if ( result == saveSoundAction ) {
// Audio data
// if ( name.indexOf( '.' ) < 0 )
// name += ".wav";
fileName = savePath + "/" + name;
fileName = QFileDialog::getSaveFileName(
parentWidget(),
tr( "Save sound" ),
fileName,
2023-07-07 12:26:40 +00:00
tr(
"Sound files (*.wav *.opus *.ogg *.oga *.mp3 *.mp4 *.aac *.flac *.mid *.wv *.ape *.spx);;All files (*.*)" ) );
}
else {
// Image data
// Check for babylon image name
if ( name[ 0 ] == '\x1E' )
name.remove( 0, 1 );
if ( name.length() && name[ name.length() - 1 ] == '\x1F' )
name.chop( 1 );
fileName = savePath + "/" + name;
fileName = QFileDialog::getSaveFileName( parentWidget(),
tr( "Save image" ),
fileName,
tr( "Image files (*.bmp *.jpg *.png *.tif);;All files (*.*)" ) );
}
if ( !fileName.isEmpty() ) {
QFileInfo fileInfo( fileName );
emit storeResourceSavePath( QDir::toNativeSeparators( fileInfo.absoluteDir().absolutePath() ) );
saveResource( url, webview->url(), fileName );
}
}
else if ( result == openImageAction ) {
QUrl url = imageUrl;
QString fileName;
QString name = Utils::Url::path( url ).section( '/', -1 );
// Image data
// Check for babylon image name
if ( name[ 0 ] == '\x1E' )
name.remove( 0, 1 );
if ( name.length() && name[ name.length() - 1 ] == '\x1F' )
name.chop( 1 );
2023-12-06 03:16:35 +00:00
fileName = QDir::temp().filePath( QString::number( QRandomGenerator::global()->generate() ) + name );
if ( !fileName.isEmpty() ) {
QFileInfo fileInfo( fileName );
auto handler = saveResource( url, webview->url(), fileName );
if ( !handler->isEmpty() ) {
connect( handler, &ResourceToSaveHandler::done, this, [ fileName ]() {
QDesktopServices::openUrl( fileName );
} );
}
else {
QDesktopServices::openUrl( fileName );
}
}
}
else {
if ( !popupView && result == maxDictionaryRefsAction )
emit showDictsPane();
// Match against table of contents
QString id = tableOfContents[ result ];
if ( id.size() )
setCurrentArticle( scrollToFromDictionaryId( id ), true );
}
}
qDebug() << "title = " << r->title();
}
void ArticleView::resourceDownloadFinished()
{
if ( resourceDownloadRequests.empty() )
return; // Stray signal
// Find any finished resources
for ( list< sptr< Dictionary::DataRequest > >::iterator i = resourceDownloadRequests.begin();
i != resourceDownloadRequests.end(); ) {
if ( ( *i )->isFinished() ) {
if ( ( *i )->dataSize() >= 0 ) {
// Ok, got one finished, all others are irrelevant now
vector< char > const & data = ( *i )->getFullData();
if ( resourceDownloadUrl.scheme() == "gdau" || Utils::Url::isWebAudioUrl( resourceDownloadUrl ) ) {
// Audio data
audioPlayer->stop();
connect( audioPlayer.data(),
&AudioPlayerInterface::error,
this,
&ArticleView::audioPlayerError,
Qt::UniqueConnection );
QString errorMessage = audioPlayer->play( data.data(), data.size() );
if ( !errorMessage.isEmpty() )
QMessageBox::critical( this, "GoldenDict", tr( "Failed to play sound file: %1" ).arg( errorMessage ) );
}
else {
// Create a temporary file
// Remove the ones previously used, if any
cleanupTemp();
QString fileName;
{
QTemporaryFile tmp( QDir::temp().filePath( "XXXXXX-" + resourceDownloadUrl.path().section( '/', -1 ) ),
this );
2012-10-31 13:58:35 +00:00
if ( !tmp.open() || (size_t)tmp.write( &data.front(), data.size() ) != data.size() ) {
2013-02-01 12:36:01 +00:00
QMessageBox::critical( this, "GoldenDict", tr( "Failed to create temporary file." ) );
return;
}
tmp.setAutoRemove( false );
desktopOpenedTempFiles.insert( fileName = tmp.fileName() );
}
if ( !QDesktopServices::openUrl( QUrl::fromLocalFile( fileName ) ) )
2013-02-01 12:36:01 +00:00
QMessageBox::critical(
this,
"GoldenDict",
tr( "Failed to auto-open resource file, try opening manually: %1." ).arg( fileName ) );
}
// Ok, whatever it was, it's finished. Remove this and any other
// requests and finish.
resourceDownloadRequests.clear();
return;
}
else {
// This one had no data. Erase it.
resourceDownloadRequests.erase( i++ );
}
}
2017-04-27 20:55:53 +00:00
else // Unfinished, wait.
break;
}
if ( resourceDownloadRequests.empty() ) {
// emit statusBarMessage(
// tr("WARNING: %1").arg(tr("The referenced resource failed to download.")),
// 10000, QPixmap(":/icons/error.svg"));
}
}
void ArticleView::audioPlayerError( QString const & message )
{
emit statusBarMessage( tr( "WARNING: Audio Player: %1" ).arg( message ), 10000, QPixmap( ":/icons/error.svg" ) );
}
void ArticleView::pasteTriggered()
{
QString word = cfg.preferences.sanitizeInputPhrase( QApplication::clipboard()->text() );
if ( !word.isEmpty() ) {
unsigned groupId = getGroup( webview->url() );
if ( groupId == 0 || groupId == Instances::Group::HelpGroupId ) {
// We couldn't figure out the group out of the URL,
// so let's try the currently selected group.
groupId = currentGroupId;
}
showDefinition( word, groupId, getCurrentArticle() );
}
}
unsigned ArticleView::getCurrentGroup()
{
if ( !groupComboBox )
return currentGroupId;
return groupComboBox->getCurrentGroup();
}
void ArticleView::moveOneArticleUp()
{
QString current = getCurrentArticle();
if ( current.size() ) {
QStringList lst = getArticlesList();
int idx = lst.indexOf( dictionaryIdFromScrollTo( current ) );
if ( idx != -1 ) {
--idx;
if ( idx < 0 )
idx = lst.size() - 1;
setCurrentArticle( scrollToFromDictionaryId( lst[ idx ] ), true );
}
}
}
void ArticleView::moveOneArticleDown()
{
QString currentDictId = getActiveArticleId();
QStringList lst = getArticlesList();
// if current article is empty .use the first as default.
if ( currentDictId.isEmpty() && !lst.isEmpty() ) {
currentDictId = lst[ 0 ];
}
int idx = lst.indexOf( currentDictId );
if ( idx != -1 ) {
idx = ( idx + 1 ) % lst.size();
setCurrentArticle( scrollToFromDictionaryId( lst[ idx ] ), true );
}
}
2009-05-16 11:14:43 +00:00
void ArticleView::openSearch()
{
2014-04-16 16:18:28 +00:00
if ( !isVisible() )
return;
if ( ftsSearchIsOpened )
closeSearch();
2009-05-16 11:14:43 +00:00
if ( !searchIsOpened ) {
searchPanel->show();
searchPanel->lineEdit->setText( getTitle() );
2009-05-16 11:14:43 +00:00
searchIsOpened = true;
}
searchPanel->lineEdit->setFocus();
searchPanel->lineEdit->selectAll();
2009-05-16 11:14:43 +00:00
// Clear any current selection
if ( webview->selectedText().size() ) {
webview->page()->runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
2009-05-16 11:14:43 +00:00
}
if ( searchPanel->lineEdit->property( "noResults" ).toBool() ) {
searchPanel->lineEdit->setProperty( "noResults", false );
Utils::Widget::setNoResultColor( searchPanel->lineEdit, false );
2009-05-16 11:14:43 +00:00
}
}
void ArticleView::on_searchPrevious_clicked()
{
if ( searchIsOpened )
performFindOperation( false, true );
2009-05-16 11:14:43 +00:00
}
void ArticleView::on_searchNext_clicked()
{
if ( searchIsOpened )
performFindOperation( false, false );
2009-05-16 11:14:43 +00:00
}
void ArticleView::on_searchText_textEdited()
{
performFindOperation( true, false );
}
void ArticleView::on_searchText_returnPressed()
{
on_searchNext_clicked();
}
void ArticleView::on_searchCloseButton_clicked()
{
closeSearch();
}
void ArticleView::on_searchCaseSensitive_clicked()
{
performFindOperation( true, false );
}
void ArticleView::on_highlightAllButton_clicked()
{
performFindOperation( false, false, true );
}
//the id start with "gdform-"
void ArticleView::onJsActiveArticleChanged( QString const & id )
{
if ( !isScrollTo( id ) )
return; // Incorrect id
2022-02-25 14:48:43 +00:00
QString dictId = dictionaryIdFromScrollTo( id );
setActiveArticleId( dictId );
emit activeArticleChanged( this, dictId );
}
void ArticleView::doubleClicked( QPoint pos )
{
// We might want to initiate translation of the selected word
audioPlayer->stop();
if ( cfg.preferences.doubleClickTranslates ) {
QString selectedText = webview->selectedText();
// ignore empty word;
if ( selectedText.isEmpty() )
return;
emit sendWordToInputLine( selectedText );
// Do some checks to make sure there's a sensible selection indeed
if ( Folding::applyWhitespaceOnly( gd::toWString( selectedText ) ).size() && selectedText.size() < 60 ) {
// Initiate translation
Qt::KeyboardModifiers kmod = QApplication::keyboardModifiers();
if ( kmod & ( Qt::ControlModifier | Qt::ShiftModifier ) ) { // open in new tab
emit showDefinitionInNewTab( selectedText, getGroup( webview->url() ), getCurrentArticle(), Contexts() );
}
else {
QUrl const & ref = webview->url();
if ( Utils::Url::hasQueryItem( ref, "dictionaries" ) ) {
QStringList dictsList = Utils::Url::queryItemValue( ref, "dictionaries" ).split( ",", Qt::SkipEmptyParts );
showDefinition( selectedText, dictsList, QRegExp(), getGroup( ref ), false );
}
else
showDefinition( selectedText, getGroup( ref ), getCurrentArticle() );
}
}
}
}
void ArticleView::performFindOperation( bool restart, bool backwards, bool checkHighlight )
2009-05-16 11:14:43 +00:00
{
QString text = searchPanel->lineEdit->text();
2009-05-16 11:14:43 +00:00
if ( restart || checkHighlight ) {
if ( restart ) {
// Anyone knows how we reset the search position?
// For now we resort to this hack:
if ( webview->selectedText().size() ) {
webview->page()->runJavaScript( "window.getSelection().removeAllRanges();_=0;" );
}
2009-05-16 11:14:43 +00:00
}
2021-07-06 13:01:50 +00:00
QWebEnginePage::FindFlags f( 0 );
if ( searchPanel->caseSensitive->isChecked() )
2021-07-06 13:01:50 +00:00
f |= QWebEnginePage::FindCaseSensitively;
webview->findText( "", f );
if ( searchPanel->highlightAll->isChecked() )
webview->findText( text, f );
if ( checkHighlight )
return;
2009-05-16 11:14:43 +00:00
}
2021-07-06 13:01:50 +00:00
QWebEnginePage::FindFlags f( 0 );
2009-05-16 11:14:43 +00:00
if ( searchPanel->caseSensitive->isChecked() )
2021-07-06 13:01:50 +00:00
f |= QWebEnginePage::FindCaseSensitively;
2009-05-16 11:14:43 +00:00
if ( backwards )
2021-07-06 13:01:50 +00:00
f |= QWebEnginePage::FindBackward;
2009-05-16 11:14:43 +00:00
2022-08-17 18:36:17 +00:00
findText( text, f, [ text, this ]( bool match ) {
bool setMark = !text.isEmpty() && !match;
2009-05-16 11:14:43 +00:00
if ( searchPanel->lineEdit->property( "noResults" ).toBool() != setMark ) {
searchPanel->lineEdit->setProperty( "noResults", setMark );
Utils::Widget::setNoResultColor( searchPanel->lineEdit, setMark );
}
} );
}
void ArticleView::findText( QString & text,
const QWebEnginePage::FindFlags & f,
const std::function< void( bool match ) > & callback )
2021-07-06 13:01:50 +00:00
{
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
webview->findText( text, f, [ callback ]( const QWebEngineFindTextResult & result ) {
auto r = result.numberOfMatches() > 0;
if ( callback )
callback( r );
} );
2022-02-27 05:17:37 +00:00
#else
webview->findText( text, f, [ callback ]( bool result ) {
if ( callback )
callback( result );
} );
2022-02-27 05:17:37 +00:00
#endif
2021-07-06 13:01:50 +00:00
}
2009-05-16 11:14:43 +00:00
bool ArticleView::closeSearch()
{
if ( searchIsOpened ) {
searchPanel->hide();
webview->setFocus();
2009-05-16 11:14:43 +00:00
searchIsOpened = false;
return true;
}
else if ( ftsSearchIsOpened ) {
firstAvailableText.clear();
uniqueMatches.clear();
ftsSearchIsOpened = false;
ftsSearchPanel->hide();
webview->setFocus();
2021-07-06 13:01:50 +00:00
QWebEnginePage::FindFlags flags( 0 );
webview->findText( "", flags );
return true;
}
2009-05-16 11:14:43 +00:00
else
return false;
}
bool ArticleView::isSearchOpened()
{
return searchIsOpened;
}
void ArticleView::showEvent( QShowEvent * ev )
{
QWidget::showEvent( ev );
if ( !searchIsOpened )
searchPanel->hide();
if ( !ftsSearchIsOpened )
ftsSearchPanel->hide();
}
void ArticleView::copyAsText()
{
QString text = webview->selectedText();
if ( !text.isEmpty() )
QApplication::clipboard()->setText( text );
}
void ArticleView::highlightFTSResults()
{
closeSearch();
// Clear any current selection
webview->findText( "" );
QString regString = Utils::Url::queryItemValue( webview->url(), "regexp" );
if ( regString.isEmpty() )
return;
//replace any unicode Number ,Symbol ,Punctuation ,Mark character to whitespace
regString.replace( QRegularExpression( R"([\p{N}\p{S}\p{P}\p{M}])", QRegularExpression::UseUnicodePropertiesOption ),
" " );
if ( regString.trimmed().isEmpty() )
return;
QString script = QString(
"var context = document.querySelector(\"body\");\n"
"var instance = new Mark(context);\n"
2023-12-06 06:29:59 +00:00
"instance.mark(\"%1\",{\"accuracy\": \"exactly\"});" )
.arg( regString );
webview->page()->runJavaScript( script );
2021-07-06 13:01:50 +00:00
}
void ArticleView::setActiveDictIds( const ActiveDictIds & ad )
{
auto groupId = ad.groupId;
if ( groupId == 0 ) {
groupId = Instances::Group::AllGroupId;
}
if ( ( ad.word == currentWord && groupId == getCurrentGroup() ) || historyMode ) {
// ignore all other signals.
qDebug() << "receive dicts, current word:" << currentWord << ad.word << ":" << ad.dictIds;
currentActiveDictIds << ad.dictIds;
currentActiveDictIds.removeDuplicates();
emit updateFoundInDictsList();
}
}
void ArticleView::dictionaryClear( const ActiveDictIds & ad )
{
auto groupId = ad.groupId;
if ( groupId == 0 ) {
groupId = Instances::Group::AllGroupId;
}
// ignore all other signals.
if ( ad.word == currentWord && groupId == getCurrentGroup() ) {
qDebug() << "clear current dictionaries:" << currentWord;
currentActiveDictIds.clear();
}
}
void ArticleView::performFtsFindOperation( bool backwards )
{
if ( !ftsSearchIsOpened )
return;
if ( firstAvailableText.isEmpty() ) {
ftsSearchPanel->statusLabel->setText( searchStatusMessageNoMatches() );
ftsSearchPanel->next->setEnabled( false );
ftsSearchPanel->previous->setEnabled( false );
return;
}
2021-07-06 13:01:50 +00:00
QWebEnginePage::FindFlags flags( 0 );
if ( ftsSearchMatchCase )
2021-07-06 13:01:50 +00:00
flags |= QWebEnginePage::FindCaseSensitively;
// Restore saved highlighted selection
webview->page()->runJavaScript(
QString( "var sel=window.getSelection();sel.removeAllRanges();sel.addRange(%1);_=0;" ).arg( rangeVarName ) );
if ( backwards ) {
2022-02-27 05:17:37 +00:00
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
webview->findText( firstAvailableText,
flags | QWebEnginePage::FindBackward,
[ this ]( const QWebEngineFindTextResult & result ) {
if ( result.numberOfMatches() == 0 )
return;
ftsSearchPanel->previous->setEnabled( true );
if ( !ftsSearchPanel->next->isEnabled() )
ftsSearchPanel->next->setEnabled( true );
ftsSearchPanel->statusLabel->setText(
searchStatusMessage( result.activeMatch(), result.numberOfMatches() ) );
} );
2022-02-27 05:17:37 +00:00
#else
webview->findText( firstAvailableText, flags | QWebEnginePage::FindBackward, [ this ]( bool res ) {
ftsSearchPanel->previous->setEnabled( res );
if ( !ftsSearchPanel->next->isEnabled() )
ftsSearchPanel->next->setEnabled( res );
} );
2022-02-27 05:17:37 +00:00
#endif
}
else {
2022-02-27 05:17:37 +00:00
#if ( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
webview->findText( firstAvailableText, flags, [ this ]( const QWebEngineFindTextResult & result ) {
if ( result.numberOfMatches() == 0 )
return;
ftsSearchPanel->next->setEnabled( true );
if ( !ftsSearchPanel->previous->isEnabled() )
ftsSearchPanel->previous->setEnabled( true );
ftsSearchPanel->statusLabel->setText( searchStatusMessage( result.activeMatch(), result.numberOfMatches() ) );
} );
2022-02-27 05:17:37 +00:00
#else
webview->findText( firstAvailableText, flags, [ this ]( bool res ) {
ftsSearchPanel->next->setEnabled( res );
if ( !ftsSearchPanel->previous->isEnabled() )
ftsSearchPanel->previous->setEnabled( res );
} );
2022-02-27 05:17:37 +00:00
#endif
}
}
void ArticleView::on_ftsSearchPrevious_clicked()
{
performFtsFindOperation( true );
}
void ArticleView::on_ftsSearchNext_clicked()
{
performFtsFindOperation( false );
}
void ArticleView::clearContent()
{
auto html = articleNetMgr.getHtml( ResourceType::BLANK );
webview->setHtml( QString::fromStdString( html ) );
}
ResourceToSaveHandler::ResourceToSaveHandler( ArticleView * view, QString fileName ):
QObject( view ),
fileName( std::move( fileName ) ),
2017-04-27 20:55:53 +00:00
alreadyDone( false )
{
connect( this, &ResourceToSaveHandler::statusBarMessage, view, &ArticleView::statusBarMessage );
2017-04-27 20:55:53 +00:00
}
void ResourceToSaveHandler::addRequest( const sptr< Dictionary::DataRequest > & req )
2017-04-27 20:55:53 +00:00
{
if ( !alreadyDone ) {
2017-04-27 20:55:53 +00:00
downloadRequests.push_back( req );
connect( req.get(), &Dictionary::Request::finished, this, &ResourceToSaveHandler::downloadFinished );
}
}
void ResourceToSaveHandler::downloadFinished()
{
2017-04-27 20:55:53 +00:00
if ( downloadRequests.empty() )
return; // Stray signal
2017-04-27 20:55:53 +00:00
// Find any finished resources
for ( auto i = downloadRequests.begin(); i != downloadRequests.end(); ) {
if ( ( *i )->isFinished() ) {
if ( ( *i )->dataSize() >= 0 && !alreadyDone ) {
2017-04-27 20:55:53 +00:00
QByteArray resourceData;
vector< char > const & data = ( *i )->getFullData();
resourceData = QByteArray( data.data(), data.size() );
2017-04-27 20:55:53 +00:00
// Write data to file
2017-04-27 20:55:53 +00:00
if ( !fileName.isEmpty() ) {
const QFileInfo fileInfo( fileName );
2017-04-27 20:55:53 +00:00
QDir().mkpath( fileInfo.absoluteDir().absolutePath() );
2017-04-27 20:55:53 +00:00
QFile file( fileName );
if ( file.open( QFile::WriteOnly ) ) {
file.write( resourceData.data(), resourceData.size() );
file.close();
}
2017-04-27 20:55:53 +00:00
if ( file.error() ) {
emit statusBarMessage( tr( "ERROR: %1" ).arg( tr( "Resource saving error: " ) + file.errorString() ),
10000,
QPixmap( ":/icons/error.svg" ) );
2017-04-27 20:55:53 +00:00
}
}
alreadyDone = true;
// Clear other requests
downloadRequests.clear();
break;
}
else {
// This one had no data. Erase it.
downloadRequests.erase( i++ );
}
}
2017-04-27 20:55:53 +00:00
else // Unfinished, wait.
break;
}
2017-04-27 20:55:53 +00:00
if ( downloadRequests.empty() ) {
if ( !alreadyDone ) {
emit statusBarMessage( tr( "WARNING: %1" ).arg( tr( "The referenced resource failed to download." ) ),
10000,
QPixmap( ":/icons/error.svg" ) );
}
2017-04-27 20:55:53 +00:00
emit done();
deleteLater();
}
}
ArticleViewAgent::ArticleViewAgent( ArticleView * articleView ):
QObject( articleView ),
articleView( articleView )
{
}
void ArticleViewAgent::onJsActiveArticleChanged( QString const & id )
{
articleView->onJsActiveArticleChanged( id );
}
void ArticleViewAgent::linkClickedInHtml( QUrl const & url )
{
articleView->linkClickedInHtml( url );
}
2023-04-15 11:10:15 +00:00
void ArticleViewAgent::collapseInHtml( QString const & dictId, bool on ) const
{
if ( GlobalBroadcaster::instance()->getPreference()->sessionCollapse ) {
if ( on ) {
GlobalBroadcaster::instance()->collapsedDicts.insert( dictId );
}
else {
GlobalBroadcaster::instance()->collapsedDicts.remove( dictId );
}
}
}