26 mTolerance = s.
value( QStringLiteral(
"/Processing/Modeler/snapTolerancePixels" ), 40 ).toInt();
36 mSnapToGrid = enabled;
43 bool snappedXToGrid =
false;
44 bool snappedYToGrid =
false;
45 const QPointF res =
snapPointToGrid( point, scaleFactor, snappedXToGrid, snappedYToGrid );
46 if ( snappedXToGrid && snapVertical )
49 point.setX( res.x() );
51 if ( snappedYToGrid && snapHorizontal )
54 point.setY( res.y() );
60QRectF
QgsModelSnapper::snapRect(
const QRectF &rect,
double scaleFactor,
bool &snapped,
bool snapHorizontal,
bool snapVertical )
const
63 QRectF snappedRect = rect;
65 bool snappedXToGrid =
false;
66 bool snappedYToGrid =
false;
67 QList<QPointF> points;
68 points << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
69 const QPointF res =
snapPointsToGrid( points, scaleFactor, snappedXToGrid, snappedYToGrid );
70 if ( snappedXToGrid && snapVertical )
73 snappedRect.translate( res.x(), 0 );
75 if ( snappedYToGrid && snapHorizontal )
78 snappedRect.translate( 0, res.y() );
87 QRectF snappedRect = rect;
89 bool snappedXToGrid =
false;
90 bool snappedYToGrid =
false;
91 QPointF res =
snapPointsToGrid( QList<QPointF>() << rect.topLeft(), scaleFactor, snappedXToGrid, snappedYToGrid );
92 if ( snappedXToGrid && snapVertical )
95 snappedRect.setLeft( snappedRect.left() + res.x() );
97 if ( snappedYToGrid && snapHorizontal )
100 snappedRect.setTop( snappedRect.top() + res.y() );
102 res =
snapPointsToGrid( QList<QPointF>() << rect.bottomRight(), scaleFactor, snappedXToGrid, snappedYToGrid );
103 if ( snappedXToGrid && snapVertical )
106 snappedRect.setRight( snappedRect.right() + res.x() );
108 if ( snappedYToGrid && snapHorizontal )
111 snappedRect.setBottom( snappedRect.bottom() + res.y() );
119 const QPointF delta =
snapPointsToGrid( QList<QPointF>() << point, scaleFactor, snappedX, snappedY );
120 return point + delta;
129 return QPointF( 0, 0 );
134 return QPointF( 0, 0 );
139 double smallestDiffX = std::numeric_limits<double>::max();
140 double smallestDiffY = std::numeric_limits<double>::max();
141 for (
const QPointF point : points )
144 const double gridRes = 30;
145 int xRatio =
static_cast<int>( ( point.x() ) / gridRes + 0.5 );
146 int yRatio =
static_cast<int>( ( point.y() ) / gridRes + 0.5 );
148 const double xSnapped = xRatio * gridRes;
149 const double ySnapped = yRatio * gridRes;
151 const double currentDiffX = std::fabs( xSnapped - point.x() );
152 if ( currentDiffX < smallestDiffX )
154 smallestDiffX = currentDiffX;
155 deltaX = xSnapped - point.x();
158 const double currentDiffY = std::fabs( ySnapped - point.y() );
159 if ( currentDiffY < smallestDiffY )
161 smallestDiffY = currentDiffY;
162 deltaY = ySnapped - point.y();
167 const double alignThreshold = mTolerance / scaleFactor;
169 QPointF delta( 0, 0 );
170 if ( smallestDiffX <= alignThreshold )
174 delta.setX( deltaX );
176 if ( smallestDiffY <= alignThreshold )
180 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.