18 #include "qgssettings.h"
24 mTolerance = s.value( QStringLiteral(
"/Processing/Modeler/snapTolerancePixels" ), 40 ).toInt();
34 mSnapToGrid = enabled;
41 bool snappedXToGrid =
false;
42 bool snappedYToGrid =
false;
43 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 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 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 ( QPointF point : points )
143 int xRatio =
static_cast< int >( ( point.x() ) / gridRes + 0.5 );
144 int yRatio =
static_cast< int >( ( point.y() ) / gridRes + 0.5 );
146 double xSnapped = xRatio * gridRes;
147 double ySnapped = yRatio * gridRes;
149 double currentDiffX = std::fabs( xSnapped - point.x() );
150 if ( currentDiffX < smallestDiffX )
152 smallestDiffX = currentDiffX;
153 deltaX = xSnapped - point.x();
156 double currentDiffY = std::fabs( ySnapped - point.y() );
157 if ( currentDiffY < smallestDiffY )
159 smallestDiffY = currentDiffY;
160 deltaY = ySnapped - point.y();
165 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 );
Contains settings relating to the appearance, spacing and offset for layout grids.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
double length() const
Returns the length of the measurement.
int snapTolerance() const
Returns the snap tolerance (in pixels) to use when snapping.
void setSnapTolerance(int snapTolerance)
Sets the snap tolerance (in pixels) to use when snapping.
QPointF snapPointsToGrid(const QList< QPointF > &points, double scaleFactor, bool &snappedX, bool &snappedY) const
Snaps a set of points to the grid.
QRectF snapRect(const QRectF &rect, double scaleFactor, bool &snapped, bool snapHorizontal=true, bool snapVertical=true) const
Snaps a layout coordinate rect.
QgsModelSnapper()
Constructor for QgsModelSnapper, attached to the specified layout.
QPointF snapPoint(QPointF point, double scaleFactor, bool &snapped, bool snapHorizontal=true, bool snapVertical=true) const
Snaps a layout coordinate point.
QRectF snapRectWithResize(const QRectF &rect, double scaleFactor, bool &snapped, bool snapHorizontal=true, bool snapVertical=true) const
Snaps a layout coordinate rect.
QPointF snapPointToGrid(QPointF point, double scaleFactor, bool &snappedX, bool &snappedY) const
Snaps a layout coordinate point to the grid.
void setSnapToGrid(bool enabled)
Sets whether snapping to grid is enabled.