Code

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