fix:single click on link,the emulated db click will still work on new page

This commit is contained in:
yifang 2022-01-14 08:19:49 +08:00
parent 9a4b119976
commit 850c7d66e8
6 changed files with 45 additions and 13 deletions

View file

@ -1119,6 +1119,10 @@ void ArticleView::linkClicked( QUrl const & url_ )
openLink( url, ui.definition->url(), getCurrentArticle(), contexts ); openLink( url, ui.definition->url(), getCurrentArticle(), contexts );
} }
void ArticleView::linkClickedInHtml( QUrl const & url_ )
{
emit ui.definition->linkClickedInHtml(url_);
}
void ArticleView::openLink( QUrl const & url, QUrl const & ref, void ArticleView::openLink( QUrl const & url, QUrl const & ref,
QString const & scrollTo, QString const & scrollTo,
Contexts const & contexts_ ) Contexts const & contexts_ )

View file

@ -285,7 +285,10 @@ public slots:
/// Selects an entire text of the current article /// Selects an entire text of the current article
void selectCurrentArticle(); void selectCurrentArticle();
//receive signal from weburlinterceptor.
void linkClicked( QUrl const & ); void linkClicked( QUrl const & );
//aim to receive signal from html. the fragment url click to navigation through page wil not be intecepted by weburlinteceptor
Q_INVOKABLE void linkClickedInHtml( QUrl const & );
private slots: private slots:
void loadFinished( bool ok ); void loadFinished( bool ok );

View file

@ -38,27 +38,37 @@ bool ArticleWebView::event( QEvent * event )
if (event->type() == QEvent::ChildAdded) { if (event->type() == QEvent::ChildAdded) {
QChildEvent *child_ev = static_cast<QChildEvent *>(event); QChildEvent *child_ev = static_cast<QChildEvent *>(event);
//should restrict the child event type? // // there is also QObject child that should be ignored here;
// // use only QOpenGLWidget child
// QOpenGLWidget *w = static_cast<QOpenGLWidget*>(child_ev->child());
child_ev->child()->installEventFilter(this); child_ev->child()->installEventFilter(this);
} }
return QWebEngineView::event(event); return QWebEngineView::event(event);
} }
bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) { void ArticleWebView::linkClickedInHtml(QUrl const& ){
//disable single click to simulate dbclick action on the new loaded pages.
singleClickToDbClick=false;
}
bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) {
if (ev->type() == QEvent::MouseButtonDblClick) { if (ev->type() == QEvent::MouseButtonDblClick) {
singleClicked = false; QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
if (Qt::MouseEventSynthesizedByApplication != pe->source()) {
singleClickToDbClick = false;
dbClicked = true; dbClicked = true;
} }
}
if (ev->type()==QEvent::MouseMove) { if (ev->type()==QEvent::MouseMove) {
singleClicked=false; singleClickToDbClick=false;
} }
if (ev->type() == QEvent::MouseButtonPress) { if (ev->type() == QEvent::MouseButtonPress) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev); QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
if(pe->button() == Qt::LeftButton) if(pe->button() == Qt::LeftButton)
{ {
singleClicked = true; singleClickToDbClick = true;
dbClicked = false; dbClicked = false;
QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){ QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){
singleClickAction(pe); singleClickAction(pe);
@ -92,9 +102,9 @@ void ArticleWebView::mousePressEvent(QMouseEvent *event)
midButtonPressed = true; midButtonPressed = true;
} }
void ArticleWebView::singleClickAction(QMouseEvent * event ) void ArticleWebView::singleClickAction(QMouseEvent *event )
{ {
if(!singleClicked) if(!singleClickToDbClick)
return; return;
if (selectionBySingleClick) { if (selectionBySingleClick) {

View file

@ -8,6 +8,8 @@
#include <QEvent> #include <QEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QWebEngineView> #include <QWebEngineView>
#include <QOpenGLWidget>
#include <QPointer>
/// A thin wrapper around QWebEngineView to accommodate to some ArticleView's needs. /// A thin wrapper around QWebEngineView to accommodate to some ArticleView's needs.
/// Currently the only added features: /// Currently the only added features:
@ -45,9 +47,6 @@ public:
/// word, which gets selected by the view in response to double-click. /// word, which gets selected by the view in response to double-click.
void doubleClicked( QPoint pos ); void doubleClicked( QPoint pos );
//the linkClicked signal was removed from webengineview. add the signal to simulate.
void linkClicked(QUrl const& url);
protected: protected:
bool event( QEvent * event ); bool event( QEvent * event );
@ -63,6 +62,7 @@ protected:
private: private:
Config::Class * cfg; Config::Class * cfg;
//QPointer<QOpenGLWidget> child_;
bool midButtonPressed; bool midButtonPressed;
bool selectionBySingleClick; bool selectionBySingleClick;
@ -70,8 +70,13 @@ private:
//MouseDbClickEvent will also emit MousePressEvent which conflict the single click event. //MouseDbClickEvent will also emit MousePressEvent which conflict the single click event.
//this variable used to distinguish the single click and real double click. //this variable used to distinguish the single click and real double click.
bool singleClicked; bool singleClickToDbClick;
bool dbClicked; bool dbClicked;
public slots:
//receive signal ,a link has been clicked.
void linkClickedInHtml(QUrl const& url);
}; };
#endif #endif

View file

@ -3,6 +3,7 @@
$(function() { $(function() {
$("a").click(function(event) { $("a").click(function(event) {
var link = $(this).attr("href"); var link = $(this).attr("href");
emitClickedEvent(link);
if(link.indexOf(":")>=0){ if(link.indexOf(":")>=0){
return; return;
} }
@ -41,3 +42,13 @@ function playSound(sound) {
var a = new Audio(sound); var a = new Audio(sound);
a.play(); a.play();
} }
function emitClickedEvent(link){
try{
articleview.linkClickedInHtml(link);
}catch(error)
{
console.log(error);
}
}

View file

@ -10,7 +10,6 @@ WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) { void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) {
if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {
emit linkClicked(info.requestUrl()); emit linkClicked(info.requestUrl());
info.block(true); info.block(true);
} }
} }