fix: 修复bug85300

Description: 重写滑动事件

Log: 重写滑动事件
Bug: https://pms.uniontech.com/zentao/bug-view-85300.html
Change-Id: I9a165f99ab15ac14ced2c62a03eb1f90ccac1182
This commit is contained in:
liuminghang 2021-09-26 14:59:55 +08:00
parent 2bf861e7cf
commit 50597c89dd
3 changed files with 206 additions and 10 deletions

View File

@ -402,8 +402,8 @@ void MainWidget::loadHtml(const QString &html)
if (!html.isEmpty()) { if (!html.isEmpty()) {
m_frameStackLayout->setContentsMargins(20, 0, 5, 0); m_frameStackLayout->setContentsMargins(20, 0, 5, 0);
m_resultWidget->setCurrentWidget(m_plainTextEdit); m_resultWidget->setCurrentWidget(m_plainTextEdit);
// m_plainTextEdit->appendHtml(html); m_plainTextEdit->appendHtml(html);
m_plainTextEdit->setHtml(html); // m_plainTextEdit->setHtml(html);
} else { } else {
resultEmpty(); resultEmpty();
} }
@ -414,8 +414,8 @@ void MainWidget::loadString(const QString &string)
if (!string.isEmpty()) { if (!string.isEmpty()) {
m_frameStackLayout->setContentsMargins(20, 0, 5, 0); m_frameStackLayout->setContentsMargins(20, 0, 5, 0);
m_resultWidget->setCurrentWidget(m_plainTextEdit); m_resultWidget->setCurrentWidget(m_plainTextEdit);
// m_plainTextEdit->appendPlainText(string); m_plainTextEdit->appendPlainText(string);
m_plainTextEdit->setText(string); // m_plainTextEdit->setText(string);
//读取完了显示在最上方 //读取完了显示在最上方
m_plainTextEdit->moveCursor(QTextCursor::Start) ; m_plainTextEdit->moveCursor(QTextCursor::Start) ;
m_plainTextEdit->ensureCursorVisible() ; m_plainTextEdit->ensureCursorVisible() ;
@ -500,7 +500,7 @@ void MainWidget::slotCopy()
m_plainTextEdit->copy(); m_plainTextEdit->copy();
} else { } else {
QTextDocument *document = m_plainTextEdit->document(); QTextDocument *document = m_plainTextEdit->document();
QTextEdit *tempTextEdit = new QTextEdit(this); QPlainTextEdit *tempTextEdit = new QPlainTextEdit(this);
tempTextEdit->setDocument(document); tempTextEdit->setDocument(document);
tempTextEdit->selectAll(); tempTextEdit->selectAll();
tempTextEdit->copy(); tempTextEdit->copy();

View File

@ -3,13 +3,20 @@
#include <QScroller> #include <QScroller>
#include <QClipboard> #include <QClipboard>
#include <QApplication> #include <QApplication>
#include <QScrollBar>
#include <math.h>
#define TAP_MOVE_DELAY 300
ResultTextView::ResultTextView(QWidget *parent) ResultTextView::ResultTextView(QWidget *parent)
: m_Menu(nullptr), m_actCopy(nullptr), m_actCut(nullptr), m_actSelectAll(nullptr) : m_Menu(nullptr), m_actCopy(nullptr), m_actCut(nullptr), m_actSelectAll(nullptr)
{ {
Q_UNUSED(parent) 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_Menu = new QMenu(this);
m_actCopy = new QAction(this); m_actCopy = new QAction(this);
m_actCopy->setText(tr("Copy")); m_actCopy->setText(tr("Copy"));
@ -39,6 +46,14 @@ ResultTextView::ResultTextView(QWidget *parent)
connect(m_actPaste, &QAction::triggered, this, [ = ]() { connect(m_actPaste, &QAction::triggered, this, [ = ]() {
emit this->paste(); 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) void ResultTextView::contextMenuEvent(QContextMenuEvent *e)
@ -67,5 +82,148 @@ void ResultTextView::resizeEvent(QResizeEvent *event)
{ {
emit sigChangeSize(); emit sigChangeSize();
this->viewport()->setFixedWidth(this->width() - 15); 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<qreal>(diffYpos) / static_cast<qreal>(diffTimeY + 0.000001);
durationY = sqrt(abs(m_stepSpeedY)) * 1000;
m_stepSpeedX = static_cast<qreal>(diffXpos) / static_cast<qreal>(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<QGestureEvent *>(event));
return QPlainTextEdit::event(event);
}
bool ResultTextView::gestureEvent(QGestureEvent *event)
{
if (QGesture *tap = event->gesture(Qt::TapGesture)) {
tapGestureTriggered(static_cast<QTapGesture *>(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<int>(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<int>(diff + delta);
delta = diff + delta - step;
horizontalScrollBar()->setValue(horizontalScrollBar()->value() + step * 10);
} }

View File

@ -2,12 +2,12 @@
#define RESULTTEXTVIEW_H #define RESULTTEXTVIEW_H
#include <QObject> #include <QObject>
#include <QTextEdit> #include <QPlainTextEdit>
#include <QAction> #include <QAction>
#include <QMenu> #include <QMenu>
#include <QGestureEvent>
class ResultTextView : public QPlainTextEdit
class ResultTextView : public QTextEdit
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -16,7 +16,17 @@ public:
protected: protected:
void contextMenuEvent(QContextMenuEvent *e) override; void contextMenuEvent(QContextMenuEvent *e) override;
void resizeEvent(QResizeEvent *event) 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: signals:
void sigChangeSize(); void sigChangeSize();
private: private:
@ -25,6 +35,34 @@ private:
QAction *m_actCut{nullptr}; QAction *m_actCut{nullptr};
QAction *m_actPaste{nullptr}; QAction *m_actPaste{nullptr};
QAction *m_actSelectAll{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 #endif // RESULTTEXTVIEW_H