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_GRID:
60 case SNAPTARGET_GRID_INTERSECTION:
61 case SNAPTARGET_GUIDE:
62 case SNAPTARGET_GUIDE_INTERSECTION:
63 case SNAPTARGET_GRID_GUIDE_INTERSECTION:
64 case SNAPTARGET_NODE:
65 case SNAPTARGET_PATH:
66 case SNAPTARGET_PATH_INTERSECTION:
67 case SNAPTARGET_BBOX_CORNER:
68 case SNAPTARGET_BBOX_EDGE:
69 case SNAPTARGET_GRADIENT:
70 case SNAPTARGET_UNDEFINED:
71 default:
72 canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
73 SP_TYPE_CTRL,
74 "anchor", GTK_ANCHOR_CENTER,
75 "size", 10.0,
76 "stroked", TRUE,
77 "stroke_color", 0xf000f0ff,
78 "mode", SP_KNOT_MODE_XOR,
79 "shape", SP_KNOT_SHAPE_CROSS,
80 NULL );
81 break;
82 }
84 SP_CTRL(canvasitem)->moveto(p.getPoint());
85 remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
86 _snaptarget = _desktop->add_temporary_canvasitem(canvasitem, 1000); // TODO add preference for snap indicator timeout
87 }
88 }
90 void
91 SnapIndicator::remove_snaptarget()
92 {
93 if (_snaptarget) {
94 _desktop->remove_temporary_canvasitem(_snaptarget);
95 _snaptarget = NULL;
96 }
97 }
99 void
100 SnapIndicator::set_new_snapsource(Geom::Point const p)
101 {
102 remove_snapsource();
104 g_assert(_desktop != NULL);
106 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
107 bool value = prefs->getBool("/options/snapindicator/value", true);
109 if (value) {
110 SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
111 SP_TYPE_CTRL,
112 "anchor", GTK_ANCHOR_CENTER,
113 "size", 10.0,
114 "stroked", TRUE,
115 "stroke_color", 0xf000f0ff,
116 "mode", SP_KNOT_MODE_XOR,
117 "shape", SP_KNOT_SHAPE_DIAMOND,
118 NULL );
120 SP_CTRL(canvasitem)->moveto(p);
121 _snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
122 }
123 }
125 void
126 SnapIndicator::remove_snapsource()
127 {
128 if (_snapsource) {
129 _desktop->remove_temporary_canvasitem(_snapsource);
130 _snapsource = NULL;
131 }
132 }
136 } //namespace Display
137 } /* namespace Inkscape */
139 /*
140 Local Variables:
141 mode:c++
142 c-file-style:"stroustrup"
143 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
144 indent-tabs-mode:nil
145 fill-column:99
146 End:
147 */
148 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :