d2f90bbca3dc180e0d145e9565831bca1feb4b1b
1 /** @file
2 * Path manipulator - 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 <string>
12 #include <sstream>
13 #include <deque>
14 #include <stdexcept>
15 #include <boost/shared_ptr.hpp>
16 #include <2geom/bezier-curve.h>
17 #include <2geom/bezier-utils.h>
18 #include <2geom/svg-path.h>
19 #include <glibmm.h>
20 #include <glibmm/i18n.h>
21 #include "ui/tool/path-manipulator.h"
22 #include "desktop.h"
23 #include "desktop-handles.h"
24 #include "display/sp-canvas.h"
25 #include "display/sp-canvas-util.h"
26 #include "display/curve.h"
27 #include "display/canvas-bpath.h"
28 #include "document.h"
29 #include "live_effects/effect.h"
30 #include "live_effects/lpeobject.h"
31 #include "live_effects/parameter/path.h"
32 #include "sp-path.h"
33 #include "helper/geom.h"
34 #include "preferences.h"
35 #include "style.h"
36 #include "ui/tool/control-point-selection.h"
37 #include "ui/tool/curve-drag-point.h"
38 #include "ui/tool/event-utils.h"
39 #include "ui/tool/multi-path-manipulator.h"
40 #include "xml/node.h"
41 #include "xml/node-observer.h"
43 namespace Inkscape {
44 namespace UI {
46 namespace {
47 /// Types of path changes that we must react to.
48 enum PathChange {
49 PATH_CHANGE_D,
50 PATH_CHANGE_TRANSFORM
51 };
53 } // anonymous namespace
55 /**
56 * Notifies the path manipulator when something changes the path being edited
57 * (e.g. undo / redo)
58 */
59 class PathManipulatorObserver : public Inkscape::XML::NodeObserver {
60 public:
61 PathManipulatorObserver(PathManipulator *p, Inkscape::XML::Node *node)
62 : _pm(p)
63 , _node(node)
64 , _blocked(false)
65 {
66 Inkscape::GC::anchor(_node);
67 _node->addObserver(*this);
68 }
69 ~PathManipulatorObserver() {
70 _node->removeObserver(*this);
71 Inkscape::GC::release(_node);
72 }
73 virtual void notifyAttributeChanged(Inkscape::XML::Node &node, GQuark attr,
74 Util::ptr_shared<char>, Util::ptr_shared<char>)
75 {
76 // do nothing if blocked
77 if (_blocked) return;
79 GQuark path_d = g_quark_from_static_string("d");
80 GQuark path_transform = g_quark_from_static_string("transform");
81 GQuark lpe_quark = _pm->_lpe_key.empty() ? 0 : g_quark_from_string(_pm->_lpe_key.data());
83 // only react to "d" (path data) and "transform" attribute changes
84 if (attr == lpe_quark || attr == path_d) {
85 _pm->_externalChange(PATH_CHANGE_D);
86 } else if (attr == path_transform) {
87 _pm->_externalChange(PATH_CHANGE_TRANSFORM);
88 }
89 }
90 void block() { _blocked = true; }
91 void unblock() { _blocked = false; }
92 private:
93 PathManipulator *_pm;
94 Inkscape::XML::Node *_node;
95 bool _blocked;
96 };
98 void build_segment(Geom::PathBuilder &, Node *, Node *);
100 PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path,
101 Geom::Matrix const &et, guint32 outline_color, Glib::ustring lpe_key)
102 : PointManipulator(mpm._path_data.node_data.desktop, *mpm._path_data.node_data.selection)
103 , _subpaths(*this)
104 , _multi_path_manipulator(mpm)
105 , _path(path)
106 , _spcurve(NULL)
107 , _dragpoint(new CurveDragPoint(*this))
108 , _observer(new PathManipulatorObserver(this, SP_OBJECT(path)->repr))
109 , _edit_transform(et)
110 , _num_selected(0)
111 , _show_handles(true)
112 , _show_outline(false)
113 , _show_path_direction(false)
114 , _live_outline(true)
115 , _live_objects(true)
116 , _lpe_key(lpe_key)
117 {
118 if (_lpe_key.empty()) {
119 _i2d_transform = sp_item_i2d_affine(SP_ITEM(path));
120 } else {
121 _i2d_transform = Geom::identity();
122 }
123 _d2i_transform = _i2d_transform.inverse();
124 _dragpoint->setVisible(false);
126 _getGeometry();
128 _outline = sp_canvas_bpath_new(_multi_path_manipulator._path_data.outline_group, NULL);
129 sp_canvas_item_hide(_outline);
130 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(_outline), outline_color, 1.0,
131 SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
132 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(_outline), 0, SP_WIND_RULE_NONZERO);
134 _selection.signal_update.connect(
135 sigc::mem_fun(*this, &PathManipulator::update));
136 _selection.signal_point_changed.connect(
137 sigc::mem_fun(*this, &PathManipulator::_selectionChanged));
138 _desktop->signal_zoom_changed.connect(
139 sigc::hide( sigc::mem_fun(*this, &PathManipulator::_updateOutlineOnZoomChange)));
141 _createControlPointsFromGeometry();
142 }
144 PathManipulator::~PathManipulator()
145 {
146 delete _dragpoint;
147 delete _observer;
148 gtk_object_destroy(_outline);
149 if (_spcurve) _spcurve->unref();
150 clear();
151 }
153 /** Handle motion events to update the position of the curve drag point. */
154 bool PathManipulator::event(GdkEvent *event)
155 {
156 if (empty()) return false;
158 switch (event->type)
159 {
160 case GDK_MOTION_NOTIFY:
161 _updateDragPoint(event_point(event->motion));
162 break;
163 default: break;
164 }
165 return false;
166 }
168 /** Check whether the manipulator has any nodes. */
169 bool PathManipulator::empty() {
170 return !_path || _subpaths.empty();
171 }
173 /** Update the display and the outline of the path. */
174 void PathManipulator::update()
175 {
176 _createGeometryFromControlPoints();
177 }
179 /** Store the changes to the path in XML. */
180 void PathManipulator::writeXML()
181 {
182 if (!_live_outline)
183 _updateOutline();
184 if (!_live_objects)
185 _setGeometry();
187 if (!_path) return;
188 _observer->block();
189 if (!empty()) {
190 SP_OBJECT(_path)->updateRepr();
191 _getXMLNode()->setAttribute(_nodetypesKey().data(), _createTypeString().data());
192 } else {
193 // this manipulator will have to be destroyed right after this call
194 _getXMLNode()->removeObserver(*_observer);
195 sp_object_ref(_path);
196 _path->deleteObject(true, true);
197 sp_object_unref(_path);
198 _path = 0;
199 }
200 _observer->unblock();
201 }
203 /** Remove all nodes from the path. */
204 void PathManipulator::clear()
205 {
206 // no longer necessary since nodes remove themselves from selection on destruction
207 //_removeNodesFromSelection();
208 _subpaths.clear();
209 }
211 /** Select all nodes in subpaths that have something selected. */
212 void PathManipulator::selectSubpaths()
213 {
214 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
215 NodeList::iterator sp_start = (*i)->begin(), sp_end = (*i)->end();
216 for (NodeList::iterator j = sp_start; j != sp_end; ++j) {
217 if (j->selected()) {
218 // if at least one of the nodes from this subpath is selected,
219 // select all nodes from this subpath
220 for (NodeList::iterator ins = sp_start; ins != sp_end; ++ins)
221 _selection.insert(ins.ptr());
222 continue;
223 }
224 }
225 }
226 }
228 /** Move the selection forward or backward by one node in each subpath, based on the sign
229 * of the parameter. */
230 void PathManipulator::shiftSelection(int dir)
231 {
232 if (dir == 0) return;
233 if (_num_selected == 0) {
234 // select the first node of the path.
235 SubpathList::iterator s = _subpaths.begin();
236 if (s == _subpaths.end()) return;
237 NodeList::iterator n = (*s)->begin();
238 if (n != (*s)->end())
239 _selection.insert(n.ptr());
240 return;
241 }
242 // We cannot do any tricks here, like iterating in different directions based on
243 // the sign and only setting the selection of nodes behind us, because it would break
244 // for closed paths.
245 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
246 std::deque<bool> sels; // I hope this is specialized for bools!
247 unsigned num = 0;
249 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
250 sels.push_back(j->selected());
251 _selection.erase(j.ptr());
252 ++num;
253 }
254 if (num == 0) continue; // should never happen! zero-node subpaths are not allowed
256 num = 0;
257 // In closed subpath, shift the selection cyclically. In an open one,
258 // let the selection 'slide into nothing' at ends.
259 if (dir > 0) {
260 if ((*i)->closed()) {
261 bool last = sels.back();
262 sels.pop_back();
263 sels.push_front(last);
264 } else {
265 sels.push_front(false);
266 }
267 } else {
268 if ((*i)->closed()) {
269 bool first = sels.front();
270 sels.pop_front();
271 sels.push_back(first);
272 } else {
273 sels.push_back(false);
274 num = 1;
275 }
276 }
278 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
279 if (sels[num]) _selection.insert(j.ptr());
280 ++num;
281 }
282 }
283 }
285 /** Invert selection in the selected subpaths. */
286 void PathManipulator::invertSelectionInSubpaths()
287 {
288 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
289 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
290 if (j->selected()) {
291 // found selected node - invert selection in this subpath
292 for (NodeList::iterator k = (*i)->begin(); k != (*i)->end(); ++k) {
293 if (k->selected()) _selection.erase(k.ptr());
294 else _selection.insert(k.ptr());
295 }
296 // next subpath
297 break;
298 }
299 }
300 }
301 }
303 /** Insert a new node in the middle of each selected segment. */
304 void PathManipulator::insertNodes()
305 {
306 if (_num_selected < 2) return;
308 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
309 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
310 NodeList::iterator k = j.next();
311 if (k && j->selected() && k->selected()) {
312 j = subdivideSegment(j, 0.5);
313 _selection.insert(j.ptr());
314 }
315 }
316 }
317 }
319 /** Replace contiguous selections of nodes in each subpath with one node. */
320 void PathManipulator::weldNodes(NodeList::iterator preserve_pos)
321 {
322 if (_num_selected < 2) return;
323 hideDragPoint();
325 bool pos_valid = preserve_pos;
326 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
327 SubpathPtr sp = *i;
328 unsigned num_selected = 0, num_unselected = 0;
329 for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) {
330 if (j->selected()) ++num_selected;
331 else ++num_unselected;
332 }
333 if (num_selected < 2) continue;
334 if (num_unselected == 0) {
335 // if all nodes in a subpath are selected, the operation doesn't make much sense
336 continue;
337 }
339 // Start from unselected node in closed paths, so that we don't start in the middle
340 // of a selection
341 NodeList::iterator sel_beg = sp->begin(), sel_end;
342 if (sp->closed()) {
343 while (sel_beg->selected()) ++sel_beg;
344 }
346 // Work loop
347 while (num_selected > 0) {
348 // Find selected node
349 while (sel_beg && !sel_beg->selected()) sel_beg = sel_beg.next();
350 if (!sel_beg) throw std::logic_error("Join nodes: end of open path reached, "
351 "but there are still nodes to process!");
353 // note: this is initialized to zero, because the loop below counts sel_beg as well
354 // the loop conditions are simpler that way
355 unsigned num_points = 0;
356 bool use_pos = false;
357 Geom::Point back_pos, front_pos;
358 back_pos = *sel_beg->back();
360 for (sel_end = sel_beg; sel_end && sel_end->selected(); sel_end = sel_end.next()) {
361 ++num_points;
362 front_pos = *sel_end->front();
363 if (pos_valid && sel_end == preserve_pos) use_pos = true;
364 }
365 if (num_points > 1) {
366 Geom::Point joined_pos;
367 if (use_pos) {
368 joined_pos = preserve_pos->position();
369 pos_valid = false;
370 } else {
371 joined_pos = Geom::middle_point(back_pos, front_pos);
372 }
373 sel_beg->setType(NODE_CUSP, false);
374 sel_beg->move(joined_pos);
375 // do not move handles if they aren't degenerate
376 if (!sel_beg->back()->isDegenerate()) {
377 sel_beg->back()->setPosition(back_pos);
378 }
379 if (!sel_end.prev()->front()->isDegenerate()) {
380 sel_beg->front()->setPosition(front_pos);
381 }
382 sel_beg = sel_beg.next();
383 while (sel_beg != sel_end) {
384 NodeList::iterator next = sel_beg.next();
385 sp->erase(sel_beg);
386 sel_beg = next;
387 --num_selected;
388 }
389 }
390 --num_selected; // for the joined node or single selected node
391 }
392 }
393 }
395 /** Remove nodes in the middle of selected segments. */
396 void PathManipulator::weldSegments()
397 {
398 if (_num_selected < 2) return;
399 hideDragPoint();
401 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
402 SubpathPtr sp = *i;
403 unsigned num_selected = 0, num_unselected = 0;
404 for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) {
405 if (j->selected()) ++num_selected;
406 else ++num_unselected;
407 }
408 if (num_selected < 3) continue;
409 if (num_unselected == 0 && sp->closed()) {
410 // if all nodes in a closed subpath are selected, the operation doesn't make much sense
411 continue;
412 }
414 // Start from unselected node in closed paths, so that we don't start in the middle
415 // of a selection
416 NodeList::iterator sel_beg = sp->begin(), sel_end;
417 if (sp->closed()) {
418 while (sel_beg->selected()) ++sel_beg;
419 }
421 // Work loop
422 while (num_selected > 0) {
423 // Find selected node
424 while (sel_beg && !sel_beg->selected()) sel_beg = sel_beg.next();
425 if (!sel_beg) throw std::logic_error("Join nodes: end of open path reached, "
426 "but there are still nodes to process!");
428 // note: this is initialized to zero, because the loop below counts sel_beg as well
429 // the loop conditions are simpler that way
430 unsigned num_points = 0;
432 // find the end of selected segment
433 for (sel_end = sel_beg; sel_end && sel_end->selected(); sel_end = sel_end.next()) {
434 ++num_points;
435 }
436 if (num_points > 2) {
437 // remove nodes in the middle
438 sel_beg = sel_beg.next();
439 while (sel_beg != sel_end.prev()) {
440 NodeList::iterator next = sel_beg.next();
441 sp->erase(sel_beg);
442 sel_beg = next;
443 }
444 sel_beg = sel_end;
445 }
446 num_selected -= num_points;
447 }
448 }
449 }
451 /** Break the subpath at selected nodes. It also works for single node closed paths. */
452 void PathManipulator::breakNodes()
453 {
454 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
455 SubpathPtr sp = *i;
456 NodeList::iterator cur = sp->begin(), end = sp->end();
457 if (!sp->closed()) {
458 // Each open path must have at least two nodes so no checks are required.
459 // For 2-node open paths, cur == end
460 ++cur;
461 --end;
462 }
463 for (; cur != end; ++cur) {
464 if (!cur->selected()) continue;
465 SubpathPtr ins;
466 bool becomes_open = false;
468 if (sp->closed()) {
469 // Move the node to break at to the beginning of path
470 if (cur != sp->begin())
471 sp->splice(sp->begin(), *sp, cur, sp->end());
472 sp->setClosed(false);
473 ins = sp;
474 becomes_open = true;
475 } else {
476 SubpathPtr new_sp(new NodeList(_subpaths));
477 new_sp->splice(new_sp->end(), *sp, sp->begin(), cur);
478 _subpaths.insert(i, new_sp);
479 ins = new_sp;
480 }
482 Node *n = new Node(_multi_path_manipulator._path_data.node_data, cur->position());
483 ins->insert(ins->end(), n);
484 cur->setType(NODE_CUSP, false);
485 n->back()->setRelativePos(cur->back()->relativePos());
486 cur->back()->retract();
487 n->sink();
489 if (becomes_open) {
490 cur = sp->begin(); // this will be increased to ++sp->begin()
491 end = --sp->end();
492 }
493 }
494 }
495 }
497 /** Delete selected nodes in the path, optionally substituting deleted segments with bezier curves
498 * in a way that attempts to preserve the original shape of the curve. */
499 void PathManipulator::deleteNodes(bool keep_shape)
500 {
501 if (_num_selected == 0) return;
502 hideDragPoint();
504 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end();) {
505 SubpathPtr sp = *i;
507 // If there are less than 2 unselected nodes in an open subpath or no unselected nodes
508 // in a closed one, delete entire subpath.
509 unsigned num_unselected = 0, num_selected = 0;
510 for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) {
511 if (j->selected()) ++num_selected;
512 else ++num_unselected;
513 }
514 if (num_selected == 0) {
515 ++i;
516 continue;
517 }
518 if (sp->closed() ? (num_unselected < 1) : (num_unselected < 2)) {
519 _subpaths.erase(i++);
520 continue;
521 }
523 // In closed paths, start from an unselected node - otherwise we might start in the middle
524 // of a selected stretch and the resulting bezier fit would be suboptimal
525 NodeList::iterator sel_beg = sp->begin(), sel_end;
526 if (sp->closed()) {
527 while (sel_beg->selected()) ++sel_beg;
528 }
529 sel_end = sel_beg;
531 while (num_selected > 0) {
532 while (sel_beg && !sel_beg->selected()) {
533 sel_beg = sel_beg.next();
534 }
535 sel_end = sel_beg;
537 while (sel_end && sel_end->selected()) {
538 sel_end = sel_end.next();
539 }
541 num_selected -= _deleteStretch(sel_beg, sel_end, keep_shape);
542 sel_beg = sel_end;
543 }
544 ++i;
545 }
546 }
548 /** @brief Delete nodes between the two iterators.
549 * The given range can cross the beginning of the subpath in closed subpaths.
550 * @param start Beginning of the range to delete
551 * @param end End of the range
552 * @param keep_shape Whether to fit the handles at surrounding nodes to approximate
553 * the shape before deletion
554 * @return Number of deleted nodes */
555 unsigned PathManipulator::_deleteStretch(NodeList::iterator start, NodeList::iterator end, bool keep_shape)
556 {
557 unsigned const samples_per_segment = 10;
558 double const t_step = 1.0 / samples_per_segment;
560 unsigned del_len = 0;
561 for (NodeList::iterator i = start; i != end; i = i.next()) {
562 ++del_len;
563 }
564 if (del_len == 0) return 0;
566 // set surrounding node types to cusp if:
567 // 1. keep_shape is on, or
568 // 2. we are deleting at the end or beginning of an open path
569 if ((keep_shape || !end) && start.prev()) start.prev()->setType(NODE_CUSP, false);
570 if ((keep_shape || !start.prev()) && end) end->setType(NODE_CUSP, false);
572 if (keep_shape && start.prev() && end) {
573 unsigned num_samples = (del_len + 1) * samples_per_segment + 1;
574 Geom::Point *bezier_data = new Geom::Point[num_samples];
575 Geom::Point result[4];
576 unsigned seg = 0;
578 for (NodeList::iterator cur = start.prev(); cur != end; cur = cur.next()) {
579 Geom::CubicBezier bc(*cur, *cur->front(), *cur.next(), *cur.next()->back());
580 for (unsigned s = 0; s < samples_per_segment; ++s) {
581 bezier_data[seg * samples_per_segment + s] = bc.pointAt(t_step * s);
582 }
583 ++seg;
584 }
585 // Fill last point
586 bezier_data[num_samples - 1] = end->position();
587 // Compute replacement bezier curve
588 // TODO the fitting algorithm sucks - rewrite it to be awesome
589 bezier_fit_cubic(result, bezier_data, num_samples, 0.5);
590 delete[] bezier_data;
592 start.prev()->front()->setPosition(result[1]);
593 end->back()->setPosition(result[2]);
594 }
596 // We can't use nl->erase(start, end), because it would break when the stretch
597 // crosses the beginning of a closed subpath
598 NodeList *nl = start->list();
599 while (start != end) {
600 NodeList::iterator next = start.next();
601 nl->erase(start);
602 start = next;
603 }
605 return del_len;
606 }
608 /** Removes selected segments */
609 void PathManipulator::deleteSegments()
610 {
611 if (_num_selected == 0) return;
612 hideDragPoint();
614 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end();) {
615 SubpathPtr sp = *i;
616 bool has_unselected = false;
617 unsigned num_selected = 0;
618 for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) {
619 if (j->selected()) {
620 ++num_selected;
621 } else {
622 has_unselected = true;
623 }
624 }
625 if (!has_unselected) {
626 _subpaths.erase(i++);
627 continue;
628 }
630 NodeList::iterator sel_beg = sp->begin();
631 if (sp->closed()) {
632 while (sel_beg && sel_beg->selected()) ++sel_beg;
633 }
634 while (num_selected > 0) {
635 if (!sel_beg->selected()) {
636 sel_beg = sel_beg.next();
637 continue;
638 }
639 NodeList::iterator sel_end = sel_beg;
640 unsigned num_points = 0;
641 while (sel_end && sel_end->selected()) {
642 sel_end = sel_end.next();
643 ++num_points;
644 }
645 if (num_points >= 2) {
646 // Retract end handles
647 sel_end.prev()->setType(NODE_CUSP, false);
648 sel_end.prev()->back()->retract();
649 sel_beg->setType(NODE_CUSP, false);
650 sel_beg->front()->retract();
651 if (sp->closed()) {
652 // In closed paths, relocate the beginning of the path to the last selected
653 // node and then unclose it. Remove the nodes from the first selected node
654 // to the new end of path.
655 if (sel_end.prev() != sp->begin())
656 sp->splice(sp->begin(), *sp, sel_end.prev(), sp->end());
657 sp->setClosed(false);
658 sp->erase(sel_beg.next(), sp->end());
659 } else {
660 // for open paths:
661 // 1. At end or beginning, delete including the node on the end or beginning
662 // 2. In the middle, delete only inner nodes
663 if (sel_beg == sp->begin()) {
664 sp->erase(sp->begin(), sel_end.prev());
665 } else if (sel_end == sp->end()) {
666 sp->erase(sel_beg.next(), sp->end());
667 } else {
668 SubpathPtr new_sp(new NodeList(_subpaths));
669 new_sp->splice(new_sp->end(), *sp, sp->begin(), sel_beg.next());
670 _subpaths.insert(i, new_sp);
671 if (sel_end.prev())
672 sp->erase(sp->begin(), sel_end.prev());
673 }
674 }
675 }
676 sel_beg = sel_end;
677 num_selected -= num_points;
678 }
679 ++i;
680 }
681 }
683 /** Reverse subpaths of the path.
684 * @param selected_only If true, only paths that have at least one selected node
685 * will be reversed. Otherwise all subpaths will be reversed. */
686 void PathManipulator::reverseSubpaths(bool selected_only)
687 {
688 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
689 if (selected_only) {
690 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
691 if (j->selected()) {
692 (*i)->reverse();
693 break; // continue with the next subpath
694 }
695 }
696 } else {
697 (*i)->reverse();
698 }
699 }
700 }
702 /** Make selected segments curves / lines. */
703 void PathManipulator::setSegmentType(SegmentType type)
704 {
705 if (_num_selected == 0) return;
706 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
707 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
708 NodeList::iterator k = j.next();
709 if (!(k && j->selected() && k->selected())) continue;
710 switch (type) {
711 case SEGMENT_STRAIGHT:
712 if (j->front()->isDegenerate() && k->back()->isDegenerate())
713 break;
714 j->front()->move(*j);
715 k->back()->move(*k);
716 break;
717 case SEGMENT_CUBIC_BEZIER:
718 if (!j->front()->isDegenerate() || !k->back()->isDegenerate())
719 break;
720 j->front()->move(j->position() + (k->position() - j->position()) / 3);
721 k->back()->move(k->position() + (j->position() - k->position()) / 3);
722 break;
723 }
724 }
725 }
726 }
728 /** Set the visibility of handles. */
729 void PathManipulator::showHandles(bool show)
730 {
731 if (show == _show_handles) return;
732 if (show) {
733 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
734 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
735 if (!j->selected()) continue;
736 j->showHandles(true);
737 if (j.prev()) j.prev()->showHandles(true);
738 if (j.next()) j.next()->showHandles(true);
739 }
740 }
741 } else {
742 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
743 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
744 j->showHandles(false);
745 }
746 }
747 }
748 _show_handles = show;
749 }
751 /** Set the visibility of outline. */
752 void PathManipulator::showOutline(bool show)
753 {
754 if (show == _show_outline) return;
755 _show_outline = show;
756 _updateOutline();
757 }
759 void PathManipulator::showPathDirection(bool show)
760 {
761 if (show == _show_path_direction) return;
762 _show_path_direction = show;
763 _updateOutline();
764 }
766 void PathManipulator::setLiveOutline(bool set)
767 {
768 _live_outline = set;
769 }
771 void PathManipulator::setLiveObjects(bool set)
772 {
773 _live_objects = set;
774 }
776 void PathManipulator::setControlsTransform(Geom::Matrix const &tnew)
777 {
778 Geom::Matrix delta = _i2d_transform.inverse() * _edit_transform.inverse() * tnew * _i2d_transform;
779 _edit_transform = tnew;
780 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
781 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
782 j->transform(delta);
783 }
784 }
785 _createGeometryFromControlPoints();
786 }
788 /** Hide the curve drag point until the next motion event.
789 * This should be called at the beginning of every method that can delete nodes.
790 * Otherwise the invalidated iterator in the dragpoint can cause crashes. */
791 void PathManipulator::hideDragPoint()
792 {
793 _dragpoint->setVisible(false);
794 _dragpoint->setIterator(NodeList::iterator());
795 }
797 /** Insert a node in the segment beginning with the supplied iterator,
798 * at the given time value */
799 NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, double t)
800 {
801 if (!first) throw std::invalid_argument("Subdivide after invalid iterator");
802 NodeList &list = NodeList::get(first);
803 NodeList::iterator second = first.next();
804 if (!second) throw std::invalid_argument("Subdivide after last node in open path");
806 // We need to insert the segment after 'first'. We can't simply use 'second'
807 // as the point of insertion, because when 'first' is the last node of closed path,
808 // the new node will be inserted as the first node instead.
809 NodeList::iterator insert_at = first;
810 ++insert_at;
812 NodeList::iterator inserted;
813 if (first->front()->isDegenerate() && second->back()->isDegenerate()) {
814 // for a line segment, insert a cusp node
815 Node *n = new Node(_multi_path_manipulator._path_data.node_data,
816 Geom::lerp(t, first->position(), second->position()));
817 n->setType(NODE_CUSP, false);
818 inserted = list.insert(insert_at, n);
819 } else {
820 // build bezier curve and subdivide
821 Geom::CubicBezier temp(first->position(), first->front()->position(),
822 second->back()->position(), second->position());
823 std::pair<Geom::CubicBezier, Geom::CubicBezier> div = temp.subdivide(t);
824 std::vector<Geom::Point> seg1 = div.first.points(), seg2 = div.second.points();
826 // set new handle positions
827 Node *n = new Node(_multi_path_manipulator._path_data.node_data, seg2[0]);
828 n->back()->setPosition(seg1[2]);
829 n->front()->setPosition(seg2[1]);
830 n->setType(NODE_SMOOTH, false);
831 inserted = list.insert(insert_at, n);
833 first->front()->move(seg1[1]);
834 second->back()->move(seg2[2]);
835 }
836 return inserted;
837 }
839 /** Find the node that is closest/farthest from the origin
840 * @param origin Point of reference
841 * @param search_selected Consider selected nodes
842 * @param search_unselected Consider unselected nodes
843 * @param closest If true, return closest node, if false, return farthest
844 * @return The matching node, or an empty iterator if none found
845 */
846 NodeList::iterator PathManipulator::extremeNode(NodeList::iterator origin, bool search_selected,
847 bool search_unselected, bool closest)
848 {
849 NodeList::iterator match;
850 double extr_dist = closest ? HUGE_VAL : -HUGE_VAL;
851 if (_num_selected == 0 && !search_unselected) return match;
853 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
854 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
855 if(j->selected()) {
856 if (!search_selected) continue;
857 } else {
858 if (!search_unselected) continue;
859 }
860 double dist = Geom::distance(*j, *origin);
861 bool cond = closest ? (dist < extr_dist) : (dist > extr_dist);
862 if (cond) {
863 match = j;
864 extr_dist = dist;
865 }
866 }
867 }
868 return match;
869 }
871 /** Called by the XML observer when something else than us modifies the path. */
872 void PathManipulator::_externalChange(unsigned type)
873 {
874 switch (type) {
875 case PATH_CHANGE_D: {
876 _getGeometry();
878 // ugly: stored offsets of selected nodes in a vector
879 // vector<bool> should be specialized so that it takes only 1 bit per value
880 std::vector<bool> selpos;
881 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
882 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
883 selpos.push_back(j->selected());
884 }
885 }
886 unsigned size = selpos.size(), curpos = 0;
888 _createControlPointsFromGeometry();
890 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
891 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
892 if (curpos >= size) goto end_restore;
893 if (selpos[curpos]) _selection.insert(j.ptr());
894 ++curpos;
895 }
896 }
897 end_restore:
899 _updateOutline();
900 } break;
901 case PATH_CHANGE_TRANSFORM: {
902 Geom::Matrix i2d_change = _d2i_transform;
903 _i2d_transform = sp_item_i2d_affine(SP_ITEM(_path));
904 _d2i_transform = _i2d_transform.inverse();
905 i2d_change *= _i2d_transform;
906 for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
907 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
908 j->transform(i2d_change);
909 }
910 }
911 _updateOutline();
912 } break;
913 default: break;
914 }
915 }
917 /** Create nodes and handles based on the XML of the edited path. */
918 void PathManipulator::_createControlPointsFromGeometry()
919 {
920 clear();
922 // sanitize pathvector and store it in SPCurve,
923 // so that _updateDragPoint doesn't crash on paths with naked movetos
924 Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers(_spcurve->get_pathvector());
925 for (Geom::PathVector::iterator i = pathv.begin(); i != pathv.end(); ) {
926 if (i->empty()) pathv.erase(i++);
927 else ++i;
928 }
929 _spcurve->set_pathvector(pathv);
931 pathv *= (_edit_transform * _i2d_transform);
933 // in this loop, we know that there are no zero-segment subpaths
934 for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
935 // prepare new subpath
936 SubpathPtr subpath(new NodeList(_subpaths));
937 _subpaths.push_back(subpath);
939 Node *previous_node = new Node(_multi_path_manipulator._path_data.node_data, pit->initialPoint());
940 subpath->push_back(previous_node);
941 Geom::Curve const &cseg = pit->back_closed();
942 bool fuse_ends = pit->closed()
943 && Geom::are_near(cseg.initialPoint(), cseg.finalPoint());
945 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit) {
946 Geom::Point pos = cit->finalPoint();
947 Node *current_node;
948 // if the closing segment is degenerate and the path is closed, we need to move
949 // the handle of the first node instead of creating a new one
950 if (fuse_ends && cit == --(pit->end_open())) {
951 current_node = subpath->begin().get_pointer();
952 } else {
953 /* regardless of segment type, create a new node at the end
954 * of this segment (unless this is the last segment of a closed path
955 * with a degenerate closing segment */
956 current_node = new Node(_multi_path_manipulator._path_data.node_data, pos);
957 subpath->push_back(current_node);
958 }
959 // if this is a bezier segment, move handles appropriately
960 if (Geom::CubicBezier const *cubic_bezier =
961 dynamic_cast<Geom::CubicBezier const*>(&*cit))
962 {
963 std::vector<Geom::Point> points = cubic_bezier->points();
965 previous_node->front()->setPosition(points[1]);
966 current_node ->back() ->setPosition(points[2]);
967 }
968 previous_node = current_node;
969 }
970 // If the path is closed, make the list cyclic
971 if (pit->closed()) subpath->setClosed(true);
972 }
974 // we need to set the nodetypes after all the handles are in place,
975 // so that pickBestType works correctly
976 // TODO maybe migrate to inkscape:node-types?
977 gchar const *nts_raw = _path ? _path->repr->attribute(_nodetypesKey().data()) : 0;
978 std::string nodetype_string = nts_raw ? nts_raw : "";
979 /* Calculate the needed length of the nodetype string.
980 * For closed paths, the entry is duplicated for the starting node,
981 * so we can just use the count of segments including the closing one
982 * to include the extra end node. */
983 std::string::size_type nodetype_len = 0;
984 for (Geom::PathVector::const_iterator i = pathv.begin(); i != pathv.end(); ++i) {
985 if (i->empty()) continue;
986 nodetype_len += i->size_closed();
987 }
988 /* pad the string to required length with a bogus value.
989 * 'b' and any other letter not recognized by the parser causes the best fit to be set
990 * as the node type */
991 if (nodetype_len > nodetype_string.size()) {
992 nodetype_string.append(nodetype_len - nodetype_string.size(), 'b');
993 }
994 std::string::iterator tsi = nodetype_string.begin();
995 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
996 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
997 j->setType(Node::parse_nodetype(*tsi++), false);
998 }
999 if ((*i)->closed()) {
1000 // STUPIDITY ALERT: it seems we need to use the duplicate type symbol instead of
1001 // the first one to remain backward compatible.
1002 (*i)->begin()->setType(Node::parse_nodetype(*tsi++), false);
1003 }
1004 }
1005 }
1007 /** Construct the geometric representation of nodes and handles, update the outline
1008 * and display */
1009 void PathManipulator::_createGeometryFromControlPoints()
1010 {
1011 Geom::PathBuilder builder;
1012 for (std::list<SubpathPtr>::iterator spi = _subpaths.begin(); spi != _subpaths.end(); ) {
1013 SubpathPtr subpath = *spi;
1014 if (subpath->empty()) {
1015 _subpaths.erase(spi++);
1016 continue;
1017 }
1018 NodeList::iterator prev = subpath->begin();
1019 builder.moveTo(prev->position());
1021 for (NodeList::iterator i = ++subpath->begin(); i != subpath->end(); ++i) {
1022 build_segment(builder, prev.ptr(), i.ptr());
1023 prev = i;
1024 }
1025 if (subpath->closed()) {
1026 // Here we link the last and first node if the path is closed.
1027 // If the last segment is Bezier, we add it.
1028 if (!prev->front()->isDegenerate() || !subpath->begin()->back()->isDegenerate()) {
1029 build_segment(builder, prev.ptr(), subpath->begin().ptr());
1030 }
1031 // if that segment is linear, we just call closePath().
1032 builder.closePath();
1033 }
1034 ++spi;
1035 }
1036 builder.finish();
1037 _spcurve->set_pathvector(builder.peek() * (_edit_transform * _i2d_transform).inverse());
1038 if (_live_outline)
1039 _updateOutline();
1040 if (_live_objects)
1041 _setGeometry();
1042 }
1044 /** Build one segment of the geometric representation.
1045 * @relates PathManipulator */
1046 void build_segment(Geom::PathBuilder &builder, Node *prev_node, Node *cur_node)
1047 {
1048 if (cur_node->back()->isDegenerate() && prev_node->front()->isDegenerate())
1049 {
1050 // NOTE: It seems like the renderer cannot correctly handle vline / hline segments,
1051 // and trying to display a path using them results in funny artifacts.
1052 builder.lineTo(cur_node->position());
1053 } else {
1054 // this is a bezier segment
1055 builder.curveTo(
1056 prev_node->front()->position(),
1057 cur_node->back()->position(),
1058 cur_node->position());
1059 }
1060 }
1062 /** Construct a node type string to store in the sodipodi:nodetypes attribute. */
1063 std::string PathManipulator::_createTypeString()
1064 {
1065 // precondition: no single-node subpaths
1066 std::stringstream tstr;
1067 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
1068 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
1069 tstr << j->type();
1070 }
1071 // nodestring format peculiarity: first node is counted twice for closed paths
1072 if ((*i)->closed()) tstr << (*i)->begin()->type();
1073 }
1074 return tstr.str();
1075 }
1077 /** Update the path outline. */
1078 void PathManipulator::_updateOutline()
1079 {
1080 if (!_show_outline) {
1081 sp_canvas_item_hide(_outline);
1082 return;
1083 }
1085 Geom::PathVector pv = _spcurve->get_pathvector();
1086 pv *= (_edit_transform * _i2d_transform);
1087 // This SPCurve thing has to be killed with extreme prejudice
1088 SPCurve *_hc = new SPCurve();
1089 if (_show_path_direction) {
1090 // To show the direction, we append additional subpaths which consist of a single
1091 // linear segment that starts at the time value of 0.5 and extends for 10 pixels
1092 // at an angle 150 degrees from the unit tangent. This creates the appearance
1093 // of little 'harpoons' that show the direction of the subpaths.
1094 Geom::PathVector arrows;
1095 for (Geom::PathVector::iterator i = pv.begin(); i != pv.end(); ++i) {
1096 Geom::Path &path = *i;
1097 for (Geom::Path::const_iterator j = path.begin(); j != path.end_default(); ++j) {
1098 Geom::Point at = j->pointAt(0.5);
1099 Geom::Point ut = j->unitTangentAt(0.5);
1100 // rotate the point
1101 ut *= Geom::Rotate(150.0 / 180.0 * M_PI);
1102 Geom::Point arrow_end = _desktop->w2d(
1103 _desktop->d2w(at) + Geom::unit_vector(_desktop->d2w(ut)) * 10.0);
1105 Geom::Path arrow(at);
1106 arrow.appendNew<Geom::LineSegment>(arrow_end);
1107 arrows.push_back(arrow);
1108 }
1109 }
1110 pv.insert(pv.end(), arrows.begin(), arrows.end());
1111 }
1112 _hc->set_pathvector(pv);
1113 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(_outline), _hc);
1114 sp_canvas_item_show(_outline);
1115 _hc->unref();
1116 }
1118 /** Retrieve the geometry of the edited object from the object tree */
1119 void PathManipulator::_getGeometry()
1120 {
1121 using namespace Inkscape::LivePathEffect;
1122 if (!_lpe_key.empty()) {
1123 Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe();
1124 if (lpe) {
1125 PathParam *pathparam = dynamic_cast<PathParam *>(lpe->getParameter(_lpe_key.data()));
1126 if (!_spcurve)
1127 _spcurve = new SPCurve(pathparam->get_pathvector());
1128 else
1129 _spcurve->set_pathvector(pathparam->get_pathvector());
1130 }
1131 } else {
1132 if (_spcurve) _spcurve->unref();
1133 _spcurve = sp_path_get_curve_for_edit(_path);
1134 }
1135 }
1137 /** Set the geometry of the edited object in the object tree, but do not commit to XML */
1138 void PathManipulator::_setGeometry()
1139 {
1140 using namespace Inkscape::LivePathEffect;
1141 if (empty()) return;
1143 if (!_lpe_key.empty()) {
1144 // copied from nodepath.cpp
1145 // NOTE: if we are editing an LPE param, _path is not actually an SPPath, it is
1146 // a LivePathEffectObject. (mad laughter)
1147 Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe();
1148 if (lpe) {
1149 PathParam *pathparam = dynamic_cast<PathParam *>(lpe->getParameter(_lpe_key.data()));
1150 pathparam->set_new_value(_spcurve->get_pathvector(), false);
1151 LIVEPATHEFFECT(_path)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1152 }
1153 } else {
1154 if (_path->repr->attribute("inkscape:original-d"))
1155 sp_path_set_original_curve(_path, _spcurve, true, false);
1156 else
1157 sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false);
1158 }
1159 }
1161 /** Figure out in what attribute to store the nodetype string. */
1162 Glib::ustring PathManipulator::_nodetypesKey()
1163 {
1164 if (_lpe_key.empty()) return "sodipodi:nodetypes";
1165 return _lpe_key + "-nodetypes";
1166 }
1168 /** Return the XML node we are editing.
1169 * This method is wrong but necessary at the moment. */
1170 Inkscape::XML::Node *PathManipulator::_getXMLNode()
1171 {
1172 if (_lpe_key.empty()) return _path->repr;
1173 return LIVEPATHEFFECT(_path)->repr;
1174 }
1176 bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event)
1177 {
1178 if (event->button != 1) return false;
1179 if (held_alt(*event) && held_control(*event)) {
1180 // Ctrl+Alt+click: delete nodes
1181 hideDragPoint();
1182 NodeList::iterator iter = NodeList::get_iterator(n);
1183 NodeList *nl = iter->list();
1185 if (nl->size() <= 1 || (nl->size() <= 2 && !nl->closed())) {
1186 // Removing last node of closed path - delete it
1187 nl->kill();
1188 } else {
1189 // In other cases, delete the node under cursor
1190 _deleteStretch(iter, iter.next(), true);
1191 }
1193 if (!empty()) {
1194 update();
1195 }
1196 // We need to call MPM's method because it could have been our last node
1197 _multi_path_manipulator._doneWithCleanup(_("Delete node"));
1199 return true;
1200 } else if (held_control(*event)) {
1201 // Ctrl+click: cycle between node types
1202 if (n->isEndNode()) {
1203 if (n->type() == NODE_CUSP) {
1204 n->setType(NODE_SMOOTH);
1205 } else {
1206 n->setType(NODE_CUSP);
1207 }
1208 } else {
1209 n->setType(static_cast<NodeType>((n->type() + 1) % NODE_LAST_REAL_TYPE));
1210 }
1211 update();
1212 _commit(_("Cycle node type"));
1213 return true;
1214 }
1215 return false;
1216 }
1218 void PathManipulator::_handleGrabbed()
1219 {
1220 _selection.hideTransformHandles();
1221 }
1223 void PathManipulator::_handleUngrabbed()
1224 {
1225 _selection.restoreTransformHandles();
1226 _commit(_("Drag handle"));
1227 }
1229 bool PathManipulator::_handleClicked(Handle *h, GdkEventButton *event)
1230 {
1231 // retracting by Ctrl+click
1232 if (event->button == 1 && held_control(*event)) {
1233 h->move(h->parent()->position());
1234 update();
1235 _commit(_("Retract handle"));
1236 return true;
1237 }
1238 return false;
1239 }
1241 void PathManipulator::_selectionChanged(SelectableControlPoint *p, bool selected)
1242 {
1243 if (selected) ++_num_selected;
1244 else --_num_selected;
1246 // don't do anything if we do not show handles
1247 if (!_show_handles) return;
1249 // only do something if a node changed selection state
1250 Node *node = dynamic_cast<Node*>(p);
1251 if (!node) return;
1253 // update handle display
1254 NodeList::iterator iters[5];
1255 iters[2] = NodeList::get_iterator(node);
1256 iters[1] = iters[2].prev();
1257 iters[3] = iters[2].next();
1258 if (selected) {
1259 // selection - show handles on this node and adjacent ones
1260 node->showHandles(true);
1261 if (iters[1]) iters[1]->showHandles(true);
1262 if (iters[3]) iters[3]->showHandles(true);
1263 } else {
1264 /* Deselection is more complex.
1265 * The change might affect 3 nodes - this one and two adjacent.
1266 * If the node and both its neighbors are deselected, hide handles.
1267 * Otherwise, leave as is. */
1268 if (iters[1]) iters[0] = iters[1].prev();
1269 if (iters[3]) iters[4] = iters[3].next();
1270 bool nodesel[5];
1271 for (int i = 0; i < 5; ++i) {
1272 nodesel[i] = iters[i] && iters[i]->selected();
1273 }
1274 for (int i = 1; i < 4; ++i) {
1275 if (iters[i] && !nodesel[i-1] && !nodesel[i] && !nodesel[i+1]) {
1276 iters[i]->showHandles(false);
1277 }
1278 }
1279 }
1280 }
1282 /** Removes all nodes belonging to this manipulator from the control pont selection */
1283 void PathManipulator::_removeNodesFromSelection()
1284 {
1285 // remove this manipulator's nodes from selection
1286 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
1287 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
1288 _selection.erase(j.get_pointer());
1289 }
1290 }
1291 }
1293 /** Update the XML representation and put the specified annotation on the undo stack */
1294 void PathManipulator::_commit(Glib::ustring const &annotation)
1295 {
1296 writeXML();
1297 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, annotation.data());
1298 }
1300 /** Update the position of the curve drag point such that it is over the nearest
1301 * point of the path. */
1302 void PathManipulator::_updateDragPoint(Geom::Point const &evp)
1303 {
1304 // TODO find a way to make this faster (no transform required)
1305 Geom::Matrix to_desktop = _edit_transform * _i2d_transform;
1306 Geom::PathVector pv = _spcurve->get_pathvector();
1307 boost::optional<Geom::PathVectorPosition> pvp
1308 = Geom::nearestPoint(pv, _desktop->w2d(evp) * to_desktop.inverse());
1309 if (!pvp) return;
1310 Geom::Point nearest_point = _desktop->d2w(pv.at(pvp->path_nr).pointAt(pvp->t) * to_desktop);
1312 double fracpart;
1313 std::list<SubpathPtr>::iterator spi = _subpaths.begin();
1314 for (unsigned i = 0; i < pvp->path_nr; ++i, ++spi) {}
1315 NodeList::iterator first = (*spi)->before(pvp->t, &fracpart);
1317 double stroke_tolerance = _getStrokeTolerance();
1318 if (Geom::distance(evp, nearest_point) < stroke_tolerance) {
1319 _dragpoint->setVisible(true);
1320 _dragpoint->setPosition(_desktop->w2d(nearest_point));
1321 _dragpoint->setSize(2 * stroke_tolerance);
1322 _dragpoint->setTimeValue(fracpart);
1323 _dragpoint->setIterator(first);
1324 } else {
1325 _dragpoint->setVisible(false);
1326 }
1327 }
1329 /// This is called on zoom change to update the direction arrows
1330 void PathManipulator::_updateOutlineOnZoomChange()
1331 {
1332 if (_show_path_direction) _updateOutline();
1333 }
1335 /** Compute the radius from the edge of the path where clicks chould initiate a curve drag
1336 * or segment selection, in window coordinates. */
1337 double PathManipulator::_getStrokeTolerance()
1338 {
1339 /* Stroke event tolerance is equal to half the stroke's width plus the global
1340 * drag tolerance setting. */
1341 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1342 double ret = prefs->getIntLimited("/options/dragtolerance/value", 2, 0, 100);
1343 if (_path && SP_OBJECT_STYLE(_path) && !SP_OBJECT_STYLE(_path)->stroke.isNone()) {
1344 ret += SP_OBJECT_STYLE(_path)->stroke_width.computed * 0.5
1345 * (_edit_transform * _i2d_transform).descrim() // scale to desktop coords
1346 * _desktop->current_zoom(); // == _d2w.descrim() - scale to window coords
1347 }
1348 return ret;
1349 }
1351 } // namespace UI
1352 } // namespace Inkscape
1354 /*
1355 Local Variables:
1356 mode:c++
1357 c-file-style:"stroustrup"
1358 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1359 indent-tabs-mode:nil
1360 fill-column:99
1361 End:
1362 */
1363 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :