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.
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;
//QEventLoop loop;

View file

@ -5,6 +5,7 @@
#include <QMouseEvent>
#include <QWebEngineView>
#include <QApplication>
#include <QTimer>
#ifdef Q_OS_WIN32
#include <qt_windows.h>
@ -47,44 +48,64 @@ bool ArticleWebView::event( QEvent * event )
bool ArticleWebView::eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() == QEvent::MouseButtonDblClick) {
//QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
firstClicked=false;
}
if (ev->type() == QEvent::MouseButtonPress) {
firstClicked=true;
}
if (ev->type() == QEvent::MouseButtonRelease) {
QMouseEvent *pe = static_cast<QMouseEvent *>(ev);
mouseDoubleClickEvent(pe);
mouseReleaseEvent(pe);
if(firstClicked){
QTimer::singleShot(QApplication::doubleClickInterval(),this,[=](){
singleClickAction(pe);
});
return true;
}
// 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;
// }
else{
doubleClickAction(pe);
}
}
if (ev->type() == QEvent::Wheel) {
QWheelEvent *pe = static_cast<QWheelEvent *>(ev);
wheelEvent(pe);
return true;
//return true;
}
if (ev->type() == QEvent::FocusIn) {
QFocusEvent *pe = static_cast<QFocusEvent *>(ev);
focusInEvent(pe);
return true;
//return true;
}
return QWebEngineView::eventFilter(obj, ev);
}
void ArticleWebView::mousePressEvent( QMouseEvent * event )
void ArticleWebView::singleClickAction( QMouseEvent * event )
{
if(!firstClicked)
return;
if ( event->buttons() & Qt::MidButton )
midButtonPressed = true;
QWebEngineView::mousePressEvent(event);
//QWebEngineView::mousePressEvent(event);
qDebug() << event->buttons();
if (selectionBySingleClick && (event->buttons() & Qt::LeftButton)) {
// findText(""); // clear the selection first, if any
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 );
}
@ -94,16 +115,15 @@ void ArticleWebView::mouseReleaseEvent( QMouseEvent * event )
{
bool noMidButton = !( event->buttons() & Qt::MidButton );
QWebEngineView::mouseReleaseEvent( event );
//QWebEngineView::mouseReleaseEvent( event );
if ( midButtonPressed & noMidButton )
midButtonPressed = false;
}
void ArticleWebView::mouseDoubleClickEvent( QMouseEvent * event )
void ArticleWebView::doubleClickAction( QMouseEvent * event )
{
//QWebEngineView::mouseDoubleClickEvent( event );
//todo
int scrollBarWidth = 0;
int scrollBarHeight = 0;

View file

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