1 /** @file
2 * Multi 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 <tr1/unordered_set>
12 #include <boost/shared_ptr.hpp>
13 #include <glib.h>
14 #include <glibmm/i18n.h>
15 #include "desktop.h"
16 #include "desktop-handles.h"
17 #include "document.h"
18 #include "live_effects/lpeobject.h"
19 #include "message-stack.h"
20 #include "preferences.h"
21 #include "sp-path.h"
22 #include "ui/tool/control-point-selection.h"
23 #include "ui/tool/event-utils.h"
24 #include "ui/tool/node.h"
25 #include "ui/tool/multi-path-manipulator.h"
26 #include "ui/tool/path-manipulator.h"
28 namespace std { using namespace tr1; }
30 namespace Inkscape {
31 namespace UI {
33 namespace {
34 typedef std::pair<NodeList::iterator, NodeList::iterator> IterPair;
35 typedef std::vector<IterPair> IterPairList;
36 typedef std::unordered_set<NodeList::iterator> IterSet;
37 typedef std::multimap<double, IterPair> DistanceMap;
38 typedef std::pair<double, IterPair> DistanceMapItem;
40 /** Find pairs of selected endnodes suitable for joining. */
41 void find_join_iterators(ControlPointSelection &sel, IterPairList &pairs)
42 {
43 IterSet join_iters;
44 DistanceMap dists;
46 // find all endnodes in selection
47 for (ControlPointSelection::iterator i = sel.begin(); i != sel.end(); ++i) {
48 Node *node = dynamic_cast<Node*>(i->first);
49 if (!node) continue;
50 NodeList::iterator iter = NodeList::get_iterator(node);
51 if (!iter.next() || !iter.prev()) join_iters.insert(iter);
52 }
54 if (join_iters.size() < 2) return;
56 // Below we find the closest pairs. The algorithm is O(N^3).
57 // We can go down to O(N^2 log N) by using O(N^2) memory, by putting all pairs
58 // with their distances in a multimap (not worth it IMO).
59 while (join_iters.size() >= 2) {
60 double closest = DBL_MAX;
61 IterPair closest_pair;
62 for (IterSet::iterator i = join_iters.begin(); i != join_iters.end(); ++i) {
63 for (IterSet::iterator j = join_iters.begin(); j != i; ++j) {
64 double dist = Geom::distance(**i, **j);
65 if (dist < closest) {
66 closest = dist;
67 closest_pair = std::make_pair(*i, *j);
68 }
69 }
70 }
71 pairs.push_back(closest_pair);
72 join_iters.erase(closest_pair.first);
73 join_iters.erase(closest_pair.second);
74 }
75 }
77 /** After this function, first should be at the end of path and second at the beginnning.
78 * @returns True if the nodes are in the same subpath */
79 bool prepare_join(IterPair &join_iters)
80 {
81 if (&NodeList::get(join_iters.first) == &NodeList::get(join_iters.second)) {
82 if (join_iters.first.next()) // if first is begin, swap the iterators
83 std::swap(join_iters.first, join_iters.second);
84 return true;
85 }
87 NodeList &sp_first = NodeList::get(join_iters.first);
88 NodeList &sp_second = NodeList::get(join_iters.second);
89 if (join_iters.first.next()) { // first is begin
90 if (join_iters.second.next()) { // second is begin
91 sp_first.reverse();
92 } else { // second is end
93 std::swap(join_iters.first, join_iters.second);
94 }
95 } else { // first is end
96 if (join_iters.second.next()) { // second is begin
97 // do nothing
98 } else { // second is end
99 sp_second.reverse();
100 }
101 }
102 return false;
103 }
104 } // anonymous namespace
107 MultiPathManipulator::MultiPathManipulator(PathSharedData &data, sigc::connection &chg)
108 : PointManipulator(data.node_data.desktop, *data.node_data.selection)
109 , _path_data(data)
110 , _changed(chg)
111 {
112 _selection.signal_commit.connect(
113 sigc::mem_fun(*this, &MultiPathManipulator::_commit));
114 _selection.signal_point_changed.connect(
115 sigc::hide( sigc::hide(
116 signal_coords_changed.make_slot())));
117 }
119 MultiPathManipulator::~MultiPathManipulator()
120 {
121 _mmap.clear();
122 }
124 /** Remove empty manipulators. */
125 void MultiPathManipulator::cleanup()
126 {
127 for (MapType::iterator i = _mmap.begin(); i != _mmap.end(); ) {
128 if (i->second->empty()) _mmap.erase(i++);
129 else ++i;
130 }
131 }
133 /** @brief Change the set of items to edit.
134 *
135 * This method attempts to preserve as much of the state as possible. */
136 void MultiPathManipulator::setItems(std::set<ShapeRecord> const &s)
137 {
138 std::set<ShapeRecord> shapes(s);
140 // iterate over currently edited items, modifying / removing them as necessary
141 for (MapType::iterator i = _mmap.begin(); i != _mmap.end();) {
142 std::set<ShapeRecord>::iterator si = shapes.find(i->first);
143 if (si == shapes.end()) {
144 // This item is no longer supposed to be edited - remove its manipulator
145 _mmap.erase(i++);
146 } else {
147 ShapeRecord const &sr = i->first;
148 ShapeRecord const &sr_new = *si;
149 // if the shape record differs, replace the key only and modify other values
150 if (sr.edit_transform != sr_new.edit_transform ||
151 sr.role != sr_new.role)
152 {
153 boost::shared_ptr<PathManipulator> hold(i->second);
154 if (sr.edit_transform != sr_new.edit_transform)
155 hold->setControlsTransform(sr_new.edit_transform);
156 if (sr.role != sr_new.role) {
157 //hold->setOutlineColor(_getOutlineColor(sr_new.role));
158 }
159 _mmap.erase(sr);
160 _mmap.insert(std::make_pair(sr_new, hold));
161 }
162 shapes.erase(si); // remove the processed record
163 ++i;
164 }
165 }
167 // add newly selected items
168 for (std::set<ShapeRecord>::iterator i = shapes.begin(); i != shapes.end(); ++i) {
169 ShapeRecord const &r = *i;
170 if (!SP_IS_PATH(r.item) && !IS_LIVEPATHEFFECT(r.item)) continue;
171 boost::shared_ptr<PathManipulator> newpm(new PathManipulator(*this, (SPPath*) r.item,
172 r.edit_transform, _getOutlineColor(r.role), r.lpe_key));
173 newpm->showHandles(_show_handles);
174 // always show outlines for clips and masks
175 newpm->showOutline(_show_outline || r.role != SHAPE_ROLE_NORMAL);
176 newpm->showPathDirection(_show_path_direction);
177 newpm->setLiveOutline(_live_outline);
178 newpm->setLiveObjects(_live_objects);
179 _mmap.insert(std::make_pair(r, newpm));
180 }
181 }
183 void MultiPathManipulator::selectSubpaths()
184 {
185 if (_selection.empty()) {
186 _selection.selectAll();
187 } else {
188 invokeForAll(&PathManipulator::selectSubpaths);
189 }
190 }
192 void MultiPathManipulator::shiftSelection(int dir)
193 {
194 invokeForAll(&PathManipulator::shiftSelection, dir);
195 }
197 void MultiPathManipulator::invertSelectionInSubpaths()
198 {
199 invokeForAll(&PathManipulator::invertSelectionInSubpaths);
200 }
202 void MultiPathManipulator::setNodeType(NodeType type)
203 {
204 if (_selection.empty()) return;
205 for (ControlPointSelection::iterator i = _selection.begin(); i != _selection.end(); ++i) {
206 Node *node = dynamic_cast<Node*>(i->first);
207 if (node) node->setType(type);
208 }
209 _done(_("Change node type"));
210 }
212 void MultiPathManipulator::setSegmentType(SegmentType type)
213 {
214 if (_selection.empty()) return;
215 invokeForAll(&PathManipulator::setSegmentType, type);
216 if (type == SEGMENT_STRAIGHT) {
217 _done(_("Straighten segments"));
218 } else {
219 _done(_("Make segments curves"));
220 }
221 }
223 void MultiPathManipulator::insertNodes()
224 {
225 invokeForAll(&PathManipulator::insertNodes);
226 _done(_("Add nodes"));
227 }
229 void MultiPathManipulator::joinNodes()
230 {
231 invokeForAll(&PathManipulator::hideDragPoint);
232 // Node join has two parts. In the first one we join two subpaths by fusing endpoints
233 // into one. In the second we fuse nodes in each subpath.
234 IterPairList joins;
235 NodeList::iterator preserve_pos;
236 Node *mouseover_node = dynamic_cast<Node*>(ControlPoint::mouseovered_point);
237 if (mouseover_node) {
238 preserve_pos = NodeList::get_iterator(mouseover_node);
239 }
240 find_join_iterators(_selection, joins);
242 for (IterPairList::iterator i = joins.begin(); i != joins.end(); ++i) {
243 bool same_path = prepare_join(*i);
244 NodeList &sp_first = NodeList::get(i->first);
245 NodeList &sp_second = NodeList::get(i->second);
246 i->first->setType(NODE_CUSP, false);
248 Geom::Point joined_pos, pos_handle_front, pos_handle_back;
249 pos_handle_front = *i->second->front();
250 pos_handle_back = *i->first->back();
252 // When we encounter the mouseover node, we unset the iterator - it will be invalidated
253 if (i->first == preserve_pos) {
254 joined_pos = *i->first;
255 preserve_pos = NodeList::iterator();
256 } else if (i->second == preserve_pos) {
257 joined_pos = *i->second;
258 preserve_pos = NodeList::iterator();
259 } else {
260 joined_pos = Geom::middle_point(*i->first, *i->second);
261 }
263 // if the handles aren't degenerate, don't move them
264 i->first->move(joined_pos);
265 Node *joined_node = i->first.ptr();
266 if (!i->second->front()->isDegenerate()) {
267 joined_node->front()->setPosition(pos_handle_front);
268 }
269 if (!i->first->back()->isDegenerate()) {
270 joined_node->back()->setPosition(pos_handle_back);
271 }
272 sp_second.erase(i->second);
274 if (same_path) {
275 sp_first.setClosed(true);
276 } else {
277 sp_first.splice(sp_first.end(), sp_second);
278 sp_second.kill();
279 }
280 _selection.insert(i->first.ptr());
281 }
283 if (joins.empty()) {
284 // Second part replaces contiguous selections of nodes with single nodes
285 invokeForAll(&PathManipulator::weldNodes, preserve_pos);
286 }
288 _doneWithCleanup(_("Join nodes"));
289 }
291 void MultiPathManipulator::breakNodes()
292 {
293 if (_selection.empty()) return;
294 invokeForAll(&PathManipulator::breakNodes);
295 _done(_("Break nodes"));
296 }
298 void MultiPathManipulator::deleteNodes(bool keep_shape)
299 {
300 if (_selection.empty()) return;
301 invokeForAll(&PathManipulator::deleteNodes, keep_shape);
302 _doneWithCleanup(_("Delete nodes"));
303 }
305 /** Join selected endpoints to create segments. */
306 void MultiPathManipulator::joinSegments()
307 {
308 IterPairList joins;
309 find_join_iterators(_selection, joins);
311 for (IterPairList::iterator i = joins.begin(); i != joins.end(); ++i) {
312 bool same_path = prepare_join(*i);
313 NodeList &sp_first = NodeList::get(i->first);
314 NodeList &sp_second = NodeList::get(i->second);
315 i->first->setType(NODE_CUSP, false);
316 i->second->setType(NODE_CUSP, false);
317 if (same_path) {
318 sp_first.setClosed(true);
319 } else {
320 sp_first.splice(sp_first.end(), sp_second);
321 sp_second.kill();
322 }
323 }
325 if (joins.empty()) {
326 invokeForAll(&PathManipulator::weldSegments);
327 }
328 _doneWithCleanup("Join segments");
329 }
331 void MultiPathManipulator::deleteSegments()
332 {
333 if (_selection.empty()) return;
334 invokeForAll(&PathManipulator::deleteSegments);
335 _doneWithCleanup("Delete segments");
336 }
338 void MultiPathManipulator::alignNodes(Geom::Dim2 d)
339 {
340 _selection.align(d);
341 if (d == Geom::X) {
342 _done("Align nodes to a horizontal line");
343 } else {
344 _done("Align nodes to a vertical line");
345 }
346 }
348 void MultiPathManipulator::distributeNodes(Geom::Dim2 d)
349 {
350 _selection.distribute(d);
351 if (d == Geom::X) {
352 _done("Distrubute nodes horizontally");
353 } else {
354 _done("Distribute nodes vertically");
355 }
356 }
358 void MultiPathManipulator::reverseSubpaths()
359 {
360 if (_selection.empty()) {
361 invokeForAll(&PathManipulator::reverseSubpaths, false);
362 _done("Reverse subpaths");
363 } else {
364 invokeForAll(&PathManipulator::reverseSubpaths, true);
365 _done("Reverse selected subpaths");
366 }
367 }
369 void MultiPathManipulator::move(Geom::Point const &delta)
370 {
371 _selection.transform(Geom::Translate(delta));
372 _done("Move nodes");
373 }
375 void MultiPathManipulator::showOutline(bool show)
376 {
377 for (MapType::iterator i = _mmap.begin(); i != _mmap.end(); ++i) {
378 // always show outlines for clipping paths and masks
379 i->second->showOutline(show || i->first.role != SHAPE_ROLE_NORMAL);
380 }
381 _show_outline = show;
382 }
384 void MultiPathManipulator::showHandles(bool show)
385 {
386 invokeForAll(&PathManipulator::showHandles, show);
387 _show_handles = show;
388 }
390 void MultiPathManipulator::showPathDirection(bool show)
391 {
392 invokeForAll(&PathManipulator::showPathDirection, show);
393 _show_path_direction = show;
394 }
396 /** @brief Set live outline update status
397 * When set to true, outline will be updated continuously when dragging
398 * or transforming nodes. Otherwise it will only update when changes are committed
399 * to XML. */
400 void MultiPathManipulator::setLiveOutline(bool set)
401 {
402 invokeForAll(&PathManipulator::setLiveOutline, set);
403 _live_outline = set;
404 }
406 /** @brief Set live object update status
407 * When set to true, objects will be updated continuously when dragging
408 * or transforming nodes. Otherwise they will only update when changes are committed
409 * to XML. */
410 void MultiPathManipulator::setLiveObjects(bool set)
411 {
412 invokeForAll(&PathManipulator::setLiveObjects, set);
413 _live_objects = set;
414 }
416 void MultiPathManipulator::updateOutlineColors()
417 {
418 //for (MapType::iterator i = _mmap.begin(); i != _mmap.end(); ++i) {
419 // i->second->setOutlineColor(_getOutlineColor(i->first.role));
420 //}
421 }
423 bool MultiPathManipulator::event(GdkEvent *event)
424 {
425 switch (event->type) {
426 case GDK_KEY_PRESS:
427 switch (shortcut_key(event->key)) {
428 case GDK_Insert:
429 case GDK_KP_Insert:
430 // Insert - insert nodes in the middle of selected segments
431 insertNodes();
432 return true;
433 case GDK_i:
434 case GDK_I:
435 if (held_only_shift(event->key)) {
436 // Shift+I - insert nodes (alternate keybinding for Mac keyboards
437 // that don't have the Insert key)
438 insertNodes();
439 return true;
440 }
441 break;
442 case GDK_j:
443 case GDK_J:
444 if (held_only_shift(event->key)) {
445 // Shift+J - join nodes
446 joinNodes();
447 return true;
448 }
449 if (held_only_alt(event->key)) {
450 // Alt+J - join segments
451 joinSegments();
452 return true;
453 }
454 break;
455 case GDK_b:
456 case GDK_B:
457 if (held_only_shift(event->key)) {
458 // Shift+B - break nodes
459 breakNodes();
460 return true;
461 }
462 break;
463 case GDK_Delete:
464 case GDK_KP_Delete:
465 case GDK_BackSpace:
466 if (held_shift(event->key)) break;
467 if (held_alt(event->key)) {
468 // Alt+Delete - delete segments
469 deleteSegments();
470 } else {
471 // Control+Delete - delete nodes
472 // Delete - delete nodes preserving shape
473 deleteNodes(!held_control(event->key));
474 }
475 return true;
476 case GDK_c:
477 case GDK_C:
478 if (held_only_shift(event->key)) {
479 // Shift+C - make nodes cusp
480 setNodeType(NODE_CUSP);
481 return true;
482 }
483 break;
484 case GDK_s:
485 case GDK_S:
486 if (held_only_shift(event->key)) {
487 // Shift+S - make nodes smooth
488 setNodeType(NODE_SMOOTH);
489 return true;
490 }
491 break;
492 case GDK_a:
493 case GDK_A:
494 if (held_only_shift(event->key)) {
495 // Shift+A - make nodes auto-smooth
496 setNodeType(NODE_AUTO);
497 return true;
498 }
499 break;
500 case GDK_y:
501 case GDK_Y:
502 if (held_only_shift(event->key)) {
503 // Shift+Y - make nodes symmetric
504 setNodeType(NODE_SYMMETRIC);
505 return true;
506 }
507 break;
508 case GDK_r:
509 case GDK_R:
510 if (held_only_shift(event->key)) {
511 // Shift+R - reverse subpaths
512 reverseSubpaths();
513 }
514 break;
515 default:
516 break;
517 }
518 break;
519 default: break;
520 }
522 for (MapType::iterator i = _mmap.begin(); i != _mmap.end(); ++i) {
523 if (i->second->event(event)) return true;
524 }
525 return false;
526 }
528 /** Commit changes to XML and add undo stack entry based on the action that was done. Invoked
529 * by sub-manipulators, for example TransformHandleSet and ControlPointSelection. */
530 void MultiPathManipulator::_commit(CommitEvent cps)
531 {
532 gchar const *reason = NULL;
533 gchar const *key = NULL;
534 switch(cps) {
535 case COMMIT_MOUSE_MOVE:
536 reason = _("Move nodes");
537 break;
538 case COMMIT_KEYBOARD_MOVE_X:
539 reason = _("Move nodes horizontally");
540 key = "node:move:x";
541 break;
542 case COMMIT_KEYBOARD_MOVE_Y:
543 reason = _("Move nodes vertically");
544 key = "node:move:y";
545 break;
546 case COMMIT_MOUSE_ROTATE:
547 reason = _("Rotate nodes");
548 break;
549 case COMMIT_KEYBOARD_ROTATE:
550 reason = _("Rotate nodes");
551 key = "node:rotate";
552 break;
553 case COMMIT_MOUSE_SCALE_UNIFORM:
554 reason = _("Scale nodes uniformly");
555 break;
556 case COMMIT_MOUSE_SCALE:
557 reason = _("Scale nodes");
558 break;
559 case COMMIT_KEYBOARD_SCALE_UNIFORM:
560 reason = _("Scale nodes uniformly");
561 key = "node:scale:uniform";
562 break;
563 case COMMIT_KEYBOARD_SCALE_X:
564 reason = _("Scale nodes horizontally");
565 key = "node:scale:x";
566 break;
567 case COMMIT_KEYBOARD_SCALE_Y:
568 reason = _("Scale nodes vertically");
569 key = "node:scale:y";
570 break;
571 case COMMIT_FLIP_X:
572 reason = _("Flip nodes horizontally");
573 break;
574 case COMMIT_FLIP_Y:
575 reason = _("Flip nodes vertically");
576 break;
577 default: return;
578 }
580 _selection.signal_update.emit();
581 invokeForAll(&PathManipulator::writeXML);
582 if (key) {
583 sp_document_maybe_done(sp_desktop_document(_desktop), key, SP_VERB_CONTEXT_NODE, reason);
584 } else {
585 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, reason);
586 }
587 signal_coords_changed.emit();
588 }
590 /** Commits changes to XML and adds undo stack entry. */
591 void MultiPathManipulator::_done(gchar const *reason) {
592 invokeForAll(&PathManipulator::update);
593 invokeForAll(&PathManipulator::writeXML);
594 sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, reason);
595 signal_coords_changed.emit();
596 }
598 /** Commits changes to XML, adds undo stack entry and removes empty manipulators. */
599 void MultiPathManipulator::_doneWithCleanup(gchar const *reason) {
600 _changed.block();
601 _done(reason);
602 cleanup();
603 _changed.unblock();
604 }
606 /** Get an outline color based on the shape's role (normal, mask, LPE parameter, etc.). */
607 guint32 MultiPathManipulator::_getOutlineColor(ShapeRole role)
608 {
609 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
610 switch(role) {
611 case SHAPE_ROLE_CLIPPING_PATH:
612 return prefs->getColor("/tools/nodes/clipping_path_color", 0x00ff00ff);
613 case SHAPE_ROLE_MASK:
614 return prefs->getColor("/tools/nodes/mask_color", 0x0000ffff);
615 case SHAPE_ROLE_LPE_PARAM:
616 return prefs->getColor("/tools/nodes/lpe_param_color", 0x009000ff);
617 case SHAPE_ROLE_NORMAL:
618 default:
619 return prefs->getColor("/tools/nodes/outline_color", 0xff0000ff);
620 }
621 }
623 } // namespace UI
624 } // namespace Inkscape
626 /*
627 Local Variables:
628 mode:c++
629 c-file-style:"stroustrup"
630 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
631 indent-tabs-mode:nil
632 fill-column:99
633 End:
634 */
635 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :