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 if (p.getTarget() == SNAPTARGET_NODE_SMOOTH || p.getTarget() == SNAPTARGET_NODE_CUSP) {
232 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
233 SP_TYPE_CTRL,
234 "anchor", GTK_ANCHOR_CENTER,
235 "size", 10.0,
236 "stroked", TRUE,
237 "stroke_color", pre_snap ? 0x7f7f7fff : 0xff0000ff,
238 "mode", SP_KNOT_MODE_XOR,
239 "shape", SP_KNOT_SHAPE_DIAMOND,
240 NULL );
241 } else {
242 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
243 SP_TYPE_CTRL,
244 "anchor", GTK_ANCHOR_CENTER,
245 "size", 10.0,
246 "stroked", TRUE,
247 "stroke_color", pre_snap ? 0x7f7f7fff : 0xff0000ff,
248 "mode", SP_KNOT_MODE_XOR,
249 "shape", SP_KNOT_SHAPE_CROSS,
250 NULL );
251 }
253 const int timeout_val = 1200; // TODO add preference for snap indicator timeout?
255 SP_CTRL(canvasitem)->moveto(p.getPoint());
256 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
257 _snaptarget_is_presnap = pre_snap;
259 // Display the tooltip, which reveals the type of snap source and the type of snap target
260 gchar *tooltip_str = NULL;
261 if (p.getSource() != SNAPSOURCE_GRID_PITCH) {
262 tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
263 } else {
264 tooltip_str = g_strdup(source_name);
265 }
266 Geom::Point tooltip_pos = p.getPoint() + _desktop->w2d(Geom::Point(15, -15));
268 SPCanvasItem *canvas_tooltip = sp_canvastext_new(sp_desktop_tempgroup(_desktop), _desktop, tooltip_pos, tooltip_str);
269 if (pre_snap) {
270 SP_CANVASTEXT(canvas_tooltip)->rgba = 0x7f7f7fff;
271 }
272 g_free(tooltip_str);
274 sp_canvastext_set_anchor((SPCanvasText* )canvas_tooltip, -1, 1);
275 _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val);
277 // Display the bounding box, if we snapped to one
278 Geom::OptRect const bbox = p.getTargetBBox();
279 if (bbox) {
280 SPCanvasItem* box = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
281 SP_TYPE_CTRLRECT,
282 NULL);
284 SP_CTRLRECT(box)->setRectangle(*bbox);
285 SP_CTRLRECT(box)->setColor(pre_snap ? 0x7f7f7fff : 0xff0000ff, 0, 0);
286 SP_CTRLRECT(box)->setDashed(true);
287 sp_canvas_item_move_to_z(box, 0);
288 _snaptarget_bbox = _desktop->add_temporary_canvasitem(box, timeout_val);
289 }
290 }
291 }
293 void
294 SnapIndicator::remove_snaptarget(bool only_if_presnap)
295 {
296 if (only_if_presnap && !_snaptarget_is_presnap) {
297 return;
298 }
300 if (_snaptarget) {
301 _desktop->remove_temporary_canvasitem(_snaptarget);
302 _snaptarget = NULL;
303 _snaptarget_is_presnap = false;
304 }
306 if (_snaptarget_tooltip) {
307 _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
308 _snaptarget_tooltip = NULL;
309 }
311 if (_snaptarget_bbox) {
312 _desktop->remove_temporary_canvasitem(_snaptarget_bbox);
313 _snaptarget_bbox = NULL;
314 }
316 }
318 void
319 SnapIndicator::set_new_snapsource(Inkscape::SnapCandidatePoint const &p)
320 {
321 remove_snapsource();
323 g_assert(_desktop != NULL);
325 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
326 bool value = prefs->getBool("/options/snapindicator/value", true);
328 if (value) {
329 SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
330 SP_TYPE_CTRL,
331 "anchor", GTK_ANCHOR_CENTER,
332 "size", 6.0,
333 "stroked", TRUE,
334 "stroke_color", 0xff0000ff,
335 "mode", SP_KNOT_MODE_XOR,
336 "shape", SP_KNOT_SHAPE_CIRCLE,
337 NULL );
339 SP_CTRL(canvasitem)->moveto(p.getPoint());
340 _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
341 }
342 }
344 void
345 SnapIndicator::remove_snapsource()
346 {
347 if (_snapsource) {
348 _desktop->remove_temporary_canvasitem(_snapsource);
349 _snapsource = NULL;
350 }
351 }
353 } //namespace Display
354 } /* namespace Inkscape */
357 /*
358 Local Variables:
359 mode:c++
360 c-file-style:"stroustrup"
361 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
362 indent-tabs-mode:nil
363 fill-column:99
364 End:
365 */
366 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 :