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) {
firstClicked=true;
QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
mousePressEvent(pe);
}
if (ev->type() == QEvent::MouseButtonRelease) {
QMouseEvent *pe = static_cast<QMouseEvent *>(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);"
" }"));
}
}

View file

@ -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 );