double click and single click selection

This commit is contained in:
yifang 2021-12-11 10:21:31 +08:00
parent 8f4f3a3766
commit 5333b79222
2 changed files with 31 additions and 25 deletions

View file

@ -53,6 +53,8 @@ bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev)
} }
if (ev->type() == QEvent::MouseButtonPress) { if (ev->type() == QEvent::MouseButtonPress) {
firstClicked=true; firstClicked=true;
QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
mousePressEvent(pe);
} }
if (ev->type() == QEvent::MouseButtonRelease) { if (ev->type() == QEvent::MouseButtonRelease) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev); QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
@ -61,8 +63,7 @@ bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev)
QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){ QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){
singleClickAction(pe); singleClickAction(pe);
}); });
} } else {
else{
doubleClickAction(pe); doubleClickAction(pe);
} }
} }
@ -80,19 +81,24 @@ bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev)
return QWebEngineView::eventFilter(obj, ev); return QWebEngineView::eventFilter(obj, ev);
} }
void ArticleWebView::singleClickAction( QMouseEvent * event ) void ArticleWebView::mousePressEvent(QMouseEvent *event)
{ {
if(!firstClicked)
return;
if (event->buttons() & Qt::MidButton) if (event->buttons() & Qt::MidButton)
midButtonPressed = true; midButtonPressed = true;
//QWebEngineView::mousePressEvent(event); //QWebEngineView::mousePressEvent(event);
}
if (selectionBySingleClick && (event->button() & Qt::LeftButton)) { void ArticleWebView::singleClickAction( QMouseEvent * event )
findText(""); // clear the selection first, if any {
page()->runJavaScript(QString("" if(!firstClicked)
return;
if (selectionBySingleClick && (event->buttons() & Qt::LeftButton)) {
// findText(""); // clear the selection first, if any
page()->runJavaScript(QString(
" var s = window.getSelection(); " " var s = window.getSelection(); "
" if(s.rangeCount>0){ "
" var range = s.getRangeAt(0); " " var range = s.getRangeAt(0); "
" var node = s.anchorNode; " " var node = s.anchorNode; "
" while (range.toString().indexOf(' ') != 0) { " " while (range.toString().indexOf(' ') != 0) { "
@ -104,9 +110,8 @@ void ArticleWebView::singleClickAction( QMouseEvent * event )
" } " " } "
" while (range.toString().indexOf(' ') == -1 && range.toString().trim() != ''); " " while (range.toString().indexOf(' ') == -1 && range.toString().trim() != ''); "
" var str = range.toString().trim(); " " var str = range.toString().trim(); "
" console.log(str);")); " console.log(str);"
// QMouseEvent ev( QEvent::MouseButtonDblClick, event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers() ); " }"));
// QApplication::sendEvent(page(), &ev );
} }
} }

View file

@ -49,6 +49,7 @@ protected:
bool event( QEvent * event ); bool event( QEvent * event );
void singleClickAction( QMouseEvent * event ); void singleClickAction( QMouseEvent * event );
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent( QMouseEvent * event ); void mouseReleaseEvent( QMouseEvent * event );
void doubleClickAction( QMouseEvent * event ); void doubleClickAction( QMouseEvent * event );
void focusInEvent( QFocusEvent * event ); void focusInEvent( QFocusEvent * event );