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