Code

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