From 08df3b44865efa1f0bf36f7117eac0aa0d2f6890 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 2 Oct 2017 16:25:18 +0300 Subject: [PATCH] Scrolling delay. When object is close to edge of view Valentina begins scrolling scene to ensure that the object is visible. In many cases this happens to fast for user. As solution after correction Valentina will wait 80 milliseconds before procedding next move. This will give users enough time to react. Such delay is called only if view was scrolled. --HG-- branch : develop --- .../tools/drawTools/toolcurve/vtoolspline.cpp | 5 +-- .../drawTools/toolcurve/vtoolsplinepath.cpp | 5 +-- .../toolsinglepoint/vtoolbasepoint.cpp | 7 +++-- src/libs/vtools/tools/vtoolseamallowance.cpp | 7 +++-- src/libs/vwidgets/vcontrolpointspline.cpp | 8 +++-- src/libs/vwidgets/vgraphicssimpletextitem.cpp | 19 +++++++++--- src/libs/vwidgets/vmaingraphicsview.cpp | 31 +++++++++++++++++++ src/libs/vwidgets/vmaingraphicsview.h | 5 +++ 8 files changed, 69 insertions(+), 18 deletions(-) diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp b/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp index a9cebcb89..06733933c 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp +++ b/src/libs/vtools/tools/drawTools/toolcurve/vtoolspline.cpp @@ -425,12 +425,13 @@ void VToolSpline::mouseMoveEvent(QGraphicsSceneMouseEvent *event) const QList viewList = scene()->views(); if (not viewList.isEmpty()) { - if (QGraphicsView *view = viewList.at(0)) + if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { VMainGraphicsScene *currentScene = qobject_cast(scene()); SCASSERT(currentScene) const QPointF cursorPosition = currentScene->getScenePos(); - view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10)); + view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10), + VMainGraphicsView::scrollDelay); } } changeFinished = true; diff --git a/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp b/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp index 49b96058e..de35e2bcf 100644 --- a/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp +++ b/src/libs/vtools/tools/drawTools/toolcurve/vtoolsplinepath.cpp @@ -565,12 +565,13 @@ void VToolSplinePath::mouseMoveEvent(QGraphicsSceneMouseEvent *event) const QList viewList = scene()->views(); if (not viewList.isEmpty()) { - if (QGraphicsView *view = viewList.at(0)) + if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { VMainGraphicsScene *currentScene = qobject_cast(scene()); SCASSERT(currentScene) const QPointF cursorPosition = currentScene->getScenePos(); - view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10)); + view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10), + VMainGraphicsView::scrollDelay); } } changeFinished = true; diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp index d23337335..417a3d3e3 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp @@ -193,7 +193,7 @@ QVariant VToolBasePoint::itemChange(QGraphicsItem::GraphicsItemChange change, co const QList viewList = scene()->views(); if (not viewList.isEmpty()) { - if (QGraphicsView *view = viewList.at(0)) + if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { const int xmargin = 50; const int ymargin = 50; @@ -205,7 +205,7 @@ QVariant VToolBasePoint::itemChange(QGraphicsItem::GraphicsItemChange change, co if (itemRect.height() + 2*ymargin < viewRect.height() && itemRect.width() + 2*xmargin < viewRect.width()) { - view->ensureVisible(itemRect, xmargin, ymargin); + view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, ymargin); } else { @@ -213,7 +213,8 @@ QVariant VToolBasePoint::itemChange(QGraphicsItem::GraphicsItemChange change, co VMainGraphicsScene *currentScene = qobject_cast(scene()); SCASSERT(currentScene) const QPointF cursorPosition = currentScene->getScenePos(); - view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10)); + view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10), + VMainGraphicsView::scrollDelay); } } } diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index eaa12dc7e..ea4738b46 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -914,7 +914,7 @@ QVariant VToolSeamAllowance::itemChange(QGraphicsItem::GraphicsItemChange change const QList viewList = scene()->views(); if (not viewList.isEmpty()) { - if (QGraphicsView *view = viewList.at(0)) + if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { const int xmargin = 50; const int ymargin = 50; @@ -926,7 +926,7 @@ QVariant VToolSeamAllowance::itemChange(QGraphicsItem::GraphicsItemChange change if (itemRect.height() + 2*ymargin < viewRect.height() && itemRect.width() + 2*xmargin < viewRect.width()) { - view->ensureVisible(itemRect, xmargin, ymargin); + view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, ymargin); } else { @@ -934,7 +934,8 @@ QVariant VToolSeamAllowance::itemChange(QGraphicsItem::GraphicsItemChange change VMainGraphicsScene *currentScene = qobject_cast(scene()); SCASSERT(currentScene); const QPointF cursorPosition = currentScene->getScenePos(); - view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10)); + view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10), + VMainGraphicsView::scrollDelay); } } } diff --git a/src/libs/vwidgets/vcontrolpointspline.cpp b/src/libs/vwidgets/vcontrolpointspline.cpp index fa8e13bce..f55fa2203 100644 --- a/src/libs/vwidgets/vcontrolpointspline.cpp +++ b/src/libs/vwidgets/vcontrolpointspline.cpp @@ -176,7 +176,7 @@ QVariant VControlPointSpline::itemChange(QGraphicsItem::GraphicsItemChange chang const QList viewList = scene()->views(); if (not viewList.isEmpty()) { - if (QGraphicsView *view = viewList.at(0)) + if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { const int xmargin = 50; const int ymargin = 50; @@ -188,7 +188,8 @@ QVariant VControlPointSpline::itemChange(QGraphicsItem::GraphicsItemChange chang if (itemRect.height() + 2*ymargin < viewRect.height() && itemRect.width() + 2*xmargin < viewRect.width()) { - view->ensureVisible(itemRect, xmargin, ymargin); + view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, + ymargin); } else { @@ -196,7 +197,8 @@ QVariant VControlPointSpline::itemChange(QGraphicsItem::GraphicsItemChange chang VMainGraphicsScene *currentScene = qobject_cast(scene()); SCASSERT(currentScene) const QPointF cursorPosition = currentScene->getScenePos(); - view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10)); + view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10), + VMainGraphicsView::scrollDelay); } } } diff --git a/src/libs/vwidgets/vgraphicssimpletextitem.cpp b/src/libs/vwidgets/vgraphicssimpletextitem.cpp index 2b9b70720..e39b7a288 100644 --- a/src/libs/vwidgets/vgraphicssimpletextitem.cpp +++ b/src/libs/vwidgets/vgraphicssimpletextitem.cpp @@ -138,10 +138,17 @@ QVariant VGraphicsSimpleTextItem::itemChange(GraphicsItemChange change, const QV const QList viewList = scene()->views(); if (not viewList.isEmpty()) { - if (QGraphicsView *view = viewList.at(0)) + if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { - const int xmargin = 50; - const int ymargin = 50; + int xmargin = 50; + int ymargin = 50; + + const int scale = qRound(SceneScale(scene())); + if (scale > 1) + { + xmargin /= scale; + ymargin /= scale; + } const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view); const QRectF itemRect = mapToScene(boundingRect()).boundingRect(); @@ -150,7 +157,7 @@ QVariant VGraphicsSimpleTextItem::itemChange(GraphicsItemChange change, const QV if (itemRect.height() + 2*ymargin < viewRect.height() && itemRect.width() + 2*xmargin < viewRect.width()) { - view->ensureVisible(itemRect, xmargin, ymargin); + view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, ymargin); } else { @@ -158,7 +165,9 @@ QVariant VGraphicsSimpleTextItem::itemChange(GraphicsItemChange change, const QV VMainGraphicsScene *currentScene = qobject_cast(scene()); SCASSERT(currentScene) const QPointF cursorPosition = currentScene->getScenePos(); - view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10)); + + view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10), + VMainGraphicsView::scrollDelay); } } } diff --git a/src/libs/vwidgets/vmaingraphicsview.cpp b/src/libs/vwidgets/vmaingraphicsview.cpp index 4350e3553..f7cadd750 100644 --- a/src/libs/vwidgets/vmaingraphicsview.cpp +++ b/src/libs/vwidgets/vmaingraphicsview.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include "../vmisc/def.h" #include "../vmisc/vmath.h" @@ -339,6 +340,8 @@ bool GraphicsViewZoom::StartHorizontalScrollings(QWheelEvent *wheel_event) return true; } +const unsigned long VMainGraphicsView::scrollDelay = 80; + //--------------------------------------------------------------------------------------------------------------------- /** * @brief VMainGraphicsView constructor. @@ -529,6 +532,34 @@ qreal VMainGraphicsView::MaxScale() return maxSceneSize / screenSize; } +//--------------------------------------------------------------------------------------------------------------------- +void VMainGraphicsView::EnsureVisibleWithDelay(const QRectF &rect, unsigned long msecs, int xmargin, int ymargin) +{ + const int hbar = horizontalScrollBar()->value(); + const int vbar = verticalScrollBar()->value(); + + ensureVisible(rect, xmargin, ymargin); + + if (hbar != horizontalScrollBar()->value() || vbar != verticalScrollBar()->value()) + { + QThread::msleep(msecs); + } +} + +//--------------------------------------------------------------------------------------------------------------------- +void VMainGraphicsView::EnsureVisibleWithDelay(const QGraphicsItem *item, unsigned long msecs, int xmargin, int ymargin) +{ + const int hbar = horizontalScrollBar()->value(); + const int vbar = verticalScrollBar()->value(); + + ensureVisible(item, xmargin, ymargin); + + if (hbar != horizontalScrollBar()->value() || vbar != verticalScrollBar()->value()) + { + QThread::msleep(msecs); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VMainGraphicsView::setShowToolOptions(bool value) { diff --git a/src/libs/vwidgets/vmaingraphicsview.h b/src/libs/vwidgets/vmaingraphicsview.h index d0699b748..68c7e3395 100644 --- a/src/libs/vwidgets/vmaingraphicsview.h +++ b/src/libs/vwidgets/vmaingraphicsview.h @@ -124,6 +124,11 @@ public: static qreal MinScale(); static qreal MaxScale(); + void EnsureVisibleWithDelay(const QRectF &rect, unsigned long msecs, int xmargin = 50, int ymargin = 50); + void EnsureVisibleWithDelay(const QGraphicsItem *item, unsigned long msecs, int xmargin = 50, int ymargin = 50); + + static const unsigned long scrollDelay; + signals: /** * @brief MouseRelease help catch mouse release event.