25using namespace Qt::StringLiterals;
30 mTolerance = s.
value( u
"/Processing/Modeler/snapTolerancePixels"_s, 40 ).toInt();
40 mSnapToGrid = enabled;
47 bool snappedXToGrid =
false;
48 bool snappedYToGrid =
false;
49 const QPointF res =
snapPointToGrid( point, scaleFactor, snappedXToGrid, snappedYToGrid );
50 if ( snappedXToGrid && snapVertical )
53 point.setX( res.x() );
55 if ( snappedYToGrid && snapHorizontal )
58 point.setY( res.y() );
64QRectF
QgsModelSnapper::snapRect(
const QRectF &rect,
double scaleFactor,
bool &snapped,
bool snapHorizontal,
bool snapVertical )
const
67 QRectF snappedRect = rect;
69 bool snappedXToGrid =
false;
70 bool snappedYToGrid =
false;
71 QList<QPointF> points;
72 points << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
73 const QPointF res =
snapPointsToGrid( points, scaleFactor, snappedXToGrid, snappedYToGrid );
74 if ( snappedXToGrid && snapVertical )
77 snappedRect.translate( res.x(), 0 );
79 if ( snappedYToGrid && snapHorizontal )
82 snappedRect.translate( 0, res.y() );
91 QRectF snappedRect = rect;
93 bool snappedXToGrid =
false;
94 bool snappedYToGrid =
false;
95 QPointF res =
snapPointsToGrid( QList<QPointF>() << rect.topLeft(), scaleFactor, snappedXToGrid, snappedYToGrid );
96 if ( snappedXToGrid && snapVertical )
99 snappedRect.setLeft( snappedRect.left() + res.x() );
101 if ( snappedYToGrid && snapHorizontal )
104 snappedRect.setTop( snappedRect.top() + res.y() );
106 res =
snapPointsToGrid( QList<QPointF>() << rect.bottomRight(), scaleFactor, snappedXToGrid, snappedYToGrid );
107 if ( snappedXToGrid && snapVertical )
110 snappedRect.setRight( snappedRect.right() + res.x() );
112 if ( snappedYToGrid && snapHorizontal )
115 snappedRect.setBottom( snappedRect.bottom() + res.y() );
123 const QPointF delta =
snapPointsToGrid( QList<QPointF>() << point, scaleFactor, snappedX, snappedY );
124 return point + delta;
133 return QPointF( 0, 0 );
138 return QPointF( 0, 0 );
143 double smallestDiffX = std::numeric_limits<double>::max();
144 double smallestDiffY = std::numeric_limits<double>::max();
145 for (
const QPointF point : points )
148 const double gridRes = 30;
149 int xRatio =
static_cast<int>( ( point.x() ) / gridRes + 0.5 );
150 int yRatio =
static_cast<int>( ( point.y() ) / gridRes + 0.5 );
152 const double xSnapped = xRatio * gridRes;
153 const double ySnapped = yRatio * gridRes;
155 const double currentDiffX = std::fabs( xSnapped - point.x() );
156 if ( currentDiffX < smallestDiffX )
158 smallestDiffX = currentDiffX;
159 deltaX = xSnapped - point.x();
162 const double currentDiffY = std::fabs( ySnapped - point.y() );
163 if ( currentDiffY < smallestDiffY )
165 smallestDiffY = currentDiffY;
166 deltaY = ySnapped - point.y();
171 const double alignThreshold = mTolerance / scaleFactor;
173 QPointF delta( 0, 0 );
174 if ( smallestDiffX <= alignThreshold )
178 delta.setX( deltaX );
180 if ( smallestDiffY <= alignThreshold )
184 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.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.