fdea9cbbfae1ecf3433bcbefee0c61e9f3664706
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 2009 <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/canvas-text.h"
20 #include "knot.h"
21 #include "preferences.h"
22 #include <glibmm/i18n.h>
24 namespace Inkscape {
25 namespace Display {
27 SnapIndicator::SnapIndicator(SPDesktop * desktop)
28 : _snaptarget(NULL),
29 _snaptarget_tooltip(NULL),
30 _snapsource(NULL),
31 _desktop(desktop)
32 {
33 }
35 SnapIndicator::~SnapIndicator()
36 {
37 // remove item that might be present
38 remove_snaptarget();
39 remove_snapsource();
40 }
42 void
43 SnapIndicator::set_new_snaptarget(Inkscape::SnappedPoint const p)
44 {
45 remove_snaptarget(); //only display one snaptarget at a time
47 g_assert(_desktop != NULL);
49 if (!p.getSnapped()) {
50 g_warning("No snapping took place, so no snap target will be displayed");
51 return; // If we haven't snapped, then it is of no use to draw a snapindicator
52 }
54 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
55 bool value = prefs->getBool("/options/snapindicator/value", true);
57 if (value) {
58 // TRANSLATORS: undefined target for snapping
59 gchar *target_name = _("UNDEFINED");
60 switch (p.getTarget()) {
61 case SNAPTARGET_UNDEFINED:
62 target_name = _("UNDEFINED");
63 break;
64 case SNAPTARGET_GRID:
65 target_name = _("grid line");
66 break;
67 case SNAPTARGET_GRID_INTERSECTION:
68 target_name = _("grid intersection");
69 break;
70 case SNAPTARGET_GUIDE:
71 target_name = _("guide");
72 break;
73 case SNAPTARGET_GUIDE_INTERSECTION:
74 target_name = _("guide intersection");
75 break;
76 case SNAPTARGET_GUIDE_ORIGIN:
77 target_name = _("guide origin");
78 break;
79 case SNAPTARGET_GRID_GUIDE_INTERSECTION:
80 target_name = _("grid-guide intersection");
81 break;
82 case SNAPTARGET_NODE_CUSP:
83 target_name = _("cusp node");
84 break;
85 case SNAPTARGET_NODE_SMOOTH:
86 target_name = _("smooth node");
87 break;
88 case SNAPTARGET_PATH:
89 target_name = _("path");
90 break;
91 case SNAPTARGET_PATH_INTERSECTION:
92 target_name = _("path intersection");
93 break;
94 case SNAPTARGET_BBOX_CORNER:
95 target_name = _("bounding box corner");
96 break;
97 case SNAPTARGET_BBOX_EDGE:
98 target_name = _("bounding box side");
99 break;
100 case SNAPTARGET_GRADIENTS_PARENT_BBOX:
101 target_name = _("bounding box");
102 break;
103 case SNAPTARGET_PAGE_BORDER:
104 target_name = _("page border");
105 break;
106 case SNAPTARGET_LINE_MIDPOINT:
107 target_name = _("line midpoint");
108 break;
109 case SNAPTARGET_OBJECT_MIDPOINT:
110 target_name = _("object midpoint");
111 break;
112 case SNAPTARGET_ROTATION_CENTER:
113 target_name = _("object rotation center");
114 break;
115 case SNAPTARGET_HANDLE:
116 target_name = _("handle");
117 break;
118 case SNAPTARGET_BBOX_EDGE_MIDPOINT:
119 target_name = _("bounding box side midpoint");
120 break;
121 case SNAPTARGET_BBOX_MIDPOINT:
122 target_name = _("bounding box midpoint");
123 break;
124 case SNAPTARGET_PAGE_CORNER:
125 target_name = _("page corner");
126 break;
127 case SNAPTARGET_CONVEX_HULL_CORNER:
128 target_name = _("convex hull corner");
129 break;
130 case SNAPTARGET_ELLIPSE_QUADRANT_POINT:
131 target_name = _("quadrant point");
132 break;
133 case SNAPTARGET_CENTER:
134 target_name = _("center");
135 break;
136 case SNAPTARGET_CORNER:
137 target_name = _("corner");
138 break;
139 case SNAPTARGET_TEXT_BASELINE:
140 target_name = _("text baseline");
141 break;
142 default:
143 g_warning("Snap target has not yet been defined!");
144 break;
145 }
147 gchar *source_name = _("UNDEFINED");
148 switch (p.getSource()) {
149 case SNAPSOURCE_UNDEFINED:
150 source_name = _("UNDEFINED");
151 break;
152 case SNAPSOURCE_BBOX_CORNER:
153 source_name = _("Bounding box corner");
154 break;
155 case SNAPSOURCE_BBOX_MIDPOINT:
156 source_name = _("Bounding box midpoint");
157 break;
158 case SNAPSOURCE_BBOX_EDGE_MIDPOINT:
159 source_name = _("Bounding box side midpoint");
160 break;
161 case SNAPSOURCE_NODE_SMOOTH:
162 source_name = _("Smooth node");
163 break;
164 case SNAPSOURCE_NODE_CUSP:
165 source_name = _("Cusp node");
166 break;
167 case SNAPSOURCE_LINE_MIDPOINT:
168 source_name = _("Line midpoint");
169 break;
170 case SNAPSOURCE_OBJECT_MIDPOINT:
171 source_name = _("Object midpoint");
172 break;
173 case SNAPSOURCE_ROTATION_CENTER:
174 source_name = _("Object rotation center");
175 break;
176 case SNAPSOURCE_HANDLE:
177 source_name = _("Handle");
178 break;
179 case SNAPSOURCE_PATH_INTERSECTION:
180 source_name = _("Path intersection");
181 break;
182 case SNAPSOURCE_GUIDE:
183 source_name = _("Guide");
184 break;
185 case SNAPSOURCE_GUIDE_ORIGIN:
186 source_name = _("Guide origin");
187 break;
188 case SNAPSOURCE_CONVEX_HULL_CORNER:
189 source_name = _("Convex hull corner");
190 break;
191 case SNAPSOURCE_ELLIPSE_QUADRANT_POINT:
192 source_name = _("Quadrant point");
193 break;
194 case SNAPSOURCE_CENTER:
195 source_name = _("Center");
196 break;
197 case SNAPSOURCE_CORNER:
198 source_name = _("Corner");
199 break;
200 case SNAPSOURCE_TEXT_BASELINE:
201 source_name = _("Text baseline");
202 break;
203 default:
204 g_warning("Snap source has not yet been defined!");
205 break;
206 }
207 //std::cout << "Snapped " << source_name << " to " << target_name << std::endl;
209 remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
211 // Display the snap indicator (i.e. the cross)
212 SPCanvasItem * canvasitem = NULL;
213 if (p.getTarget() == SNAPTARGET_NODE_SMOOTH || p.getTarget() == SNAPTARGET_NODE_CUSP) {
214 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
215 SP_TYPE_CTRL,
216 "anchor", GTK_ANCHOR_CENTER,
217 "size", 10.0,
218 "stroked", TRUE,
219 "stroke_color", 0xf000f0ff,
220 "mode", SP_KNOT_MODE_XOR,
221 "shape", SP_KNOT_SHAPE_DIAMOND,
222 NULL );
223 } else {
224 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
225 SP_TYPE_CTRL,
226 "anchor", GTK_ANCHOR_CENTER,
227 "size", 10.0,
228 "stroked", TRUE,
229 "stroke_color", 0xf000f0ff,
230 "mode", SP_KNOT_MODE_XOR,
231 "shape", SP_KNOT_SHAPE_CROSS,
232 NULL );
233 }
235 const int timeout_val = 1200; // TODO add preference for snap indicator timeout?
237 SP_CTRL(canvasitem)->moveto(p.getPoint());
238 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
240 gchar *tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
241 Geom::Point tooltip_pos = p.getPoint() + _desktop->w2d(Geom::Point(15, -15));
243 SPCanvasItem *canvas_tooltip = sp_canvastext_new(sp_desktop_tempgroup(_desktop), _desktop, tooltip_pos, tooltip_str);
244 g_free(tooltip_str);
246 sp_canvastext_set_anchor((SPCanvasText* )canvas_tooltip, -1, 1);
247 _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val);
248 }
249 }
251 void
252 SnapIndicator::remove_snaptarget()
253 {
254 if (_snaptarget) {
255 _desktop->remove_temporary_canvasitem(_snaptarget);
256 _snaptarget = NULL;
257 }
259 if (_snaptarget_tooltip) {
260 _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
261 _snaptarget_tooltip = NULL;
262 }
264 }
266 void
267 SnapIndicator::set_new_snapsource(std::pair<Geom::Point, int> const p)
268 {
269 remove_snapsource();
271 g_assert(_desktop != NULL);
273 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
274 bool value = prefs->getBool("/options/snapindicator/value", true);
276 if (value) {
277 SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
278 SP_TYPE_CTRL,
279 "anchor", GTK_ANCHOR_CENTER,
280 "size", 6.0,
281 "stroked", TRUE,
282 "stroke_color", 0xf000f0ff,
283 "mode", SP_KNOT_MODE_XOR,
284 "shape", SP_KNOT_SHAPE_CIRCLE,
285 NULL );
287 SP_CTRL(canvasitem)->moveto(p.first);
288 _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
289 }
290 }
292 void
293 SnapIndicator::remove_snapsource()
294 {
295 if (_snapsource) {
296 _desktop->remove_temporary_canvasitem(_snapsource);
297 _snapsource = NULL;
298 }
299 }
301 } //namespace Display
302 } /* namespace Inkscape */
305 /*
306 Local Variables:
307 mode:c++
308 c-file-style:"stroustrup"
309 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
310 indent-tabs-mode:nil
311 fill-column:99
312 End:
313 */
314 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 :