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,16 +53,17 @@ 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);
mouseReleaseEvent(pe); mouseReleaseEvent(pe);
if(firstClicked){ if (firstClicked) {
QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){ QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){
singleClickAction(pe); singleClickAction(pe);
}); });
} } else {
else{
doubleClickAction(pe); doubleClickAction(pe);
} }
} }
@ -80,33 +81,37 @@ bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev)
return QWebEngineView::eventFilter(obj, 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 ) void ArticleWebView::singleClickAction( QMouseEvent * event )
{ {
if(!firstClicked) if(!firstClicked)
return; return;
if ( event->buttons() & Qt::MidButton )
midButtonPressed = true;
//QWebEngineView::mousePressEvent(event); if (selectionBySingleClick && (event->buttons() & Qt::LeftButton)) {
// findText(""); // clear the selection first, if any
if (selectionBySingleClick && (event->button() & Qt::LeftButton)) { page()->runJavaScript(QString(
findText(""); // clear the selection first, if any " var s = window.getSelection(); "
page()->runJavaScript(QString("" " if(s.rangeCount>0){ "
" var s = window.getSelection(); " " 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) { " " range.setStart(node, (range.startOffset - 1)); "
" range.setStart(node, (range.startOffset - 1)); " " } "
" } " " range.setStart(node, range.startOffset + 1); "
" range.setStart(node, range.startOffset + 1); " " do { "
" do { " " range.setEnd(node, range.endOffset+1); "
" range.setEnd(node, range.endOffset + 1); " " } "
" } " " 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 );