Code

4d05b9f01b4e46ef456ab95fbe40bd9a13f4e02c
[inkscape.git] / src / nodepath.h
1 #ifndef __SP_NODEPATH_H__
2 #define __SP_NODEPATH_H__
4 /** \file
5  * Path handler in node edit mode
6  */
8 /*
9  * Authors:
10  *   Lauris Kaplinski <lauris@kaplinski.com>
11  *
12  * This code is in public domain
13  */
15 //#include "knot.h"
16 //#include "sp-path.h"
17 //#include "desktop-handles.h"
18 #include "libnr/nr-path-code.h"
19 #include <glibmm/ustring.h>
20 #include <gdk/gdkevents.h>
21 #include <list>
22 #include <2geom/forward.h>
23 #include <boost/optional.hpp>
26 struct SPCanvasItem;
27 class SPCurve;
28 struct SPItem;
29 class SPObject;
30 class SPDesktop;
31 class SPPath;
32 class SPKnot;
33 class LivePathEffectObject;
35 namespace Inkscape {
36   namespace XML {
37     class Node;
38   }
40   namespace LivePathEffect {
41     class Effect;
42   }
43 }
45 typedef std::map<Inkscape::LivePathEffect::Effect *, std::vector<SPCanvasItem *> > HelperPathList;
47 /**
48  * Radial objects are represented by an angle and a distance from
49  * 0,0.  0,0 is represented by a == big_num.
50  */
51 class Radial{
52  public:
53 /**  Radius */
54         double r;
55 /**  Amplitude */
56         double a;
57         Radial() {}
58         //      Radial(Geom::Point const &p); // Convert a point to radial coordinates
59         Radial(Radial &p) : r(p.r),a(p.a) {}
60         //      operator Geom::Point() const;
62 /**
63  * Construct Radial from Geom::Point.
64  */
65 Radial(Geom::Point const &p)
66 {
67         r = Geom::L2(p);
68         if (r > 0) {
69                 a = Geom::atan2 (p);
70         } else {
71                 a = HUGE_VAL; //undefined
72         }
73 }
75 /**
76  * Cast Radial to cartesian Geom::Point.
77  */
78 operator Geom::Point() const
79 {
80         if (a == HUGE_VAL) {
81                 return Geom::Point(0,0);
82         } else {
83                 return r*Geom::Point(cos(a), sin(a));
84         }
85 }
87 };
89 class ShapeEditor;
91 namespace Inkscape {
92 namespace NodePath {
94 /**
95  * The entire nodepath, containing multiple subpaths
96  */
97 class Path;
99 /**
100  * A subpath is a continuous chain of linked nodes
101  */
102 class SubPath;
104 /**
105  * One side of a node, i.e. prev or next
106  */
107 class NodeSide;
109 /**
110  * A node on a subpath
111  */
112 class Node;
115 /**
116  *  This is the lowest list item, a simple list of nodes.
117  */
118 class SubPath {
119  public:
120 /**  The parent of this subpath */
121         Path * nodepath;
122 /**  Is this path closed (no endpoints) or not?*/
123         gboolean closed;
124 /**  The nodes in this subpath. */
125         GList * nodes;
126 /**  The first node of the subpath (does not imply open/closed)*/
127         Node * first;
128 /**  The last node of the subpath */
129         Node * last;
130 };
134 /**
135  *  What kind of node is this?  This is the value for the node->type
136  *  field.  NodeType indicates the degree of continuity required for
137  *  the node.  I think that the corresponding integer indicates which
138  *  derivate is connected. (Thus 2 means that the node is continuous
139  *  to the second derivative, i.e. has matching endpoints and tangents)
140  */
141 typedef enum {
142 /**  A normal node */
143         NODE_NONE,
144 /**  This node non-continuously joins two segments.*/
145         NODE_CUSP,
146 /**  This node continuously joins two segments. */
147         NODE_SMOOTH,
148 /**  This node has automatic handles. */
149         NODE_AUTO,
150 /**  This node is symmetric. */
151         NODE_SYMM
152 } NodeType;
156 /**
157  * A NodeSide is a datarecord which may be on either side (n or p) of a node,
158  * which describes the segment going to the next node.
159  */
160 class NodeSide{
161  public:
162 /**  Pointer to the next node, */
163         Node * other;
164 /**  Position */
165         Geom::Point pos;
166 /**  Origin (while dragging) in radial notation */
167         Radial origin_radial;
168 /**  Origin (while dragging) in x/y notation */
169         Geom::Point origin;
170 /**  Knots are Inkscape's way of providing draggable points.  This
171  *  Knot is the point on the curve representing the control point in a
172  *  bezier curve.*/
173         SPKnot * knot;
174 /**  What kind of rendering? */
175         SPCanvasItem * line;
176 };
178 /**
179  * A node along a NodePath
180  */
181 class Node {
182  public:
183 /**  The parent subpath of this node */
184         SubPath * subpath;
185 /**  Type is selected from NodeType.*/
186         guint type : 4;
187 /**  Code refers to which ArtCode is used to represent the segment
188  *  (which segment?).*/
189         guint code : 4;
190 /**  Boolean.  Am I currently selected or not? */
191         guint selected : 1;
192 /**  */
193         Geom::Point pos;
194 /**  */
195         Geom::Point origin;
196 /**  Knots are Inkscape's way of providing draggable points.  This
197  *  Knot is the point on the curve representing the endpoint.*/
198         SPKnot * knot;
199 /**  The NodeSide in the 'next' direction */
200         NodeSide n;
201 /**  The NodeSide in the 'previous' direction */
202         NodeSide p;
204         /** The pointer to the nodeside which we are dragging out with Shift */
205         NodeSide *dragging_out;
206   
207   /** Boolean.  Am I being dragged? */
208   guint is_dragging : 1;
209 };
211 /**
212  *  This is a collection of subpaths which contain nodes
213  *
214  * In the following data model.   Nodepaths are made up of subpaths which
215  * are comprised of nodes.
216  *
217  * Nodes are linked thus:
218  * \verbatim
219            n              other
220     node -----> nodeside ------> node            \endverbatim
221  */
222 class Path {
223  public:
224 /**  Pointer to the current desktop, for reporting purposes */
225         SPDesktop * desktop;
226 /**  The parent path of this nodepath */
227         SPObject * object;
228 /**  The parent livepatheffect of this nodepath, if applicable */
229     SPItem * item;
230 /**  The context which created this nodepath.  Important if this nodepath is deleted */
231         ShapeEditor *shape_editor;
232 /**  The subpaths which comprise this NodePath */
233         GList * subpaths;
234 /**  A list of nodes which are currently selected */
235         GList * selected;
236 /**  Transforms (userspace <---> virtual space?   someone please describe )
237          njh: I'd be guessing that these are item <-> desktop transforms.*/
238         Geom::Matrix i2d, d2i;
239 /**  The DOM node which describes this NodePath */
240     Inkscape::XML::Node *repr;
241     gchar *repr_key;
242     gchar *repr_nodetypes_key;
243         //STL compliant method to get the selected nodes
244         void selection(std::list<Node *> &l);
246         guint numSelected() {return (selected? g_list_length(selected) : 0);}
247         Geom::Point& singleSelectedCoords() {return (((Node *) selected->data)->pos);}
249     /// draw a "sketch" of the path by using these variables
250     SPCanvasItem *helper_path;
251     SPCurve *curve;
252     bool show_helperpath;
253     guint32 helperpath_rgba;
254     gdouble helperpath_width;
256     // the helperpaths provided by all LPEs (and their paramaters) of the current item
257     HelperPathList* helper_path_vec;
259       /// true if we changed repr, to tell this change from an external one such as from undo, simplify, or another desktop
260         unsigned int local_change;
262         /// true if we're showing selected nodes' handles
263         bool show_handles;
265     /// true if the path cannot contain curves, just straight lines
266     bool straight_path;
268         /// active_node points to the node that is currently mouseovered (= NULL if
269         /// there isn't any); we also consider the node mouseovered if it is covered
270         /// by one of its handles and the latter is mouseovered
271         static Node *active_node;
272 };
274 }  // namespace NodePath
275 }  // namespace Inkscape
277 enum {
278   SCULPT_PROFILE_LINEAR,
279   SCULPT_PROFILE_BELL,
280   SCULPT_PROFILE_ELLIPTIC
281 };
283 // Do function documentation in nodepath.cpp
284 Inkscape::NodePath::Path * sp_nodepath_new (SPDesktop * desktop, SPObject *object, bool show_handles, const char * repr_key = NULL, SPItem *item = NULL);
285 void sp_nodepath_destroy (Inkscape::NodePath::Path * nodepath);
286 void sp_nodepath_deselect (Inkscape::NodePath::Path *nodepath);
287 void sp_nodepath_select_all (Inkscape::NodePath::Path *nodepath, bool invert);
288 void sp_nodepath_select_all_from_subpath(Inkscape::NodePath::Path *nodepath, bool invert);
289 void sp_nodepath_select_next (Inkscape::NodePath::Path *nodepath);
290 void sp_nodepath_select_prev (Inkscape::NodePath::Path *nodepath);
291 void sp_nodepath_select_rect (Inkscape::NodePath::Path * nodepath, Geom::Rect const &b, gboolean incremental);
292 GList *save_nodepath_selection (Inkscape::NodePath::Path *nodepath);
293 void restore_nodepath_selection (Inkscape::NodePath::Path *nodepath, GList *r);
294 gboolean nodepath_repr_d_changed (Inkscape::NodePath::Path * np, const char *newd);
295 gboolean nodepath_repr_typestr_changed (Inkscape::NodePath::Path * np, const char *newtypestr);
296 gboolean node_key (GdkEvent * event);
297 void sp_nodepath_update_repr(Inkscape::NodePath::Path *np, const gchar *annotation);
298 void sp_nodepath_update_statusbar (Inkscape::NodePath::Path *nodepath);
299 void sp_nodepath_selected_align(Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis);
300 void sp_nodepath_selected_distribute(Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis);
301 void sp_nodepath_select_segment_near_point(Inkscape::NodePath::Path *nodepath, Geom::Point p, bool toggle);
302 void sp_nodepath_add_node_near_point(Inkscape::NodePath::Path *nodepath, Geom::Point p);
303 void sp_nodepath_curve_drag(Inkscape::NodePath::Path *nodepath, int node, double t, Geom::Point delta);
304 Inkscape::NodePath::Node * sp_nodepath_get_node_by_index(Inkscape::NodePath::Path *np, int index);
305 bool sp_node_side_is_line (Inkscape::NodePath::Node *node, Inkscape::NodePath::NodeSide *side);
307 /* possibly private functions */
309 void sp_node_selected_add_node (Inkscape::NodePath::Path *nodepath);
310 void sp_node_selected_break (Inkscape::NodePath::Path *nodepath);
311 void sp_node_selected_duplicate (Inkscape::NodePath::Path *nodepath);
312 void sp_node_selected_join (Inkscape::NodePath::Path *nodepath);
313 void sp_node_selected_join_segment (Inkscape::NodePath::Path *nodepath);
314 void sp_node_delete_preserve (GList *nodes_to_delete);
315 void sp_node_selected_delete (Inkscape::NodePath::Path *nodepath);
316 void sp_node_selected_delete_segment (Inkscape::NodePath::Path *nodepath);
317 void sp_node_selected_set_type (Inkscape::NodePath::Path *nodepath, Inkscape::NodePath::NodeType type);
318 void sp_node_selected_set_line_type (Inkscape::NodePath::Path *nodepath, NRPathcode code);
319 void sp_node_selected_move (Inkscape::NodePath::Path *nodepath, gdouble dx, gdouble dy);
320 void sp_node_selected_move_screen (SPDesktop *desktop, Inkscape::NodePath::Path *nodepath, gdouble dx, gdouble dy);
321 void sp_node_selected_move_absolute (Inkscape::NodePath::Path *nodepath, Geom::Coord val, Geom::Dim2 axis);
322 Geom::Rect sp_node_selected_bbox (Inkscape::NodePath::Path *nodepath);
323 boost::optional<Geom::Coord> sp_node_selected_common_coord (Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis);
325 void sp_nodepath_show_handles(Inkscape::NodePath::Path *nodepath, bool show);
326 //SPCanvasItem *sp_nodepath_generate_helperpath(SPDesktop *desktop, SPCurve *curve, const SPItem *item, guint32 color);
327 //SPCanvasItem *sp_nodepath_generate_helperpath(SPDesktop *desktop, SPPath *path);
328 SPCanvasItem *sp_nodepath_helperpath_from_path(SPDesktop *desktop, SPPath *path);
329 void sp_nodepath_show_helperpath(Inkscape::NodePath::Path *nodepath, bool show);
330 void sp_nodepath_update_helperpaths(Inkscape::NodePath::Path *np);
331 void sp_nodepath_make_straight_path(Inkscape::NodePath::Path *np);
333 void sp_nodepath_selected_nodes_rotate (Inkscape::NodePath::Path * nodepath, gdouble angle, int which, bool screen);
335 void sp_nodepath_selected_nodes_scale (Inkscape::NodePath::Path * nodepath, gdouble grow, int which);
336 void sp_nodepath_selected_nodes_scale_screen (Inkscape::NodePath::Path * nodepath, gdouble grow, int which);
338 void sp_nodepath_flip (Inkscape::NodePath::Path *nodepath, Geom::Dim2 axis, boost::optional<Geom::Point> center);
340 #endif