From 5333b79222a2edda8121a94e5314fe0ec8b3aeb0 Mon Sep 17 00:00:00 2001 From: yifang Date: Sat, 11 Dec 2021 10:21:31 +0800 Subject: [PATCH] double click and single click selection --- articlewebview.cc | 55 ++++++++++++++++++++++++++--------------------- articlewebview.hh | 1 + 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/articlewebview.cc b/articlewebview.cc index 63a54267..3848494e 100644 --- a/articlewebview.cc +++ b/articlewebview.cc @@ -53,16 +53,17 @@ bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) } if (ev->type() == QEvent::MouseButtonPress) { firstClicked=true; + QMouseEvent *pe = static_cast(ev); + mousePressEvent(pe); } if (ev->type() == QEvent::MouseButtonRelease) { QMouseEvent *pe = static_cast(ev); mouseReleaseEvent(pe); - if(firstClicked){ + if (firstClicked) { QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){ singleClickAction(pe); }); - } - else{ + } else { doubleClickAction(pe); } } @@ -80,33 +81,37 @@ bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) return QWebEngineView::eventFilter(obj, ev); } +void ArticleWebView::mousePressEvent(QMouseEvent *event) +{ + if (event->buttons() & Qt::MidButton) + midButtonPressed = true; + + //QWebEngineView::mousePressEvent(event); +} + void ArticleWebView::singleClickAction( QMouseEvent * event ) { if(!firstClicked) return; - if ( event->buttons() & Qt::MidButton ) - midButtonPressed = true; - //QWebEngineView::mousePressEvent(event); - - 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 ); + if (selectionBySingleClick && (event->buttons() & Qt::LeftButton)) { + // findText(""); // clear the selection first, if any + page()->runJavaScript(QString( + " var s = window.getSelection(); " + " if(s.rangeCount>0){ " + " 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);" + " }")); } } diff --git a/articlewebview.hh b/articlewebview.hh index 9fffae31..84ae8a99 100644 --- a/articlewebview.hh +++ b/articlewebview.hh @@ -49,6 +49,7 @@ protected: bool event( QEvent * event ); void singleClickAction( QMouseEvent * event ); + void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent( QMouseEvent * event ); void doubleClickAction( QMouseEvent * event ); void focusInEvent( QFocusEvent * event );