a8582ccc5d551017508f3f4e7cf49b5fd0f022ef
1 /** @file
2 * Editable node - implementation
3 */
4 /* Authors:
5 * Krzysztof KosiĆski <tweenk.pl@gmail.com>
6 *
7 * Copyright (C) 2009 Authors
8 * Released under GNU GPL, read the file 'COPYING' for more information
9 */
11 #include <iostream>
12 #include <stdexcept>
13 #include <boost/utility.hpp>
14 #include <glib.h>
15 #include <glib/gi18n.h>
16 #include <2geom/bezier-utils.h>
17 #include <2geom/transforms.h>
19 #include "display/sp-ctrlline.h"
20 #include "display/sp-canvas.h"
21 #include "display/sp-canvas-util.h"
22 #include "desktop.h"
23 #include "desktop-handles.h"
24 #include "preferences.h"
25 #include "snap.h"
26 #include "snap-preferences.h"
27 #include "sp-metrics.h"
28 #include "sp-namedview.h"
29 #include "ui/tool/control-point-selection.h"
30 #include "ui/tool/event-utils.h"
31 #include "ui/tool/multi-path-manipulator.h"
32 #include "ui/tool/node.h"
33 #include "ui/tool/path-manipulator.h"
35 namespace Inkscape {
36 namespace UI {
38 static SelectableControlPoint::ColorSet node_colors = {
39 {
40 {0xbfbfbf00, 0x000000ff}, // normal fill, stroke
41 {0xff000000, 0x000000ff}, // mouseover fill, stroke
42 {0xff000000, 0x000000ff} // clicked fill, stroke
43 },
44 {0x0000ffff, 0x000000ff}, // normal fill, stroke when selected
45 {0xff000000, 0x000000ff}, // mouseover fill, stroke when selected
46 {0xff000000, 0x000000ff} // clicked fill, stroke when selected
47 };
49 static ControlPoint::ColorSet handle_colors = {
50 {0xffffffff, 0x000000ff}, // normal fill, stroke
51 {0xff000000, 0x000000ff}, // mouseover fill, stroke
52 {0xff000000, 0x000000ff} // clicked fill, stroke
53 };
55 std::ostream &operator<<(std::ostream &out, NodeType type)
56 {
57 switch(type) {
58 case NODE_CUSP: out << 'c'; break;
59 case NODE_SMOOTH: out << 's'; break;
60 case NODE_AUTO: out << 'a'; break;
61 case NODE_SYMMETRIC: out << 'z'; break;
62 default: out << 'b'; break;
63 }
64 return out;
65 }
67 /** Computes an unit vector of the direction from first to second control point */
68 static Geom::Point direction(Geom::Point const &first, Geom::Point const &second) {
69 return Geom::unit_vector(second - first);
70 }
72 /**
73 * @class Handle
74 * @brief Control point of a cubic Bezier curve in a path.
75 *
76 * Handle keeps the node type invariant only for the opposite handle of the same node.
77 * Keeping the invariant on node moves is left to the %Node class.
78 */
80 Geom::Point Handle::_saved_other_pos(0, 0);
81 double Handle::_saved_length = 0.0;
82 bool Handle::_drag_out = false;
84 Handle::Handle(NodeSharedData const &data, Geom::Point const &initial_pos, Node *parent)
85 : ControlPoint(data.desktop, initial_pos, Gtk::ANCHOR_CENTER, SP_CTRL_SHAPE_CIRCLE, 7.0,
86 &handle_colors, data.handle_group)
87 , _parent(parent)
88 , _degenerate(true)
89 {
90 _cset = &handle_colors;
91 _handle_line = sp_canvas_item_new(data.handle_line_group, SP_TYPE_CTRLLINE, NULL);
92 setVisible(false);
93 }
94 Handle::~Handle()
95 {
96 //sp_canvas_item_hide(_handle_line);
97 gtk_object_destroy(GTK_OBJECT(_handle_line));
98 }
100 void Handle::setVisible(bool v)
101 {
102 ControlPoint::setVisible(v);
103 if (v) sp_canvas_item_show(_handle_line);
104 else sp_canvas_item_hide(_handle_line);
105 }
107 void Handle::move(Geom::Point const &new_pos)
108 {
109 Handle *other = this->other();
110 Node *node_towards = _parent->nodeToward(this); // node in direction of this handle
111 Node *node_away = _parent->nodeAwayFrom(this); // node in the opposite direction
112 Handle *towards = node_towards ? node_towards->handleAwayFrom(_parent) : NULL;
113 Handle *towards_second = node_towards ? node_towards->handleToward(_parent) : NULL;
115 if (Geom::are_near(new_pos, _parent->position())) {
116 // The handle becomes degenerate. If the segment between it and the node
117 // in its direction becomes linear and there are smooth nodes
118 // at its ends, make their handles colinear with the segment
119 if (towards && towards->isDegenerate()) {
120 if (node_towards->type() == NODE_SMOOTH) {
121 towards_second->setDirection(*_parent, *node_towards);
122 }
123 if (_parent->type() == NODE_SMOOTH) {
124 other->setDirection(*node_towards, *_parent);
125 }
126 }
127 setPosition(new_pos);
128 return;
129 }
131 if (_parent->type() == NODE_SMOOTH && Node::_is_line_segment(_parent, node_away)) {
132 // restrict movement to the line joining the nodes
133 Geom::Point direction = _parent->position() - node_away->position();
134 Geom::Point delta = new_pos - _parent->position();
135 // project the relative position on the direction line
136 Geom::Point new_delta = (Geom::dot(delta, direction)
137 / Geom::L2sq(direction)) * direction;
138 setRelativePos(new_delta);
139 return;
140 }
142 switch (_parent->type()) {
143 case NODE_AUTO:
144 _parent->setType(NODE_SMOOTH, false);
145 // fall through - auto nodes degrade into smooth nodes
146 case NODE_SMOOTH: {
147 /* for smooth nodes, we need to rotate the other handle so that it's colinear
148 * with the dragged one while conserving length. */
149 other->setDirection(new_pos, *_parent);
150 } break;
151 case NODE_SYMMETRIC:
152 // for symmetric nodes, place the other handle on the opposite side
153 other->setRelativePos(-(new_pos - _parent->position()));
154 break;
155 default: break;
156 }
158 setPosition(new_pos);
159 }
161 void Handle::setPosition(Geom::Point const &p)
162 {
163 ControlPoint::setPosition(p);
164 sp_ctrlline_set_coords(SP_CTRLLINE(_handle_line), _parent->position(), position());
166 // update degeneration info and visibility
167 if (Geom::are_near(position(), _parent->position()))
168 _degenerate = true;
169 else _degenerate = false;
170 if (_parent->_handles_shown && _parent->visible() && !_degenerate) {
171 setVisible(true);
172 } else {
173 setVisible(false);
174 }
175 // If both handles become degenerate, convert to parent cusp node
176 if (_parent->isDegenerate()) {
177 _parent->setType(NODE_CUSP, false);
178 }
179 }
181 void Handle::setLength(double len)
182 {
183 if (isDegenerate()) return;
184 Geom::Point dir = Geom::unit_vector(relativePos());
185 setRelativePos(dir * len);
186 }
188 void Handle::retract()
189 {
190 setPosition(_parent->position());
191 }
193 void Handle::setDirection(Geom::Point const &from, Geom::Point const &to)
194 {
195 setDirection(to - from);
196 }
198 void Handle::setDirection(Geom::Point const &dir)
199 {
200 Geom::Point unitdir = Geom::unit_vector(dir);
201 setRelativePos(unitdir * length());
202 }
204 char const *Handle::handle_type_to_localized_string(NodeType type)
205 {
206 switch(type) {
207 case NODE_CUSP: return _("Cusp node handle");
208 case NODE_SMOOTH: return _("Smooth node handle");
209 case NODE_SYMMETRIC: return _("Symmetric node handle");
210 case NODE_AUTO: return _("Auto-smooth node handle");
211 default: return "";
212 }
213 }
215 bool Handle::grabbed(GdkEventMotion *)
216 {
217 _saved_other_pos = other()->position();
218 _saved_length = _drag_out ? 0 : length();
219 _pm()._handleGrabbed();
220 return false;
221 }
223 void Handle::dragged(Geom::Point &new_pos, GdkEventMotion *event)
224 {
225 Geom::Point parent_pos = _parent->position();
226 Geom::Point origin = _last_drag_origin();
227 SnapManager &sm = _desktop->namedview->snap_manager;
228 bool snap = sm.someSnapperMightSnap();
230 // with Alt, preserve length
231 if (held_alt(*event)) {
232 new_pos = parent_pos + Geom::unit_vector(new_pos - parent_pos) * _saved_length;
233 snap = false;
234 }
235 // with Ctrl, constrain to M_PI/rotationsnapsperpi increments from vertical
236 // and the original position.
237 if (held_control(*event)) {
238 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
239 int snaps = 2 * prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
241 // note: if snapping to the original position is only desired in the original
242 // direction of the handle, change to Ray instead of Line
243 Geom::Line original_line(parent_pos, origin);
244 Geom::Point snap_pos = parent_pos + Geom::constrain_angle(
245 Geom::Point(0,0), new_pos - parent_pos, snaps, Geom::Point(1,0));
246 Geom::Point orig_pos = original_line.pointAt(original_line.nearestPoint(new_pos));
248 if (Geom::distance(snap_pos, new_pos) < Geom::distance(orig_pos, new_pos)) {
249 new_pos = snap_pos;
250 } else {
251 new_pos = orig_pos;
252 }
253 snap = false;
254 }
256 std::vector<Inkscape::SnapCandidatePoint> unselected;
257 if (snap) {
258 typedef ControlPointSelection::Set Set;
259 Set &nodes = _parent->_selection.allPoints();
260 for (Set::iterator i = nodes.begin(); i != nodes.end(); ++i) {
261 Node *n = static_cast<Node*>(*i);
262 Inkscape::SnapCandidatePoint p(n->position(), n->_snapSourceType(), n->_snapTargetType());
263 unselected.push_back(p);
264 }
265 sm.setupIgnoreSelection(_desktop, true, &unselected);
267 Node *node_away = (this == &_parent->_front ? _parent->_prev() : _parent->_next());
268 if (_parent->type() == NODE_SMOOTH && Node::_is_line_segment(_parent, node_away)) {
269 Inkscape::Snapper::SnapConstraint cl(_parent->position(),
270 _parent->position() - node_away->position());
271 Inkscape::SnappedPoint p;
272 p = sm.constrainedSnap(Inkscape::SnapCandidatePoint(new_pos, SNAPSOURCE_NODE_HANDLE), cl);
273 new_pos = p.getPoint();
274 } else {
275 sm.freeSnapReturnByRef(new_pos, SNAPSOURCE_NODE_HANDLE);
276 }
277 }
279 // with Shift, if the node is cusp, rotate the other handle as well
280 if (_parent->type() == NODE_CUSP && !_drag_out) {
281 if (held_shift(*event)) {
282 Geom::Point other_relpos = _saved_other_pos - parent_pos;
283 other_relpos *= Geom::Rotate(Geom::angle_between(origin - parent_pos, new_pos - parent_pos));
284 other()->setRelativePos(other_relpos);
285 } else {
286 // restore the position
287 other()->setPosition(_saved_other_pos);
288 }
289 }
290 move(new_pos); // needed for correct update, even though it's redundant
291 _pm().update();
292 }
294 void Handle::ungrabbed(GdkEventButton *event)
295 {
296 // hide the handle if it's less than dragtolerance away from the node
297 // TODO is this actually desired?
298 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
299 int drag_tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
301 Geom::Point dist = _desktop->d2w(_parent->position()) - _desktop->d2w(position());
302 if (dist.length() <= drag_tolerance) {
303 move(_parent->position());
304 }
306 // HACK: If the handle was dragged out, call parent's ungrabbed handler,
307 // so that transform handles reappear
308 if (_drag_out) {
309 _parent->ungrabbed(event);
310 }
311 _drag_out = false;
313 _pm()._handleUngrabbed();
314 }
316 bool Handle::clicked(GdkEventButton *event)
317 {
318 _pm()._handleClicked(this, event);
319 return true;
320 }
322 Handle *Handle::other()
323 {
324 if (this == &_parent->_front) return &_parent->_back;
325 return &_parent->_front;
326 }
328 static double snap_increment_degrees() {
329 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
330 int snaps = prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
331 return 180.0 / snaps;
332 }
334 Glib::ustring Handle::_getTip(unsigned state)
335 {
336 char const *more;
337 bool can_shift_rotate = _parent->type() == NODE_CUSP && !other()->isDegenerate();
338 if (can_shift_rotate) {
339 more = C_("Path handle tip", "more: Shift, Ctrl, Alt");
340 } else {
341 more = C_("Path handle tip", "more: Ctrl, Alt");
342 }
343 if (state_held_alt(state)) {
344 if (state_held_control(state)) {
345 if (state_held_shift(state) && can_shift_rotate) {
346 return format_tip(C_("Path handle tip",
347 "<b>Shift+Ctrl+Alt</b>: preserve length and snap rotation angle to %g° "
348 "increments while rotating both handles"),
349 snap_increment_degrees());
350 } else {
351 return format_tip(C_("Path handle tip",
352 "<b>Ctrl+Alt</b>: preserve length and snap rotation angle to %g° increments"),
353 snap_increment_degrees());
354 }
355 } else {
356 if (state_held_shift(state) && can_shift_rotate) {
357 return C_("Path handle tip",
358 "<b>Shift+Alt</b>: preserve handle length and rotate both handles");
359 } else {
360 return C_("Path handle tip",
361 "<b>Alt</b>: preserve handle length while dragging");
362 }
363 }
364 } else {
365 if (state_held_control(state)) {
366 if (state_held_shift(state) && can_shift_rotate) {
367 return format_tip(C_("Path handle tip",
368 "<b>Shift+Ctrl</b>: snap rotation angle to %g° increments and rotate both handles"),
369 snap_increment_degrees());
370 } else {
371 return format_tip(C_("Path handle tip",
372 "<b>Ctrl</b>: snap rotation angle to %g° increments, click to retract"),
373 snap_increment_degrees());
374 }
375 } else if (state_held_shift(state) && can_shift_rotate) {
376 return C_("Path hande tip",
377 "<b>Shift</b>: rotate both handles by the same angle");
378 }
379 }
381 switch (_parent->type()) {
382 case NODE_AUTO:
383 return format_tip(C_("Path handle tip",
384 "<b>Auto node handle</b>: drag to convert to smooth node (%s)"), more);
385 default:
386 return format_tip(C_("Path handle tip",
387 "<b>%s</b>: drag to shape the segment (%s)"),
388 handle_type_to_localized_string(_parent->type()), more);
389 }
390 }
392 Glib::ustring Handle::_getDragTip(GdkEventMotion */*event*/)
393 {
394 Geom::Point dist = position() - _last_drag_origin();
395 // report angle in mathematical convention
396 double angle = Geom::angle_between(Geom::Point(-1,0), position() - _parent->position());
397 angle += M_PI; // angle is (-M_PI...M_PI] - offset by +pi and scale to 0...360
398 angle *= 360.0 / (2 * M_PI);
399 GString *x = SP_PX_TO_METRIC_STRING(dist[Geom::X], _desktop->namedview->getDefaultMetric());
400 GString *y = SP_PX_TO_METRIC_STRING(dist[Geom::Y], _desktop->namedview->getDefaultMetric());
401 GString *len = SP_PX_TO_METRIC_STRING(length(), _desktop->namedview->getDefaultMetric());
402 Glib::ustring ret = format_tip(C_("Path handle tip",
403 "Move handle by %s, %s; angle %.2f°, length %s"), x->str, y->str, angle, len->str);
404 g_string_free(x, TRUE);
405 g_string_free(y, TRUE);
406 g_string_free(len, TRUE);
407 return ret;
408 }
410 /**
411 * @class Node
412 * @brief Curve endpoint in an editable path.
413 *
414 * The method move() keeps node type invariants during translations.
415 */
417 Node::Node(NodeSharedData const &data, Geom::Point const &initial_pos)
418 : SelectableControlPoint(data.desktop, initial_pos, Gtk::ANCHOR_CENTER,
419 SP_CTRL_SHAPE_DIAMOND, 9.0, *data.selection, &node_colors, data.node_group)
420 , _front(data, initial_pos, this)
421 , _back(data, initial_pos, this)
422 , _type(NODE_CUSP)
423 , _handles_shown(false)
424 {
425 // NOTE we do not set type here, because the handles are still degenerate
426 }
428 // NOTE: not using iterators won't make this much quicker because iterators can be 100% inlined.
429 Node *Node::_next()
430 {
431 NodeList::iterator n = NodeList::get_iterator(this).next();
432 if (n) return n.ptr();
433 return NULL;
434 }
435 Node *Node::_prev()
436 {
437 NodeList::iterator p = NodeList::get_iterator(this).prev();
438 if (p) return p.ptr();
439 return NULL;
440 }
442 void Node::move(Geom::Point const &new_pos)
443 {
444 // move handles when the node moves.
445 Geom::Point old_pos = position();
446 Geom::Point delta = new_pos - position();
447 setPosition(new_pos);
448 _front.setPosition(_front.position() + delta);
449 _back.setPosition(_back.position() + delta);
451 // if the node has a smooth handle after a line segment, it should be kept colinear
452 // with the segment
453 _fixNeighbors(old_pos, new_pos);
454 }
456 void Node::transform(Geom::Matrix const &m)
457 {
458 Geom::Point old_pos = position();
459 setPosition(position() * m);
460 _front.setPosition(_front.position() * m);
461 _back.setPosition(_back.position() * m);
463 /* Affine transforms keep handle invariants for smooth and symmetric nodes,
464 * but smooth nodes at ends of linear segments and auto nodes need special treatment */
465 _fixNeighbors(old_pos, position());
466 }
468 Geom::Rect Node::bounds()
469 {
470 Geom::Rect b(position(), position());
471 b.expandTo(_front.position());
472 b.expandTo(_back.position());
473 return b;
474 }
476 void Node::_fixNeighbors(Geom::Point const &old_pos, Geom::Point const &new_pos)
477 {
478 /* This method restores handle invariants for neighboring nodes,
479 * and invariants that are based on positions of those nodes for this one. */
481 /* Fix auto handles */
482 if (_type == NODE_AUTO) _updateAutoHandles();
483 if (old_pos != new_pos) {
484 if (_next() && _next()->_type == NODE_AUTO) _next()->_updateAutoHandles();
485 if (_prev() && _prev()->_type == NODE_AUTO) _prev()->_updateAutoHandles();
486 }
488 /* Fix smooth handles at the ends of linear segments.
489 * Rotate the appropriate handle to be colinear with the segment.
490 * If there is a smooth node at the other end of the segment, rotate it too. */
491 Handle *handle, *other_handle;
492 Node *other;
493 if (_is_line_segment(this, _next())) {
494 handle = &_back;
495 other = _next();
496 other_handle = &_next()->_front;
497 } else if (_is_line_segment(_prev(), this)) {
498 handle = &_front;
499 other = _prev();
500 other_handle = &_prev()->_back;
501 } else return;
503 if (_type == NODE_SMOOTH && !handle->isDegenerate()) {
504 handle->setDirection(other->position(), new_pos);
505 }
506 // also update the handle on the other end of the segment
507 if (other->_type == NODE_SMOOTH && !other_handle->isDegenerate()) {
508 other_handle->setDirection(new_pos, other->position());
509 }
510 }
512 void Node::_updateAutoHandles()
513 {
514 // Recompute the position of automatic handles.
515 // For endnodes, retract both handles. (It's only possible to create an end auto node
516 // through the XML editor.)
517 if (isEndNode()) {
518 _front.retract();
519 _back.retract();
520 return;
521 }
523 // Auto nodes automaticaly adjust their handles to give an appearance of smoothness,
524 // no matter what their surroundings are.
525 Geom::Point vec_next = _next()->position() - position();
526 Geom::Point vec_prev = _prev()->position() - position();
527 double len_next = vec_next.length(), len_prev = vec_prev.length();
528 if (len_next > 0 && len_prev > 0) {
529 // "dir" is an unit vector perpendicular to the bisector of the angle created
530 // by the previous node, this auto node and the next node.
531 Geom::Point dir = Geom::unit_vector((len_prev / len_next) * vec_next - vec_prev);
532 // Handle lengths are equal to 1/3 of the distance from the adjacent node.
533 _back.setRelativePos(-dir * (len_prev / 3));
534 _front.setRelativePos(dir * (len_next / 3));
535 } else {
536 // If any of the adjacent nodes coincides, retract both handles.
537 _front.retract();
538 _back.retract();
539 }
540 }
542 void Node::showHandles(bool v)
543 {
544 _handles_shown = v;
545 if (!_front.isDegenerate()) _front.setVisible(v);
546 if (!_back.isDegenerate()) _back.setVisible(v);
547 }
549 /** Sets the node type and optionally restores the invariants associated with the given type.
550 * @param type The type to set
551 * @param update_handles Whether to restore invariants associated with the given type.
552 * Passing false is useful e.g. wen initially creating the path,
553 * and when making cusp nodes during some node algorithms.
554 * Pass true when used in response to an UI node type button.
555 */
556 void Node::setType(NodeType type, bool update_handles)
557 {
558 if (type == NODE_PICK_BEST) {
559 pickBestType();
560 updateState(); // The size of the control might have changed
561 return;
562 }
564 // if update_handles is true, adjust handle positions to match the node type
565 // handle degenerate handles appropriately
566 if (update_handles) {
567 switch (type) {
568 case NODE_CUSP:
569 // if the existing type is also NODE_CUSP, retract handles
570 if (_type == NODE_CUSP) {
571 _front.retract();
572 _back.retract();
573 }
574 break;
575 case NODE_AUTO:
576 // auto handles make no sense for endnodes
577 if (isEndNode()) return;
578 _updateAutoHandles();
579 break;
580 case NODE_SMOOTH: {
581 // rotate handles to be colinear
582 // for degenerate nodes set positions like auto handles
583 bool prev_line = _is_line_segment(_prev(), this);
584 bool next_line = _is_line_segment(this, _next());
585 if (_type == NODE_SMOOTH) {
586 // for a node that is already smooth and has a degenerate handle,
587 // drag out the second handle to 1/3 the length of the linear segment
588 if (_front.isDegenerate()) {
589 double dist = Geom::distance(_next()->position(), position());
590 _front.setRelativePos(Geom::unit_vector(-_back.relativePos()) * dist / 3);
591 }
592 if (_back.isDegenerate()) {
593 double dist = Geom::distance(_prev()->position(), position());
594 _back.setRelativePos(Geom::unit_vector(-_front.relativePos()) * dist / 3);
595 }
596 } else if (isDegenerate()) {
597 _updateAutoHandles();
598 } else if (_front.isDegenerate()) {
599 // if the front handle is degenerate and this...next is a line segment,
600 // make back colinear; otherwise pull out the other handle
601 // to 1/3 of distance to prev
602 if (next_line) {
603 _back.setDirection(*_next(), *this);
604 } else if (_prev()) {
605 Geom::Point dir = direction(_back, *this);
606 _front.setRelativePos(Geom::distance(_prev()->position(), position()) / 3 * dir);
607 }
608 } else if (_back.isDegenerate()) {
609 if (prev_line) {
610 _front.setDirection(*_prev(), *this);
611 } else if (_next()) {
612 Geom::Point dir = direction(_front, *this);
613 _back.setRelativePos(Geom::distance(_next()->position(), position()) / 3 * dir);
614 }
615 } else {
616 // both handles are extended. make colinear while keeping length
617 // first make back colinear with the vector front ---> back,
618 // then make front colinear with back ---> node
619 // (not back ---> front because back's position was changed in the first call)
620 _back.setDirection(_front, _back);
621 _front.setDirection(_back, *this);
622 }
623 } break;
624 case NODE_SYMMETRIC:
625 if (isEndNode()) return; // symmetric handles make no sense for endnodes
626 if (isDegenerate()) {
627 // similar to auto handles but set the same length for both
628 Geom::Point vec_next = _next()->position() - position();
629 Geom::Point vec_prev = _prev()->position() - position();
630 double len_next = vec_next.length(), len_prev = vec_prev.length();
631 double len = (len_next + len_prev) / 6; // take 1/3 of average
632 if (len == 0) return;
634 Geom::Point dir = Geom::unit_vector((len_prev / len_next) * vec_next - vec_prev);
635 _back.setRelativePos(-dir * len);
636 _front.setRelativePos(dir * len);
637 } else {
638 // Both handles are extended. Compute average length, use direction from
639 // back handle to front handle. This also works correctly for degenerates
640 double len = (_front.length() + _back.length()) / 2;
641 Geom::Point dir = direction(_back, _front);
642 _front.setRelativePos(dir * len);
643 _back.setRelativePos(-dir * len);
644 }
645 break;
646 default: break;
647 }
648 }
649 _type = type;
650 _setShape(_node_type_to_shape(type));
651 updateState();
652 }
654 /** Pick the best type for this node, based on the position of its handles.
655 * This is what assigns types to nodes created using the pen tool. */
656 void Node::pickBestType()
657 {
658 _type = NODE_CUSP;
659 bool front_degen = _front.isDegenerate();
660 bool back_degen = _back.isDegenerate();
661 bool both_degen = front_degen && back_degen;
662 bool neither_degen = !front_degen && !back_degen;
663 do {
664 // if both handles are degenerate, do nothing
665 if (both_degen) break;
666 // if neither are degenerate, check their respective positions
667 if (neither_degen) {
668 Geom::Point front_delta = _front.position() - position();
669 Geom::Point back_delta = _back.position() - position();
670 // for now do not automatically make nodes symmetric, it can be annoying
671 /*if (Geom::are_near(front_delta, -back_delta)) {
672 _type = NODE_SYMMETRIC;
673 break;
674 }*/
675 if (Geom::are_near(Geom::unit_vector(front_delta),
676 Geom::unit_vector(-back_delta)))
677 {
678 _type = NODE_SMOOTH;
679 break;
680 }
681 }
682 // check whether the handle aligns with the previous line segment.
683 // we know that if front is degenerate, back isn't, because
684 // both_degen was false
685 if (front_degen && _next() && _next()->_back.isDegenerate()) {
686 Geom::Point segment_delta = Geom::unit_vector(_next()->position() - position());
687 Geom::Point handle_delta = Geom::unit_vector(_back.position() - position());
688 if (Geom::are_near(segment_delta, -handle_delta)) {
689 _type = NODE_SMOOTH;
690 break;
691 }
692 } else if (back_degen && _prev() && _prev()->_front.isDegenerate()) {
693 Geom::Point segment_delta = Geom::unit_vector(_prev()->position() - position());
694 Geom::Point handle_delta = Geom::unit_vector(_front.position() - position());
695 if (Geom::are_near(segment_delta, -handle_delta)) {
696 _type = NODE_SMOOTH;
697 break;
698 }
699 }
700 } while (false);
701 _setShape(_node_type_to_shape(_type));
702 updateState();
703 }
705 bool Node::isEndNode()
706 {
707 return !_prev() || !_next();
708 }
710 /** Move the node to the bottom of its canvas group. Useful for node break, to ensure that
711 * the selected nodes are above the unselected ones. */
712 void Node::sink()
713 {
714 sp_canvas_item_move_to_z(_canvas_item, 0);
715 }
717 NodeType Node::parse_nodetype(char x)
718 {
719 switch (x) {
720 case 'a': return NODE_AUTO;
721 case 'c': return NODE_CUSP;
722 case 's': return NODE_SMOOTH;
723 case 'z': return NODE_SYMMETRIC;
724 default: return NODE_PICK_BEST;
725 }
726 }
728 /** Customized event handler to catch scroll events needed for selection grow/shrink. */
729 bool Node::_eventHandler(GdkEvent *event)
730 {
731 static NodeList::iterator origin;
732 static int dir;
734 switch (event->type)
735 {
736 case GDK_SCROLL:
737 if (event->scroll.direction == GDK_SCROLL_UP) {
738 dir = 1;
739 } else if (event->scroll.direction == GDK_SCROLL_DOWN) {
740 dir = -1;
741 } else break;
742 if (held_control(event->scroll)) {
743 _selection.spatialGrow(this, dir);
744 } else {
745 _linearGrow(dir);
746 }
747 return true;
748 default:
749 break;
750 }
751 return ControlPoint::_eventHandler(event);
752 }
754 // TODO Move this to 2Geom!
755 static double bezier_length (Geom::Point a0, Geom::Point a1, Geom::Point a2, Geom::Point a3)
756 {
757 double lower = Geom::distance(a0, a3);
758 double upper = Geom::distance(a0, a1) + Geom::distance(a1, a2) + Geom::distance(a2, a3);
760 if (upper - lower < Geom::EPSILON) return (lower + upper)/2;
762 Geom::Point // Casteljau subdivision
763 b0 = a0,
764 c0 = a3,
765 b1 = 0.5*(a0 + a1),
766 t0 = 0.5*(a1 + a2),
767 c1 = 0.5*(a2 + a3),
768 b2 = 0.5*(b1 + t0),
769 c2 = 0.5*(t0 + c1),
770 b3 = 0.5*(b2 + c2); // == c3
771 return bezier_length(b0, b1, b2, b3) + bezier_length(b3, c2, c1, c0);
772 }
774 /** Select or deselect a node in this node's subpath based on its path distance from this node.
775 * @param dir If negative, shrink selection by one node; if positive, grow by one node */
776 void Node::_linearGrow(int dir)
777 {
778 // Interestingly, we do not need any help from PathManipulator when doing linear grow.
779 // First handle the trivial case of growing over an unselected node.
780 if (!selected() && dir > 0) {
781 _selection.insert(this);
782 return;
783 }
785 NodeList::iterator this_iter = NodeList::get_iterator(this);
786 NodeList::iterator fwd = this_iter, rev = this_iter;
787 double distance_back = 0, distance_front = 0;
789 // Linear grow is simple. We find the first unselected nodes in each direction
790 // and compare the linear distances to them.
791 if (dir > 0) {
792 if (!selected()) {
793 _selection.insert(this);
794 return;
795 }
797 // find first unselected nodes on both sides
798 while (fwd && fwd->selected()) {
799 NodeList::iterator n = fwd.next();
800 distance_front += bezier_length(*fwd, fwd->_front, n->_back, *n);
801 fwd = n;
802 if (fwd == this_iter)
803 // there is no unselected node in this cyclic subpath
804 return;
805 }
806 // do the same for the second direction. Do not check for equality with
807 // this node, because there is at least one unselected node in the subpath,
808 // so we are guaranteed to stop.
809 while (rev && rev->selected()) {
810 NodeList::iterator p = rev.prev();
811 distance_back += bezier_length(*rev, rev->_back, p->_front, *p);
812 rev = p;
813 }
815 NodeList::iterator t; // node to select
816 if (fwd && rev) {
817 if (distance_front <= distance_back) t = fwd;
818 else t = rev;
819 } else {
820 if (fwd) t = fwd;
821 if (rev) t = rev;
822 }
823 if (t) _selection.insert(t.ptr());
825 // Linear shrink is more complicated. We need to find the farthest selected node.
826 // This means we have to check the entire subpath. We go in the direction in which
827 // the distance we traveled is lower. We do this until we run out of nodes (ends of path)
828 // or the two iterators meet. On the way, we store the last selected node and its distance
829 // in each direction (if any). At the end, we choose the one that is farther and deselect it.
830 } else {
831 // both iterators that store last selected nodes are initially empty
832 NodeList::iterator last_fwd, last_rev;
833 double last_distance_back = 0, last_distance_front = 0;
835 while (rev || fwd) {
836 if (fwd && (!rev || distance_front <= distance_back)) {
837 if (fwd->selected()) {
838 last_fwd = fwd;
839 last_distance_front = distance_front;
840 }
841 NodeList::iterator n = fwd.next();
842 if (n) distance_front += bezier_length(*fwd, fwd->_front, n->_back, *n);
843 fwd = n;
844 } else if (rev && (!fwd || distance_front > distance_back)) {
845 if (rev->selected()) {
846 last_rev = rev;
847 last_distance_back = distance_back;
848 }
849 NodeList::iterator p = rev.prev();
850 if (p) distance_back += bezier_length(*rev, rev->_back, p->_front, *p);
851 rev = p;
852 }
853 // Check whether we walked the entire cyclic subpath.
854 // This is initially true because both iterators start from this node,
855 // so this check cannot go in the while condition.
856 // When this happens, we need to check the last node, pointed to by the iterators.
857 if (fwd && fwd == rev) {
858 if (!fwd->selected()) break;
859 NodeList::iterator fwdp = fwd.prev(), revn = rev.next();
860 double df = distance_front + bezier_length(*fwdp, fwdp->_front, fwd->_back, *fwd);
861 double db = distance_back + bezier_length(*revn, revn->_back, rev->_front, *rev);
862 if (df > db) {
863 last_fwd = fwd;
864 last_distance_front = df;
865 } else {
866 last_rev = rev;
867 last_distance_back = db;
868 }
869 break;
870 }
871 }
873 NodeList::iterator t;
874 if (last_fwd && last_rev) {
875 if (last_distance_front >= last_distance_back) t = last_fwd;
876 else t = last_rev;
877 } else {
878 if (last_fwd) t = last_fwd;
879 if (last_rev) t = last_rev;
880 }
881 if (t) _selection.erase(t.ptr());
882 }
883 }
885 void Node::_setState(State state)
886 {
887 // change node size to match type and selection state
888 switch (_type) {
889 case NODE_AUTO:
890 case NODE_CUSP:
891 if (selected()) _setSize(11);
892 else _setSize(9);
893 break;
894 default:
895 if(selected()) _setSize(9);
896 else _setSize(7);
897 break;
898 }
899 SelectableControlPoint::_setState(state);
900 }
902 bool Node::grabbed(GdkEventMotion *event)
903 {
904 if (SelectableControlPoint::grabbed(event))
905 return true;
907 // Dragging out handles with Shift + drag on a node.
908 if (!held_shift(*event)) return false;
910 Handle *h;
911 Geom::Point evp = event_point(*event);
912 Geom::Point rel_evp = evp - _last_click_event_point();
914 // This should work even if dragtolerance is zero and evp coincides with node position.
915 double angle_next = HUGE_VAL;
916 double angle_prev = HUGE_VAL;
917 bool has_degenerate = false;
918 // determine which handle to drag out based on degeneration and the direction of drag
919 if (_front.isDegenerate() && _next()) {
920 Geom::Point next_relpos = _desktop->d2w(_next()->position())
921 - _desktop->d2w(position());
922 angle_next = fabs(Geom::angle_between(rel_evp, next_relpos));
923 has_degenerate = true;
924 }
925 if (_back.isDegenerate() && _prev()) {
926 Geom::Point prev_relpos = _desktop->d2w(_prev()->position())
927 - _desktop->d2w(position());
928 angle_prev = fabs(Geom::angle_between(rel_evp, prev_relpos));
929 has_degenerate = true;
930 }
931 if (!has_degenerate) return false;
932 h = angle_next < angle_prev ? &_front : &_back;
934 h->setPosition(_desktop->w2d(evp));
935 h->setVisible(true);
936 h->transferGrab(this, event);
937 Handle::_drag_out = true;
938 return true;
939 }
941 void Node::dragged(Geom::Point &new_pos, GdkEventMotion *event)
942 {
943 // For a note on how snapping is implemented in Inkscape, see snap.h.
944 SnapManager &sm = _desktop->namedview->snap_manager;
945 bool snap = sm.someSnapperMightSnap();
946 Inkscape::SnappedPoint sp;
947 if (snap) {
948 /* setup
949 * TODO We are doing this every time a snap happens. It should once be done only once
950 * per drag - maybe in the grabbed handler?
951 * TODO Unselected nodes vector must be valid during the snap run, because it is not
952 * copied. Fix this in snap.h and snap.cpp, then the above.
953 * TODO Snapping to unselected segments of selected paths doesn't work yet. */
955 // Build the list of unselected nodes.
956 std::vector<Inkscape::SnapCandidatePoint> unselected;
957 typedef ControlPointSelection::Set Set;
958 Set &nodes = _selection.allPoints();
959 for (Set::iterator i = nodes.begin(); i != nodes.end(); ++i) {
960 if (!(*i)->selected()) {
961 Node *n = static_cast<Node*>(*i);
962 Inkscape::SnapCandidatePoint p(n->position(), n->_snapSourceType(), n->_snapTargetType());
963 unselected.push_back(p);
964 }
965 }
966 sm.setupIgnoreSelection(_desktop, true, &unselected);
967 }
969 if (held_control(*event)) {
970 Geom::Point origin = _last_drag_origin();
971 std::vector<Inkscape::Snapper::SnapConstraint> constraints;
972 if (held_alt(*event)) {
973 // with Ctrl+Alt, constrain to handle lines
974 // project the new position onto a handle line that is closer;
975 // also snap to perpendiculars of handle lines
977 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
978 int snaps = prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000);
979 double min_angle = M_PI / snaps;
981 boost::optional<Geom::Point> front_point, back_point, fperp_point, bperp_point;
982 if (_front.isDegenerate()) {
983 if (_is_line_segment(this, _next()))
984 front_point = _next()->position() - origin;
985 } else {
986 front_point = _front.relativePos();
987 }
988 if (_back.isDegenerate()) {
989 if (_is_line_segment(_prev(), this))
990 back_point = _prev()->position() - origin;
991 } else {
992 back_point = _back.relativePos();
993 }
994 if (front_point) {
995 constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *front_point));
996 fperp_point = Geom::rot90(*front_point);
997 }
998 if (back_point) {
999 constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *back_point));
1000 bperp_point = Geom::rot90(*back_point);
1001 }
1002 // perpendiculars only snap when they are further than snap increment away
1003 // from the second handle constraint
1004 if (fperp_point && (!back_point ||
1005 (fabs(Geom::angle_between(*fperp_point, *back_point)) > min_angle &&
1006 fabs(Geom::angle_between(*fperp_point, *back_point)) < M_PI - min_angle)))
1007 {
1008 constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *fperp_point));
1009 }
1010 if (bperp_point && (!front_point ||
1011 (fabs(Geom::angle_between(*bperp_point, *front_point)) > min_angle &&
1012 fabs(Geom::angle_between(*bperp_point, *front_point)) < M_PI - min_angle)))
1013 {
1014 constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, *bperp_point));
1015 }
1017 sp = sm.multipleConstrainedSnaps(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()), constraints);
1018 } else {
1019 // with Ctrl, constrain to axes
1020 constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, Geom::Point(1, 0)));
1021 constraints.push_back(Inkscape::Snapper::SnapConstraint(origin, Geom::Point(0, 1)));
1022 sp = sm.multipleConstrainedSnaps(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()), constraints);
1023 }
1024 new_pos = sp.getPoint();
1025 } else if (snap) {
1026 sp = sm.freeSnap(Inkscape::SnapCandidatePoint(new_pos, _snapSourceType()));
1027 new_pos = sp.getPoint();
1028 }
1030 SelectableControlPoint::dragged(new_pos, event);
1031 }
1033 bool Node::clicked(GdkEventButton *event)
1034 {
1035 if(_pm()._nodeClicked(this, event))
1036 return true;
1037 return SelectableControlPoint::clicked(event);
1038 }
1040 Inkscape::SnapSourceType Node::_snapSourceType()
1041 {
1042 if (_type == NODE_SMOOTH || _type == NODE_AUTO)
1043 return SNAPSOURCE_NODE_SMOOTH;
1044 return SNAPSOURCE_NODE_CUSP;
1045 }
1046 Inkscape::SnapTargetType Node::_snapTargetType()
1047 {
1048 if (_type == NODE_SMOOTH || _type == NODE_AUTO)
1049 return SNAPTARGET_NODE_SMOOTH;
1050 return SNAPTARGET_NODE_CUSP;
1051 }
1053 /** @brief Gets the handle that faces the given adjacent node.
1054 * Will abort with error if the given node is not adjacent. */
1055 Handle *Node::handleToward(Node *to)
1056 {
1057 if (_next() == to) {
1058 return front();
1059 }
1060 if (_prev() == to) {
1061 return back();
1062 }
1063 g_error("Node::handleToward(): second node is not adjacent!");
1064 }
1066 /** @brief Gets the node in the direction of the given handle.
1067 * Will abort with error if the handle doesn't belong to this node. */
1068 Node *Node::nodeToward(Handle *dir)
1069 {
1070 if (front() == dir) {
1071 return _next();
1072 }
1073 if (back() == dir) {
1074 return _prev();
1075 }
1076 g_error("Node::nodeToward(): handle is not a child of this node!");
1077 }
1079 /** @brief Gets the handle that goes in the direction opposite to the given adjacent node.
1080 * Will abort with error if the given node is not adjacent. */
1081 Handle *Node::handleAwayFrom(Node *to)
1082 {
1083 if (_next() == to) {
1084 return back();
1085 }
1086 if (_prev() == to) {
1087 return front();
1088 }
1089 g_error("Node::handleAwayFrom(): second node is not adjacent!");
1090 }
1092 /** @brief Gets the node in the direction opposite to the given handle.
1093 * Will abort with error if the handle doesn't belong to this node. */
1094 Node *Node::nodeAwayFrom(Handle *h)
1095 {
1096 if (front() == h) {
1097 return _prev();
1098 }
1099 if (back() == h) {
1100 return _next();
1101 }
1102 g_error("Node::nodeAwayFrom(): handle is not a child of this node!");
1103 }
1105 Glib::ustring Node::_getTip(unsigned state)
1106 {
1107 if (state_held_shift(state)) {
1108 bool can_drag_out = (_next() && _front.isDegenerate()) || (_prev() && _back.isDegenerate());
1109 if (can_drag_out) {
1110 /*if (state_held_control(state)) {
1111 return format_tip(C_("Path node tip",
1112 "<b>Shift+Ctrl:</b> drag out a handle and snap its angle "
1113 "to %f° increments"), snap_increment_degrees());
1114 }*/
1115 return C_("Path node tip",
1116 "<b>Shift</b>: drag out a handle, click to toggle selection");
1117 }
1118 return C_("Path node tip", "<b>Shift</b>: click to toggle selection");
1119 }
1121 if (state_held_control(state)) {
1122 if (state_held_alt(state)) {
1123 return C_("Path node tip", "<b>Ctrl+Alt</b>: move along handle lines, click to delete node");
1124 }
1125 return C_("Path node tip",
1126 "<b>Ctrl</b>: move along axes, click to change node type");
1127 }
1129 if (state_held_alt(state)) {
1130 return C_("Path node tip", "<b>Alt</b>: sculpt nodes");
1131 }
1133 // No modifiers: assemble tip from node type
1134 char const *nodetype = node_type_to_localized_string(_type);
1135 if (_selection.transformHandlesEnabled() && selected()) {
1136 if (_selection.size() == 1) {
1137 return format_tip(C_("Path node tip",
1138 "<b>%s</b>: drag to shape the path (more: Shift, Ctrl, Alt)"), nodetype);
1139 }
1140 return format_tip(C_("Path node tip",
1141 "<b>%s</b>: drag to shape the path, click to toggle scale/rotation handles (more: Shift, Ctrl, Alt)"), nodetype);
1142 }
1143 return format_tip(C_("Path node tip",
1144 "<b>%s</b>: drag to shape the path, click to select only this node (more: Shift, Ctrl, Alt)"), nodetype);
1145 }
1147 Glib::ustring Node::_getDragTip(GdkEventMotion */*event*/)
1148 {
1149 Geom::Point dist = position() - _last_drag_origin();
1150 GString *x = SP_PX_TO_METRIC_STRING(dist[Geom::X], _desktop->namedview->getDefaultMetric());
1151 GString *y = SP_PX_TO_METRIC_STRING(dist[Geom::Y], _desktop->namedview->getDefaultMetric());
1152 Glib::ustring ret = format_tip(C_("Path node tip", "Move node by %s, %s"),
1153 x->str, y->str);
1154 g_string_free(x, TRUE);
1155 g_string_free(y, TRUE);
1156 return ret;
1157 }
1159 char const *Node::node_type_to_localized_string(NodeType type)
1160 {
1161 switch (type) {
1162 case NODE_CUSP: return _("Cusp node");
1163 case NODE_SMOOTH: return _("Smooth node");
1164 case NODE_SYMMETRIC: return _("Symmetric node");
1165 case NODE_AUTO: return _("Auto-smooth node");
1166 default: return "";
1167 }
1168 }
1170 /** Determine whether two nodes are joined by a linear segment. */
1171 bool Node::_is_line_segment(Node *first, Node *second)
1172 {
1173 if (!first || !second) return false;
1174 if (first->_next() == second)
1175 return first->_front.isDegenerate() && second->_back.isDegenerate();
1176 if (second->_next() == first)
1177 return second->_front.isDegenerate() && first->_back.isDegenerate();
1178 return false;
1179 }
1181 SPCtrlShapeType Node::_node_type_to_shape(NodeType type)
1182 {
1183 switch(type) {
1184 case NODE_CUSP: return SP_CTRL_SHAPE_DIAMOND;
1185 case NODE_SMOOTH: return SP_CTRL_SHAPE_SQUARE;
1186 case NODE_AUTO: return SP_CTRL_SHAPE_CIRCLE;
1187 case NODE_SYMMETRIC: return SP_CTRL_SHAPE_SQUARE;
1188 default: return SP_CTRL_SHAPE_DIAMOND;
1189 }
1190 }
1193 /**
1194 * @class NodeList
1195 * @brief An editable list of nodes representing a subpath.
1196 *
1197 * It can optionally be cyclic to represent a closed path.
1198 * The list has iterators that act like plain node iterators, but can also be used
1199 * to obtain shared pointers to nodes.
1200 */
1202 NodeList::NodeList(SubpathList &splist)
1203 : _list(splist)
1204 , _closed(false)
1205 {
1206 this->ln_list = this;
1207 this->ln_next = this;
1208 this->ln_prev = this;
1209 }
1211 NodeList::~NodeList()
1212 {
1213 clear();
1214 }
1216 bool NodeList::empty()
1217 {
1218 return ln_next == this;
1219 }
1221 NodeList::size_type NodeList::size()
1222 {
1223 size_type sz = 0;
1224 for (ListNode *ln = ln_next; ln != this; ln = ln->ln_next) ++sz;
1225 return sz;
1226 }
1228 bool NodeList::closed()
1229 {
1230 return _closed;
1231 }
1233 /** A subpath is degenerate if it has no segments - either one node in an open path
1234 * or no nodes in a closed path */
1235 bool NodeList::degenerate()
1236 {
1237 return closed() ? empty() : ++begin() == end();
1238 }
1240 NodeList::iterator NodeList::before(double t, double *fracpart)
1241 {
1242 double intpart;
1243 *fracpart = std::modf(t, &intpart);
1244 int index = intpart;
1246 iterator ret = begin();
1247 std::advance(ret, index);
1248 return ret;
1249 }
1251 // insert a node before i
1252 NodeList::iterator NodeList::insert(iterator i, Node *x)
1253 {
1254 ListNode *ins = i._node;
1255 x->ln_next = ins;
1256 x->ln_prev = ins->ln_prev;
1257 ins->ln_prev->ln_next = x;
1258 ins->ln_prev = x;
1259 x->ln_list = this;
1260 return iterator(x);
1261 }
1263 void NodeList::splice(iterator pos, NodeList &list)
1264 {
1265 splice(pos, list, list.begin(), list.end());
1266 }
1268 void NodeList::splice(iterator pos, NodeList &list, iterator i)
1269 {
1270 NodeList::iterator j = i;
1271 ++j;
1272 splice(pos, list, i, j);
1273 }
1275 void NodeList::splice(iterator pos, NodeList &/*list*/, iterator first, iterator last)
1276 {
1277 ListNode *ins_beg = first._node, *ins_end = last._node, *at = pos._node;
1278 for (ListNode *ln = ins_beg; ln != ins_end; ln = ln->ln_next) {
1279 ln->ln_list = this;
1280 }
1281 ins_beg->ln_prev->ln_next = ins_end;
1282 ins_end->ln_prev->ln_next = at;
1283 at->ln_prev->ln_next = ins_beg;
1285 ListNode *atprev = at->ln_prev;
1286 at->ln_prev = ins_end->ln_prev;
1287 ins_end->ln_prev = ins_beg->ln_prev;
1288 ins_beg->ln_prev = atprev;
1289 }
1291 void NodeList::shift(int n)
1292 {
1293 // 1. make the list perfectly cyclic
1294 ln_next->ln_prev = ln_prev;
1295 ln_prev->ln_next = ln_next;
1296 // 2. find new begin
1297 ListNode *new_begin = ln_next;
1298 if (n > 0) {
1299 for (; n > 0; --n) new_begin = new_begin->ln_next;
1300 } else {
1301 for (; n < 0; ++n) new_begin = new_begin->ln_prev;
1302 }
1303 // 3. relink begin to list
1304 ln_next = new_begin;
1305 ln_prev = new_begin->ln_prev;
1306 new_begin->ln_prev->ln_next = this;
1307 new_begin->ln_prev = this;
1308 }
1310 void NodeList::reverse()
1311 {
1312 for (ListNode *ln = ln_next; ln != this; ln = ln->ln_prev) {
1313 std::swap(ln->ln_next, ln->ln_prev);
1314 Node *node = static_cast<Node*>(ln);
1315 Geom::Point save_pos = node->front()->position();
1316 node->front()->setPosition(node->back()->position());
1317 node->back()->setPosition(save_pos);
1318 }
1319 std::swap(ln_next, ln_prev);
1320 }
1322 void NodeList::clear()
1323 {
1324 for (iterator i = begin(); i != end();) erase (i++);
1325 }
1327 NodeList::iterator NodeList::erase(iterator i)
1328 {
1329 // some gymnastics are required to ensure that the node is valid when deleted;
1330 // otherwise the code that updates handle visibility will break
1331 Node *rm = static_cast<Node*>(i._node);
1332 ListNode *rmnext = rm->ln_next, *rmprev = rm->ln_prev;
1333 ++i;
1334 delete rm;
1335 rmprev->ln_next = rmnext;
1336 rmnext->ln_prev = rmprev;
1337 return i;
1338 }
1340 // TODO this method is very ugly!
1341 // converting SubpathList to an intrusive list might allow us to get rid of it
1342 void NodeList::kill()
1343 {
1344 for (SubpathList::iterator i = _list.begin(); i != _list.end(); ++i) {
1345 if (i->get() == this) {
1346 _list.erase(i);
1347 return;
1348 }
1349 }
1350 }
1352 NodeList &NodeList::get(Node *n) {
1353 return n->nodeList();
1354 }
1355 NodeList &NodeList::get(iterator const &i) {
1356 return *(i._node->ln_list);
1357 }
1360 /**
1361 * @class SubpathList
1362 * @brief Editable path composed of one or more subpaths
1363 */
1365 } // namespace UI
1366 } // namespace Inkscape
1368 /*
1369 Local Variables:
1370 mode:c++
1371 c-file-style:"stroustrup"
1372 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1373 indent-tabs-mode:nil
1374 fill-column:99
1375 End:
1376 */
1377 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :