Code

49433efe2228ae8522952548d523491f27bb81ef
[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 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     /* Commented out for now, because this might hide any snapping bug!
50     if (!p.getSnapped()) {
51        return; // If we haven't snapped, then it is of no use to draw a snapindicator
52     }
53     */
55     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
56     bool value = prefs->getBool("/options/snapindicator/value", true);
58     if (value) {
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_GRID_GUIDE_INTERSECTION:
77                 target_name = _("grid-guide intersection");
78                 break;
79             case SNAPTARGET_NODE_CUSP:
80                                 target_name = _("cusp node");
81                                 break;
82             case SNAPTARGET_NODE_SMOOTH:
83                                 target_name = _("smooth node");
84                                 break;
85                         case SNAPTARGET_PATH:
86                 target_name = _("path");
87                 break;
88             case SNAPTARGET_PATH_INTERSECTION:
89                 target_name = _("path intersection");
90                 break;
91             case SNAPTARGET_BBOX_CORNER:
92                 target_name = _("bounding box corner");
93                 break;
94             case SNAPTARGET_BBOX_EDGE:
95                 target_name = _("bounding box side");
96                 break;
97             case SNAPTARGET_GRADIENTS_PARENT_BBOX:
98                 target_name = _("bounding box");
99                 break;
100             case SNAPTARGET_PAGE_BORDER:
101                 target_name = _("page border");
102                                 break;
103             case SNAPTARGET_LINE_MIDPOINT:
104                 target_name = _("line midpoint");
105                 break;
106             case SNAPTARGET_OBJECT_MIDPOINT:
107                 target_name = _("object midpoint");
108                 break;
109             case SNAPTARGET_ROTATION_CENTER:
110                 target_name = _("object rotation center");
111                 break;
112             case SNAPTARGET_HANDLE:
113                 target_name = _("handle");
114                 break;
115             case SNAPTARGET_BBOX_EDGE_MIDPOINT:
116                 target_name = _("bounding box side midpoint");
117                 break;
118             case SNAPTARGET_BBOX_MIDPOINT:
119                 target_name = _("bounding box midpoint");
120                 break;
121             case SNAPTARGET_PAGE_CORNER:
122                 target_name = _("page corner");
123                 break;
124             case SNAPTARGET_CONVEX_HULL_CORNER:
125                 target_name = _("convex hull corner");
126                 break;
127             case SNAPTARGET_ELLIPSE_QUADRANT_POINT:
128                 target_name = _("quadrant point");
129                 break;
130             case SNAPTARGET_CENTER:
131                 target_name = _("center");
132                                 break;
133             case SNAPTARGET_CORNER:
134                 target_name = _("corner");
135                                 break;
136             case SNAPTARGET_TEXT_BASELINE:
137                 target_name = _("text baseline");
138                                 break;
139             default:
140                 g_warning("Snap target has not yet been defined!");
141                 break;
142         }
144         gchar *source_name = _("UNDEFINED");
145                 switch (p.getSource()) {
146                         case SNAPSOURCE_UNDEFINED:
147                                 source_name = _("UNDEFINED");
148                                 break;
149                         case SNAPSOURCE_BBOX_CORNER:
150                                 source_name = _("Bounding box corner");
151                                 break;
152                         case SNAPSOURCE_BBOX_MIDPOINT:
153                                 source_name = _("Bounding box midpoint");
154                                 break;
155                         case SNAPSOURCE_BBOX_EDGE_MIDPOINT:
156                                 source_name = _("Bounding box side midpoint");
157                                 break;
158                         case SNAPSOURCE_NODE_SMOOTH:
159                                 source_name = _("Smooth node");
160                                 break;
161                         case SNAPSOURCE_NODE_CUSP:
162                                 source_name = _("Cusp node");
163                                 break;
164                         case SNAPSOURCE_LINE_MIDPOINT:
165                                 source_name = _("Line midpoint");
166                                 break;
167                         case SNAPSOURCE_OBJECT_MIDPOINT:
168                                 source_name = _("Object midpoint");
169                                 break;
170                         case SNAPSOURCE_ROTATION_CENTER:
171                                 source_name = _("Object rotation center");
172                                 break;
173                         case SNAPSOURCE_HANDLE:
174                                 source_name = _("Handle");
175                                 break;
176                         case SNAPSOURCE_PATH_INTERSECTION:
177                                 source_name = _("Path intersection");
178                                 break;
179                         case SNAPSOURCE_GUIDE:
180                                 source_name = _("Guide");
181                                 break;
182                         case SNAPSOURCE_CONVEX_HULL_CORNER:
183                                 source_name = _("Convex hull corner");
184                                 break;
185                         case SNAPSOURCE_ELLIPSE_QUADRANT_POINT:
186                                 source_name = _("Quadrant point");
187                                 break;
188                         case SNAPSOURCE_CENTER:
189                                 source_name = _("Center");
190                                 break;
191                         case SNAPSOURCE_CORNER:
192                                 source_name = _("Corner");
193                                 break;
194                         case SNAPSOURCE_TEXT_BASELINE:
195                                 source_name = _("Text baseline");
196                                 break;
197                         default:
198                                 g_warning("Snap source has not yet been defined!");
199                                 break;
200                 }
201         //std::cout << "Snapped " << source_name << " to " << target_name << std::endl;
203                 remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
205         // Display the snap indicator (i.e. the cross)
206         SPCanvasItem * canvasitem = NULL;
207                 if (p.getTarget() == SNAPTARGET_NODE_SMOOTH || p.getTarget() == SNAPTARGET_NODE_CUSP) {
208                         canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
209                                                                                         SP_TYPE_CTRL,
210                                                                                         "anchor", GTK_ANCHOR_CENTER,
211                                                                                         "size", 10.0,
212                                                                                         "stroked", TRUE,
213                                                                                         "stroke_color", 0xf000f0ff,
214                                                                                         "mode", SP_KNOT_MODE_XOR,
215                                                                                         "shape", SP_KNOT_SHAPE_DIAMOND,
216                                                                                         NULL );
217                 } else {
218                         canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
219                                                                                         SP_TYPE_CTRL,
220                                                                                         "anchor", GTK_ANCHOR_CENTER,
221                                                                                         "size", 10.0,
222                                                                                         "stroked", TRUE,
223                                                                                         "stroke_color", 0xf000f0ff,
224                                                                                         "mode", SP_KNOT_MODE_XOR,
225                                                                                         "shape", SP_KNOT_SHAPE_CROSS,
226                                                                                         NULL );
227                 }
229                 const int timeout_val = 1200; // TODO add preference for snap indicator timeout?
231                 SP_CTRL(canvasitem)->moveto(p.getPoint());
232                 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
234                 gchar *tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
235                 Geom::Point tooltip_pos = p.getPoint() + _desktop->w2d(Geom::Point(15, -15));
237                 SPCanvasItem *canvas_tooltip = sp_canvastext_new(sp_desktop_tempgroup(_desktop), _desktop, tooltip_pos, tooltip_str);
238                 g_free(tooltip_str);
240                 sp_canvastext_set_anchor((SPCanvasText* )canvas_tooltip, -1, 1);
241                 _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val);
242         }
245 void
246 SnapIndicator::remove_snaptarget()
248     if (_snaptarget) {
249         _desktop->remove_temporary_canvasitem(_snaptarget);
250         _snaptarget = NULL;
251     }
253     if (_snaptarget_tooltip) {
254                 _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
255                 _snaptarget_tooltip = NULL;
256         }
260 void
261 SnapIndicator::set_new_snapsource(std::pair<Geom::Point, int> const p)
263         remove_snapsource();
265     g_assert(_desktop != NULL);
267     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
268     bool value = prefs->getBool("/options/snapindicator/value", true);
270     if (value) {
271         SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
272                                                         SP_TYPE_CTRL,
273                                                         "anchor", GTK_ANCHOR_CENTER,
274                                                         "size", 6.0,
275                                                         "stroked", TRUE,
276                                                         "stroke_color", 0xf000f0ff,
277                                                         "mode", SP_KNOT_MODE_XOR,
278                                                         "shape", SP_KNOT_SHAPE_CIRCLE,
279                                                         NULL );
281         SP_CTRL(canvasitem)->moveto(p.first);
282         _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
283         }
286 void
287 SnapIndicator::remove_snapsource()
289     if (_snapsource) {
290         _desktop->remove_temporary_canvasitem(_snapsource);
291         _snapsource = NULL;
292     }
295 } //namespace Display
296 } /* namespace Inkscape */
299 /*
300   Local Variables:
301   mode:c++
302   c-file-style:"stroustrup"
303   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
304   indent-tabs-mode:nil
305   fill-column:99
306   End:
307 */
308 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 :