Code

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