27using namespace Qt::StringLiterals;
48 mSnapToGrid = enabled;
53 mSnapToGuides = enabled;
58 mSnapToItems = enabled;
62 QPointF point,
double scaleFactor,
bool &snapped, QGraphicsLineItem *horizontalSnapLine, QGraphicsLineItem *verticalSnapLine,
const QList< QgsLayoutItem * > *ignoreItems
68 bool snappedXToGuides =
false;
69 double newX =
snapPointToGuides( point.x(), Qt::Vertical, scaleFactor, snappedXToGuides );
70 if ( snappedXToGuides )
74 if ( verticalSnapLine )
75 verticalSnapLine->setVisible(
false );
77 bool snappedYToGuides =
false;
78 double newY =
snapPointToGuides( point.y(), Qt::Horizontal, scaleFactor, snappedYToGuides );
79 if ( snappedYToGuides )
83 if ( horizontalSnapLine )
84 horizontalSnapLine->setVisible(
false );
87 bool snappedXToItems =
false;
88 bool snappedYToItems =
false;
89 if ( !snappedXToGuides )
91 newX =
snapPointToItems( point.x(), Qt::Horizontal, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedXToItems, verticalSnapLine );
92 if ( snappedXToItems )
98 if ( !snappedYToGuides )
100 newY =
snapPointToItems( point.y(), Qt::Vertical, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedYToItems, horizontalSnapLine );
101 if ( snappedYToItems )
108 bool snappedXToGrid =
false;
109 bool snappedYToGrid =
false;
110 QPointF res =
snapPointToGrid( point, scaleFactor, snappedXToGrid, snappedYToGrid );
111 if ( snappedXToGrid && !snappedXToGuides && !snappedXToItems )
114 point.setX( res.x() );
116 if ( snappedYToGrid && !snappedYToGuides && !snappedYToItems )
119 point.setY( res.y() );
126 const QRectF &rect,
double scaleFactor,
bool &snapped, QGraphicsLineItem *horizontalSnapLine, QGraphicsLineItem *verticalSnapLine,
const QList<QgsLayoutItem *> *ignoreItems
130 QRectF snappedRect = rect;
132 QList< double > xCoords;
133 xCoords << rect.left() << rect.center().x() << rect.right();
134 QList< double > yCoords;
135 yCoords << rect.top() << rect.center().y() << rect.bottom();
138 bool snappedXToGuides =
false;
139 double deltaX =
snapPointsToGuides( xCoords, Qt::Vertical, scaleFactor, snappedXToGuides );
140 if ( snappedXToGuides )
143 snappedRect.translate( deltaX, 0 );
144 if ( verticalSnapLine )
145 verticalSnapLine->setVisible(
false );
147 bool snappedYToGuides =
false;
148 double deltaY =
snapPointsToGuides( yCoords, Qt::Horizontal, scaleFactor, snappedYToGuides );
149 if ( snappedYToGuides )
152 snappedRect.translate( 0, deltaY );
153 if ( horizontalSnapLine )
154 horizontalSnapLine->setVisible(
false );
157 bool snappedXToItems =
false;
158 bool snappedYToItems =
false;
159 if ( !snappedXToGuides )
161 deltaX =
snapPointsToItems( xCoords, Qt::Horizontal, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedXToItems, verticalSnapLine );
162 if ( snappedXToItems )
165 snappedRect.translate( deltaX, 0 );
168 if ( !snappedYToGuides )
170 deltaY =
snapPointsToItems( yCoords, Qt::Vertical, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedYToItems, horizontalSnapLine );
171 if ( snappedYToItems )
174 snappedRect.translate( 0, deltaY );
178 bool snappedXToGrid =
false;
179 bool snappedYToGrid =
false;
180 QList< QPointF > points;
181 points << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
182 QPointF res =
snapPointsToGrid( points, scaleFactor, snappedXToGrid, snappedYToGrid );
183 if ( snappedXToGrid && !snappedXToGuides && !snappedXToItems )
186 snappedRect.translate( res.x(), 0 );
188 if ( snappedYToGrid && !snappedYToGuides && !snappedYToItems )
191 snappedRect.translate( 0, res.y() );
199 QPointF delta =
snapPointsToGrid( QList< QPointF >() << point, scaleFactor, snappedX, snappedY );
200 return point + delta;
207 if ( !mLayout || !mSnapToGrid )
209 return QPointF( 0, 0 );
213 return QPointF( 0, 0 );
217 double smallestDiffX = std::numeric_limits<double>::max();
218 double smallestDiffY = std::numeric_limits<double>::max();
219 for ( QPointF point : points )
222 QPointF pagePoint = mLayout->pageCollection()->positionOnPage( point );
224 double yPage = pagePoint.y();
225 double yAtTopOfPage = mLayout->pageCollection()->page( mLayout->pageCollection()->pageNumberForPoint( point ) )->pos().y();
228 double gridRes = mLayout->convertToLayoutUnits( grid.
resolution() );
229 QPointF gridOffset = mLayout->convertToLayoutUnits( grid.
offset() );
230 int xRatio =
static_cast< int >( ( point.x() - gridOffset.x() ) / gridRes + 0.5 );
231 int yRatio =
static_cast< int >( ( yPage - gridOffset.y() ) / gridRes + 0.5 );
233 double xSnapped = xRatio * gridRes + gridOffset.x();
234 double ySnapped = yRatio * gridRes + gridOffset.y() + yAtTopOfPage;
236 double currentDiffX = std::fabs( xSnapped - point.x() );
237 if ( currentDiffX < smallestDiffX )
239 smallestDiffX = currentDiffX;
240 deltaX = xSnapped - point.x();
243 double currentDiffY = std::fabs( ySnapped - point.y() );
244 if ( currentDiffY < smallestDiffY )
246 smallestDiffY = currentDiffY;
247 deltaY = ySnapped - point.y();
252 double alignThreshold = mTolerance / scaleFactor;
254 QPointF delta( 0, 0 );
255 if ( smallestDiffX <= alignThreshold )
259 delta.setX( deltaX );
261 if ( smallestDiffY <= alignThreshold )
265 delta.setY( deltaY );
273 double delta =
snapPointsToGuides( QList< double >() << original, orientation, scaleFactor, snapped );
274 return original + delta;
280 if ( !mLayout || !mSnapToGuides )
286 double alignThreshold = mTolerance / scaleFactor;
288 double bestDelta = 0;
289 double smallestDiff = std::numeric_limits<double>::max();
291 for (
double p : points )
293 const auto constGuides = mLayout->guides().guides( orientation );
296 double guidePos = guide->layoutPosition();
297 double diff = std::fabs( p - guidePos );
298 if ( diff < smallestDiff )
301 bestDelta = guidePos - p;
306 if ( smallestDiff <= alignThreshold )
317double QgsLayoutSnapper::snapPointToItems(
double original, Qt::Orientation orientation,
double scaleFactor,
const QList<QgsLayoutItem *> &ignoreItems,
bool &snapped, QGraphicsLineItem *snapLine )
const
319 double delta =
snapPointsToItems( QList< double >() << original, orientation, scaleFactor, ignoreItems, snapped, snapLine );
320 return original + delta;
324 const QList<double> &points, Qt::Orientation orientation,
double scaleFactor,
const QList<QgsLayoutItem *> &ignoreItems,
bool &snapped, QGraphicsLineItem *snapLine
328 if ( !mLayout || !mSnapToItems )
331 snapLine->setVisible(
false );
335 double alignThreshold = mTolerance / scaleFactor;
337 double bestDelta = 0;
338 double smallestDiff = std::numeric_limits<double>::max();
340 const QList<QGraphicsItem *> itemList = mLayout->items();
341 QList< double > currentCoords;
342 for ( QGraphicsItem *item : itemList )
345 if ( !currentItem || ignoreItems.contains( currentItem ) )
349 if ( !currentItem->isVisible() )
357 itemRect = currentItem->mapRectToScene( currentItem->rect() );
361 itemRect = currentItem->mapRectToScene( currentItem->
rectWithFrame() );
364 currentCoords.clear();
365 switch ( orientation )
369 currentCoords << itemRect.left();
370 currentCoords << itemRect.right();
371 currentCoords << itemRect.center().x();
377 currentCoords << itemRect.top();
378 currentCoords << itemRect.center().y();
379 currentCoords << itemRect.bottom();
384 for (
double val : std::as_const( currentCoords ) )
386 for (
double p : points )
388 double dist = std::fabs( p - val );
389 if ( dist <= alignThreshold && dist < smallestDiff )
404 snapLine->setVisible(
true );
405 switch ( orientation )
409 snapLine->setLine( QLineF( -100000, closest, 100000, closest ) );
415 snapLine->setLine( QLineF( closest, -100000, closest, 100000 ) );
422 snapLine->setVisible(
false );
432 QDomElement element = document.createElement( u
"Snapper"_s );
434 element.setAttribute( u
"tolerance"_s, mTolerance );
435 element.setAttribute( u
"snapToGrid"_s, mSnapToGrid );
436 element.setAttribute( u
"snapToGuides"_s, mSnapToGuides );
437 element.setAttribute( u
"snapToItems"_s, mSnapToItems );
439 parentElement.appendChild( element );
445 QDomElement element = e;
446 if ( element.nodeName() !=
"Snapper"_L1 )
448 element = element.firstChildElement( u
"Snapper"_s );
451 if ( element.nodeName() !=
"Snapper"_L1 )
456 mTolerance = element.attribute( u
"tolerance"_s, u
"5"_s ).toInt();
457 mSnapToGrid = element.attribute( u
"snapToGrid"_s, u
"0"_s ) !=
"0"_L1;
458 mSnapToGuides = element.attribute( u
"snapToGuides"_s, u
"0"_s ) !=
"0"_L1;
459 mSnapToItems = element.attribute( u
"snapToItems"_s, u
"0"_s ) !=
"0"_L1;
Contains settings relating to the appearance, spacing and offset for layout grids.
QgsLayoutMeasurement resolution() const
Returns the page/snap grid resolution.
QgsLayoutPoint offset() const
Returns the offset of the page/snap grid.
Contains the configuration for a single snap guide used by a layout.
Item representing the paper in a layout.
@ LayoutGroup
Grouped item.
Base class for graphical items within a QgsLayout.
virtual QRectF rectWithFrame() const
Returns the item's rectangular bounds, including any bleed caused by the item's frame.
int type() const override
Returns a unique graphics item type identifier.
double length() const
Returns the length of the measurement.
QPointF snapPoint(QPointF point, double scaleFactor, bool &snapped, QGraphicsLineItem *horizontalSnapLine=nullptr, QGraphicsLineItem *verticalSnapLine=nullptr, const QList< QgsLayoutItem * > *ignoreItems=nullptr) const
Snaps a layout coordinate point.
void setSnapToItems(bool enabled)
Sets whether snapping to items is enabled.
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, QGraphicsLineItem *horizontalSnapLine=nullptr, QGraphicsLineItem *verticalSnapLine=nullptr, const QList< QgsLayoutItem * > *ignoreItems=nullptr) const
Snaps a layout coordinate rect.
int snapTolerance() const
Returns the snap tolerance (in pixels) to use when snapping.
double snapPointsToItems(const QList< double > &points, Qt::Orientation orientation, double scaleFactor, const QList< QgsLayoutItem * > &ignoreItems, bool &snapped, QGraphicsLineItem *snapLine=nullptr) const
Snaps a set of points to the item bounds.
bool readXml(const QDomElement &gridElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets the snapper's state from a DOM element.
void setSnapToGuides(bool enabled)
Sets whether snapping to guides is enabled.
QgsLayoutSnapper(QgsLayout *layout)
Constructor for QgsLayoutSnapper, attached to the specified layout.
void setSnapTolerance(int snapTolerance)
Sets the snap tolerance (in pixels) to use when snapping.
double snapPointsToGuides(const QList< double > &points, Qt::Orientation orientation, double scaleFactor, bool &snapped) const
Snaps a set of points to the guides.
void setSnapToGrid(bool enabled)
Sets whether snapping to grid is enabled.
QgsLayout * layout() override
Returns the layout the object belongs to.
double snapPointToItems(double original, Qt::Orientation orientation, double scaleFactor, const QList< QgsLayoutItem * > &ignoreItems, bool &snapped, QGraphicsLineItem *snapLine=nullptr) const
Snaps an original layout coordinate to the item bounds.
double snapPointToGuides(double original, Qt::Orientation orientation, double scaleFactor, bool &snapped) const
Snaps an original layout coordinate to the guides.
QPointF snapPointToGrid(QPointF point, double scaleFactor, bool &snappedX, bool &snappedY) const
Snaps a layout coordinate point to the grid.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores the snapper's state in a DOM element.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
A container for the context for various read/write operations on objects.
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.