Merge pull request #85 from xiaoyifang/feature/bookmark

a workround to achieve bookmark feature
This commit is contained in:
xiaoyifang 2022-06-02 21:11:52 +08:00 committed by GitHub
commit 3ec948a13c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 105 additions and 40 deletions

View file

@ -198,9 +198,6 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co
QUrl refererUrl = QUrl::fromEncoded( referer );
//GD_DPRINTF( "Considering %s vs %s\n", getHostBase( req.url() ).toUtf8().data(),
// getHostBase( refererUrl ).toUtf8().data() );
if ( !url.host().endsWith( refererUrl.host() ) &&
getHostBaseFromUrl( url ) != getHostBaseFromUrl( refererUrl ) && !url.scheme().startsWith("data") )
{
@ -449,7 +446,7 @@ qint64 ArticleResourceReply::readData( char * out, qint64 maxSize )
qint64 left = avail - alreadyRead;
qint64 toRead = maxSize < left ? maxSize : left;
GD_DPRINTF( "====reading %d bytes\n", (int)toRead );
GD_DPRINTF( "====reading %d bytes", (int)toRead );
try
{
@ -457,7 +454,7 @@ qint64 ArticleResourceReply::readData( char * out, qint64 maxSize )
}
catch( std::exception & e )
{
qWarning( "getDataSlice error: %s\n", e.what() );
qWarning( "getDataSlice error: %s", e.what() );
}
alreadyRead += toRead;

View file

@ -402,6 +402,9 @@ void ArticleView::showDefinition( Config::InputPhrase const & phrase, unsigned g
if ( scrollTo.size() )
Utils::Url::addQueryItem( req, "scrollto", scrollTo );
if(delayedHighlightText.size())
Utils::Url::addQueryItem( req, "regexp", delayedHighlightText );
Contexts::Iterator pos = contexts.find( "gdanchor" );
if( pos != contexts.end() )
{
@ -579,6 +582,12 @@ void ArticleView::loadFinished( bool result )
}
if( Utils::Url::hasQueryItem( ui.definition->url(), "regexp" ) )
highlightFTSResults();
if( !delayedHighlightText.isEmpty() )
{
// findText( delayedHighlightText, QWebEnginePage::FindCaseSensitively ,[](bool){});
delayedHighlightText.clear();
}
}
void ArticleView::loadProgress(int ){
@ -1592,6 +1601,11 @@ void ArticleView::setSelectionBySingleClick( bool set )
ui.definition->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
@ -1712,6 +1726,7 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
QAction * sendWordToInputLineAction = 0;
QAction * saveImageAction = 0;
QAction * saveSoundAction = 0;
QAction * saveBookmark = 0;
#if( QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 ) )
const QWebEngineContextMenuData * menuData = &(r->contextMenuData());
@ -1832,6 +1847,13 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
}
}
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( !text.isEmpty() && cfg.preferences.ankiConnectServer.enabled )
{
@ -1931,7 +1953,11 @@ void ArticleView::contextMenuRequested( QPoint const & pos )
QDesktopServices::openUrl( targetUrl );
else
if ( result == lookupSelection )
showDefinition( selectedText, getGroup( ui.definition->url() ), getCurrentArticle() );
showDefinition( text, getGroup( ui.definition->url() ), getCurrentArticle() );
else if( result == saveBookmark )
{
emit saveBookmarkSignal( text.left( 60 ) );
}
else if( result == sendToAnkiAction )
{
sendToAnki( ui.definition->title(), ui.definition->selectedText() );
@ -2333,42 +2359,44 @@ void ArticleView::performFindOperation( bool restart, bool backwards, bool check
if ( backwards )
f |= QWebEnginePage::FindBackward;
bool setMark = text.size() && !findText(text, f);
findText( text,
f,
[ &text, this ]( bool match )
{
bool setMark = !text.isEmpty() && !match;
if ( ui.searchText->property( "noResults" ).toBool() != setMark )
if( ui.searchText->property( "noResults" ).toBool() != setMark )
{
ui.searchText->setProperty( "noResults", setMark );
// Reload stylesheet
reloadStyleSheet();
}
} );
}
bool ArticleView::findText(QString& text, const QWebEnginePage::FindFlags& f)
void ArticleView::findText( QString & text,
const QWebEnginePage::FindFlags & f,
const std::function< void( bool match ) > & callback )
{
bool r;
// turn async to sync invoke.
QSharedPointer<QEventLoop> loop = QSharedPointer<QEventLoop>(new QEventLoop());
QTimer::singleShot(1000, loop.data(), &QEventLoop::quit);
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
ui.definition->findText(text, f, [&](const QWebEngineFindTextResult& result)
#if( QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 ) )
ui.definition->findText( text,
f,
[ callback ]( const QWebEngineFindTextResult & result )
{
if(loop->isRunning()){
r = result.numberOfMatches()>0;
loop->quit();
} });
auto r = result.numberOfMatches() > 0;
if( callback )
callback( r );
} );
#else
ui.definition->findText(text, f, [&](bool result)
ui.definition->findText( text,
f,
[ callback ]( bool result )
{
if(loop->isRunning()){
r = result;
loop->quit();
} });
if( callback )
callback( result );
} );
#endif
loop->exec();
return r;
}
void ArticleView::reloadStyleSheet()

View file

@ -79,6 +79,8 @@ class ArticleView: public QFrame
bool ftsSearchIsOpened, ftsSearchMatchCase;
int ftsPosition;
QString delayedHighlightText;
void highlightFTSResults();
void highlightAllFtsOccurences( QWebEnginePage::FindFlags flags );
void performFtsFindOperation( bool backwards );
@ -157,6 +159,8 @@ public:
/// Called when preference changes
void setSelectionBySingleClick( bool set );
void setDelayedHighlightText(QString const & text);
public slots:
/// Goes back in history
@ -227,6 +231,10 @@ public:
ResourceToSaveHandler * saveResource( const QUrl & url, const QString & fileName );
ResourceToSaveHandler * saveResource( const QUrl & url, const QUrl & ref, const QString & fileName );
void findText( QString & text,
const QWebEnginePage::FindFlags & f,
const std::function< void( bool match ) > & callback = nullptr );
signals:
void iconChanged( ArticleView *, QIcon const & icon );
@ -285,6 +293,8 @@ signals:
void inspectSignal(QWebEngineView * view);
void saveBookmarkSignal( const QString & bookmark );
public slots:
void on_searchPrevious_clicked();
@ -391,7 +401,6 @@ private:
void performFindOperation( bool restart, bool backwards, bool checkHighlight = false );
bool findText(QString& text, const QWebEnginePage::FindFlags& f);
void reloadStyleSheet();

View file

@ -64,7 +64,7 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )
if ( !ZipFile::readLocalHeader( zip, header ) )
{
GD_DPRINTF( "Failed to load header\n" );
GD_DPRINTF( "Failed to load header" );
return false;
}
@ -73,13 +73,13 @@ bool IndexedZip::loadFile( uint32_t offset, vector< char > & data )
switch( header.compressionMethod )
{
case ZipFile::Uncompressed:
GD_DPRINTF( "Uncompressed\n" );
GD_DPRINTF( "Uncompressed" );
data.resize( header.uncompressedSize );
return (size_t) zip.read( &data.front(), data.size() ) == data.size();
case ZipFile::Deflated:
{
GD_DPRINTF( "Deflated\n" );
GD_DPRINTF( "Deflated" );
// Now do the deflation

View file

@ -329,7 +329,12 @@
<translation></translation>
</message>
<message>
<location filename="../articleview.cc" line="1867"/>
<location filename="../articleview.cc" line="1853"/>
<source>Save &amp;Bookmark &quot;%1...&quot;</source>
<translation>&amp;S%1...</translation>
</message>
<message>
<location filename="../articleview.cc" line="1861"/>
<source>&amp;Send &quot;%1&quot; to anki with selected text.</source>
<translation>%1anki并附带选择的文本</translation>
</message>

View file

@ -1690,6 +1690,7 @@ ArticleView * MainWindow::createNewTab( bool switchToIt,
connect( view, SIGNAL( zoomIn()), this, SLOT( zoomin() ) );
connect( view, SIGNAL( zoomOut()), this, SLOT( zoomout() ) );
connect( view, &ArticleView::saveBookmarkSignal, this, &MainWindow::addBookmarkToFavorite );
view->setSelectionBySingleClick( cfg.preferences.selectWordBySingleClick );
@ -4656,6 +4657,15 @@ void MainWindow::addWordToFavorites( QString const & word, unsigned groupId )
ui.favoritesPaneWidget->addHeadword( folder, word );
}
void MainWindow::addBookmarkToFavorite( QString const & text )
{
// get current tab word.
QString word = unescapeTabHeader( ui.tabWidget->tabText( ui.tabWidget->currentIndex() ) );
const auto bookmark = QString( "%1~~~%2" ).arg( word, text );
ui.favoritesPaneWidget->addHeadword( nullptr, bookmark );
}
void MainWindow::addAllTabsToFavorites()
{
QString folder;
@ -4728,8 +4738,22 @@ void MainWindow::headwordFromFavorites( QString const & headword,
}
// Show headword without lost of focus on Favorites tree
setTranslateBoxTextAndClearSuffix( headword, EscapeWildcards, DisablePopup );
showTranslationFor(headword );
// bookmark cases: the favorite item may like this "word~~~selectedtext"
auto words = headword.split( "~~~" );
setTranslateBoxTextAndClearSuffix( words[0], EscapeWildcards, DisablePopup );
//must be a bookmark.
if(words.size()>1)
{
auto view = getCurrentArticleView();
if(view)
{
view->setDelayedHighlightText(words[1]);// findText( words[ 1 ], QWebEnginePage::FindCaseSensitively );
}
}
showTranslationFor( words[ 0 ] );
}
#ifdef Q_OS_WIN32

View file

@ -462,6 +462,8 @@ private slots:
void addWordToFavorites( QString const & word, unsigned groupId );
void addBookmarkToFavorite( QString const & text );
bool isWordPresentedInFavorites( QString const & word, unsigned groupId );
void sendWordToInputLine( QString const & word );