24 mTolerance = s.
value( QStringLiteral(
"/Processing/Modeler/snapTolerancePixels" ), 40 ).toInt();
34 mSnapToGrid = enabled;
41 bool snappedXToGrid =
false;
42 bool snappedYToGrid =
false;
43 const QPointF res =
snapPointToGrid( point, scaleFactor, snappedXToGrid, snappedYToGrid );
44 if ( snappedXToGrid && snapVertical )
47 point.setX( res.x() );
49 if ( snappedYToGrid && snapHorizontal )
52 point.setY( res.y() );
58 QRectF
QgsModelSnapper::snapRect(
const QRectF &rect,
double scaleFactor,
bool &snapped,
bool snapHorizontal,
bool snapVertical )
const
61 QRectF snappedRect = rect;
63 bool snappedXToGrid =
false;
64 bool snappedYToGrid =
false;
65 QList< QPointF > points;
66 points << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
67 const QPointF res =
snapPointsToGrid( points, scaleFactor, snappedXToGrid, snappedYToGrid );
68 if ( snappedXToGrid && snapVertical )
71 snappedRect.translate( res.x(), 0 );
73 if ( snappedYToGrid && snapHorizontal )
76 snappedRect.translate( 0, res.y() );
85 QRectF snappedRect = rect;
87 bool snappedXToGrid =
false;
88 bool snappedYToGrid =
false;
89 QPointF res =
snapPointsToGrid( QList< QPointF >() << rect.topLeft(), scaleFactor, snappedXToGrid, snappedYToGrid );
90 if ( snappedXToGrid && snapVertical )
93 snappedRect.setLeft( snappedRect.left() + res.x() );
95 if ( snappedYToGrid && snapHorizontal )
98 snappedRect.setTop( snappedRect.top() + res.y() );
100 res =
snapPointsToGrid( QList< QPointF >() << rect.bottomRight(), scaleFactor, snappedXToGrid, snappedYToGrid );
101 if ( snappedXToGrid && snapVertical )
104 snappedRect.setRight( snappedRect.right() + res.x() );
106 if ( snappedYToGrid && snapHorizontal )
109 snappedRect.setBottom( snappedRect.bottom() + res.y() );
117 const QPointF delta =
snapPointsToGrid( QList< QPointF >() << point, scaleFactor, snappedX, snappedY );
118 return point + delta;
127 return QPointF( 0, 0 );
132 return QPointF( 0, 0 );
137 double smallestDiffX = std::numeric_limits<double>::max();
138 double smallestDiffY = std::numeric_limits<double>::max();
139 for (
const QPointF point : points )
142 const double gridRes = 30;
143 int xRatio =
static_cast< int >( ( point.x() ) / gridRes + 0.5 );
144 int yRatio =
static_cast< int >( ( point.y() ) / gridRes + 0.5 );
146 const double xSnapped = xRatio * gridRes;
147 const double ySnapped = yRatio * gridRes;
149 const double currentDiffX = std::fabs( xSnapped - point.x() );
150 if ( currentDiffX < smallestDiffX )
152 smallestDiffX = currentDiffX;
153 deltaX = xSnapped - point.x();
156 const double currentDiffY = std::fabs( ySnapped - point.y() );
157 if ( currentDiffY < smallestDiffY )
159 smallestDiffY = currentDiffY;
160 deltaY = ySnapped - point.y();
165 const double alignThreshold = mTolerance / scaleFactor;
167 QPointF delta( 0, 0 );
168 if ( smallestDiffX <= alignThreshold )
172 delta.setX( deltaX );
174 if ( smallestDiffY <= alignThreshold )
178 delta.setY( deltaY );