/
qgslayoutsnapper.cpp
452 lines (399 loc) · 13.9 KB
/
qgslayoutsnapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/***************************************************************************
qgslayoutsnapper.cpp
--------------------
begin : July 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgslayoutsnapper.h"
#include "qgslayout.h"
#include "qgsreadwritecontext.h"
#include "qgsproject.h"
#include "qgslayoutpagecollection.h"
#include "qgssettings.h"
QgsLayoutSnapper::QgsLayoutSnapper( QgsLayout *layout )
: mLayout( layout )
{
QgsSettings s;
mTolerance = s.value( QStringLiteral( "LayoutDesigner/defaultSnapTolerancePixels" ), 5, QgsSettings::Gui ).toInt();
}
QgsLayout *QgsLayoutSnapper::layout()
{
return mLayout;
}
void QgsLayoutSnapper::setSnapTolerance( const int snapTolerance )
{
mTolerance = snapTolerance;
}
void QgsLayoutSnapper::setSnapToGrid( bool enabled )
{
mSnapToGrid = enabled;
}
void QgsLayoutSnapper::setSnapToGuides( bool enabled )
{
mSnapToGuides = enabled;
}
void QgsLayoutSnapper::setSnapToItems( bool enabled )
{
mSnapToItems = enabled;
}
QPointF QgsLayoutSnapper::snapPoint( QPointF point, double scaleFactor, bool &snapped, QGraphicsLineItem *horizontalSnapLine, QGraphicsLineItem *verticalSnapLine,
const QList< QgsLayoutItem * > *ignoreItems ) const
{
snapped = false;
// highest priority - guides
bool snappedXToGuides = false;
double newX = snapPointToGuides( point.x(), Qt::Vertical, scaleFactor, snappedXToGuides );
if ( snappedXToGuides )
{
snapped = true;
point.setX( newX );
if ( verticalSnapLine )
verticalSnapLine->setVisible( false );
}
bool snappedYToGuides = false;
double newY = snapPointToGuides( point.y(), Qt::Horizontal, scaleFactor, snappedYToGuides );
if ( snappedYToGuides )
{
snapped = true;
point.setY( newY );
if ( horizontalSnapLine )
horizontalSnapLine->setVisible( false );
}
bool snappedXToItems = false;
bool snappedYToItems = false;
if ( !snappedXToGuides )
{
newX = snapPointToItems( point.x(), Qt::Horizontal, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedXToItems, verticalSnapLine );
if ( snappedXToItems )
{
snapped = true;
point.setX( newX );
}
}
if ( !snappedYToGuides )
{
newY = snapPointToItems( point.y(), Qt::Vertical, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedYToItems, horizontalSnapLine );
if ( snappedYToItems )
{
snapped = true;
point.setY( newY );
}
}
bool snappedXToGrid = false;
bool snappedYToGrid = false;
QPointF res = snapPointToGrid( point, scaleFactor, snappedXToGrid, snappedYToGrid );
if ( snappedXToGrid && !snappedXToGuides && !snappedXToItems )
{
snapped = true;
point.setX( res.x() );
}
if ( snappedYToGrid && !snappedYToGuides && !snappedYToItems )
{
snapped = true;
point.setY( res.y() );
}
return point;
}
QRectF QgsLayoutSnapper::snapRect( const QRectF &rect, double scaleFactor, bool &snapped, QGraphicsLineItem *horizontalSnapLine, QGraphicsLineItem *verticalSnapLine, const QList<QgsLayoutItem *> *ignoreItems ) const
{
snapped = false;
QRectF snappedRect = rect;
QList< double > xCoords;
xCoords << rect.left() << rect.center().x() << rect.right();
QList< double > yCoords;
yCoords << rect.top() << rect.center().y() << rect.bottom();
// highest priority - guides
bool snappedXToGuides = false;
double deltaX = snapPointsToGuides( xCoords, Qt::Vertical, scaleFactor, snappedXToGuides );
if ( snappedXToGuides )
{
snapped = true;
snappedRect.translate( deltaX, 0 );
if ( verticalSnapLine )
verticalSnapLine->setVisible( false );
}
bool snappedYToGuides = false;
double deltaY = snapPointsToGuides( yCoords, Qt::Horizontal, scaleFactor, snappedYToGuides );
if ( snappedYToGuides )
{
snapped = true;
snappedRect.translate( 0, deltaY );
if ( horizontalSnapLine )
horizontalSnapLine->setVisible( false );
}
bool snappedXToItems = false;
bool snappedYToItems = false;
if ( !snappedXToGuides )
{
deltaX = snapPointsToItems( xCoords, Qt::Horizontal, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedXToItems, verticalSnapLine );
if ( snappedXToItems )
{
snapped = true;
snappedRect.translate( deltaX, 0 );
}
}
if ( !snappedYToGuides )
{
deltaY = snapPointsToItems( yCoords, Qt::Vertical, scaleFactor, ignoreItems ? *ignoreItems : QList< QgsLayoutItem * >(), snappedYToItems, horizontalSnapLine );
if ( snappedYToItems )
{
snapped = true;
snappedRect.translate( 0, deltaY );
}
}
bool snappedXToGrid = false;
bool snappedYToGrid = false;
QList< QPointF > points;
points << rect.topLeft() << rect.topRight() << rect.bottomLeft() << rect.bottomRight();
QPointF res = snapPointsToGrid( points, scaleFactor, snappedXToGrid, snappedYToGrid );
if ( snappedXToGrid && !snappedXToGuides && !snappedXToItems )
{
snapped = true;
snappedRect.translate( res.x(), 0 );
}
if ( snappedYToGrid && !snappedYToGuides && !snappedYToItems )
{
snapped = true;
snappedRect.translate( 0, res.y() );
}
return snappedRect;
}
QPointF QgsLayoutSnapper::snapPointToGrid( QPointF point, double scaleFactor, bool &snappedX, bool &snappedY ) const
{
QPointF delta = snapPointsToGrid( QList< QPointF >() << point, scaleFactor, snappedX, snappedY );
return point + delta;
}
QPointF QgsLayoutSnapper::snapPointsToGrid( const QList<QPointF> &points, double scaleFactor, bool &snappedX, bool &snappedY ) const
{
snappedX = false;
snappedY = false;
if ( !mLayout || !mSnapToGrid )
{
return QPointF( 0, 0 );
}
const QgsLayoutGridSettings &grid = mLayout->gridSettings();
if ( grid.resolution().length() <= 0 )
return QPointF( 0, 0 );
double deltaX = 0;
double deltaY = 0;
double smallestDiffX = std::numeric_limits<double>::max();
double smallestDiffY = std::numeric_limits<double>::max();
for ( QPointF point : points )
{
//calculate y offset to current page
QPointF pagePoint = mLayout->pageCollection()->positionOnPage( point );
double yPage = pagePoint.y(); //y-coordinate relative to current page
double yAtTopOfPage = mLayout->pageCollection()->page( mLayout->pageCollection()->pageNumberForPoint( point ) )->pos().y();
//snap x coordinate
double gridRes = mLayout->convertToLayoutUnits( grid.resolution() );
QPointF gridOffset = mLayout->convertToLayoutUnits( grid.offset() );
int xRatio = static_cast< int >( ( point.x() - gridOffset.x() ) / gridRes + 0.5 ); //NOLINT
int yRatio = static_cast< int >( ( yPage - gridOffset.y() ) / gridRes + 0.5 ); //NOLINT
double xSnapped = xRatio * gridRes + gridOffset.x();
double ySnapped = yRatio * gridRes + gridOffset.y() + yAtTopOfPage;
double currentDiffX = std::fabs( xSnapped - point.x() );
if ( currentDiffX < smallestDiffX )
{
smallestDiffX = currentDiffX;
deltaX = xSnapped - point.x();
}
double currentDiffY = std::fabs( ySnapped - point.y() );
if ( currentDiffY < smallestDiffY )
{
smallestDiffY = currentDiffY;
deltaY = ySnapped - point.y();
}
}
//convert snap tolerance from pixels to layout units
double alignThreshold = mTolerance / scaleFactor;
QPointF delta( 0, 0 );
if ( smallestDiffX <= alignThreshold )
{
//snap distance is inside of tolerance
snappedX = true;
delta.setX( deltaX );
}
if ( smallestDiffY <= alignThreshold )
{
//snap distance is inside of tolerance
snappedY = true;
delta.setY( deltaY );
}
return delta;
}
double QgsLayoutSnapper::snapPointToGuides( double original, Qt::Orientation orientation, double scaleFactor, bool &snapped ) const
{
double delta = snapPointsToGuides( QList< double >() << original, orientation, scaleFactor, snapped );
return original + delta;
}
double QgsLayoutSnapper::snapPointsToGuides( const QList<double> &points, Qt::Orientation orientation, double scaleFactor, bool &snapped ) const
{
snapped = false;
if ( !mLayout || !mSnapToGuides )
{
return 0;
}
//convert snap tolerance from pixels to layout units
double alignThreshold = mTolerance / scaleFactor;
double bestDelta = 0;
double smallestDiff = std::numeric_limits<double>::max();
for ( double p : points )
{
const auto constGuides = mLayout->guides().guides( orientation );
for ( QgsLayoutGuide *guide : constGuides )
{
double guidePos = guide->layoutPosition();
double diff = std::fabs( p - guidePos );
if ( diff < smallestDiff )
{
smallestDiff = diff;
bestDelta = guidePos - p;
}
}
}
if ( smallestDiff <= alignThreshold )
{
snapped = true;
return bestDelta;
}
else
{
return 0;
}
}
double QgsLayoutSnapper::snapPointToItems( double original, Qt::Orientation orientation, double scaleFactor, const QList<QgsLayoutItem *> &ignoreItems, bool &snapped,
QGraphicsLineItem *snapLine ) const
{
double delta = snapPointsToItems( QList< double >() << original, orientation, scaleFactor, ignoreItems, snapped, snapLine );
return original + delta;
}
double QgsLayoutSnapper::snapPointsToItems( const QList<double> &points, Qt::Orientation orientation, double scaleFactor, const QList<QgsLayoutItem *> &ignoreItems, bool &snapped, QGraphicsLineItem *snapLine ) const
{
snapped = false;
if ( !mLayout || !mSnapToItems )
{
if ( snapLine )
snapLine->setVisible( false );
return 0;
}
double alignThreshold = mTolerance / scaleFactor;
double bestDelta = 0;
double smallestDiff = std::numeric_limits<double>::max();
double closest = 0;
const QList<QGraphicsItem *> itemList = mLayout->items();
QList< double > currentCoords;
for ( QGraphicsItem *item : itemList )
{
QgsLayoutItem *currentItem = dynamic_cast< QgsLayoutItem *>( item );
if ( !currentItem || ignoreItems.contains( currentItem ) )
continue;
if ( currentItem->type() == QgsLayoutItemRegistry::LayoutGroup )
continue; // don't snap to group bounds, instead we snap to group item bounds
if ( !currentItem->isVisible() )
continue; // don't snap to invisible items
QRectF itemRect;
if ( dynamic_cast<const QgsLayoutItemPage *>( currentItem ) )
{
//if snapping to paper use the paper item's rect rather then the bounding rect,
//since we want to snap to the page edge and not any outlines drawn around the page
itemRect = currentItem->mapRectToScene( currentItem->rect() );
}
else
{
itemRect = currentItem->mapRectToScene( currentItem->rectWithFrame() );
}
currentCoords.clear();
switch ( orientation )
{
case Qt::Horizontal:
{
currentCoords << itemRect.left();
currentCoords << itemRect.right();
currentCoords << itemRect.center().x();
break;
}
case Qt::Vertical:
{
currentCoords << itemRect.top();
currentCoords << itemRect.center().y();
currentCoords << itemRect.bottom();
break;
}
}
for ( double val : qgis::as_const( currentCoords ) )
{
for ( double p : points )
{
double dist = std::fabs( p - val );
if ( dist <= alignThreshold && dist < smallestDiff )
{
snapped = true;
smallestDiff = dist;
bestDelta = val - p;
closest = val;
}
}
}
}
if ( snapLine )
{
if ( snapped )
{
snapLine->setVisible( true );
switch ( orientation )
{
case Qt::Vertical:
{
snapLine->setLine( QLineF( -100000, closest, 100000, closest ) );
break;
}
case Qt::Horizontal:
{
snapLine->setLine( QLineF( closest, -100000, closest, 100000 ) );
break;
}
}
}
else
{
snapLine->setVisible( false );
}
}
return bestDelta;
}
bool QgsLayoutSnapper::writeXml( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext & ) const
{
QDomElement element = document.createElement( QStringLiteral( "Snapper" ) );
element.setAttribute( QStringLiteral( "tolerance" ), mTolerance );
element.setAttribute( QStringLiteral( "snapToGrid" ), mSnapToGrid );
element.setAttribute( QStringLiteral( "snapToGuides" ), mSnapToGuides );
element.setAttribute( QStringLiteral( "snapToItems" ), mSnapToItems );
parentElement.appendChild( element );
return true;
}
bool QgsLayoutSnapper::readXml( const QDomElement &e, const QDomDocument &, const QgsReadWriteContext & )
{
QDomElement element = e;
if ( element.nodeName() != QStringLiteral( "Snapper" ) )
{
element = element.firstChildElement( QStringLiteral( "Snapper" ) );
}
if ( element.nodeName() != QStringLiteral( "Snapper" ) )
{
return false;
}
mTolerance = element.attribute( QStringLiteral( "tolerance" ), QStringLiteral( "5" ) ).toInt();
mSnapToGrid = element.attribute( QStringLiteral( "snapToGrid" ), QStringLiteral( "0" ) ) != QLatin1String( "0" );
mSnapToGuides = element.attribute( QStringLiteral( "snapToGuides" ), QStringLiteral( "0" ) ) != QLatin1String( "0" );
mSnapToItems = element.attribute( QStringLiteral( "snapToItems" ), QStringLiteral( "0" ) ) != QLatin1String( "0" );
return true;
}