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 2008 <j.b.c.engelen@utwente.nl>
9 * Copyright (C) Diederik van Lierop 2008 <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 "knot.h"
20 #include "preferences.h"
22 namespace Inkscape {
23 namespace Display {
25 SnapIndicator::SnapIndicator(SPDesktop * desktop)
26 : _snaptarget(NULL),
27 _snapsource(NULL),
28 _desktop(desktop)
29 {
30 }
32 SnapIndicator::~SnapIndicator()
33 {
34 // remove item that might be present
35 remove_snaptarget();
36 remove_snapsource();
37 }
39 void
40 SnapIndicator::set_new_snaptarget(Inkscape::SnappedPoint const p)
41 {
42 remove_snaptarget();
44 g_assert(_desktop != NULL);
46 /* Commented out for now, because this might hide any snapping bug!
47 if (!p.getSnapped()) {
48 return; // If we haven't snapped, then it is of no use to draw a snapindicator
49 }
50 */
52 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
53 bool value = prefs->getBool("/options/snapindicator/value", true);
55 if (value) {
56 SPCanvasItem * canvasitem = NULL;
57 switch (p.getTarget()) {
58 /// @todo add the different kinds of snapindicator visuals
59 case SNAPTARGET_NODE:
60 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
61 SP_TYPE_CTRL,
62 "anchor", GTK_ANCHOR_CENTER,
63 "size", 10.0,
64 "stroked", TRUE,
65 "stroke_color", 0xf000f0ff,
66 "mode", SP_KNOT_MODE_XOR,
67 "shape", SP_KNOT_SHAPE_DIAMOND,
68 NULL );
69 break;
70 case SNAPTARGET_GRID:
71 case SNAPTARGET_GRID_INTERSECTION:
72 case SNAPTARGET_GUIDE:
73 case SNAPTARGET_GUIDE_INTERSECTION:
74 case SNAPTARGET_GRID_GUIDE_INTERSECTION:
75 case SNAPTARGET_PATH:
76 case SNAPTARGET_PATH_INTERSECTION:
77 case SNAPTARGET_BBOX_CORNER:
78 case SNAPTARGET_BBOX_EDGE:
79 case SNAPTARGET_GRADIENT:
80 case SNAPTARGET_UNDEFINED:
81 default:
82 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
83 SP_TYPE_CTRL,
84 "anchor", GTK_ANCHOR_CENTER,
85 "size", 10.0,
86 "stroked", TRUE,
87 "stroke_color", 0xf000f0ff,
88 "mode", SP_KNOT_MODE_XOR,
89 "shape", SP_KNOT_SHAPE_CROSS,
90 NULL );
91 break;
92 }
94 SP_CTRL(canvasitem)->moveto(p.getPoint());
95 remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
96 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, 1000); // TODO add preference for snap indicator timeout
97 }
98 }
100 void
101 SnapIndicator::remove_snaptarget()
102 {
103 if (_snaptarget) {
104 _desktop->remove_temporary_canvasitem(_snaptarget);
105 _snaptarget = NULL;
106 }
107 }
109 void
110 SnapIndicator::set_new_snapsource(Geom::Point const p)
111 {
112 remove_snapsource();
114 g_assert(_desktop != NULL);
116 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
117 bool value = prefs->getBool("/options/snapindicator/value", true);
119 if (value) {
120 SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
121 SP_TYPE_CTRL,
122 "anchor", GTK_ANCHOR_CENTER,
123 "size", 10.0,
124 "stroked", TRUE,
125 "stroke_color", 0xf000f0ff,
126 "mode", SP_KNOT_MODE_XOR,
127 "shape", SP_KNOT_SHAPE_CIRCLE,
128 NULL );
130 SP_CTRL(canvasitem)->moveto(p);
131 _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
132 }
133 }
135 void
136 SnapIndicator::remove_snapsource()
137 {
138 if (_snapsource) {
139 _desktop->remove_temporary_canvasitem(_snapsource);
140 _snapsource = NULL;
141 }
142 }
146 } //namespace Display
147 } /* namespace Inkscape */
149 /*
150 Local Variables:
151 mode:c++
152 c-file-style:"stroustrup"
153 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
154 indent-tabs-mode:nil
155 fill-column:99
156 End:
157 */
158 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :