Skip to content

Commit

Permalink
Merge pull request #85 from xiaoyifang/feature/bookmark
Browse files Browse the repository at this point in the history
 a workround to achieve bookmark feature
  • Loading branch information
xiaoyifang authored Jun 2, 2022
2 parents 86ca8f3 + 235bd8d commit 3ec948a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 40 deletions.
7 changes: 2 additions & 5 deletions article_netmgr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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") )
{
Expand Down Expand Up @@ -449,15 +446,15 @@ 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
{
req->getDataSlice( alreadyRead, toRead, out );
}
catch( std::exception & e )
{
qWarning( "getDataSlice error: %s\n", e.what() );
qWarning( "getDataSlice error: %s", e.what() );
}

alreadyRead += toRead;
Expand Down
84 changes: 56 additions & 28 deletions articleview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
{
Expand Down Expand Up @@ -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 ){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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 )
{
ui.searchText->setProperty( "noResults", setMark );
if( ui.searchText->property( "noResults" ).toBool() != setMark )
{
ui.searchText->setProperty( "noResults", setMark );

// Reload stylesheet
reloadStyleSheet();
}
// 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()
Expand Down
11 changes: 10 additions & 1 deletion articleview.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -157,6 +159,8 @@ public:
/// Called when preference changes
void setSelectionBySingleClick( bool set );

void setDelayedHighlightText(QString const & text);

public slots:

/// Goes back in history
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -285,6 +293,8 @@ signals:

void inspectSignal(QWebEngineView * view);

void saveBookmarkSignal( const QString & bookmark );

public slots:

void on_searchPrevious_clicked();
Expand Down Expand Up @@ -391,7 +401,6 @@ private:

void performFindOperation( bool restart, bool backwards, bool checkHighlight = false );

bool findText(QString& text, const QWebEnginePage::FindFlags& f);

void reloadStyleSheet();

Expand Down
6 changes: 3 additions & 3 deletions indexedzip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion locale/zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>将“%1”发送到anki并附带选择的文本。</translation>
</message>
Expand Down
28 changes: 26 additions & 2 deletions mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 3ec948a

Please sign in to comment.