82fe5344026a443e447f0b823cde9067f7e8c070
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(new SPCurve())
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 _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 // NOTE: this utilizes the fact that Geom::PathVector is an std::vector.
927 // When we erase an element, the next one slides into position,
928 // so we do not increment the iterator even though it is theoretically invalidated.
929 if (i->empty()) {
930 pathv.erase(i);
931 } else {
932 ++i;
933 }
934 }
935 _spcurve->set_pathvector(pathv);
937 pathv *= (_edit_transform * _i2d_transform);
939 // in this loop, we know that there are no zero-segment subpaths
940 for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) {
941 // prepare new subpath
942 SubpathPtr subpath(new NodeList(_subpaths));
943 _subpaths.push_back(subpath);
945 Node *previous_node = new Node(_multi_path_manipulator._path_data.node_data, pit->initialPoint());
946 subpath->push_back(previous_node);
947 Geom::Curve const &cseg = pit->back_closed();
948 bool fuse_ends = pit->closed()
949 && Geom::are_near(cseg.initialPoint(), cseg.finalPoint());
951 for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit) {
952 Geom::Point pos = cit->finalPoint();
953 Node *current_node;
954 // if the closing segment is degenerate and the path is closed, we need to move
955 // the handle of the first node instead of creating a new one
956 if (fuse_ends && cit == --(pit->end_open())) {
957 current_node = subpath->begin().get_pointer();
958 } else {
959 /* regardless of segment type, create a new node at the end
960 * of this segment (unless this is the last segment of a closed path
961 * with a degenerate closing segment */
962 current_node = new Node(_multi_path_manipulator._path_data.node_data, pos);
963 subpath->push_back(current_node);
964 }
965 // if this is a bezier segment, move handles appropriately
966 if (Geom::CubicBezier const *cubic_bezier =
967 dynamic_cast<Geom::CubicBezier const*>(&*cit))
968 {
969 std::vector<Geom::Point> points = cubic_bezier->points();
971 previous_node->front()->setPosition(points[1]);
972 current_node ->back() ->setPosition(points[2]);
973 }
974 previous_node = current_node;
975 }
976 // If the path is closed, make the list cyclic
977 if (pit->closed()) subpath->setClosed(true);
978 }
980 // we need to set the nodetypes after all the handles are in place,
981 // so that pickBestType works correctly
982 // TODO maybe migrate to inkscape:node-types?
983 gchar const *nts_raw = _path ? _path->repr->attribute(_nodetypesKey().data()) : 0;
984 std::string nodetype_string = nts_raw ? nts_raw : "";
985 /* Calculate the needed length of the nodetype string.
986 * For closed paths, the entry is duplicated for the starting node,
987 * so we can just use the count of segments including the closing one
988 * to include the extra end node. */
989 std::string::size_type nodetype_len = 0;
990 for (Geom::PathVector::const_iterator i = pathv.begin(); i != pathv.end(); ++i) {
991 if (i->empty()) continue;
992 nodetype_len += i->size_closed();
993 }
994 /* pad the string to required length with a bogus value.
995 * 'b' and any other letter not recognized by the parser causes the best fit to be set
996 * as the node type */
997 if (nodetype_len > nodetype_string.size()) {
998 nodetype_string.append(nodetype_len - nodetype_string.size(), 'b');
999 }
1000 std::string::iterator tsi = nodetype_string.begin();
1001 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
1002 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
1003 j->setType(Node::parse_nodetype(*tsi++), false);
1004 }
1005 if ((*i)->closed()) {
1006 // STUPIDITY ALERT: it seems we need to use the duplicate type symbol instead of
1007 // the first one to remain backward compatible.
1008 (*i)->begin()->setType(Node::parse_nodetype(*tsi++), false);
1009 }
1010 }
1011 }
1013 /** Construct the geometric representation of nodes and handles, update the outline
1014 * and display */
1015 void PathManipulator::_createGeometryFromControlPoints()
1016 {
1017 Geom::PathBuilder builder;
1018 for (std::list<SubpathPtr>::iterator spi = _subpaths.begin(); spi != _subpaths.end(); ) {
1019 SubpathPtr subpath = *spi;
1020 if (subpath->empty()) {
1021 _subpaths.erase(spi++);
1022 continue;
1023 }
1024 NodeList::iterator prev = subpath->begin();
1025 builder.moveTo(prev->position());
1027 for (NodeList::iterator i = ++subpath->begin(); i != subpath->end(); ++i) {
1028 build_segment(builder, prev.ptr(), i.ptr());
1029 prev = i;
1030 }
1031 if (subpath->closed()) {
1032 // Here we link the last and first node if the path is closed.
1033 // If the last segment is Bezier, we add it.
1034 if (!prev->front()->isDegenerate() || !subpath->begin()->back()->isDegenerate()) {
1035 build_segment(builder, prev.ptr(), subpath->begin().ptr());
1036 }
1037 // if that segment is linear, we just call closePath().
1038 builder.closePath();
1039 }
1040 ++spi;
1041 }
1042 builder.finish();
1043 _spcurve->set_pathvector(builder.peek() * (_edit_transform * _i2d_transform).inverse());
1044 if (_live_outline)
1045 _updateOutline();
1046 if (_live_objects)
1047 _setGeometry();
1048 }
1050 /** Build one segment of the geometric representation.
1051 * @relates PathManipulator */
1052 void build_segment(Geom::PathBuilder &builder, Node *prev_node, Node *cur_node)
1053 {
1054 if (cur_node->back()->isDegenerate() && prev_node->front()->isDegenerate())
1055 {
1056 // NOTE: It seems like the renderer cannot correctly handle vline / hline segments,
1057 // and trying to display a path using them results in funny artifacts.
1058 builder.lineTo(cur_node->position());
1059 } else {
1060 // this is a bezier segment
1061 builder.curveTo(
1062 prev_node->front()->position(),
1063 cur_node->back()->position(),
1064 cur_node->position());
1065 }
1066 }
1068 /** Construct a node type string to store in the sodipodi:nodetypes attribute. */
1069 std::string PathManipulator::_createTypeString()
1070 {
1071 // precondition: no single-node subpaths
1072 std::stringstream tstr;
1073 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
1074 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
1075 tstr << j->type();
1076 }
1077 // nodestring format peculiarity: first node is counted twice for closed paths
1078 if ((*i)->closed()) tstr << (*i)->begin()->type();
1079 }
1080 return tstr.str();
1081 }
1083 /** Update the path outline. */
1084 void PathManipulator::_updateOutline()
1085 {
1086 if (!_show_outline) {
1087 sp_canvas_item_hide(_outline);
1088 return;
1089 }
1091 Geom::PathVector pv = _spcurve->get_pathvector();
1092 pv *= (_edit_transform * _i2d_transform);
1093 // This SPCurve thing has to be killed with extreme prejudice
1094 SPCurve *_hc = new SPCurve();
1095 if (_show_path_direction) {
1096 // To show the direction, we append additional subpaths which consist of a single
1097 // linear segment that starts at the time value of 0.5 and extends for 10 pixels
1098 // at an angle 150 degrees from the unit tangent. This creates the appearance
1099 // of little 'harpoons' that show the direction of the subpaths.
1100 Geom::PathVector arrows;
1101 for (Geom::PathVector::iterator i = pv.begin(); i != pv.end(); ++i) {
1102 Geom::Path &path = *i;
1103 for (Geom::Path::const_iterator j = path.begin(); j != path.end_default(); ++j) {
1104 Geom::Point at = j->pointAt(0.5);
1105 Geom::Point ut = j->unitTangentAt(0.5);
1106 // rotate the point
1107 ut *= Geom::Rotate(150.0 / 180.0 * M_PI);
1108 Geom::Point arrow_end = _desktop->w2d(
1109 _desktop->d2w(at) + Geom::unit_vector(_desktop->d2w(ut)) * 10.0);
1111 Geom::Path arrow(at);
1112 arrow.appendNew<Geom::LineSegment>(arrow_end);
1113 arrows.push_back(arrow);
1114 }
1115 }
1116 pv.insert(pv.end(), arrows.begin(), arrows.end());
1117 }
1118 _hc->set_pathvector(pv);
1119 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(_outline), _hc);
1120 sp_canvas_item_show(_outline);
1121 _hc->unref();
1122 }
1124 /** Retrieve the geometry of the edited object from the object tree */
1125 void PathManipulator::_getGeometry()
1126 {
1127 using namespace Inkscape::LivePathEffect;
1128 if (!_lpe_key.empty()) {
1129 Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe();
1130 if (lpe) {
1131 PathParam *pathparam = dynamic_cast<PathParam *>(lpe->getParameter(_lpe_key.data()));
1132 _spcurve->unref();
1133 _spcurve = new SPCurve(pathparam->get_pathvector());
1134 }
1135 } else {
1136 _spcurve->unref();
1137 _spcurve = sp_path_get_curve_for_edit(_path);
1138 }
1139 }
1141 /** Set the geometry of the edited object in the object tree, but do not commit to XML */
1142 void PathManipulator::_setGeometry()
1143 {
1144 using namespace Inkscape::LivePathEffect;
1145 if (empty()) return;
1147 if (!_lpe_key.empty()) {
1148 // copied from nodepath.cpp
1149 // NOTE: if we are editing an LPE param, _path is not actually an SPPath, it is
1150 // a LivePathEffectObject. (mad laughter)
1151 Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe();
1152 if (lpe) {
1153 PathParam *pathparam = dynamic_cast<PathParam *>(lpe->getParameter(_lpe_key.data()));
1154 pathparam->set_new_value(_spcurve->get_pathvector(), false);
1155 LIVEPATHEFFECT(_path)->requestModified(SP_OBJECT_MODIFIED_FLAG);
1156 }
1157 } else {
1158 if (_path->repr->attribute("inkscape:original-d"))
1159 sp_path_set_original_curve(_path, _spcurve, false, false);
1160 else
1161 sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false);
1162 }
1163 }
1165 /** Figure out in what attribute to store the nodetype string. */
1166 Glib::ustring PathManipulator::_nodetypesKey()
1167 {
1168 if (_lpe_key.empty()) return "sodipodi:nodetypes";
1169 return _lpe_key + "-nodetypes";
1170 }
1172 /** Return the XML node we are editing.
1173 * This method is wrong but necessary at the moment. */
1174 Inkscape::XML::Node *PathManipulator::_getXMLNode()
1175 {
1176 if (_lpe_key.empty()) return _path->repr;
1177 return LIVEPATHEFFECT(_path)->repr;
1178 }
1180 bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event)
1181 {
1182 if (event->button != 1) return false;
1183 if (held_alt(*event) && held_control(*event)) {
1184 // Ctrl+Alt+click: delete nodes
1185 hideDragPoint();
1186 NodeList::iterator iter = NodeList::get_iterator(n);
1187 NodeList *nl = iter->list();
1189 if (nl->size() <= 1 || (nl->size() <= 2 && !nl->closed())) {
1190 // Removing last node of closed path - delete it
1191 nl->kill();
1192 } else {
1193 // In other cases, delete the node under cursor
1194 _deleteStretch(iter, iter.next(), true);
1195 }
1197 if (!empty()) {
1198 update();
1199 }
1200 // We need to call MPM's method because it could have been our last node
1201 _multi_path_manipulator._doneWithCleanup(_("Delete node"));
1203 return true;
1204 } else if (held_control(*event)) {
1205 // Ctrl+click: cycle between node types
1206 if (n->isEndNode()) {
1207 if (n->type() == NODE_CUSP) {
1208 n->setType(NODE_SMOOTH);
1209 } else {
1210 n->setType(NODE_CUSP);
1211 }
1212 } else {
1213 n->setType(static_cast<NodeType>((n->type() + 1) % NODE_LAST_REAL_TYPE));
1214 }
1215 update();
1216 _commit(_("Cycle node type"));
1217 return true;
1218 }
1219 return false;
1220 }
1222 void PathManipulator::_handleGrabbed()
1223 {
1224 _selection.hideTransformHandles();
1225 }
1227 void PathManipulator::_handleUngrabbed()
1228 {
1229 _selection.restoreTransformHandles();
1230 _commit(_("Drag handle"));
1231 }
1233 bool PathManipulator::_handleClicked(Handle *h, GdkEventButton *event)
1234 {
1235 // retracting by Ctrl+click
1236 if (event->button == 1 && held_control(*event)) {
1237 h->move(h->parent()->position());
1238 update();
1239 _commit(_("Retract handle"));
1240 return true;
1241 }
1242 return false;
1243 }
1245 void PathManipulator::_selectionChanged(SelectableControlPoint *p, bool selected)
1246 {
1247 if (selected) ++_num_selected;
1248 else --_num_selected;
1250 // don't do anything if we do not show handles
1251 if (!_show_handles) return;
1253 // only do something if a node changed selection state
1254 Node *node = dynamic_cast<Node*>(p);
1255 if (!node) return;
1257 // update handle display
1258 NodeList::iterator iters[5];
1259 iters[2] = NodeList::get_iterator(node);
1260 iters[1] = iters[2].prev();
1261 iters[3] = iters[2].next();
1262 if (selected) {
1263 // selection - show handles on this node and adjacent ones
1264 node->showHandles(true);
1265 if (iters[1]) iters[1]->showHandles(true);
1266 if (iters[3]) iters[3]->showHandles(true);
1267 } else {
1268 /* Deselection is more complex.
1269 * The change might affect 3 nodes - this one and two adjacent.
1270 * If the node and both its neighbors are deselected, hide handles.
1271 * Otherwise, leave as is. */
1272 if (iters[1]) iters[0] = iters[1].prev();
1273 if (iters[3]) iters[4] = iters[3].next();
1274 bool nodesel[5];
1275 for (int i = 0; i < 5; ++i) {
1276 nodesel[i] = iters[i] && iters[i]->selected();
1277 }
1278 for (int i = 1; i < 4; ++i) {
1279 if (iters[i] && !nodesel[i-1] && !nodesel[i] && !nodesel[i+1]) {
1280 iters[i]->showHandles(false);
1281 }
1282 }
1283 }
1284 }
1286 /** Removes all nodes belonging to this manipulator from the control pont selection */
1287 void PathManipulator::_removeNodesFromSelection()
1288 {
1289 // remove this manipulator's nodes from selection
1290 for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
1291 for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
1292 _selection.erase(j.get_pointer());
1293 }
1294 }
1295 }
1297 /** Update the XML representation and put the specified annotation on the undo stack */
1298 void PathManipulator::_commit(Glib::ustring const &annotation)
1299 {
1300 writeXML();
1301 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, annotation.data());
1302 }
1304 /** Update the position of the curve drag point such that it is over the nearest
1305 * point of the path. */
1306 void PathManipulator::_updateDragPoint(Geom::Point const &evp)
1307 {
1308 Geom::Matrix to_desktop = _edit_transform * _i2d_transform;
1309 Geom::PathVector pv = _spcurve->get_pathvector();
1310 boost::optional<Geom::PathVectorPosition> pvp
1311 = Geom::nearestPoint(pv, _desktop->w2d(evp) * to_desktop.inverse());
1312 if (!pvp) return;
1313 Geom::Point nearest_point = _desktop->d2w(pv.at(pvp->path_nr).pointAt(pvp->t) * to_desktop);
1315 double fracpart;
1316 std::list<SubpathPtr>::iterator spi = _subpaths.begin();
1317 for (unsigned i = 0; i < pvp->path_nr; ++i, ++spi) {}
1318 NodeList::iterator first = (*spi)->before(pvp->t, &fracpart);
1320 double stroke_tolerance = _getStrokeTolerance();
1321 if (Geom::distance(evp, nearest_point) < stroke_tolerance) {
1322 _dragpoint->setVisible(true);
1323 _dragpoint->setPosition(_desktop->w2d(nearest_point));
1324 _dragpoint->setSize(2 * stroke_tolerance);
1325 _dragpoint->setTimeValue(fracpart);
1326 _dragpoint->setIterator(first);
1327 } else {
1328 _dragpoint->setVisible(false);
1329 }
1330 }
1332 /// This is called on zoom change to update the direction arrows
1333 void PathManipulator::_updateOutlineOnZoomChange()
1334 {
1335 if (_show_path_direction) _updateOutline();
1336 }
1338 /** Compute the radius from the edge of the path where clicks chould initiate a curve drag
1339 * or segment selection, in window coordinates. */
1340 double PathManipulator::_getStrokeTolerance()
1341 {
1342 /* Stroke event tolerance is equal to half the stroke's width plus the global
1343 * drag tolerance setting. */
1344 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1345 double ret = prefs->getIntLimited("/options/dragtolerance/value", 2, 0, 100);
1346 if (_path && SP_OBJECT_STYLE(_path) && !SP_OBJECT_STYLE(_path)->stroke.isNone()) {
1347 ret += SP_OBJECT_STYLE(_path)->stroke_width.computed * 0.5
1348 * (_edit_transform * _i2d_transform).descrim() // scale to desktop coords
1349 * _desktop->current_zoom(); // == _d2w.descrim() - scale to window coords
1350 }
1351 return ret;
1352 }
1354 } // namespace UI
1355 } // namespace Inkscape
1357 /*
1358 Local Variables:
1359 mode:c++
1360 c-file-style:"stroustrup"
1361 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1362 indent-tabs-mode:nil
1363 fill-column:99
1364 End:
1365 */
1366 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :