From 608f39244e17b2ae927c3311da8117de1923a0e3 Mon Sep 17 00:00:00 2001 From: "yifang.xiao" Date: Thu, 9 Dec 2021 16:02:29 +0800 Subject: [PATCH] single click distinguish with double click --- articleview.cc | 2 +- articlewebview.cc | 70 ++++++++++++++++++++++++++++++----------------- articlewebview.hh | 6 ++-- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/articleview.cc b/articleview.cc index 3a6611f6..4efbf78d 100644 --- a/articleview.cc +++ b/articleview.cc @@ -178,7 +178,7 @@ void ArticleView::emitJavascriptFinished(){ //a better solution would be to replace it with callback etc. QString ArticleView::runJavaScriptSync(QWebEnginePage* frame, const QString& variable) { - qDebug(QString("runJavascriptScriptSync with :%1").arg(variable).toLatin1().data()); + qDebug(QString("runJavascriptScriptSync:%1").arg(variable).toLatin1().data()); QString result; //QEventLoop loop; diff --git a/articlewebview.cc b/articlewebview.cc index 8e80057a..c3b9b0b5 100644 --- a/articlewebview.cc +++ b/articlewebview.cc @@ -5,6 +5,7 @@ #include #include #include +#include #ifdef Q_OS_WIN32 #include @@ -47,46 +48,66 @@ bool ArticleWebView::event( QEvent * event ) bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) { if (ev->type() == QEvent::MouseButtonDblClick) { - QMouseEvent *pe = static_cast(ev); - mouseDoubleClickEvent(pe); - return true; + //QMouseEvent *pe = static_cast(ev); + firstClicked=false; + } + if (ev->type() == QEvent::MouseButtonPress) { + firstClicked=true; + } + if (ev->type() == QEvent::MouseButtonRelease) { + QMouseEvent *pe = static_cast(ev); + mouseReleaseEvent(pe); + if(firstClicked){ + QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){ + singleClickAction(pe); + }); + return true; + } + else{ + doubleClickAction(pe); + } } - // if (ev->type() == QEvent::MouseButtonPress) { - // QMouseEvent *pe = static_cast(ev); - // mousePressEvent(pe); - // return true; - // } - // if (ev->type() == QEvent::MouseButtonRelease) { - // QMouseEvent *pe = static_cast(ev); - // mouseReleaseEvent(pe); - // return true; - // } if (ev->type() == QEvent::Wheel) { QWheelEvent *pe = static_cast(ev); wheelEvent(pe); - return true; + //return true; } if (ev->type() == QEvent::FocusIn) { QFocusEvent *pe = static_cast(ev); focusInEvent(pe); - return true; + //return true; } return QWebEngineView::eventFilter(obj, ev); } -void ArticleWebView::mousePressEvent( QMouseEvent * event ) +void ArticleWebView::singleClickAction( QMouseEvent * event ) { + if(!firstClicked) + return; if ( event->buttons() & Qt::MidButton ) midButtonPressed = true; - QWebEngineView::mousePressEvent(event); + //QWebEngineView::mousePressEvent(event); - qDebug() << event->buttons(); - if (selectionBySingleClick && (event->buttons() & Qt::LeftButton)) { - // findText(""); // clear the selection first, if any - // QMouseEvent ev( QEvent::MouseButtonDblClick, event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers() ); - // QApplication::sendEvent( page(), &ev ); + if (selectionBySingleClick && (event->button() & Qt::LeftButton)) { + findText(""); // clear the selection first, if any + page()->runJavaScript(QString("" +" var s = window.getSelection(); " +" var range = s.getRangeAt(0); " +" var node = s.anchorNode; " +" while (range.toString().indexOf(' ') != 0) { " +" range.setStart(node, (range.startOffset - 1)); " +" } " +" range.setStart(node, range.startOffset + 1); " +" do { " +" range.setEnd(node, range.endOffset + 1); " +" } " +" while (range.toString().indexOf(' ') == -1 && range.toString().trim() != ''); " +" var str = range.toString().trim(); " +" console.log(str);")); +// QMouseEvent ev( QEvent::MouseButtonDblClick, event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers() ); +// QApplication::sendEvent(page(), &ev ); } } @@ -94,16 +115,15 @@ void ArticleWebView::mouseReleaseEvent( QMouseEvent * event ) { bool noMidButton = !( event->buttons() & Qt::MidButton ); - QWebEngineView::mouseReleaseEvent( event ); + //QWebEngineView::mouseReleaseEvent( event ); if ( midButtonPressed & noMidButton ) midButtonPressed = false; } -void ArticleWebView::mouseDoubleClickEvent( QMouseEvent * event ) +void ArticleWebView::doubleClickAction( QMouseEvent * event ) { //QWebEngineView::mouseDoubleClickEvent( event ); - //todo int scrollBarWidth = 0; int scrollBarHeight = 0; diff --git a/articlewebview.hh b/articlewebview.hh index f9c50d76..9fffae31 100644 --- a/articlewebview.hh +++ b/articlewebview.hh @@ -48,9 +48,9 @@ public: protected: bool event( QEvent * event ); - void mousePressEvent( QMouseEvent * event ); + void singleClickAction( QMouseEvent * event ); void mouseReleaseEvent( QMouseEvent * event ); - void mouseDoubleClickEvent( QMouseEvent * event ); + void doubleClickAction( QMouseEvent * event ); void focusInEvent( QFocusEvent * event ); void wheelEvent( QWheelEvent * event ); @@ -61,6 +61,8 @@ private: bool midButtonPressed; bool selectionBySingleClick; bool showInspectorDirectly; + + bool firstClicked; }; #endif