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