c3198cd37ea0392724a7b8174d7c4a4a7a6e463c
1 /** \file
2 * Provides a class that shows a temporary indicator on the canvas of where the snap was, and what kind of snap
3 *
4 * Authors:
5 * Johan Engelen
6 * Diederik van Lierop
7 *
8 * Copyright (C) Johan Engelen 2009 <j.b.c.engelen@utwente.nl>
9 * Copyright (C) Diederik van Lierop 2010 <mail@diedenrezi.nl>
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #include "display/snap-indicator.h"
16 #include "desktop.h"
17 #include "desktop-handles.h"
18 #include "display/sodipodi-ctrl.h"
19 #include "display/sodipodi-ctrlrect.h"
20 #include "display/canvas-text.h"
21 #include "display/sp-canvas-util.h"
22 #include "knot.h"
23 #include "preferences.h"
24 #include <glibmm/i18n.h>
26 namespace Inkscape {
27 namespace Display {
29 SnapIndicator::SnapIndicator(SPDesktop * desktop)
30 : _snaptarget(NULL),
31 _snaptarget_tooltip(NULL),
32 _snaptarget_bbox(NULL),
33 _snapsource(NULL),
34 _snaptarget_is_presnap(false),
35 _desktop(desktop)
36 {
37 }
39 SnapIndicator::~SnapIndicator()
40 {
41 // remove item that might be present
42 remove_snaptarget();
43 remove_snapsource();
44 }
46 void
47 SnapIndicator::set_new_snaptarget(Inkscape::SnappedPoint const &p, bool pre_snap)
48 {
49 remove_snaptarget(); //only display one snaptarget at a time
51 g_assert(_desktop != NULL);
53 if (!p.getSnapped()) {
54 return; // If we haven't snapped, then it is of no use to draw a snapindicator
55 }
57 if (p.getTarget() == SNAPTARGET_CONSTRAINT) {
58 // This is not a real snap, although moving along the constraint did affect the mouse pointer's position.
59 // Maybe we should only show a snap indicator when the user explicitly asked for a constraint by pressing ctrl?
60 // We should not show a snap indicator when stretching a selection box, which is also constrained. That would be
61 // too much information.
62 return;
63 }
65 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
66 bool value = prefs->getBool("/options/snapindicator/value", true);
68 if (value) {
69 // TRANSLATORS: undefined target for snapping
70 gchar *target_name = _("UNDEFINED");
71 switch (p.getTarget()) {
72 case SNAPTARGET_UNDEFINED:
73 target_name = _("UNDEFINED");
74 break;
75 case SNAPTARGET_GRID:
76 target_name = _("grid line");
77 break;
78 case SNAPTARGET_GRID_INTERSECTION:
79 target_name = _("grid intersection");
80 break;
81 case SNAPTARGET_GUIDE:
82 target_name = _("guide");
83 break;
84 case SNAPTARGET_GUIDE_INTERSECTION:
85 target_name = _("guide intersection");
86 break;
87 case SNAPTARGET_GUIDE_ORIGIN:
88 target_name = _("guide origin");
89 break;
90 case SNAPTARGET_GRID_GUIDE_INTERSECTION:
91 target_name = _("grid-guide intersection");
92 break;
93 case SNAPTARGET_NODE_CUSP:
94 target_name = _("cusp node");
95 break;
96 case SNAPTARGET_NODE_SMOOTH:
97 target_name = _("smooth node");
98 break;
99 case SNAPTARGET_PATH:
100 target_name = _("path");
101 break;
102 case SNAPTARGET_PATH_INTERSECTION:
103 target_name = _("path intersection");
104 break;
105 case SNAPTARGET_BBOX_CORNER:
106 target_name = _("bounding box corner");
107 break;
108 case SNAPTARGET_BBOX_EDGE:
109 target_name = _("bounding box side");
110 break;
111 case SNAPTARGET_PAGE_BORDER:
112 target_name = _("page border");
113 break;
114 case SNAPTARGET_LINE_MIDPOINT:
115 target_name = _("line midpoint");
116 break;
117 case SNAPTARGET_OBJECT_MIDPOINT:
118 target_name = _("object midpoint");
119 break;
120 case SNAPTARGET_ROTATION_CENTER:
121 target_name = _("object rotation center");
122 break;
123 case SNAPTARGET_HANDLE:
124 target_name = _("handle");
125 break;
126 case SNAPTARGET_BBOX_EDGE_MIDPOINT:
127 target_name = _("bounding box side midpoint");
128 break;
129 case SNAPTARGET_BBOX_MIDPOINT:
130 target_name = _("bounding box midpoint");
131 break;
132 case SNAPTARGET_PAGE_CORNER:
133 target_name = _("page corner");
134 break;
135 case SNAPTARGET_CONVEX_HULL_CORNER:
136 target_name = _("convex hull corner");
137 break;
138 case SNAPTARGET_ELLIPSE_QUADRANT_POINT:
139 target_name = _("quadrant point");
140 break;
141 case SNAPTARGET_CENTER:
142 target_name = _("center");
143 break;
144 case SNAPTARGET_CORNER:
145 target_name = _("corner");
146 break;
147 case SNAPTARGET_TEXT_BASELINE:
148 target_name = _("text baseline");
149 break;
150 case SNAPTARGET_CONSTRAINED_ANGLE:
151 target_name = _("constrained angle");
152 break;
153 case SNAPTARGET_CONSTRAINT:
154 target_name = _("constraint");
155 break;
156 default:
157 g_warning("Snap target has not yet been defined!");
158 break;
159 }
161 gchar *source_name = _("UNDEFINED");
162 switch (p.getSource()) {
163 case SNAPSOURCE_UNDEFINED:
164 source_name = _("UNDEFINED");
165 break;
166 case SNAPSOURCE_BBOX_CORNER:
167 source_name = _("Bounding box corner");
168 break;
169 case SNAPSOURCE_BBOX_MIDPOINT:
170 source_name = _("Bounding box midpoint");
171 break;
172 case SNAPSOURCE_BBOX_EDGE_MIDPOINT:
173 source_name = _("Bounding box side midpoint");
174 break;
175 case SNAPSOURCE_NODE_SMOOTH:
176 source_name = _("Smooth node");
177 break;
178 case SNAPSOURCE_NODE_CUSP:
179 source_name = _("Cusp node");
180 break;
181 case SNAPSOURCE_LINE_MIDPOINT:
182 source_name = _("Line midpoint");
183 break;
184 case SNAPSOURCE_OBJECT_MIDPOINT:
185 source_name = _("Object midpoint");
186 break;
187 case SNAPSOURCE_ROTATION_CENTER:
188 source_name = _("Object rotation center");
189 break;
190 case SNAPSOURCE_NODE_HANDLE:
191 case SNAPSOURCE_OTHER_HANDLE:
192 source_name = _("Handle");
193 break;
194 case SNAPSOURCE_PATH_INTERSECTION:
195 source_name = _("Path intersection");
196 break;
197 case SNAPSOURCE_GUIDE:
198 source_name = _("Guide");
199 break;
200 case SNAPSOURCE_GUIDE_ORIGIN:
201 source_name = _("Guide origin");
202 break;
203 case SNAPSOURCE_CONVEX_HULL_CORNER:
204 source_name = _("Convex hull corner");
205 break;
206 case SNAPSOURCE_ELLIPSE_QUADRANT_POINT:
207 source_name = _("Quadrant point");
208 break;
209 case SNAPSOURCE_CENTER:
210 source_name = _("Center");
211 break;
212 case SNAPSOURCE_CORNER:
213 source_name = _("Corner");
214 break;
215 case SNAPSOURCE_TEXT_BASELINE:
216 source_name = _("Text baseline");
217 break;
218 case SNAPSOURCE_GRID_PITCH:
219 source_name = _("Multiple of grid spacing");
220 break;
221 default:
222 g_warning("Snap source has not yet been defined!");
223 break;
224 }
225 //std::cout << "Snapped " << source_name << " to " << target_name << std::endl;
227 remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
229 // Display the snap indicator (i.e. the cross)
230 SPCanvasItem * canvasitem = NULL;
231 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
232 SP_TYPE_CTRL,
233 "anchor", GTK_ANCHOR_CENTER,
234 "size", 10.0,
235 "stroked", TRUE,
236 "stroke_color", pre_snap ? 0x7f7f7fff : 0xff0000ff,
237 "mode", SP_KNOT_MODE_XOR,
238 "shape", SP_KNOT_SHAPE_CROSS,
239 NULL );
241 const int timeout_val = 1200; // TODO add preference for snap indicator timeout?
243 SP_CTRL(canvasitem)->moveto(p.getPoint());
244 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
245 _snaptarget_is_presnap = pre_snap;
247 // Display the tooltip, which reveals the type of snap source and the type of snap target
248 gchar *tooltip_str = NULL;
249 if (p.getSource() != SNAPSOURCE_GRID_PITCH) {
250 tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
251 } else {
252 tooltip_str = g_strdup(source_name);
253 }
254 Geom::Point tooltip_pos = p.getPoint() + _desktop->w2d(Geom::Point(15, -15));
256 SPCanvasItem *canvas_tooltip = sp_canvastext_new(sp_desktop_tempgroup(_desktop), _desktop, tooltip_pos, tooltip_str);
257 if (pre_snap) {
258 SP_CANVASTEXT(canvas_tooltip)->rgba = 0x7f7f7fff;
259 }
260 g_free(tooltip_str);
262 sp_canvastext_set_anchor((SPCanvasText* )canvas_tooltip, -1, 1);
263 _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val);
265 // Display the bounding box, if we snapped to one
266 Geom::OptRect const bbox = p.getTargetBBox();
267 if (bbox) {
268 SPCanvasItem* box = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
269 SP_TYPE_CTRLRECT,
270 NULL);
272 SP_CTRLRECT(box)->setRectangle(*bbox);
273 SP_CTRLRECT(box)->setColor(pre_snap ? 0x7f7f7fff : 0xff0000ff, 0, 0);
274 SP_CTRLRECT(box)->setDashed(true);
275 sp_canvas_item_move_to_z(box, 0);
276 _snaptarget_bbox = _desktop->add_temporary_canvasitem(box, timeout_val);
277 }
278 }
279 }
281 void
282 SnapIndicator::remove_snaptarget(bool only_if_presnap)
283 {
284 if (only_if_presnap && !_snaptarget_is_presnap) {
285 return;
286 }
288 if (_snaptarget) {
289 _desktop->remove_temporary_canvasitem(_snaptarget);
290 _snaptarget = NULL;
291 _snaptarget_is_presnap = false;
292 }
294 if (_snaptarget_tooltip) {
295 _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
296 _snaptarget_tooltip = NULL;
297 }
299 if (_snaptarget_bbox) {
300 _desktop->remove_temporary_canvasitem(_snaptarget_bbox);
301 _snaptarget_bbox = NULL;
302 }
304 }
306 void
307 SnapIndicator::set_new_snapsource(Inkscape::SnapCandidatePoint const &p)
308 {
309 remove_snapsource();
311 g_assert(_desktop != NULL);
313 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
314 bool value = prefs->getBool("/options/snapindicator/value", true);
316 if (value) {
317 SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
318 SP_TYPE_CTRL,
319 "anchor", GTK_ANCHOR_CENTER,
320 "size", 6.0,
321 "stroked", TRUE,
322 "stroke_color", 0xff0000ff,
323 "mode", SP_KNOT_MODE_XOR,
324 "shape", SP_KNOT_SHAPE_CIRCLE,
325 NULL );
327 SP_CTRL(canvasitem)->moveto(p.getPoint());
328 _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
329 }
330 }
332 void
333 SnapIndicator::remove_snapsource()
334 {
335 if (_snapsource) {
336 _desktop->remove_temporary_canvasitem(_snapsource);
337 _snapsource = NULL;
338 }
339 }
341 } //namespace Display
342 } /* namespace Inkscape */
345 /*
346 Local Variables:
347 mode:c++
348 c-file-style:"stroustrup"
349 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
350 indent-tabs-mode:nil
351 fill-column:99
352 End:
353 */
354 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 :