From 50597c89ddd87b48e7d9b80870ff631caf3d1e41 Mon Sep 17 00:00:00 2001 From: liuminghang Date: Sun, 26 Sep 2021 14:59:55 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dbug85300?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 重写滑动事件 Log: 重写滑动事件 Bug: https://pms.uniontech.com/zentao/bug-view-85300.html Change-Id: I9a165f99ab15ac14ced2c62a03eb1f90ccac1182 --- src/mainwidget.cpp | 10 +-- src/resulttextview.cpp | 162 ++++++++++++++++++++++++++++++++++++++++- src/resulttextview.h | 44 ++++++++++- 3 files changed, 206 insertions(+), 10 deletions(-) diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index 29f6d93..3be28c8 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -402,8 +402,8 @@ void MainWidget::loadHtml(const QString &html) if (!html.isEmpty()) { m_frameStackLayout->setContentsMargins(20, 0, 5, 0); m_resultWidget->setCurrentWidget(m_plainTextEdit); -// m_plainTextEdit->appendHtml(html); - m_plainTextEdit->setHtml(html); + m_plainTextEdit->appendHtml(html); +// m_plainTextEdit->setHtml(html); } else { resultEmpty(); } @@ -414,8 +414,8 @@ void MainWidget::loadString(const QString &string) if (!string.isEmpty()) { m_frameStackLayout->setContentsMargins(20, 0, 5, 0); m_resultWidget->setCurrentWidget(m_plainTextEdit); -// m_plainTextEdit->appendPlainText(string); - m_plainTextEdit->setText(string); + m_plainTextEdit->appendPlainText(string); +// m_plainTextEdit->setText(string); //读取完了显示在最上方 m_plainTextEdit->moveCursor(QTextCursor::Start) ; m_plainTextEdit->ensureCursorVisible() ; @@ -500,7 +500,7 @@ void MainWidget::slotCopy() m_plainTextEdit->copy(); } else { QTextDocument *document = m_plainTextEdit->document(); - QTextEdit *tempTextEdit = new QTextEdit(this); + QPlainTextEdit *tempTextEdit = new QPlainTextEdit(this); tempTextEdit->setDocument(document); tempTextEdit->selectAll(); tempTextEdit->copy(); diff --git a/src/resulttextview.cpp b/src/resulttextview.cpp index 70c9d63..b7169fe 100644 --- a/src/resulttextview.cpp +++ b/src/resulttextview.cpp @@ -3,13 +3,20 @@ #include #include #include +#include + +#include + +#define TAP_MOVE_DELAY 300 ResultTextView::ResultTextView(QWidget *parent) : m_Menu(nullptr), m_actCopy(nullptr), m_actCut(nullptr), m_actSelectAll(nullptr) { Q_UNUSED(parent) //鼠标滑动事件接入 - QScroller::grabGesture(this->viewport(), QScroller::ScrollerGestureType::TouchGesture); +// QScroller::grabGesture(this->viewport(), QScroller::ScrollerGestureType::TouchGesture); +// QScroller::grabGesture(this->viewport(), QScroller::TouchGesture); + viewport()->setCursor(Qt::IBeamCursor); m_Menu = new QMenu(this); m_actCopy = new QAction(this); m_actCopy->setText(tr("Copy")); @@ -39,6 +46,14 @@ ResultTextView::ResultTextView(QWidget *parent) connect(m_actPaste, &QAction::triggered, this, [ = ]() { emit this->paste(); }); + setFrameShape(QFrame::NoFrame); + setFocusPolicy(Qt::StrongFocus); + setMouseTracking(true); + grabGesture(Qt::TapGesture); +// grabGesture(Qt::TapAndHoldGesture); +// grabGesture(Qt::SwipeGesture); +// grabGesture(Qt::PanGesture); +// grabGesture(Qt::PinchGesture); } void ResultTextView::contextMenuEvent(QContextMenuEvent *e) @@ -67,5 +82,148 @@ void ResultTextView::resizeEvent(QResizeEvent *event) { emit sigChangeSize(); this->viewport()->setFixedWidth(this->width() - 15); - QTextEdit::resizeEvent(event); + QPlainTextEdit::resizeEvent(event); +} + +void ResultTextView::mouseMoveEvent(QMouseEvent *e) +{ +// if (Qt::MouseEventSynthesizedByQt == e->source()) { +// m_endY = e->y(); +// m_endX = e->x(); +// } + //add for single refers to the sliding + if (e->type() == QEvent::MouseMove && e->source() == Qt::MouseEventSynthesizedByQt) { + const ulong diffTimeX = e->timestamp() - m_lastMouseTimeX; + const ulong diffTimeY = e->timestamp() - m_lastMouseTimeY; + const int diffYpos = e->pos().y() - m_lastMouseYpos; + const int diffXpos = e->pos().x() - m_lastMouseXpos; + m_lastMouseTimeX = e->timestamp(); + m_lastMouseTimeY = e->timestamp(); + m_lastMouseYpos = e->pos().y(); + m_lastMouseXpos = e->pos().x(); + + if (m_gestureAction == GA_slide) { + QFont font = this->font(); + + /*开根号时数值越大衰减比例越大*/ + qreal direction = diffYpos > 0 ? 1.0 : -1.0; + slideGestureY(-direction * sqrt(abs(diffYpos)) / font.pointSize()); + qreal directionX = diffXpos > 0 ? 1.0 : -1.0; + slideGestureX(-directionX * sqrt(abs(diffXpos)) / font.pointSize()); + + /*预算惯性滑动时间*/ + m_stepSpeedY = static_cast(diffYpos) / static_cast(diffTimeY + 0.000001); + durationY = sqrt(abs(m_stepSpeedY)) * 1000; + m_stepSpeedX = static_cast(diffXpos) / static_cast(diffTimeX + 0.000001); + durationX = sqrt(abs(m_stepSpeedX)) * 1000; + + /*预算惯性滑动距离,4.0为调优数值*/ + m_stepSpeedY /= sqrt(font.pointSize() * 4.0); + changeY = m_stepSpeedY * sqrt(abs(m_stepSpeedY)) * 100; + m_stepSpeedX /= sqrt(font.pointSize() * 4.0); + changeX = m_stepSpeedX * sqrt(abs(m_stepSpeedX)) * 100; + + //return true; + } + + if (m_gestureAction != GA_null) { + //return true; + } + } + + QApplication::restoreOverrideCursor(); + + if (viewport()->cursor().shape() != Qt::IBeamCursor) { + viewport()->setCursor(Qt::IBeamCursor); + } + QPlainTextEdit::mouseMoveEvent(e); +} + +void ResultTextView::mousePressEvent(QMouseEvent *e) +{ + if (e->type() == QEvent::MouseButtonPress && e->source() == Qt::MouseEventSynthesizedByQt) { + m_lastMouseTimeX = e->timestamp(); + m_lastMouseTimeY = e->timestamp(); + m_lastMouseYpos = e->pos().y(); + m_lastMouseXpos = e->pos().x(); + +// if (tweenY.activeY()) { +// m_slideContinueY = true; +// tweenY.stopY(); +// } + +// if (tweenX.activeX()) { +// m_slideContinueX = true; +// tweenX.stopX(); +// } + } + QPlainTextEdit::mousePressEvent(e); +} + +bool ResultTextView::event(QEvent *event) +{ + if (event->type() == QEvent::Gesture) + gestureEvent(static_cast(event)); + return QPlainTextEdit::event(event); +} + +bool ResultTextView::gestureEvent(QGestureEvent *event) +{ + if (QGesture *tap = event->gesture(Qt::TapGesture)) { + tapGestureTriggered(static_cast(tap)); + } + return true; +} + +void ResultTextView::tapGestureTriggered(QTapGesture *tap) +{ + + switch (tap->state()) { + case Qt::GestureStarted: { + m_gestureAction = GA_tap; + m_tapBeginTime = QDateTime::currentDateTime().toMSecsSinceEpoch(); + break; + } + case Qt::GestureUpdated: { + break; + } + case Qt::GestureCanceled: { + //根据时间长短区分轻触滑动 + qint64 timeSpace = QDateTime::currentDateTime().toMSecsSinceEpoch() - m_tapBeginTime; + if (timeSpace < TAP_MOVE_DELAY || m_slideContinue) { + m_slideContinue = false; + m_gestureAction = GA_slide; + qDebug() << "slide start" << timeSpace; + } else { + m_gestureAction = GA_null; + qDebug() << "null start" << timeSpace; + } + break; + } + case Qt::GestureFinished: { + m_gestureAction = GA_null; + break; + } + default: { + break; + } + } +} + +void ResultTextView::slideGestureY(qreal diff) +{ + static qreal delta = 0.0; + int step = static_cast(diff + delta); + delta = diff + delta - step; + + verticalScrollBar()->setValue(verticalScrollBar()->value() + step); +} + +void ResultTextView::slideGestureX(qreal diff) +{ + static qreal delta = 0.0; + int step = static_cast(diff + delta); + delta = diff + delta - step; + + horizontalScrollBar()->setValue(horizontalScrollBar()->value() + step * 10); } diff --git a/src/resulttextview.h b/src/resulttextview.h index 5a47fdd..a3fb514 100644 --- a/src/resulttextview.h +++ b/src/resulttextview.h @@ -2,12 +2,12 @@ #define RESULTTEXTVIEW_H #include -#include +#include #include #include +#include - -class ResultTextView : public QTextEdit +class ResultTextView : public QPlainTextEdit { Q_OBJECT public: @@ -16,7 +16,17 @@ public: protected: void contextMenuEvent(QContextMenuEvent *e) override; void resizeEvent(QResizeEvent *event) override; + void mouseMoveEvent(QMouseEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; + bool event(QEvent *event) override; + //触摸屏功能函数 + bool gestureEvent(QGestureEvent *event); + void tapGestureTriggered(QTapGesture *tap); + + //add for single refers to the sliding + void slideGestureY(qreal diff); + void slideGestureX(qreal diff); signals: void sigChangeSize(); private: @@ -25,6 +35,34 @@ private: QAction *m_actCut{nullptr}; QAction *m_actPaste{nullptr}; QAction *m_actSelectAll{nullptr}; + + bool m_slideContinue = false; + + //触摸屏 + enum GestureAction { + GA_null, + GA_tap, + GA_slide, + GA_pinch, + GA_hold, + GA_pan, + GA_swipe + }; + GestureAction m_gestureAction = GA_null; + + qint64 m_tapBeginTime = 0; + ulong m_lastMouseTime; + int m_lastMouseYpos; + int m_lastMouseXpos; + qreal m_stepSpeedY = 0; + qreal m_stepSpeedX = 0; + qreal durationY = {0.0}; + qreal durationX = {0.0}; + qreal changeY = {0.0}; + qreal changeX = {0.0}; + ulong m_lastMouseTimeX; + ulong m_lastMouseTimeY; + }; #endif // RESULTTEXTVIEW_H