From 5f2bec91d9c4990f3c163fafe838a6c918253357 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 20 Jun 2020 12:20:39 +0300 Subject: [PATCH] DRY. Refactoring item move code. smart-pattern/valentina#53 --- .../toolsinglepoint/vtoolbasepoint.cpp | 24 +--------------- src/libs/vtools/tools/vtoolseamallowance.cpp | 24 +--------------- src/libs/vwidgets/vcontrolpointspline.cpp | 26 +---------------- src/libs/vwidgets/vgraphicssimpletextitem.cpp | 25 +---------------- src/libs/vwidgets/vmaingraphicsview.cpp | 28 +++++++++++++++++++ src/libs/vwidgets/vmaingraphicsview.h | 2 ++ 6 files changed, 34 insertions(+), 95 deletions(-) diff --git a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp index 4f450934a..b49d2c546 100644 --- a/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp +++ b/src/libs/vtools/tools/drawTools/toolpoint/toolsinglepoint/vtoolbasepoint.cpp @@ -194,29 +194,7 @@ QVariant VToolBasePoint::itemChange(QGraphicsItem::GraphicsItemChange change, co { if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { - const qreal scale = SceneScale(scene()); - const int xmargin = 5; - const int ymargin = 5; - - const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view); - const QRectF itemRect = mapToScene(boundingRect()).boundingRect(); - - // If item's rect is bigger than view's rect ensureVisible works very unstable. - if (itemRect.height() + 2*ymargin < viewRect.height() && - itemRect.width() + 2*xmargin < viewRect.width()) - { - view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, ymargin); - } - else - { - // Ensure visible only small rect around a cursor - VMainGraphicsScene *currentScene = qobject_cast(scene()); - SCASSERT(currentScene) - const QPointF cursorPosition = currentScene->getScenePos(); - view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5/scale, cursorPosition.y()-5/scale, - 10/scale, 10/scale), - VMainGraphicsView::scrollDelay); - } + view->EnsureItemVisibleWithDelay(this, VMainGraphicsView::scrollDelay); } } changeFinished = true; diff --git a/src/libs/vtools/tools/vtoolseamallowance.cpp b/src/libs/vtools/tools/vtoolseamallowance.cpp index cad35d14a..261691386 100644 --- a/src/libs/vtools/tools/vtoolseamallowance.cpp +++ b/src/libs/vtools/tools/vtoolseamallowance.cpp @@ -977,29 +977,7 @@ QVariant VToolSeamAllowance::itemChange(QGraphicsItem::GraphicsItemChange change { if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { - const qreal scale = SceneScale(scene()); - const int xmargin = 5; - const int ymargin = 5; - - const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view); - const QRectF itemRect = mapToScene(boundingRect()|childrenBoundingRect()).boundingRect(); - - // If item's rect is bigger than view's rect ensureVisible works very unstable. - if (itemRect.height() + 2*ymargin < viewRect.height() && - itemRect.width() + 2*xmargin < viewRect.width()) - { - view->EnsureVisibleWithDelay(this, VMainGraphicsView::scrollDelay, xmargin, ymargin); - } - else - { - // Ensure visible only small rect around a cursor - VMainGraphicsScene *currentScene = qobject_cast(scene()); - SCASSERT(currentScene); - const QPointF cursorPosition = currentScene->getScenePos(); - view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5/scale, cursorPosition.y()-5/scale, - 10/scale, 10/scale), - VMainGraphicsView::scrollDelay); - } + view->EnsureItemVisibleWithDelay(this, VMainGraphicsView::scrollDelay); } } diff --git a/src/libs/vwidgets/vcontrolpointspline.cpp b/src/libs/vwidgets/vcontrolpointspline.cpp index 1d0a56b01..4fdb3a1ed 100644 --- a/src/libs/vwidgets/vcontrolpointspline.cpp +++ b/src/libs/vwidgets/vcontrolpointspline.cpp @@ -174,31 +174,7 @@ QVariant VControlPointSpline::itemChange(QGraphicsItem::GraphicsItemChange chang { if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { - const qreal scale = SceneScale(scene()); - const int xmargin = 5; - const int ymargin = 5; - - const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view); - const QRectF itemRect = sceneBoundingRect(); - - // If item's rect is bigger than view's rect ensureVisible works very unstable. - if (itemRect.height() + 2*ymargin < viewRect.height() && - itemRect.width() + 2*xmargin < viewRect.width()) - { - view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, - ymargin); - } - else - { - // Ensure visible only small rect around a cursor - VMainGraphicsScene *currentScene = qobject_cast(scene()); - SCASSERT(currentScene) - - const QPointF cursorPosition = currentScene->getScenePos(); - view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5/scale, - cursorPosition.y()-5/scale, 10/scale, 10/scale), - VMainGraphicsView::scrollDelay); - } + view->EnsureItemVisibleWithDelay(this, VMainGraphicsView::scrollDelay); } } } diff --git a/src/libs/vwidgets/vgraphicssimpletextitem.cpp b/src/libs/vwidgets/vgraphicssimpletextitem.cpp index e0529aaef..370bc876f 100644 --- a/src/libs/vwidgets/vgraphicssimpletextitem.cpp +++ b/src/libs/vwidgets/vgraphicssimpletextitem.cpp @@ -201,30 +201,7 @@ QVariant VGraphicsSimpleTextItem::itemChange(GraphicsItemChange change, const QV { if (VMainGraphicsView *view = qobject_cast(viewList.at(0))) { - const qreal scale = SceneScale(scene()); - int xmargin = 5; - int ymargin = 5; - - const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view); - const QRectF itemRect = mapToScene(boundingRect()).boundingRect(); - - // If item's rect is bigger than view's rect ensureVisible works very unstable. - if (itemRect.height() + 2*ymargin < viewRect.height() && - itemRect.width() + 2*xmargin < viewRect.width()) - { - view->EnsureVisibleWithDelay(itemRect, VMainGraphicsView::scrollDelay, xmargin, ymargin); - } - else - { - // Ensure visible only small rect around a cursor - VMainGraphicsScene *currentScene = qobject_cast(scene()); - SCASSERT(currentScene) - const QPointF cursorPosition = currentScene->getScenePos(); - - view->EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5/scale, cursorPosition.y()-5/scale, - 10/scale, 10/scale), - VMainGraphicsView::scrollDelay); - } + view->EnsureItemVisibleWithDelay(this, VMainGraphicsView::scrollDelay); } } } diff --git a/src/libs/vwidgets/vmaingraphicsview.cpp b/src/libs/vwidgets/vmaingraphicsview.cpp index 3b4e69f46..100ffab15 100644 --- a/src/libs/vwidgets/vmaingraphicsview.cpp +++ b/src/libs/vwidgets/vmaingraphicsview.cpp @@ -58,6 +58,7 @@ #include "../vmisc/vabstractapplication.h" #include "../vmisc/vsettings.h" #include "vabstractmainwindow.h" +#include "global.h" const qreal maxSceneSize = ((20.0 * 1000.0) / 25.4) * PrintDPI; // 20 meters in pixels @@ -670,6 +671,33 @@ qreal VMainGraphicsView::MaxScale() return maxSceneSize / screenSize; } +//--------------------------------------------------------------------------------------------------------------------- +void VMainGraphicsView::EnsureItemVisibleWithDelay(const QGraphicsItem *item, unsigned long msecs, int xmargin, + int ymargin) +{ + SCASSERT(item != nullptr) + const qreal scale = SceneScale(item->scene()); + + const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(this); + const QRectF itemRect = item->mapToScene(item->boundingRect()).boundingRect(); + + // If item's rect is bigger than view's rect ensureVisible works very unstable. + if (itemRect.height() + 2*ymargin < viewRect.height() && + itemRect.width() + 2*xmargin < viewRect.width()) + { + EnsureVisibleWithDelay(item, msecs, xmargin, ymargin); + } + else + { + // Ensure visible only small rect around a cursor + VMainGraphicsScene *currentScene = qobject_cast(item->scene()); + SCASSERT(currentScene); + const QPointF cursorPosition = currentScene->getScenePos(); + EnsureVisibleWithDelay(QRectF(cursorPosition.x()-5/scale, cursorPosition.y()-5/scale, 10/scale, 10/scale), + msecs); + } +} + //--------------------------------------------------------------------------------------------------------------------- void VMainGraphicsView::EnsureVisibleWithDelay(const QRectF &rect, unsigned long msecs, int xmargin, int ymargin) { diff --git a/src/libs/vwidgets/vmaingraphicsview.h b/src/libs/vwidgets/vmaingraphicsview.h index 9d8bdd092..09998a8ca 100644 --- a/src/libs/vwidgets/vmaingraphicsview.h +++ b/src/libs/vwidgets/vmaingraphicsview.h @@ -127,6 +127,8 @@ public: static qreal MinScale(); static qreal MaxScale(); + void EnsureItemVisibleWithDelay(const QGraphicsItem *item, unsigned long msecs, int xmargin = 5, int ymargin = 5); + 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);