DRY. Refactoring item move code. smart-pattern/valentina#53

This commit is contained in:
Roman Telezhynskyi 2020-06-20 12:20:39 +03:00
parent d08da56764
commit 5f2bec91d9
6 changed files with 34 additions and 95 deletions

View File

@ -194,29 +194,7 @@ QVariant VToolBasePoint::itemChange(QGraphicsItem::GraphicsItemChange change, co
{
if (VMainGraphicsView *view = qobject_cast<VMainGraphicsView *>(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<VMainGraphicsScene *>(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;

View File

@ -977,29 +977,7 @@ QVariant VToolSeamAllowance::itemChange(QGraphicsItem::GraphicsItemChange change
{
if (VMainGraphicsView *view = qobject_cast<VMainGraphicsView *>(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<VMainGraphicsScene *>(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);
}
}

View File

@ -174,31 +174,7 @@ QVariant VControlPointSpline::itemChange(QGraphicsItem::GraphicsItemChange chang
{
if (VMainGraphicsView *view = qobject_cast<VMainGraphicsView *>(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<VMainGraphicsScene *>(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);
}
}
}

View File

@ -201,30 +201,7 @@ QVariant VGraphicsSimpleTextItem::itemChange(GraphicsItemChange change, const QV
{
if (VMainGraphicsView *view = qobject_cast<VMainGraphicsView *>(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<VMainGraphicsScene *>(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);
}
}
}

View File

@ -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<VMainGraphicsScene *>(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)
{

View File

@ -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);