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