single click distinguish with double click

This commit is contained in:
yifang.xiao 2021-12-09 16:02:29 +08:00 committed by yifang
parent 7e62a0f860
commit 608f39244e
3 changed files with 50 additions and 28 deletions

View file

@ -178,7 +178,7 @@ void ArticleView::emitJavascriptFinished(){
//a better solution would be to replace it with callback etc. //a better solution would be to replace it with callback etc.
QString ArticleView::runJavaScriptSync(QWebEnginePage* frame, const QString& variable) 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; QString result;
//QEventLoop loop; //QEventLoop loop;

View file

@ -5,6 +5,7 @@
#include <QMouseEvent> #include <QMouseEvent>
#include <QWebEngineView> #include <QWebEngineView>
#include <QApplication> #include <QApplication>
#include <QTimer>
#ifdef Q_OS_WIN32 #ifdef Q_OS_WIN32
#include <qt_windows.h> #include <qt_windows.h>
@ -47,46 +48,66 @@ bool ArticleWebView::event( QEvent * event )
bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev) bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev)
{ {
if (ev->type() == QEvent::MouseButtonDblClick) { if (ev->type() == QEvent::MouseButtonDblClick) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev); //QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
mouseDoubleClickEvent(pe); firstClicked=false;
return true; }
if (ev->type() == QEvent::MouseButtonPress) {
firstClicked=true;
}
if (ev->type() == QEvent::MouseButtonRelease) {
QMouseEvent *pe = static_cast<QMouseEvent *>(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<QMouseEvent *>(ev);
// mousePressEvent(pe);
// return true;
// }
// if (ev->type() == QEvent::MouseButtonRelease) {
// QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
// mouseReleaseEvent(pe);
// return true;
// }
if (ev->type() == QEvent::Wheel) { if (ev->type() == QEvent::Wheel) {
QWheelEvent *pe = static_cast<QWheelEvent *>(ev); QWheelEvent *pe = static_cast<QWheelEvent *>(ev);
wheelEvent(pe); wheelEvent(pe);
return true; //return true;
} }
if (ev->type() == QEvent::FocusIn) { if (ev->type() == QEvent::FocusIn) {
QFocusEvent *pe = static_cast<QFocusEvent *>(ev); QFocusEvent *pe = static_cast<QFocusEvent *>(ev);
focusInEvent(pe); focusInEvent(pe);
return true; //return true;
} }
return QWebEngineView::eventFilter(obj, ev); return QWebEngineView::eventFilter(obj, ev);
} }
void ArticleWebView::mousePressEvent( QMouseEvent * event ) void ArticleWebView::singleClickAction( 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);
qDebug() << event->buttons(); if (selectionBySingleClick && (event->button() & Qt::LeftButton)) {
if (selectionBySingleClick && (event->buttons() & Qt::LeftButton)) { findText(""); // clear the selection first, if any
// findText(""); // clear the selection first, if any page()->runJavaScript(QString(""
// QMouseEvent ev( QEvent::MouseButtonDblClick, event->pos(), Qt::LeftButton, Qt::LeftButton, event->modifiers() ); " var s = window.getSelection(); "
// QApplication::sendEvent( page(), &ev ); " 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 ); bool noMidButton = !( event->buttons() & Qt::MidButton );
QWebEngineView::mouseReleaseEvent( event ); //QWebEngineView::mouseReleaseEvent( event );
if ( midButtonPressed & noMidButton ) if ( midButtonPressed & noMidButton )
midButtonPressed = false; midButtonPressed = false;
} }
void ArticleWebView::mouseDoubleClickEvent( QMouseEvent * event ) void ArticleWebView::doubleClickAction( QMouseEvent * event )
{ {
//QWebEngineView::mouseDoubleClickEvent( event ); //QWebEngineView::mouseDoubleClickEvent( event );
//todo
int scrollBarWidth = 0; int scrollBarWidth = 0;
int scrollBarHeight = 0; int scrollBarHeight = 0;

View file

@ -48,9 +48,9 @@ public:
protected: protected:
bool event( QEvent * event ); bool event( QEvent * event );
void mousePressEvent( QMouseEvent * event ); void singleClickAction( QMouseEvent * event );
void mouseReleaseEvent( QMouseEvent * event ); void mouseReleaseEvent( QMouseEvent * event );
void mouseDoubleClickEvent( QMouseEvent * event ); void doubleClickAction( QMouseEvent * event );
void focusInEvent( QFocusEvent * event ); void focusInEvent( QFocusEvent * event );
void wheelEvent( QWheelEvent * event ); void wheelEvent( QWheelEvent * event );
@ -61,6 +61,8 @@ private:
bool midButtonPressed; bool midButtonPressed;
bool selectionBySingleClick; bool selectionBySingleClick;
bool showInspectorDirectly; bool showInspectorDirectly;
bool firstClicked;
}; };
#endif #endif