Code

1) Fix moving by an integer multiple of the grid spacing (<alt>-dragging in the selec...
[inkscape.git] / src / display / snap-indicator.cpp
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         g_warning("No snapping took place, so no snap target will be displayed");
55         return; // If we haven't snapped, then it is of no use to draw a snapindicator
56     }
58     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
59     bool value = prefs->getBool("/options/snapindicator/value", true);
61     if (value) {
62         // TRANSLATORS: undefined target for snapping
63         gchar *target_name = _("UNDEFINED");
64         switch (p.getTarget()) {
65             case SNAPTARGET_UNDEFINED:
66                 target_name = _("UNDEFINED");
67                 break;
68             case SNAPTARGET_GRID:
69                 target_name = _("grid line");
70                 break;
71             case SNAPTARGET_GRID_INTERSECTION:
72                 target_name = _("grid intersection");
73                 break;
74             case SNAPTARGET_GUIDE:
75                 target_name = _("guide");
76                 break;
77             case SNAPTARGET_GUIDE_INTERSECTION:
78                 target_name = _("guide intersection");
79                 break;
80             case SNAPTARGET_GUIDE_ORIGIN:
81                 target_name = _("guide origin");
82                 break;
83             case SNAPTARGET_GRID_GUIDE_INTERSECTION:
84                 target_name = _("grid-guide intersection");
85                 break;
86             case SNAPTARGET_NODE_CUSP:
87                 target_name = _("cusp node");
88                 break;
89             case SNAPTARGET_NODE_SMOOTH:
90                 target_name = _("smooth node");
91                 break;
92             case SNAPTARGET_PATH:
93                 target_name = _("path");
94                 break;
95             case SNAPTARGET_PATH_INTERSECTION:
96                 target_name = _("path intersection");
97                 break;
98             case SNAPTARGET_BBOX_CORNER:
99                 target_name = _("bounding box corner");
100                 break;
101             case SNAPTARGET_BBOX_EDGE:
102                 target_name = _("bounding box side");
103                 break;
104             case SNAPTARGET_PAGE_BORDER:
105                 target_name = _("page border");
106                 break;
107             case SNAPTARGET_LINE_MIDPOINT:
108                 target_name = _("line midpoint");
109                 break;
110             case SNAPTARGET_OBJECT_MIDPOINT:
111                 target_name = _("object midpoint");
112                 break;
113             case SNAPTARGET_ROTATION_CENTER:
114                 target_name = _("object rotation center");
115                 break;
116             case SNAPTARGET_HANDLE:
117                 target_name = _("handle");
118                 break;
119             case SNAPTARGET_BBOX_EDGE_MIDPOINT:
120                 target_name = _("bounding box side midpoint");
121                 break;
122             case SNAPTARGET_BBOX_MIDPOINT:
123                 target_name = _("bounding box midpoint");
124                 break;
125             case SNAPTARGET_PAGE_CORNER:
126                 target_name = _("page corner");
127                 break;
128             case SNAPTARGET_CONVEX_HULL_CORNER:
129                 target_name = _("convex hull corner");
130                 break;
131             case SNAPTARGET_ELLIPSE_QUADRANT_POINT:
132                 target_name = _("quadrant point");
133                 break;
134             case SNAPTARGET_CENTER:
135                 target_name = _("center");
136                 break;
137             case SNAPTARGET_CORNER:
138                 target_name = _("corner");
139                 break;
140             case SNAPTARGET_TEXT_BASELINE:
141                 target_name = _("text baseline");
142                 break;
143             case SNAPTARGET_CONSTRAINED_ANGLE:
144                 target_name = _("constrained angle");
145                 break;
146             case SNAPTARGET_CONSTRAINT:
147                 target_name = _("constraint");
148                 break;
149             default:
150                 g_warning("Snap target has not yet been defined!");
151                 break;
152         }
154         gchar *source_name = _("UNDEFINED");
155         switch (p.getSource()) {
156             case SNAPSOURCE_UNDEFINED:
157                 source_name = _("UNDEFINED");
158                 break;
159             case SNAPSOURCE_BBOX_CORNER:
160                 source_name = _("Bounding box corner");
161                 break;
162             case SNAPSOURCE_BBOX_MIDPOINT:
163                 source_name = _("Bounding box midpoint");
164                 break;
165             case SNAPSOURCE_BBOX_EDGE_MIDPOINT:
166                 source_name = _("Bounding box side midpoint");
167                 break;
168             case SNAPSOURCE_NODE_SMOOTH:
169                 source_name = _("Smooth node");
170                 break;
171             case SNAPSOURCE_NODE_CUSP:
172                 source_name = _("Cusp node");
173                 break;
174             case SNAPSOURCE_LINE_MIDPOINT:
175                 source_name = _("Line midpoint");
176                 break;
177             case SNAPSOURCE_OBJECT_MIDPOINT:
178                 source_name = _("Object midpoint");
179                 break;
180             case SNAPSOURCE_ROTATION_CENTER:
181                 source_name = _("Object rotation center");
182                 break;
183             case SNAPSOURCE_NODE_HANDLE:
184             case SNAPSOURCE_OTHER_HANDLE:
185                 source_name = _("Handle");
186                 break;
187             case SNAPSOURCE_PATH_INTERSECTION:
188                 source_name = _("Path intersection");
189                 break;
190             case SNAPSOURCE_GUIDE:
191                 source_name = _("Guide");
192                 break;
193             case SNAPSOURCE_GUIDE_ORIGIN:
194                 source_name = _("Guide origin");
195                 break;
196             case SNAPSOURCE_CONVEX_HULL_CORNER:
197                 source_name = _("Convex hull corner");
198                 break;
199             case SNAPSOURCE_ELLIPSE_QUADRANT_POINT:
200                 source_name = _("Quadrant point");
201                 break;
202             case SNAPSOURCE_CENTER:
203                 source_name = _("Center");
204                 break;
205             case SNAPSOURCE_CORNER:
206                 source_name = _("Corner");
207                 break;
208             case SNAPSOURCE_TEXT_BASELINE:
209                 source_name = _("Text baseline");
210                 break;
211             case SNAPSOURCE_GRID_PITCH:
212                 source_name = _("Multiple of grid spacing");
213                 break;
214             default:
215                 g_warning("Snap source has not yet been defined!");
216                 break;
217         }
218         //std::cout << "Snapped " << source_name << " to " << target_name << std::endl;
220         remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
222         // Display the snap indicator (i.e. the cross)
223         SPCanvasItem * canvasitem = NULL;
224         if (p.getTarget() == SNAPTARGET_NODE_SMOOTH || p.getTarget() == SNAPTARGET_NODE_CUSP) {
225             canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
226                                             SP_TYPE_CTRL,
227                                             "anchor", GTK_ANCHOR_CENTER,
228                                             "size", 10.0,
229                                             "stroked", TRUE,
230                                             "stroke_color", pre_snap ? 0x7f7f7fff : 0xff0000ff,
231                                             "mode", SP_KNOT_MODE_XOR,
232                                             "shape", SP_KNOT_SHAPE_DIAMOND,
233                                             NULL );
234         } else {
235             canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
236                                             SP_TYPE_CTRL,
237                                             "anchor", GTK_ANCHOR_CENTER,
238                                             "size", 10.0,
239                                             "stroked", TRUE,
240                                             "stroke_color", pre_snap ? 0x7f7f7fff : 0xff0000ff,
241                                             "mode", SP_KNOT_MODE_XOR,
242                                             "shape", SP_KNOT_SHAPE_CROSS,
243                                             NULL );
244         }
246         const int timeout_val = 1200; // TODO add preference for snap indicator timeout?
248         SP_CTRL(canvasitem)->moveto(p.getPoint());
249         _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
250         _snaptarget_is_presnap = pre_snap;
252         // Display the tooltip, which reveals the type of snap source and the type of snap target
253         gchar *tooltip_str = NULL;
254         if (p.getSource() != SNAPSOURCE_GRID_PITCH) {
255             tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
256         } else {
257             tooltip_str = g_strdup(source_name);
258         }
259         Geom::Point tooltip_pos = p.getPoint() + _desktop->w2d(Geom::Point(15, -15));
261         SPCanvasItem *canvas_tooltip = sp_canvastext_new(sp_desktop_tempgroup(_desktop), _desktop, tooltip_pos, tooltip_str);
262         if (pre_snap) {
263             SP_CANVASTEXT(canvas_tooltip)->rgba = 0x7f7f7fff;
264         }
265         g_free(tooltip_str);
267         sp_canvastext_set_anchor((SPCanvasText* )canvas_tooltip, -1, 1);
268         _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val);
270         // Display the bounding box, if we snapped to one
271         Geom::OptRect const bbox = p.getTargetBBox();
272         if (bbox) {
273             SPCanvasItem* box = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
274                                                      SP_TYPE_CTRLRECT,
275                                                      NULL);
277             SP_CTRLRECT(box)->setRectangle(*bbox);
278             SP_CTRLRECT(box)->setColor(pre_snap ? 0x7f7f7fff : 0xff0000ff, 0, 0);
279             SP_CTRLRECT(box)->setDashed(true);
280             sp_canvas_item_move_to_z(box, 0);
281             _snaptarget_bbox = _desktop->add_temporary_canvasitem(box, timeout_val);
282         }
283     }
286 void
287 SnapIndicator::remove_snaptarget(bool only_if_presnap)
289     if (only_if_presnap && !_snaptarget_is_presnap) {
290         return;
291     }
293     if (_snaptarget) {
294         _desktop->remove_temporary_canvasitem(_snaptarget);
295         _snaptarget = NULL;
296         _snaptarget_is_presnap = false;
297     }
299     if (_snaptarget_tooltip) {
300         _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
301         _snaptarget_tooltip = NULL;
302     }
304     if (_snaptarget_bbox) {
305         _desktop->remove_temporary_canvasitem(_snaptarget_bbox);
306         _snaptarget_bbox = NULL;
307     }
311 void
312 SnapIndicator::set_new_snapsource(Inkscape::SnapCandidatePoint const &p)
314     remove_snapsource();
316     g_assert(_desktop != NULL);
318     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
319     bool value = prefs->getBool("/options/snapindicator/value", true);
321     if (value) {
322         SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
323                                                         SP_TYPE_CTRL,
324                                                         "anchor", GTK_ANCHOR_CENTER,
325                                                         "size", 6.0,
326                                                         "stroked", TRUE,
327                                                         "stroke_color", 0xff0000ff,
328                                                         "mode", SP_KNOT_MODE_XOR,
329                                                         "shape", SP_KNOT_SHAPE_CIRCLE,
330                                                         NULL );
332         SP_CTRL(canvasitem)->moveto(p.getPoint());
333         _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
334     }
337 void
338 SnapIndicator::remove_snapsource()
340     if (_snapsource) {
341         _desktop->remove_temporary_canvasitem(_snapsource);
342         _snapsource = NULL;
343     }
346 } //namespace Display
347 } /* namespace Inkscape */
350 /*
351   Local Variables:
352   mode:c++
353   c-file-style:"stroustrup"
354   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
355   indent-tabs-mode:nil
356   fill-column:99
357   End:
358 */
359 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 :