Code

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