21
21
QgsLayoutMeasurement ,
22
22
QgsUnitTypes ,
23
23
QgsLayoutPoint ,
24
- QgsLayoutItemPage )
24
+ QgsLayoutItemPage ,
25
+ QgsLayoutGuide )
25
26
from qgis .PyQt .QtCore import QPointF
26
- from qgis .PyQt .QtGui import (QPen ,
27
- QColor )
28
27
29
28
from qgis .testing import start_app , unittest
30
29
@@ -65,58 +64,112 @@ def testSnapPointToGrid(self):
65
64
s .setSnapToGrid (True )
66
65
s .setSnapTolerance (1 )
67
66
68
- point , snapped = s .snapPointToGrid (QPointF (1 , 1 ), 1 )
69
- self .assertTrue (snapped )
67
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (1 , 1 ), 1 )
68
+ self .assertTrue (snappedX )
69
+ self .assertTrue (snappedY )
70
70
self .assertEqual (point , QPointF (0 , 0 ))
71
71
72
- point , snapped = s .snapPointToGrid (QPointF (9 , 1 ), 1 )
73
- self .assertTrue (snapped )
72
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (9 , 1 ), 1 )
73
+ self .assertTrue (snappedX )
74
+ self .assertTrue (snappedY )
74
75
self .assertEqual (point , QPointF (10 , 0 ))
75
76
76
- point , snapped = s .snapPointToGrid (QPointF (1 , 11 ), 1 )
77
- self .assertTrue (snapped )
77
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (1 , 11 ), 1 )
78
+ self .assertTrue (snappedX )
79
+ self .assertTrue (snappedY )
78
80
self .assertEqual (point , QPointF (0 , 10 ))
79
81
80
- point , snapped = s .snapPointToGrid (QPointF (13 , 11 ), 1 )
81
- self .assertTrue (snapped )
82
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (13 , 11 ), 1 )
83
+ self .assertFalse (snappedX )
84
+ self .assertTrue (snappedY )
82
85
self .assertEqual (point , QPointF (13 , 10 ))
83
86
84
- point , snapped = s .snapPointToGrid (QPointF (11 , 13 ), 1 )
85
- self .assertTrue (snapped )
87
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (11 , 13 ), 1 )
88
+ self .assertTrue (snappedX )
89
+ self .assertFalse (snappedY )
86
90
self .assertEqual (point , QPointF (10 , 13 ))
87
91
88
- point , snapped = s .snapPointToGrid (QPointF (13 , 23 ), 1 )
89
- self .assertFalse (snapped )
92
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (13 , 23 ), 1 )
93
+ self .assertFalse (snappedX )
94
+ self .assertFalse (snappedY )
90
95
self .assertEqual (point , QPointF (13 , 23 ))
91
96
92
97
# grid disabled
93
98
s .setSnapToGrid (False )
94
- point , snapped = s .snapPointToGrid (QPointF (1 , 1 ), 1 )
95
- self .assertFalse (snapped )
99
+ point , nappedX , snappedY = s .snapPointToGrid (QPointF (1 , 1 ), 1 )
100
+ self .assertFalse (nappedX )
101
+ self .assertFalse (snappedY )
96
102
self .assertEqual (point , QPointF (1 , 1 ))
97
103
s .setSnapToGrid (True )
98
104
99
105
# with different pixel scale
100
- point , snapped = s .snapPointToGrid (QPointF (0.5 , 0.5 ), 1 )
101
- self .assertTrue (snapped )
106
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (0.5 , 0.5 ), 1 )
107
+ self .assertTrue (snappedX )
108
+ self .assertTrue (snappedY )
102
109
self .assertEqual (point , QPointF (0 , 0 ))
103
- point , snapped = s .snapPointToGrid (QPointF (0.5 , 0.5 ), 3 )
104
- self .assertFalse (snapped )
110
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (0.5 , 0.5 ), 3 )
111
+ self .assertFalse (snappedX )
112
+ self .assertFalse (snappedY )
105
113
self .assertEqual (point , QPointF (0.5 , 0.5 ))
106
114
107
115
# with offset grid
108
116
l .gridSettings ().setOffset (QgsLayoutPoint (2 , 0 ))
109
- point , snapped = s .snapPointToGrid (QPointF (13 , 23 ), 1 )
110
- self .assertTrue (snapped )
117
+ point , snappedX , snappedY = s .snapPointToGrid (QPointF (13 , 23 ), 1 )
118
+ self .assertTrue (snappedX )
119
+ self .assertFalse (snappedY )
111
120
self .assertEqual (point , QPointF (12 , 23 ))
112
121
122
+ def testSnapPointToGuides (self ):
123
+ p = QgsProject ()
124
+ l = QgsLayout (p )
125
+ page = QgsLayoutItemPage (l )
126
+ page .setPageSize ('A4' )
127
+ l .pageCollection ().addPage (page )
128
+ s = QgsLayoutSnapper (l )
129
+ guides = l .guides ()
130
+
131
+ s .setSnapToGuides (True )
132
+ s .setSnapTolerance (1 )
133
+
134
+ # no guides
135
+ point , snapped = s .snapPointToGuides (0.5 , QgsLayoutGuide .Vertical , 1 )
136
+ self .assertFalse (snapped )
137
+
138
+ guides .addGuide (QgsLayoutGuide (QgsLayoutGuide .Vertical , QgsLayoutMeasurement (1 )))
139
+ point , snapped = s .snapPointToGuides (0.5 , QgsLayoutGuide .Vertical , 1 )
140
+ self .assertTrue (snapped )
141
+ self .assertEqual (point , 1 )
142
+
143
+ # outside tolerance
144
+ point , snapped = s .snapPointToGuides (5.5 , QgsLayoutGuide .Vertical , 1 )
145
+ self .assertFalse (snapped )
146
+
147
+ # snapping off
148
+ s .setSnapToGuides (False )
149
+ point , snapped = s .snapPointToGuides (0.5 , QgsLayoutGuide .Vertical , 1 )
150
+ self .assertFalse (snapped )
151
+
152
+ s .setSnapToGuides (True )
153
+ # snap to hoz
154
+ point , snapped = s .snapPointToGuides (0.5 , QgsLayoutGuide .Horizontal , 1 )
155
+ self .assertFalse (snapped )
156
+ guides .addGuide (QgsLayoutGuide (QgsLayoutGuide .Horizontal , QgsLayoutMeasurement (1 )))
157
+ point , snapped = s .snapPointToGuides (0.5 , QgsLayoutGuide .Horizontal , 1 )
158
+ self .assertTrue (snapped )
159
+ self .assertEqual (point , 1 )
160
+
161
+ # with different pixel scale
162
+ point , snapped = s .snapPointToGuides (0.5 , QgsLayoutGuide .Horizontal , 3 )
163
+ self .assertFalse (snapped )
164
+
113
165
def testSnapPoint (self ):
114
166
p = QgsProject ()
115
167
l = QgsLayout (p )
116
168
page = QgsLayoutItemPage (l )
117
169
page .setPageSize ('A4' )
118
170
l .pageCollection ().addPage (page )
119
171
s = QgsLayoutSnapper (l )
172
+ guides = l .guides ()
120
173
121
174
# first test snapping to grid
122
175
l .gridSettings ().setResolution (QgsLayoutMeasurement (5 , QgsUnitTypes .LayoutMillimeters ))
@@ -132,6 +185,14 @@ def testSnapPoint(self):
132
185
self .assertFalse (snapped )
133
186
self .assertEqual (point , QPointF (1 , 1 ))
134
187
188
+ # test that guide takes precedence
189
+ s .setSnapToGrid (True )
190
+ s .setSnapToGuides (True )
191
+ guides .addGuide (QgsLayoutGuide (QgsLayoutGuide .Horizontal , QgsLayoutMeasurement (0.5 )))
192
+ point , snapped = s .snapPoint (QPointF (1 , 1 ), 1 )
193
+ self .assertTrue (snapped )
194
+ self .assertEqual (point , QPointF (0 , 0.5 ))
195
+
135
196
136
197
if __name__ == '__main__' :
137
198
unittest .main ()
0 commit comments