Code

Use on-cavas text instead of a tooltip for the snapindicator (fixes some tooltip...
[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-bpath.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();
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_GRADIENT:
98                 target_name = _("gradient");
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             default:
131                 g_warning("Snap target has not yet been defined!");
132                 break;
133         }
135         gchar *source_name = _("UNDEFINED");
136                 switch (p.getSource()) {
137                         case SNAPSOURCE_UNDEFINED:
138                                 source_name = _("UNDEFINED");
139                                 break;
140                         case SNAPSOURCE_BBOX_CORNER:
141                                 source_name = _("Bounding box corner");
142                                 break;
143                         case SNAPSOURCE_BBOX_MIDPOINT:
144                                 source_name = _("Bounding box midpoint");
145                                 break;
146                         case SNAPSOURCE_BBOX_EDGE_MIDPOINT:
147                                 source_name = _("Bounding box side midpoint");
148                                 break;
149                         case SNAPSOURCE_NODE_SMOOTH:
150                                 source_name = _("Smooth node");
151                                 break;
152                         case SNAPSOURCE_NODE_CUSP:
153                                 source_name = _("Cusp node");
154                                 break;
155                         case SNAPSOURCE_LINE_MIDPOINT:
156                                 source_name = _("Line midpoint");
157                                 break;
158                         case SNAPSOURCE_OBJECT_MIDPOINT:
159                                 source_name = _("Object midpoint");
160                                 break;
161                         case SNAPSOURCE_ROTATION_CENTER:
162                                 source_name = _("Object rotation center");
163                                 break;
164                         case SNAPSOURCE_HANDLE:
165                                 source_name = _("Handle");
166                                 break;
167                         case SNAPSOURCE_PATH_INTERSECTION:
168                                 source_name = _("Path intersection");
169                                 break;
170                         case SNAPSOURCE_GUIDE:
171                                 source_name = _("Guide");
172                                 break;
173                         case SNAPSOURCE_CONVEX_HULL_CORNER:
174                                 source_name = _("Convex hull corner");
175                                 break;
176                         case SNAPSOURCE_ELLIPSE_QUADRANT_POINT:
177                                 source_name = _("Quadrant point");
178                                 break;
179                         default:
180                                 g_warning("Snap source has not yet been defined!");
181                                 break;
182                 }
183         //std::cout << "Snapped " << source_name << " to " << target_name << std::endl;
185                 remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
187         // Display the snap indicator (i.e. the cross)
188         SPCanvasItem * canvasitem = NULL;
189                 if (p.getTarget() == SNAPTARGET_NODE_SMOOTH || p.getTarget() == SNAPTARGET_NODE_CUSP) {
190                         canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
191                                                                                         SP_TYPE_CTRL,
192                                                                                         "anchor", GTK_ANCHOR_CENTER,
193                                                                                         "size", 10.0,
194                                                                                         "stroked", TRUE,
195                                                                                         "stroke_color", 0xf000f0ff,
196                                                                                         "mode", SP_KNOT_MODE_XOR,
197                                                                                         "shape", SP_KNOT_SHAPE_DIAMOND,
198                                                                                         NULL );
199                 } else {
200                         canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
201                                                                                         SP_TYPE_CTRL,
202                                                                                         "anchor", GTK_ANCHOR_CENTER,
203                                                                                         "size", 10.0,
204                                                                                         "stroked", TRUE,
205                                                                                         "stroke_color", 0xf000f0ff,
206                                                                                         "mode", SP_KNOT_MODE_XOR,
207                                                                                         "shape", SP_KNOT_SHAPE_CROSS,
208                                                                                         NULL );
209                 }
211                 const int timeout_val = 1200; // TODO add preference for snap indicator timeout?
213                 SP_CTRL(canvasitem)->moveto(p.getPoint());
214                 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
216                 gchar *tooltip_str = g_strconcat(source_name, _(" to "), target_name, NULL);
218                 SPCanvasItem *canvas_tooltip = sp_canvastext_new(sp_desktop_tempgroup(_desktop), _desktop, p.getPoint() + Geom::Point(15, -15), tooltip_str);
219                 g_free(tooltip_str);
221                 sp_canvastext_set_anchor((SPCanvasText* )canvas_tooltip, -1, 1);
222                 _snaptarget_tooltip = _desktop->add_temporary_canvasitem(canvas_tooltip, timeout_val);
223         }
226 void
227 SnapIndicator::remove_snaptarget()
229     if (_snaptarget) {
230         _desktop->remove_temporary_canvasitem(_snaptarget);
231         _snaptarget = NULL;
232     }
234     if (_snaptarget_tooltip) {
235                 _desktop->remove_temporary_canvasitem(_snaptarget_tooltip);
236                 _snaptarget_tooltip = NULL;
237         }
241 void
242 SnapIndicator::set_new_snapsource(std::pair<Geom::Point, int> const p)
244         remove_snapsource();
246     g_assert(_desktop != NULL);
248     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
249     bool value = prefs->getBool("/options/snapindicator/value", true);
251     if (value) {
252         SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
253                                                         SP_TYPE_CTRL,
254                                                         "anchor", GTK_ANCHOR_CENTER,
255                                                         "size", 10.0,
256                                                         "stroked", TRUE,
257                                                         "stroke_color", 0xf000f0ff,
258                                                         "mode", SP_KNOT_MODE_XOR,
259                                                         "shape", SP_KNOT_SHAPE_CIRCLE,
260                                                         NULL );
262         SP_CTRL(canvasitem)->moveto(p.first);
263         _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
264         }
267 void
268 SnapIndicator::remove_snapsource()
270     if (_snapsource) {
271         _desktop->remove_temporary_canvasitem(_snapsource);
272         _snapsource = NULL;
273     }
276 } //namespace Display
277 } /* namespace Inkscape */
280 /*
281   Local Variables:
282   mode:c++
283   c-file-style:"stroustrup"
284   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
285   indent-tabs-mode:nil
286   fill-column:99
287   End:
288 */
289 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :