From 23bc192c043feeec67b9cf4c86d393adae6c5aef Mon Sep 17 00:00:00 2001 From: liuminghang Date: Wed, 29 Sep 2021 10:44:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E8=A7=A6?= =?UTF-8?q?=E6=91=B8=E6=BB=91=E5=8A=A8=E5=8F=AF=E4=BB=A5=E7=82=B9=E5=87=BB?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Description: 移植了editor的代码 Log: 修复了触摸滑动可以点击的问题 Change-Id: I97a94973f9f6921b7c74dfc3a8df044d5d273742 --- src/main.cpp | 21 +++++++- src/resulttextview.cpp | 115 +++++++++++++++++++++++++++++++++++++++-- src/resulttextview.h | 12 +++++ 3 files changed, 143 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 4780852..7f8e09c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,8 +33,23 @@ #include #include + DWIDGET_USE_NAMESPACE +//判断是否是wayland +bool CheckWayland() +{ + auto e = QProcessEnvironment::systemEnvironment(); + QString XDG_SESSION_TYPE = e.value(QStringLiteral("XDG_SESSION_TYPE")); + QString WAYLAND_DISPLAY = e.value(QStringLiteral("WAYLAND_DISPLAY")); + + if (XDG_SESSION_TYPE == QLatin1String("wayland") || WAYLAND_DISPLAY.contains(QLatin1String("wayland"), Qt::CaseInsensitive)) + return true; + else { + return false; + } +} + int main(int argc, char *argv[]) { @@ -42,7 +57,11 @@ int main(int argc, char *argv[]) qDebug() << "Cant open a null file"; return 0; } - + //判断是否是wayland + if (CheckWayland()) { + //默认走xdgv6,该库没有维护了,因此需要添加该代码 + qputenv("QT_WAYLAND_SHELL_INTEGRATION", "kwayland-shell"); + } DGuiApplicationHelper::setUseInactiveColorGroup(false); #if(DTK_VERSION < DTK_VERSION_CHECK(5,4,0,0)) diff --git a/src/resulttextview.cpp b/src/resulttextview.cpp index b7169fe..8bbece4 100644 --- a/src/resulttextview.cpp +++ b/src/resulttextview.cpp @@ -46,14 +46,15 @@ ResultTextView::ResultTextView(QWidget *parent) connect(m_actPaste, &QAction::triggered, this, [ = ]() { emit this->paste(); }); + connect(this, &QPlainTextEdit::selectionChanged, this, &ResultTextView::onSelectionArea); setFrameShape(QFrame::NoFrame); setFocusPolicy(Qt::StrongFocus); setMouseTracking(true); grabGesture(Qt::TapGesture); -// grabGesture(Qt::TapAndHoldGesture); -// grabGesture(Qt::SwipeGesture); -// grabGesture(Qt::PanGesture); -// grabGesture(Qt::PinchGesture); + grabGesture(Qt::TapAndHoldGesture); + grabGesture(Qt::SwipeGesture); + grabGesture(Qt::PanGesture); + grabGesture(Qt::PinchGesture); } void ResultTextView::contextMenuEvent(QContextMenuEvent *e) @@ -124,6 +125,7 @@ void ResultTextView::mouseMoveEvent(QMouseEvent *e) changeX = m_stepSpeedX * sqrt(abs(m_stepSpeedX)) * 100; //return true; + } if (m_gestureAction != GA_null) { @@ -172,6 +174,15 @@ bool ResultTextView::gestureEvent(QGestureEvent *event) if (QGesture *tap = event->gesture(Qt::TapGesture)) { tapGestureTriggered(static_cast(tap)); } + if (QGesture *tapAndHold = event->gesture(Qt::TapAndHoldGesture)) + tapAndHoldGestureTriggered(static_cast(tapAndHold)); + if (QGesture *pan = event->gesture(Qt::PanGesture)) + panTriggered(static_cast(pan)); + if (QGesture *pinch = event->gesture(Qt::PinchGesture)) + pinchTriggered(static_cast(pinch)); + // qDebug()<gesture(Qt::SwipeGesture)) +// swipeTriggered(static_cast(swipe)); return true; } @@ -210,6 +221,91 @@ void ResultTextView::tapGestureTriggered(QTapGesture *tap) } } +void ResultTextView::tapAndHoldGestureTriggered(QTapAndHoldGesture *tapAndHold) +{ + //单指长按 + switch (tapAndHold->state()) { + case Qt::GestureStarted: + m_gestureAction = GA_hold; + break; + case Qt::GestureUpdated: + break; + case Qt::GestureCanceled: + break; + case Qt::GestureFinished: + m_gestureAction = GA_null; + break; + default: + break; + } +} + +void ResultTextView::panTriggered(QPanGesture *pan) +{ + //两指平移 + switch (pan->state()) { + case Qt::GestureStarted: + m_gestureAction = GA_pan; + break; + case Qt::GestureUpdated: + break; + case Qt::GestureCanceled: + break; + case Qt::GestureFinished: + m_gestureAction = GA_null; + break; + default: + break; + } +} + +void ResultTextView::pinchTriggered(QPinchGesture *pinch) +{ + //两指拉伸 -----缩放or放大 + switch (pinch->state()) { + case Qt::GestureStarted: { + m_gestureAction = GA_pinch; + if (static_cast(m_scaleFactor) != m_fontSize) { + m_scaleFactor = m_fontSize; + } + break; + } + case Qt::GestureUpdated: { + QPinchGesture::ChangeFlags changeFlags = pinch->changeFlags(); + if (changeFlags & QPinchGesture::ScaleFactorChanged) { + m_currentStepScaleFactor = pinch->totalScaleFactor(); + } + break; + } + case Qt::GestureCanceled: { + Q_ASSERT(false); + break; + } + case Qt::GestureFinished: { + m_gestureAction = GA_null; + m_scaleFactor *= m_currentStepScaleFactor; + m_currentStepScaleFactor = 1; + break; + } + default: { + Q_ASSERT(false); + break; + } + }//switch + + //QFont font = getVTFont(); + int size = static_cast(m_scaleFactor * m_currentStepScaleFactor); + if (size < 8) + size = 8; + if (size > 50) + size = 50; + //根据移动距离设置字体大小 +// setFontSize(size); + //同步设置界面字体栏数值 +// qobject_cast(this->window())->changeSettingDialogComboxFontNumber(size); +} + + void ResultTextView::slideGestureY(qreal diff) { static qreal delta = 0.0; @@ -227,3 +323,14 @@ void ResultTextView::slideGestureX(qreal diff) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + step * 10); } + +void ResultTextView::onSelectionArea() +{ + if (m_gestureAction != GA_null) { + QTextCursor cursor = textCursor(); + if (cursor.selectedText() != "") { + cursor.clearSelection(); + setTextCursor(cursor); + } + } +} diff --git a/src/resulttextview.h b/src/resulttextview.h index a3fb514..ad87955 100644 --- a/src/resulttextview.h +++ b/src/resulttextview.h @@ -23,10 +23,18 @@ protected: //触摸屏功能函数 bool gestureEvent(QGestureEvent *event); void tapGestureTriggered(QTapGesture *tap); + void tapAndHoldGestureTriggered(QTapAndHoldGesture *tapAndHold); + void panTriggered(QPanGesture *pan); + void pinchTriggered(QPinchGesture *pinch); +// void swipeTriggered(QSwipeGesture *swipe); //add for single refers to the sliding void slideGestureY(qreal diff); void slideGestureX(qreal diff); + + +private slots: + void onSelectionArea(); signals: void sigChangeSize(); private: @@ -63,6 +71,10 @@ private: ulong m_lastMouseTimeX; ulong m_lastMouseTimeY; + qreal m_scaleFactor = 1; + qreal m_currentStepScaleFactor = 1; + Qt::GestureState m_tapStatus = Qt::NoGesture; + int m_fontSize = 16; }; #endif // RESULTTEXTVIEW_H