Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
snap-preferences.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Storing of snapping preferences.
4 *
5 * Authors:
6 * Diederik van Lierop <mail@diedenrezi.nl>
7 *
8 * Copyright (C) 2008 - 2011 Authors
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include <cassert>
14#include <cstddef> // for size_t
15#include <glib.h> // for g_assert
16
17#include "snap-preferences.h"
18
20 _snap_enabled_globally(true),
21 _snap_postponed_globally(false),
22 _strict_snapping(true)
23{
24 // Check for powers of two; see the comments in snap-enums.h
31
32 for (int & _active_snap_target : _active_snap_targets) {
33 _active_snap_target = -1;
34 }
36
37 for (bool& b : _simple_snapping) {
38 b = false;
39 }
40}
41
43 auto index = static_cast<size_t>(option);
45 return _simple_snapping[index];
46}
47
49 auto index = static_cast<size_t>(option);
51 _simple_snapping[index] = enable;
52}
53
58
64
65void Inkscape::SnapPreferences::_mapTargetToArrayIndex(Inkscape::SnapTargetType &target, bool &always_on, bool &group_on) const
66{
67 if (target == SNAPTARGET_BBOX_CATEGORY ||
68 target == SNAPTARGET_NODE_CATEGORY ||
73 // These main targets should be handled separately, because otherwise we might call isTargetSnappable()
74 // for them (to check whether the corresponding group is on) which would lead to an infinite recursive loop
75 always_on = (target == SNAPTARGET_DATUMS_CATEGORY);
76 group_on = true;
77 return;
78 }
79
80 if (target & SNAPTARGET_BBOX_CATEGORY) {
81 group_on = isTargetSnappable(SNAPTARGET_BBOX_CATEGORY); // Only if the group with bbox sources/targets has been enabled, then we might snap to any of the bbox targets
82 return;
83 }
84
85 if (target & SNAPTARGET_NODE_CATEGORY) {
86 group_on = isTargetSnappable(SNAPTARGET_NODE_CATEGORY); // Only if the group with path/node sources/targets has been enabled, then we might snap to any of the nodes/paths
87 switch (target) {
89 target = SNAPTARGET_NODE_CUSP;
90 break;
93 break;
96 break;
97 // case SNAPTARGET_PATH_PERPENDICULAR:
98 // case SNAPTARGET_PATH_TANGENTIAL:
99 // target = SNAPTARGET_PATH;
100 break;
101 default:
102 break;
103 }
104
105 return;
106 }
107
108 if (target & SNAPTARGET_DATUMS_CATEGORY) {
109 group_on = true; // These snap targets cannot be disabled as part of a disabled group;
110 switch (target) {
111 // Some snap targets don't have their own toggle. These targets are called "secondary targets". We will re-map
112 // them to their cousin which does have a toggle, and which is called a "primary target"
115 target = SNAPTARGET_GRID;
116 break;
120 target = SNAPTARGET_GUIDE;
121 break;
125 break;
126
130 break;
131
134 break;
135
136
137 // Some snap targets cannot be toggled at all, and are therefore always enabled
139 always_on = true; // Doesn't have it's own button
140 break;
141
142 // These are only listed for completeness
143 case SNAPTARGET_GRID:
145 case SNAPTARGET_GUIDE:
150 break;
151 default:
152 g_warning("Snap-preferences warning: Undefined snap target (#%i)", target);
153 break;
154 }
155 return;
156 }
157
158 if (target & SNAPTARGET_ALIGNMENT_CATEGORY) {
159 group_on = isTargetSnappable(SNAPTARGET_ALIGNMENT_CATEGORY);
160 return;
161 }
162
164 group_on = isTargetSnappable(SNAPTARGET_DISTRIBUTION_CATEGORY);
165 return;
166 }
167
168 if (target & SNAPTARGET_OTHERS_CATEGORY) {
169 // Only if the group with "other" snap sources/targets has been enabled, then we might snap to any of those targets
170 // ... but this doesn't hold for the page border, grids, and guides
171 group_on = isTargetSnappable(SNAPTARGET_OTHERS_CATEGORY);
172 switch (target) {
173 // Some snap targets don't have their own toggle. These targets are called "secondary targets". We will re-map
174 // them to their cousin which does have a toggle, and which is called a "primary target"
177 break;
178
179 case SNAPTARGET_IMG_CORNER: // Doesn't have its own button, on if the group is on
181 break;
182 // Some snap targets cannot be toggled at all, and are therefore always enabled
185 always_on = true; // Doesn't have it's own button
186 break;
187
188 // These are only listed for completeness
193 break;
194 default:
195 g_warning("Snap-preferences warning: Undefined snap target (#%i)", target);
196 break;
197 }
198
199 return;
200 }
201
202 if (target == SNAPTARGET_UNDEFINED ) {
203 g_warning("Snap-preferences warning: Undefined snaptarget (#%i)", target);
204 } else {
205 g_warning("Snap-preferences warning: Snaptarget not handled (#%i)", target);
206 }
207}
208
210{
211 bool always_on = false;
212 bool group_on = false; // Only needed as a dummy
214
215 _mapTargetToArrayIndex(index, always_on, group_on);
216
217 if (always_on) { // If true, then this snap target is always active and cannot be toggled
218 // Catch coding errors
219 g_warning("Snap-preferences warning: Trying to enable/disable a snap target (#%i) that's always on by definition", index);
220 } else {
221 if (index == target) { // I.e. if it has not been re-mapped, then we have a primary target at hand
222 _active_snap_targets[index] = enabled;
223 } else { // If it has been re-mapped though, then this target does not have its own toggle button and should therefore not be set
224 g_warning("Snap-preferences warning: Trying to enable/disable a secondary snap target (#%i); only primary targets can be set", index);
225 }
226 }
227}
228
239{
240 bool always_on = false;
241 bool group_on = false; // Only needed as a dummy
243
244 _mapTargetToArrayIndex(index, always_on, group_on);
245
246 _active_mask_targets[index] = enabled;
247}
248
258{
259 for (int & _active_mask_targets : _active_mask_targets) {
260 _active_mask_targets = enabled;
261 }
262}
263
265{
266 bool always_on = false;
267 bool group_on = false;
269
270 _mapTargetToArrayIndex(index, always_on, group_on);
271
272 // Check masking first, it over-rides even group_on
273 if (_active_mask_targets[index] != -1) {
274 return _active_mask_targets[index];
275 }
276
277 if (group_on) { // If true, then this snap target is in a snap group that has been enabled (e.g. bbox group, nodes/paths group, or "others" group
278 if (always_on) { // If true, then this snap target is always active and cannot be toggled
279 return true;
280 } else {
281 if (_active_snap_targets[index] == -1) {
282 // Catch coding errors
283 g_warning("Snap-preferences warning: Using an uninitialized snap target setting (#%i)", index);
284 // This happens if setTargetSnappable() has not been called for this parameter, e.g. from within sp_namedview_set,
285 // or if this target index doesn't exist at all
286 }
287 return _active_snap_targets[index];
288 }
289 } else {
290 return false;
291 }
292}
293
295 return isTargetSnappable(target1) || isTargetSnappable(target2);
296}
297
299 return isTargetSnappable(target1) || isTargetSnappable(target2) || isTargetSnappable(target3);
300}
301
303 return isTargetSnappable(target1) || isTargetSnappable(target2) || isTargetSnappable(target3) || isTargetSnappable(target4);
304}
305
307 return isTargetSnappable(target1) || isTargetSnappable(target2) || isTargetSnappable(target3) || isTargetSnappable(target4) || isTargetSnappable(target5);
308}
309
311{
312 bool always_on = false; // Only needed as a dummy
313 bool group_on = false; // Only needed as a dummy
315
316 _mapTargetToArrayIndex(index, always_on, group_on);
317
318 if (_active_snap_targets[index] == -1) {
319 // Catch coding errors
320 g_warning("Snap-preferences warning: Using an uninitialized snap target setting (#%i)", index);
321 // This happens if setTargetSnappable() has not been called for this parameter, e.g. from within sp_namedview_set,
322 // or if this target index doesn't exist at all
323 } else {
324 if (index == target) { // I.e. if it has not been re-mapped, then we have a primary target at hand, which does have its own toggle button
325 return _active_snap_targets[index];
326 } else { // If it has been re-mapped though, then this target does not have its own toggle button and therefore the button status cannot be read
327 g_warning("Snap-preferences warning: Trying to determine the button status of a secondary snap target (#%i); However, only primary targets have a button", index);
328 }
329 }
330
331 return false;
332}
333
335{
336 switch (source)
337 {
364 case SNAPSOURCE_GUIDE:
365 return SNAPTARGET_GUIDE;
378
382 // For these snapsources there doesn't exist an equivalent snap target
385 return SNAPTARGET_GRID;
386
391
406
407 default:
408 g_warning("Mapping of snap source to snap target undefined (#%i)", source);
410 }
411}
412
414{
415 return isTargetSnappable(source2target(source));
416}
417
418
419/*
420 Local Variables:
421 mode:c++
422 c-file-style:"stroustrup"
423 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
424 indent-tabs-mode:nil
425 fill-column:99
426 End:
427*/
428// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
SimpleSnap option
bool _simple_snapping[static_cast< int >(Inkscape::SimpleSnap::_MaxEnumValue)]
bool isSourceSnappable(Inkscape::SnapSourceType const source) const
void setTargetSnappable(Inkscape::SnapTargetType const target, bool enabled)
int _active_snap_targets[Inkscape::SNAPTARGET_MAX_ENUM_VALUE]
void clearTargetMask(int enabled=-1)
Clear the target mask, this should be done in a four step process.
SnapTargetType source2target(SnapSourceType source) const
bool get_simple_snap(Inkscape::SimpleSnap option) const
bool isSnapButtonEnabled(Inkscape::SnapTargetType const target) const
void set_simple_snap(Inkscape::SimpleSnap option, bool enable)
bool isTargetSnappable(Inkscape::SnapTargetType const target) const
void setTargetMask(Inkscape::SnapTargetType const target, int enabled=1)
Set a target mask, which will turn off all other targets except the masked ones.
void _mapTargetToArrayIndex(Inkscape::SnapTargetType &target, bool &always_on, bool &group_on) const
Map snap target to array index.
SnapSourceType
enumerations of snap source types and snap target types.
Definition snap-enums.h:18
@ SNAPSOURCE_OTHERS_CATEGORY
Definition snap-enums.h:51
@ SNAPSOURCE_ALIGNMENT_BBOX_MIDPOINT
Definition snap-enums.h:63
@ SNAPSOURCE_NODE_CUSP
Definition snap-enums.h:37
@ SNAPSOURCE_BBOX_MIDPOINT
Definition snap-enums.h:26
@ SNAPSOURCE_GUIDE
Definition snap-enums.h:47
@ SNAPSOURCE_PATH_INTERSECTION
Definition snap-enums.h:39
@ SNAPSOURCE_UNDEFINED
Definition snap-enums.h:19
@ SNAPSOURCE_OTHER_HANDLE
Definition snap-enums.h:56
@ SNAPSOURCE_BBOX_CORNER
Definition snap-enums.h:25
@ SNAPSOURCE_ALIGNMENT_HANDLE
Definition snap-enums.h:67
@ SNAPSOURCE_TEXT_ANCHOR
Definition snap-enums.h:55
@ SNAPSOURCE_NODE_SMOOTH
Definition snap-enums.h:36
@ SNAPSOURCE_RECT_CORNER
Definition snap-enums.h:40
@ SNAPSOURCE_DATUMS_CATEGORY
Definition snap-enums.h:46
@ SNAPSOURCE_GUIDE_ORIGIN
Definition snap-enums.h:48
@ SNAPSOURCE_CONVEX_HULL_CORNER
Definition snap-enums.h:41
@ SNAPSOURCE_PAGE_CORNER
Definition snap-enums.h:31
@ SNAPSOURCE_ALIGNMENT_PAGE_CORNER
Definition snap-enums.h:66
@ SNAPSOURCE_ALIGNMENT_PAGE_CENTER
Definition snap-enums.h:65
@ SNAPSOURCE_ALIGNMENT_BBOX_CORNER
Definition snap-enums.h:62
@ SNAPSOURCE_GRID_PITCH
Definition snap-enums.h:57
@ SNAPSOURCE_NODE_CATEGORY
Definition snap-enums.h:35
@ SNAPSOURCE_OBJECT_MIDPOINT
Definition snap-enums.h:53
@ SNAPSOURCE_IMG_CORNER
Definition snap-enums.h:54
@ SNAPSOURCE_NODE_HANDLE
Definition snap-enums.h:43
@ SNAPSOURCE_LINE_MIDPOINT
Definition snap-enums.h:38
@ SNAPSOURCE_BBOX_EDGE_MIDPOINT
Definition snap-enums.h:27
@ SNAPSOURCE_PAGE_CENTER
Definition snap-enums.h:30
@ SNAPSOURCE_ELLIPSE_QUADRANT_POINT
Definition snap-enums.h:42
@ SNAPSOURCE_ALIGNMENT_CATEGORY
Definition snap-enums.h:61
@ SNAPSOURCE_ROTATION_CENTER
Definition snap-enums.h:52
@ SNAPSOURCE_BBOX_CATEGORY
Definition snap-enums.h:23
@ SNAPSOURCE_ALIGNMENT_BBOX_EDGE_MIDPOINT
Definition snap-enums.h:64
@ SNAPTARGET_GUIDE_ORIGIN
Definition snap-enums.h:102
@ SNAPTARGET_DATUMS_CATEGORY
Definition snap-enums.h:95
@ SNAPTARGET_BBOX_EDGE_MIDPOINT
Definition snap-enums.h:78
@ SNAPTARGET_UNDEFINED
Definition snap-enums.h:71
@ SNAPTARGET_PATH_GUIDE_INTERSECTION
Definition snap-enums.h:89
@ SNAPTARGET_RECT_CORNER
Definition snap-enums.h:93
@ SNAPTARGET_CONSTRAINT
Definition snap-enums.h:121
@ SNAPTARGET_PAGE_MARGIN_BORDER
Definition snap-enums.h:108
@ SNAPTARGET_NODE_CUSP
Definition snap-enums.h:83
@ SNAPTARGET_PAGE_MARGIN_CENTER
Definition snap-enums.h:109
@ SNAPTARGET_TEXT_ANCHOR
Definition snap-enums.h:118
@ SNAPTARGET_ROTATION_CENTER
Definition snap-enums.h:117
@ SNAPTARGET_LINE_MIDPOINT
Definition snap-enums.h:84
@ SNAPTARGET_GUIDE_PERPENDICULAR
Definition snap-enums.h:103
@ SNAPTARGET_ALIGNMENT_PAGE_EDGE_CENTER
Definition snap-enums.h:129
@ SNAPTARGET_OTHERS_CATEGORY
Definition snap-enums.h:114
@ SNAPTARGET_PAGE_BLEED_CORNER
Definition snap-enums.h:112
@ SNAPTARGET_BBOX_MIDPOINT
Definition snap-enums.h:79
@ SNAPTARGET_GRID_INTERSECTION
Definition snap-enums.h:98
@ SNAPTARGET_NODE_SMOOTH
Definition snap-enums.h:82
@ SNAPTARGET_ALIGNMENT_CATEGORY
Definition snap-enums.h:125
@ SNAPTARGET_PAGE_EDGE_CENTER
Definition snap-enums.h:106
@ SNAPTARGET_GUIDE
Definition snap-enums.h:100
@ SNAPTARGET_ELLIPSE_QUADRANT_POINT
Definition snap-enums.h:92
@ SNAPTARGET_DISTRIBUTION_CATEGORY
Definition snap-enums.h:139
@ SNAPTARGET_PAGE_EDGE_CORNER
Definition snap-enums.h:107
@ SNAPTARGET_ALIGNMENT_BBOX_EDGE_MIDPOINT
Definition snap-enums.h:128
@ SNAPTARGET_NODE_CATEGORY
Definition snap-enums.h:81
@ SNAPTARGET_PAGE_EDGE_BORDER
Definition snap-enums.h:105
@ SNAPTARGET_GRID
Definition snap-enums.h:96
@ SNAPTARGET_GRID_GUIDE_INTERSECTION
Definition snap-enums.h:104
@ SNAPTARGET_ALIGNMENT_HANDLE
Definition snap-enums.h:134
@ SNAPTARGET_TEXT_BASELINE
Definition snap-enums.h:119
@ SNAPTARGET_GRID_PERPENDICULAR
Definition snap-enums.h:99
@ SNAPTARGET_GUIDE_INTERSECTION
Definition snap-enums.h:101
@ SNAPTARGET_ALIGNMENT_PAGE_EDGE_CORNER
Definition snap-enums.h:130
@ SNAPTARGET_IMG_CORNER
Definition snap-enums.h:116
@ SNAPTARGET_ALIGNMENT_BBOX_CORNER
Definition snap-enums.h:126
@ SNAPTARGET_PAGE_MARGIN_CORNER
Definition snap-enums.h:110
@ SNAPTARGET_OBJECT_MIDPOINT
Definition snap-enums.h:115
@ SNAPTARGET_BBOX_CORNER
Definition snap-enums.h:76
@ SNAPTARGET_GRID_LINE
Definition snap-enums.h:97
@ SNAPTARGET_PATH_INTERSECTION
Definition snap-enums.h:88
@ SNAPTARGET_PAGE_BLEED_BORDER
Definition snap-enums.h:111
@ SNAPTARGET_BBOX_CATEGORY
Definition snap-enums.h:73
@ SNAPTARGET_CONSTRAINED_ANGLE
Definition snap-enums.h:120
int index