From 31bb8269c26a781036448ed8f8cd93cc84fb2118 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 29 Nov 2009 16:33:18 +0100 Subject: First GSoC node tool commit to Bazaar (bzr r8846.1.1) --- src/ui/tool/path-manipulator.cpp | 1183 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 1183 insertions(+) create mode 100644 src/ui/tool/path-manipulator.cpp (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp new file mode 100644 index 000000000..ef8572330 --- /dev/null +++ b/src/ui/tool/path-manipulator.cpp @@ -0,0 +1,1183 @@ +/** @file + * Path manipulator - implementation + */ +/* Authors: + * Krzysztof KosiƄski + * + * Copyright (C) 2009 Authors + * Released under GNU GPL, read the file 'COPYING' for more information + */ + +#include +#include +#include +#include +#include +#include <2geom/bezier-curve.h> +#include <2geom/bezier-utils.h> +#include <2geom/svg-path.h> +#include +#include +#include "ui/tool/path-manipulator.h" +#include "desktop.h" +#include "desktop-handles.h" +#include "display/sp-canvas.h" +#include "display/sp-canvas-util.h" +#include "display/curve.h" +#include "display/canvas-bpath.h" +#include "document.h" +#include "sp-path.h" +#include "helper/geom.h" +#include "preferences.h" +#include "style.h" +#include "ui/tool/control-point-selection.h" +#include "ui/tool/curve-drag-point.h" +#include "ui/tool/event-utils.h" +#include "ui/tool/multi-path-manipulator.h" +#include "xml/node.h" +#include "xml/node-observer.h" + +namespace Inkscape { +namespace UI { + +namespace { +/// Types of path changes that we must react to. +enum PathChange { + PATH_CHANGE_D, + PATH_CHANGE_TRANSFORM +}; + +} // anonymous namespace + +/** + * Notifies the path manipulator when something changes the path being edited + * (e.g. undo / redo) + */ +class PathManipulatorObserver : public Inkscape::XML::NodeObserver { +public: + PathManipulatorObserver(PathManipulator *p) : _pm(p), _blocked(false) {} + virtual void notifyAttributeChanged(Inkscape::XML::Node &, GQuark attr, + Util::ptr_shared, Util::ptr_shared) + { + GQuark path_d = g_quark_from_static_string("d"); + GQuark path_transform = g_quark_from_static_string("transform"); + // do nothing if blocked + if (_blocked) return; + + // only react to "d" (path data) and "transform" attribute changes + if (attr == path_d) { + _pm->_externalChange(PATH_CHANGE_D); + } else if (attr == path_transform) { + _pm->_externalChange(PATH_CHANGE_TRANSFORM); + } + } + void block() { _blocked = true; } + void unblock() { _blocked = false; } +private: + PathManipulator *_pm; + bool _blocked; +}; + +void build_segment(Geom::PathBuilder &, Node *, Node *); + +PathManipulator::PathManipulator(PathSharedData const &data, SPPath *path, + Geom::Matrix const &et, guint32 outline_color) + : PointManipulator(data.node_data.desktop, *data.node_data.selection) + , _path_data(data) + , _path(path) + , _spcurve(sp_path_get_curve_for_edit(path)) + , _dragpoint(new CurveDragPoint(*this)) + , _observer(new PathManipulatorObserver(this)) + , _edit_transform(et) + , _show_handles(true) + , _show_outline(false) +{ + /* Because curve drag point is always created first, it does not cover nodes */ + _i2d_transform = sp_item_i2d_affine(SP_ITEM(path)); + _d2i_transform = _i2d_transform.inverse(); + _dragpoint->setVisible(false); + + _outline = sp_canvas_bpath_new(_path_data.outline_group, NULL); + sp_canvas_item_hide(_outline); + sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(_outline), outline_color, 1.0, + SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT); + sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(_outline), 0, SP_WIND_RULE_NONZERO); + + _subpaths.signal_insert_node.connect( + sigc::mem_fun(*this, &PathManipulator::_attachNodeHandlers)); + _subpaths.signal_remove_node.connect( + sigc::mem_fun(*this, &PathManipulator::_removeNodeHandlers)); + _selection.signal_update.connect( + sigc::mem_fun(*this, &PathManipulator::update)); + _selection.signal_point_changed.connect( + sigc::mem_fun(*this, &PathManipulator::_selectionChanged)); + _dragpoint->signal_update.connect( + sigc::mem_fun(*this, &PathManipulator::update)); + _desktop->signal_zoom_changed.connect( + sigc::hide( sigc::mem_fun(*this, &PathManipulator::_updateOutlineOnZoomChange))); + + _createControlPointsFromGeometry(); + + _path->repr->addObserver(*_observer); +} + +PathManipulator::~PathManipulator() +{ + delete _dragpoint; + if (_path) _path->repr->removeObserver(*_observer); + delete _observer; + gtk_object_destroy(_outline); + _spcurve->unref(); + clear(); +} + +/** Handle motion events to update the position of the curve drag point. */ +bool PathManipulator::event(GdkEvent *event) +{ + if (empty()) return false; + + switch (event->type) + { + case GDK_MOTION_NOTIFY: + _updateDragPoint(event_point(event->motion)); + break; + default: break; + } + return false; +} + +/** Check whether the manipulator has any nodes. */ +bool PathManipulator::empty() { + return !_path || _subpaths.empty(); +} + +/** Update the display and the outline of the path. */ +void PathManipulator::update() +{ + _createGeometryFromControlPoints(); +} + +/** Store the changes to the path in XML. */ +void PathManipulator::writeXML() +{ + if (!_path) return; + _observer->block(); + if (!empty()) { + _path->updateRepr(); + _path->repr->setAttribute("sodipodi:nodetypes", _createTypeString().data()); + } else { + // this manipulator will have to be destroyed right after this call + _path->repr->removeObserver(*_observer); + sp_object_ref(_path); + _path->deleteObject(true, true); + sp_object_unref(_path); + _path = 0; + } + _observer->unblock(); +} + +/** Remove all nodes from the path. */ +void PathManipulator::clear() +{ + // no longer necessary since nodes remove themselves from selection on destruction + //_removeNodesFromSelection(); + _subpaths.clear(); +} + +/** Select all nodes in subpaths that have something selected. */ +void PathManipulator::selectSubpaths() +{ + for (std::list::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + NodeList::iterator sp_start = (*i)->begin(), sp_end = (*i)->end(); + for (NodeList::iterator j = sp_start; j != sp_end; ++j) { + if (j->selected()) { + // if at least one of the nodes from this subpath is selected, + // select all nodes from this subpath + for (NodeList::iterator ins = sp_start; ins != sp_end; ++ins) + _selection.insert(ins.ptr()); + continue; + } + } + } +} + +/** Select all nodes in the path. */ +void PathManipulator::selectAll() +{ + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + _selection.insert(j.ptr()); + } + } +} + +/** Select points inside the given rectangle. If all points inside it are already selected, + * they will be deselected. + * @param area Area to select + */ +void PathManipulator::selectArea(Geom::Rect const &area) +{ + bool nothing_selected = true; + std::vector in_area; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (area.contains(j->position())) { + in_area.push_back(j.ptr()); + if (!j->selected()) { + _selection.insert(j.ptr()); + nothing_selected = false; + } + } + } + } + if (nothing_selected) { + for (std::vector::iterator i = in_area.begin(); i != in_area.end(); ++i) { + _selection.erase(*i); + } + } +} + +/** Move the selection forward or backward by one node in each subpath, based on the sign + * of the parameter. */ +void PathManipulator::shiftSelection(int dir) +{ + if (dir == 0) return; + // We cannot do any tricks here, like iterating in different directions based on + // the sign and only setting the selection of nodes behind us, because it would break + // for closed paths. + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + std::deque sels; // I hope this is specialized for bools! + unsigned num = 0; + + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + sels.push_back(j->selected()); + _selection.erase(j.ptr()); + ++num; + } + if (num == 0) continue; // should never happen! + + num = 0; + // In closed subpath, shift the selection cyclically. In an open one, + // let the selection 'slide into nothing' at ends. + if (dir > 0) { + if ((*i)->closed()) { + bool last = sels.back(); + sels.pop_back(); + sels.push_front(last); + } else { + sels.push_front(false); + } + } else { + if ((*i)->closed()) { + bool first = sels.front(); + sels.pop_front(); + sels.push_back(first); + } else { + sels.push_back(false); + num = 1; + } + } + + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (sels[num]) _selection.insert(j.ptr()); + ++num; + } + } +} + +/** Invert selection in the entire path. */ +void PathManipulator::invertSelection() +{ + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (j->selected()) _selection.erase(j.ptr()); + else _selection.insert(j.ptr()); + } + } +} + +/** Invert selection in the selected subpaths. */ +void PathManipulator::invertSelectionInSubpaths() +{ + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (j->selected()) { + // found selected node - invert selection in this subpath + for (NodeList::iterator k = (*i)->begin(); k != (*i)->end(); ++k) { + if (k->selected()) _selection.erase(k.ptr()); + else _selection.insert(k.ptr()); + } + // next subpath + break; + } + } + } +} + +/** Insert a new node in the middle of each selected segment. */ +void PathManipulator::insertNodes() +{ + if (!_num_selected) return; + + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + NodeList::iterator k = j.next(); + if (k && j->selected() && k->selected()) { + j = subdivideSegment(j, 0.5); + _selection.insert(j.ptr()); + } + } + } +} + +/** Replace contiguous selections of nodes in each subpath with one node. */ +void PathManipulator::weldNodes(NodeList::iterator const &preserve_pos) +{ + bool pos_valid = preserve_pos; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + SubpathPtr sp = *i; + unsigned num_selected = 0, num_unselected = 0; + for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) { + if (j->selected()) ++num_selected; + else ++num_unselected; + } + if (num_selected < 2) continue; + if (num_unselected == 0) { + // if all nodes in a subpath are selected, the operation doesn't make much sense + continue; + } + + // Start from unselected node in closed paths, so that we don't start in the middle + // of a contiguous selection + NodeList::iterator sel_beg = sp->begin(), sel_end; + if (sp->closed()) { + while (sel_beg->selected()) ++sel_beg; + } + + // Main loop + while (num_selected > 0) { + // Find selected node + while (sel_beg && !sel_beg->selected()) sel_beg = sel_beg.next(); + if (!sel_beg) throw std::logic_error("Join nodes: end of open path reached, " + "but there are still nodes to process!"); + + unsigned num_points = 0; + bool use_pos = false; + Geom::Point back_pos, front_pos; + back_pos = *sel_beg->back(); + + for (sel_end = sel_beg; sel_end && sel_end->selected(); sel_end = sel_end.next()) { + ++num_points; + front_pos = *sel_end->front(); + if (pos_valid && sel_end == preserve_pos) use_pos = true; + } + if (num_points > 1) { + Geom::Point joined_pos; + if (use_pos) { + joined_pos = preserve_pos->position(); + pos_valid = false; + } else { + joined_pos = Geom::middle_point(back_pos, front_pos); + } + sel_beg->setType(NODE_CUSP, false); + sel_beg->move(joined_pos); + // do not move handles if they aren't degenerate + if (!sel_beg->back()->isDegenerate()) { + sel_beg->back()->setPosition(back_pos); + } + if (!sel_end.prev()->front()->isDegenerate()) { + sel_beg->front()->setPosition(front_pos); + } + sel_beg = sel_beg.next(); + while (sel_beg != sel_end) { + NodeList::iterator next = sel_beg.next(); + sp->erase(sel_beg); + sel_beg = next; + --num_selected; + } + } + --num_selected; // for the joined node or single selected node + } + } +} + +/** Remove nodes in the middle of selected segments. */ +void PathManipulator::weldSegments() +{ + // TODO +} + +/** Break the subpath at selected nodes. It also works for single node closed paths. */ +void PathManipulator::breakNodes() +{ + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + SubpathPtr sp = *i; + NodeList::iterator cur = sp->begin(), end = sp->end(); + if (!sp->closed()) { + // Each open path must have at least two nodes so no checks are required. + // For 2-node open paths, cur == end + ++cur; + --end; + } + for (; cur != end; ++cur) { + if (!cur->selected()) continue; + SubpathPtr ins; + bool becomes_open = false; + + if (sp->closed()) { + // Move the node to break at to the beginning of path + if (cur != sp->begin()) + sp->splice(sp->begin(), *sp, cur, sp->end()); + sp->setClosed(false); + ins = sp; + becomes_open = true; + } else { + SubpathPtr new_sp(new NodeList(_subpaths)); + new_sp->splice(new_sp->end(), *sp, sp->begin(), cur); + _subpaths.insert(i, new_sp); + ins = new_sp; + } + + Node *n = new Node(_path_data.node_data, cur->position()); + ins->insert(ins->end(), n); + cur->setType(NODE_CUSP, false); + n->back()->setRelativePos(cur->back()->relativePos()); + cur->back()->retract(); + n->sink(); + + if (becomes_open) { + cur = sp->begin(); // this will be increased to ++sp->begin() + end = --sp->end(); + } + } + } +} + +/** Delete selected nodes in the path, optionally substituting deleted segments with bezier curves + * in a way that attempts to preserve the original shape of the curve. */ +void PathManipulator::deleteNodes(bool keep_shape) +{ + if (!_num_selected) return; + + unsigned const samples_per_segment = 10; + double const t_step = 1.0 / samples_per_segment; + + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end();) { + SubpathPtr sp = *i; + + // If there are less than 2 unselected nodes in an open subpath or no unselected nodes + // in a closed one, delete entire subpath. + unsigned num_unselected = 0, num_selected = 0; + for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) { + if (j->selected()) ++num_selected; + else ++num_unselected; + } + if (num_selected == 0) continue; + if (sp->closed() ? (num_unselected < 1) : (num_unselected < 2)) { + _subpaths.erase(i++); + continue; + } + + // In closed paths, start from an unselected node - otherwise we might start in the middle + // of a selected stretch and the resulting bezier fit would be suboptimal + NodeList::iterator sel_beg = sp->begin(), sel_end; + if (sp->closed()) { + while (sel_beg->selected()) ++sel_beg; + } + sel_end = sel_beg; + + while (num_selected > 0) { + while (!sel_beg->selected()) sel_beg = sel_beg.next(); + sel_end = sel_beg; + unsigned del_len = 0; + while (sel_end && sel_end->selected()) { + ++del_len; + sel_end = sel_end.next(); + } + + // set surrounding node types to cusp if: + // 1. keep_shape is on, or + // 2. we are deleting at the end or beginning of an open path + // if !sel_end then sel_beg.prev() must be valid, otherwise the entire subpath + // would be deleted before we get here + if (keep_shape || !sel_end) sel_beg.prev()->setType(NODE_CUSP, false); + if (keep_shape || !sel_beg.prev()) sel_end->setType(NODE_CUSP, false); + + if (keep_shape && sel_beg.prev() && sel_end) { + // Fill fit data + unsigned num_samples = (del_len + 1) * samples_per_segment + 1; + Geom::Point *bezier_data = new Geom::Point[num_samples]; + Geom::Point result[4]; + unsigned seg = 0; + + for (NodeList::iterator cur = sel_beg.prev(); cur != sel_end; cur = cur.next()) { + Geom::CubicBezier bc(*cur, *cur->front(), *cur.next(), *cur.next()->back()); + for (unsigned s = 0; s < samples_per_segment; ++s) { + bezier_data[seg * samples_per_segment + s] = bc.pointAt(t_step * s); + } + ++seg; + } + // Fill last point + bezier_data[num_samples - 1] = sel_end->position(); + // Compute replacement bezier curve + // TODO find out optimal error value + bezier_fit_cubic(result, bezier_data, num_samples, 0.5); + delete[] bezier_data; + + sel_beg.prev()->front()->setPosition(result[1]); + sel_end->back()->setPosition(result[2]); + } + // We cannot simply use sp->erase(sel_beg, sel_end), because it would break + // for cases when the selected stretch crosses the beginning of the path + while (sel_beg != sel_end) { + NodeList::iterator next = sel_beg.next(); + sp->erase(sel_beg); + sel_beg = next; + } + num_selected -= del_len; + } + ++i; + } +} + +/** Removes selected segments */ +void PathManipulator::deleteSegments() +{ + if (_num_selected == 0) return; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end();) { + SubpathPtr sp = *i; + bool has_unselected = false; + unsigned num_selected = 0; + for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) { + if (j->selected()) { + ++num_selected; + } else { + has_unselected = true; + } + } + if (!has_unselected) { + _subpaths.erase(i++); + continue; + } + + NodeList::iterator sel_beg = sp->begin(); + if (sp->closed()) { + while (sel_beg && sel_beg->selected()) ++sel_beg; + } + while (num_selected > 0) { + if (!sel_beg->selected()) { + sel_beg = sel_beg.next(); + continue; + } + NodeList::iterator sel_end = sel_beg; + unsigned num_points = 0; + while (sel_end && sel_end->selected()) { + sel_end = sel_end.next(); + ++num_points; + } + if (num_points >= 2) { + // Retract end handles + sel_end.prev()->setType(NODE_CUSP, false); + sel_end.prev()->back()->retract(); + sel_beg->setType(NODE_CUSP, false); + sel_beg->front()->retract(); + if (sp->closed()) { + // In closed paths, relocate the beginning of the path to the last selected + // node and then unclose it. Remove the nodes from the first selected node + // to the new end of path. + if (sel_end.prev() != sp->begin()) + sp->splice(sp->begin(), *sp, sel_end.prev(), sp->end()); + sp->setClosed(false); + sp->erase(sel_beg.next(), sp->end()); + } else { + // for open paths: + // 1. At end or beginning, delete including the node on the end or beginning + // 2. In the middle, delete only inner nodes + if (sel_beg == sp->begin()) { + sp->erase(sp->begin(), sel_end.prev()); + } else if (sel_end == sp->end()) { + sp->erase(sel_beg.next(), sp->end()); + } else { + SubpathPtr new_sp(new NodeList(_subpaths)); + new_sp->splice(new_sp->end(), *sp, sp->begin(), sel_beg.next()); + _subpaths.insert(i, new_sp); + if (sel_end.prev()) + sp->erase(sp->begin(), sel_end.prev()); + } + } + } + sel_beg = sel_end; + num_selected -= num_points; + } + ++i; + } +} + +/** Reverse the subpaths that have anything selected. */ +void PathManipulator::reverseSubpaths() +{ + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (j->selected()) { + (*i)->reverse(); + break; // continue with the next subpath + } + } + } +} + +/** Make selected segments curves / lines. */ +void PathManipulator::setSegmentType(SegmentType type) +{ + if (!_num_selected) return; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + NodeList::iterator k = j.next(); + if (!(k && j->selected() && k->selected())) continue; + switch (type) { + case SEGMENT_STRAIGHT: + if (j->front()->isDegenerate() && k->back()->isDegenerate()) + break; + j->front()->move(*j); + k->back()->move(*k); + break; + case SEGMENT_CUBIC_BEZIER: + if (!j->front()->isDegenerate() || !k->back()->isDegenerate()) + break; + j->front()->move(j->position() + (k->position() - j->position()) / 3); + k->back()->move(k->position() + (j->position() - k->position()) / 3); + break; + } + } + } +} + +/** Set the visibility of handles. */ +void PathManipulator::showHandles(bool show) +{ + if (show == _show_handles) return; + if (show) { + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (!j->selected()) continue; + j->showHandles(true); + if (j.prev()) j.prev()->showHandles(true); + if (j.next()) j.next()->showHandles(true); + } + } + } else { + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + j->showHandles(false); + } + } + } + _show_handles = show; +} + +/** Set the visibility of outline. */ +void PathManipulator::showOutline(bool show) +{ + if (show == _show_outline) return; + _show_outline = show; + _updateOutline(); +} + +void PathManipulator::showPathDirection(bool show) +{ + if (show == _show_path_direction) return; + _show_path_direction = show; + _updateOutline(); +} + +/** Insert a node in the segment beginning with the supplied iterator, + * at the given time value */ +NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, double t) +{ + if (!first) throw std::invalid_argument("Subdivide after invalid iterator"); + NodeList &list = NodeList::get(first); + NodeList::iterator second = first.next(); + if (!second) throw std::invalid_argument("Subdivide after last node in open path"); + + // We need to insert the segment after 'first'. We can't simply use 'second' + // as the point of insertion, because when 'first' is the last node of closed path, + // the new node will be inserted as the first node instead. + NodeList::iterator insert_at = first; + ++insert_at; + + NodeList::iterator inserted; + if (first->front()->isDegenerate() && second->back()->isDegenerate()) { + // for a line segment, insert a cusp node + Node *n = new Node(_path_data.node_data, + Geom::lerp(t, first->position(), second->position())); + n->setType(NODE_CUSP, false); + inserted = list.insert(insert_at, n); + } else { + // build bezier curve and subdivide + Geom::CubicBezier temp(first->position(), first->front()->position(), + second->back()->position(), second->position()); + std::pair div = temp.subdivide(t); + std::vector seg1 = div.first.points(), seg2 = div.second.points(); + + // set new handle positions + Node *n = new Node(_path_data.node_data, seg2[0]); + n->back()->setPosition(seg1[2]); + n->front()->setPosition(seg2[1]); + n->setType(NODE_SMOOTH, false); + inserted = list.insert(insert_at, n); + + first->front()->move(seg1[1]); + second->back()->move(seg2[2]); + } + return inserted; +} + +/** Called by the XML observer when something else than us modifies the path. */ +void PathManipulator::_externalChange(unsigned type) +{ + switch (type) { + case PATH_CHANGE_D: { + _spcurve->unref(); + _spcurve = sp_path_get_curve_for_edit(_path); + + // ugly: stored offsets of selected nodes in a vector + // vector should be specialized so that it takes only 1 bit per value + std::vector selpos; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + selpos.push_back(j->selected()); + } + } + unsigned size = selpos.size(), curpos = 0; + + _createControlPointsFromGeometry(); + + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (curpos >= size) goto end_restore; + if (selpos[curpos]) _selection.insert(j.ptr()); + ++curpos; + } + } + end_restore: + + _updateOutline(); + } break; + case PATH_CHANGE_TRANSFORM: { + Geom::Matrix i2d_change = _d2i_transform; + _i2d_transform = sp_item_i2d_affine(SP_ITEM(_path)); + _d2i_transform = _i2d_transform.inverse(); + i2d_change *= _i2d_transform; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + j->transform(i2d_change); + } + } + _updateOutline(); + } break; + default: break; + } +} + +/** Create nodes and handles based on the XML of the edited path. */ +void PathManipulator::_createControlPointsFromGeometry() +{ + clear(); + + // sanitize pathvector and store it in SPCurve, + // so that _updateDragPoint doesn't crash on paths with naked movetos + Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers(_spcurve->get_pathvector()); + for (Geom::PathVector::iterator i = pathv.begin(); i != pathv.end(); ) { + if (i->empty()) pathv.erase(i++); + else ++i; + } + _spcurve->set_pathvector(pathv); + + pathv *= (_edit_transform * _i2d_transform); + + // in this loop, we know that there are no zero-segment subpaths + for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) { + // prepare new subpath + SubpathPtr subpath(new NodeList(_subpaths)); + _subpaths.push_back(subpath); + + Node *previous_node = new Node(_path_data.node_data, pit->initialPoint()); + subpath->push_back(previous_node); + Geom::Curve const &cseg = pit->back_closed(); + bool fuse_ends = pit->closed() + && Geom::are_near(cseg.initialPoint(), cseg.finalPoint()); + + for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_open(); ++cit) { + Geom::Point pos = cit->finalPoint(); + Node *current_node; + // if the closing segment is degenerate and the path is closed, we need to move + // the handle of the first node instead of creating a new one + if (fuse_ends && cit == --(pit->end_open())) { + current_node = subpath->begin().get_pointer(); + } else { + /* regardless of segment type, create a new node at the end + * of this segment (unless this is the last segment of a closed path + * with a degenerate closing segment */ + current_node = new Node(_path_data.node_data, pos); + subpath->push_back(current_node); + } + // if this is a bezier segment, move handles appropriately + if (Geom::CubicBezier const *cubic_bezier = + dynamic_cast(&*cit)) + { + std::vector points = cubic_bezier->points(); + + previous_node->front()->setPosition(points[1]); + current_node ->back() ->setPosition(points[2]); + } + previous_node = current_node; + } + // If the path is closed, make the list cyclic + if (pit->closed()) subpath->setClosed(true); + } + + // we need to set the nodetypes after all the handles are in place, + // so that pickBestType works correctly + // TODO maybe migrate to inkscape:node-types? + gchar const *nts_raw = _path ? _path->repr->attribute("sodipodi:nodetypes") : 0; + std::string nodetype_string = nts_raw ? nts_raw : ""; + /* Calculate the needed length of the nodetype string. + * For closed paths, the entry is duplicated for the starting node, + * so we can just use the count of segments including the closing one + * to include the extra end node. */ + std::string::size_type nodetype_len = 0; + for (Geom::PathVector::const_iterator i = pathv.begin(); i != pathv.end(); ++i) { + if (i->empty()) continue; + nodetype_len += i->size_closed(); + } + /* pad the string to required length with a bogus value. + * 'b' and any other letter not recognized by the parser causes the best fit to be set + * as the node type */ + if (nodetype_len > nodetype_string.size()) { + nodetype_string.append(nodetype_len - nodetype_string.size(), 'b'); + } + std::string::iterator tsi = nodetype_string.begin(); + for (std::list::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + j->setType(Node::parse_nodetype(*tsi++), false); + } + if ((*i)->closed()) { + // STUPIDITY ALERT: it seems we need to use the duplicate type symbol instead of + // the first one to remain backward compatible. + (*i)->begin()->setType(Node::parse_nodetype(*tsi++), false); + } + } +} + +/** Construct the geometric representation of nodes and handles, update the outline + * and display */ +void PathManipulator::_createGeometryFromControlPoints() +{ + Geom::PathBuilder builder; + for (std::list::iterator spi = _subpaths.begin(); spi != _subpaths.end(); ) { + SubpathPtr subpath = *spi; + if (subpath->empty()) { + _subpaths.erase(spi++); + continue; + } + NodeList::iterator prev = subpath->begin(); + builder.moveTo(prev->position()); + + for (NodeList::iterator i = ++subpath->begin(); i != subpath->end(); ++i) { + build_segment(builder, prev.ptr(), i.ptr()); + prev = i; + } + if (subpath->closed()) { + // Here we link the last and first node if the path is closed. + // If the last segment is Bezier, we add it. + if (!prev->front()->isDegenerate() || !subpath->begin()->back()->isDegenerate()) { + build_segment(builder, prev.ptr(), subpath->begin().ptr()); + } + // if that segment is linear, we just call closePath(). + builder.closePath(); + } + ++spi; + } + builder.finish(); + _spcurve->set_pathvector(builder.peek() * (_edit_transform * _i2d_transform).inverse()); + _updateOutline(); + if (!empty()) sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false); +} + +/** Build one segment of the geometric representation. + * @relates PathManipulator */ +void build_segment(Geom::PathBuilder &builder, Node *prev_node, Node *cur_node) +{ + if (cur_node->back()->isDegenerate() && prev_node->front()->isDegenerate()) + { + // NOTE: It seems like the renderer cannot correctly handle vline / hline segments, + // and trying to display a path using them results in funny artifacts. + builder.lineTo(cur_node->position()); + } else { + // this is a bezier segment + builder.curveTo( + prev_node->front()->position(), + cur_node->back()->position(), + cur_node->position()); + } +} + +/** Construct a node type string to store in the sodipodi:nodetypes attribute. */ +std::string PathManipulator::_createTypeString() +{ + // precondition: no single-node subpaths + std::stringstream tstr; + for (std::list::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + tstr << j->type(); + } + // nodestring format peculiarity: first node is counted twice for closed paths + if ((*i)->closed()) tstr << (*i)->begin()->type(); + } + return tstr.str(); +} + +/** Update the path outline. */ +void PathManipulator::_updateOutline() +{ + if (!_show_outline) { + sp_canvas_item_hide(_outline); + return; + } + + Geom::PathVector pv = _spcurve->get_pathvector(); + pv *= (_edit_transform * _i2d_transform); + // This SPCurve thing has to be killed with extreme prejudice + SPCurve *_hc = new SPCurve(); + if (_show_path_direction) { + // To show the direction, we append additional subpaths which consist of a single + // linear segment that starts at the time value of 0.5 and extends for 10 pixels + // at an angle 150 degrees from the unit tangent. This creates the appearance + // of little 'harpoons' that show the direction of the subpaths. + Geom::PathVector arrows; + for (Geom::PathVector::iterator i = pv.begin(); i != pv.end(); ++i) { + Geom::Path &path = *i; + for (Geom::Path::const_iterator j = path.begin(); j != path.end_default(); ++j) { + Geom::Point at = j->pointAt(0.5); + Geom::Point ut = j->unitTangentAt(0.5); + // rotate the point + ut *= Geom::Rotate(150.0 / 180.0 * M_PI); + Geom::Point arrow_end = _desktop->w2d( + _desktop->d2w(at) + Geom::unit_vector(_desktop->d2w(ut)) * 10.0); + + Geom::Path arrow(at); + arrow.appendNew(arrow_end); + arrows.push_back(arrow); + } + } + pv.insert(pv.end(), arrows.begin(), arrows.end()); + } + _hc->set_pathvector(pv); + sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(_outline), _hc); + sp_canvas_item_show(_outline); + _hc->unref(); +} + +void PathManipulator::_attachNodeHandlers(Node *node) +{ + Handle *handles[2] = { node->front(), node->back() }; + for (int i = 0; i < 2; ++i) { + handles[i]->signal_update.connect( + sigc::mem_fun(*this, &PathManipulator::update)); + handles[i]->signal_ungrabbed.connect( + sigc::hide( + sigc::mem_fun(*this, &PathManipulator::_handleUngrabbed))); + handles[i]->signal_grabbed.connect( + sigc::bind_return( + sigc::hide( + sigc::mem_fun(*this, &PathManipulator::_handleGrabbed)), + false)); + handles[i]->signal_clicked.connect( + sigc::bind<0>( + sigc::mem_fun(*this, &PathManipulator::_handleClicked), + handles[i])); + } + node->signal_clicked.connect( + sigc::bind<0>( + sigc::mem_fun(*this, &PathManipulator::_nodeClicked), + node)); +} +void PathManipulator::_removeNodeHandlers(Node *node) +{ + // It is safe to assume that nobody else connected to handles' signals after us, + // so we pop our slots from the back. This preserves existing connections + // created by Node and Handle constructors. + Handle *handles[2] = { node->front(), node->back() }; + for (int i = 0; i < 2; ++i) { + handles[i]->signal_update.slots().pop_back(); + handles[i]->signal_grabbed.slots().pop_back(); + handles[i]->signal_ungrabbed.slots().pop_back(); + handles[i]->signal_clicked.slots().pop_back(); + } + // Same for this one: CPS only connects to grab, drag, and ungrab + node->signal_clicked.slots().pop_back(); +} + +bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event) +{ + // cycle between node types on ctrl+click + if (event->button != 1 || !held_control(*event)) return false; + if (n->isEndNode()) { + if (n->type() == NODE_CUSP) { + n->setType(NODE_SMOOTH); + } else { + n->setType(NODE_CUSP); + } + } else { + n->setType(static_cast((n->type() + 1) % NODE_LAST_REAL_TYPE)); + } + update(); + _commit(_("Cycle node type")); + return true; +} + +void PathManipulator::_handleGrabbed() +{ + _selection.hideTransformHandles(); +} + +void PathManipulator::_handleUngrabbed() +{ + _selection.restoreTransformHandles(); + _commit(_("Drag handle")); +} + +bool PathManipulator::_handleClicked(Handle *h, GdkEventButton *event) +{ + // retracting by Ctrl+click + if (event->button == 1 && held_control(*event)) { + h->move(h->parent()->position()); + update(); + _commit(_("Retract handle")); + return true; + } + return false; +} + +void PathManipulator::_selectionChanged(SelectableControlPoint *p, bool selected) +{ + // don't do anything if we do not show handles + if (!_show_handles) return; + + // only do something if a node changed selection state + Node *node = dynamic_cast(p); + if (!node) return; + + // update handle display + NodeList::iterator iters[5]; + iters[2] = NodeList::get_iterator(node); + iters[1] = iters[2].prev(); + iters[3] = iters[2].next(); + if (selected) { + // selection - show handles on this node and adjacent ones + node->showHandles(true); + if (iters[1]) iters[1]->showHandles(true); + if (iters[3]) iters[3]->showHandles(true); + } else { + /* Deselection is more complex. + * The change might affect 3 nodes - this one and two adjacent. + * If the node and both its neighbors are deselected, hide handles. + * Otherwise, leave as is. */ + if (iters[1]) iters[0] = iters[1].prev(); + if (iters[3]) iters[4] = iters[3].next(); + bool nodesel[5]; + for (int i = 0; i < 5; ++i) { + nodesel[i] = iters[i] && iters[i]->selected(); + } + for (int i = 1; i < 4; ++i) { + if (iters[i] && !nodesel[i-1] && !nodesel[i] && !nodesel[i+1]) { + iters[i]->showHandles(false); + } + } + } + + if (selected) ++_num_selected; + else --_num_selected; +} + +/** Removes all nodes belonging to this manipulator from the control pont selection */ +void PathManipulator::_removeNodesFromSelection() +{ + // remove this manipulator's nodes from selection + for (std::list::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + _selection.erase(j.get_pointer()); + } + } +} + +/** Update the XML representation and put the specified annotation on the undo stack */ +void PathManipulator::_commit(Glib::ustring const &annotation) +{ + writeXML(); + sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, annotation.data()); +} + +/** Update the position of the curve drag point such that it is over the nearest + * point of the path. */ +void PathManipulator::_updateDragPoint(Geom::Point const &evp) +{ + // TODO find a way to make this faster (no transform required) + Geom::PathVector pv = _spcurve->get_pathvector() * (_edit_transform * _i2d_transform); + boost::optional pvp + = Geom::nearestPoint(pv, _desktop->w2d(evp)); + if (!pvp) return; + Geom::Point nearest_point = _desktop->d2w(pv.at(pvp->path_nr).pointAt(pvp->t)); + + double fracpart; + std::list::iterator spi = _subpaths.begin(); + for (unsigned i = 0; i < pvp->path_nr; ++i, ++spi) {} + NodeList::iterator first = (*spi)->before(pvp->t, &fracpart); + + double stroke_tolerance = _getStrokeTolerance(); + if (Geom::distance(evp, nearest_point) < stroke_tolerance) { + _dragpoint->setVisible(true); + _dragpoint->setPosition(_desktop->w2d(nearest_point)); + _dragpoint->setSize(2 * stroke_tolerance); + _dragpoint->setTimeValue(fracpart); + _dragpoint->setIterator(first); + } else { + _dragpoint->setVisible(false); + } +} + +/// This is called on zoom change to update the direction arrows +void PathManipulator::_updateOutlineOnZoomChange() +{ + if (_show_path_direction) _updateOutline(); +} + +/** Compute the radius from the edge of the path where clicks chould initiate a curve drag + * or segment selection, in window coordinates. */ +double PathManipulator::_getStrokeTolerance() +{ + /* Stroke event tolerance is equal to half the stroke's width plus the global + * drag tolerance setting. */ + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + double ret = prefs->getIntLimited("/options/dragtolerance/value", 2, 0, 100); + if (_path && !SP_OBJECT_STYLE(_path)->stroke.isNone()) { + ret += SP_OBJECT_STYLE(_path)->stroke_width.computed * 0.5 + * (_edit_transform * _i2d_transform).descrim() // scale to desktop coords + * _desktop->current_zoom(); // == _d2w.descrim() - scale to window coords + } + return ret; +} + +} // namespace UI +} // namespace Inkscape + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 : -- cgit v1.2.3 From a79eab7e518e7c1b3540075552ecb3e7aa62b0df Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sat, 5 Dec 2009 03:48:07 +0100 Subject: Fix mask editing behavior on undo and outline display for masks/clips; prepare to fix LPE path parameters (bzr r8846.2.2) --- src/ui/tool/path-manipulator.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index ef8572330..e9ec78b2e 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -690,6 +690,18 @@ void PathManipulator::showPathDirection(bool show) _updateOutline(); } +void PathManipulator::setControlsTransform(Geom::Matrix const &tnew) +{ + Geom::Matrix delta = _i2d_transform.inverse() * _edit_transform.inverse() * tnew * _i2d_transform; + _edit_transform = tnew; + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + j->transform(delta); + } + } + _createGeometryFromControlPoints(); +} + /** Insert a node in the segment beginning with the supplied iterator, * at the given time value */ NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, double t) -- cgit v1.2.3 From e2b9f78d271e5fea988138d49020e704e72c83b1 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Tue, 8 Dec 2009 03:21:08 +0100 Subject: Fix LPEs and break mask transform undo (bzr r8846.2.3) --- src/ui/tool/path-manipulator.cpp | 121 +++++++++++++++++++++++++++++++++------ 1 file changed, 102 insertions(+), 19 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index e9ec78b2e..0ad509a9b 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -26,6 +26,9 @@ #include "display/curve.h" #include "display/canvas-bpath.h" #include "document.h" +#include "live_effects/effect.h" +#include "live_effects/lpeobject.h" +#include "live_effects/parameter/path.h" #include "sp-path.h" #include "helper/geom.h" #include "preferences.h" @@ -59,13 +62,15 @@ public: virtual void notifyAttributeChanged(Inkscape::XML::Node &, GQuark attr, Util::ptr_shared, Util::ptr_shared) { - GQuark path_d = g_quark_from_static_string("d"); - GQuark path_transform = g_quark_from_static_string("transform"); // do nothing if blocked if (_blocked) return; + GQuark path_d = g_quark_from_static_string("d"); + GQuark path_transform = g_quark_from_static_string("transform"); + GQuark lpe_quark = _pm->_lpe_key.empty() ? 0 : g_quark_from_string(_pm->_lpe_key.data()); + // only react to "d" (path data) and "transform" attribute changes - if (attr == path_d) { + if (attr == lpe_quark || attr == path_d) { _pm->_externalChange(PATH_CHANGE_D); } else if (attr == path_transform) { _pm->_externalChange(PATH_CHANGE_TRANSFORM); @@ -81,22 +86,29 @@ private: void build_segment(Geom::PathBuilder &, Node *, Node *); PathManipulator::PathManipulator(PathSharedData const &data, SPPath *path, - Geom::Matrix const &et, guint32 outline_color) + Geom::Matrix const &et, guint32 outline_color, Glib::ustring lpe_key) : PointManipulator(data.node_data.desktop, *data.node_data.selection) , _path_data(data) , _path(path) - , _spcurve(sp_path_get_curve_for_edit(path)) + , _spcurve(NULL) , _dragpoint(new CurveDragPoint(*this)) , _observer(new PathManipulatorObserver(this)) , _edit_transform(et) , _show_handles(true) , _show_outline(false) + , _lpe_key(lpe_key) { /* Because curve drag point is always created first, it does not cover nodes */ - _i2d_transform = sp_item_i2d_affine(SP_ITEM(path)); + if (_lpe_key.empty()) { + _i2d_transform = sp_item_i2d_affine(SP_ITEM(path)); + } else { + _i2d_transform = Geom::identity(); + } _d2i_transform = _i2d_transform.inverse(); _dragpoint->setVisible(false); + _getGeometry(); + _outline = sp_canvas_bpath_new(_path_data.outline_group, NULL); sp_canvas_item_hide(_outline); sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(_outline), outline_color, 1.0, @@ -127,7 +139,7 @@ PathManipulator::~PathManipulator() if (_path) _path->repr->removeObserver(*_observer); delete _observer; gtk_object_destroy(_outline); - _spcurve->unref(); + if (_spcurve) _spcurve->unref(); clear(); } @@ -163,11 +175,11 @@ void PathManipulator::writeXML() if (!_path) return; _observer->block(); if (!empty()) { - _path->updateRepr(); - _path->repr->setAttribute("sodipodi:nodetypes", _createTypeString().data()); + SP_OBJECT(_path)->updateRepr(); + _getXMLNode()->setAttribute(_nodetypesKey().data(), _createTypeString().data()); } else { // this manipulator will have to be destroyed right after this call - _path->repr->removeObserver(*_observer); + _getXMLNode()->removeObserver(*_observer); sp_object_ref(_path); _path->deleteObject(true, true); sp_object_unref(_path); @@ -333,6 +345,9 @@ void PathManipulator::insertNodes() /** Replace contiguous selections of nodes in each subpath with one node. */ void PathManipulator::weldNodes(NodeList::iterator const &preserve_pos) { + if (!_num_selected) return; + _dragpoint->setVisible(false); + bool pos_valid = preserve_pos; for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { SubpathPtr sp = *i; @@ -458,7 +473,8 @@ void PathManipulator::breakNodes() void PathManipulator::deleteNodes(bool keep_shape) { if (!_num_selected) return; - + hideDragPoint(); + unsigned const samples_per_segment = 10; double const t_step = 1.0 / samples_per_segment; @@ -472,7 +488,10 @@ void PathManipulator::deleteNodes(bool keep_shape) if (j->selected()) ++num_selected; else ++num_unselected; } - if (num_selected == 0) continue; + if (num_selected == 0) { + ++i; + continue; + } if (sp->closed() ? (num_unselected < 1) : (num_unselected < 2)) { _subpaths.erase(i++); continue; @@ -500,8 +519,8 @@ void PathManipulator::deleteNodes(bool keep_shape) // 2. we are deleting at the end or beginning of an open path // if !sel_end then sel_beg.prev() must be valid, otherwise the entire subpath // would be deleted before we get here - if (keep_shape || !sel_end) sel_beg.prev()->setType(NODE_CUSP, false); - if (keep_shape || !sel_beg.prev()) sel_end->setType(NODE_CUSP, false); + if ((keep_shape || !sel_end) && sel_beg.prev()) sel_beg.prev()->setType(NODE_CUSP, false); + if ((keep_shape || !sel_beg.prev()) && sel_end) sel_end->setType(NODE_CUSP, false); if (keep_shape && sel_beg.prev() && sel_end) { // Fill fit data @@ -520,7 +539,7 @@ void PathManipulator::deleteNodes(bool keep_shape) // Fill last point bezier_data[num_samples - 1] = sel_end->position(); // Compute replacement bezier curve - // TODO find out optimal error value + // TODO the fitting algorithm sucks - rewrite it to be awesome bezier_fit_cubic(result, bezier_data, num_samples, 0.5); delete[] bezier_data; @@ -544,6 +563,8 @@ void PathManipulator::deleteNodes(bool keep_shape) void PathManipulator::deleteSegments() { if (_num_selected == 0) return; + hideDragPoint(); + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end();) { SubpathPtr sp = *i; bool has_unselected = false; @@ -702,6 +723,12 @@ void PathManipulator::setControlsTransform(Geom::Matrix const &tnew) _createGeometryFromControlPoints(); } +void PathManipulator::hideDragPoint() +{ + _dragpoint->setVisible(false); + _dragpoint->setIterator(NodeList::iterator()); +} + /** Insert a node in the segment beginning with the supplied iterator, * at the given time value */ NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, double t) @@ -749,8 +776,7 @@ void PathManipulator::_externalChange(unsigned type) { switch (type) { case PATH_CHANGE_D: { - _spcurve->unref(); - _spcurve = sp_path_get_curve_for_edit(_path); + _getGeometry(); // ugly: stored offsets of selected nodes in a vector // vector should be specialized so that it takes only 1 bit per value @@ -851,7 +877,7 @@ void PathManipulator::_createControlPointsFromGeometry() // we need to set the nodetypes after all the handles are in place, // so that pickBestType works correctly // TODO maybe migrate to inkscape:node-types? - gchar const *nts_raw = _path ? _path->repr->attribute("sodipodi:nodetypes") : 0; + gchar const *nts_raw = _path ? _path->repr->attribute(_nodetypesKey().data()) : 0; std::string nodetype_string = nts_raw ? nts_raw : ""; /* Calculate the needed length of the nodetype string. * For closed paths, the entry is duplicated for the starting node, @@ -913,7 +939,7 @@ void PathManipulator::_createGeometryFromControlPoints() builder.finish(); _spcurve->set_pathvector(builder.peek() * (_edit_transform * _i2d_transform).inverse()); _updateOutline(); - if (!empty()) sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false); + _setGeometry(); } /** Build one segment of the geometric representation. @@ -990,6 +1016,63 @@ void PathManipulator::_updateOutline() _hc->unref(); } +/** Retrieve the geometry of the edited object from the object tree */ +void PathManipulator::_getGeometry() +{ + using namespace Inkscape::LivePathEffect; + if (!_lpe_key.empty()) { + Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe(); + if (lpe) { + PathParam *pathparam = dynamic_cast(lpe->getParameter(_lpe_key.data())); + if (!_spcurve) + _spcurve = new SPCurve(pathparam->get_pathvector()); + else + _spcurve->set_pathvector(pathparam->get_pathvector()); + } + } else { + if (_spcurve) _spcurve->unref(); + _spcurve = sp_path_get_curve_for_edit(_path); + } +} + +/** Set the geometry of the edited object in the object tree, but do not commit to XML */ +void PathManipulator::_setGeometry() +{ + using namespace Inkscape::LivePathEffect; + if (empty()) return; + + if (!_lpe_key.empty()) { + // LPE brain damage follows - copied from nodepath.cpp + // NOTE: if we are editing an LPE param, _path is not actually an SPPath, it is + // a LivePathEffectObject. + Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe(); + if (lpe) { + PathParam *pathparam = dynamic_cast(lpe->getParameter(_lpe_key.data())); + pathparam->set_new_value(_spcurve->get_pathvector(), false); + LIVEPATHEFFECT(_path)->requestModified(SP_OBJECT_MODIFIED_FLAG); + } + } else { + if (_path->repr->attribute("inkscape:original-d")) + sp_path_set_original_curve(_path, _spcurve, true, false); + else + sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false); + } +} + +/** LPE brain damage */ +Glib::ustring PathManipulator::_nodetypesKey() +{ + if (_lpe_key.empty()) return "sodipodi:nodetypes"; + return _lpe_key + "-nodetypes"; +} + +/** LPE brain damage */ +Inkscape::XML::Node *PathManipulator::_getXMLNode() +{ + if (_lpe_key.empty()) return _path->repr; + return LIVEPATHEFFECT(_path)->repr; +} + void PathManipulator::_attachNodeHandlers(Node *node) { Handle *handles[2] = { node->front(), node->back() }; -- cgit v1.2.3 From de6736751124f92b2f88a7ac146434edbfad824c Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Wed, 23 Dec 2009 20:51:48 +0100 Subject: Comment cleanup (bzr r8846.2.5) --- src/ui/tool/path-manipulator.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 0ad509a9b..f247e5537 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -1042,9 +1042,9 @@ void PathManipulator::_setGeometry() if (empty()) return; if (!_lpe_key.empty()) { - // LPE brain damage follows - copied from nodepath.cpp + // copied from nodepath.cpp // NOTE: if we are editing an LPE param, _path is not actually an SPPath, it is - // a LivePathEffectObject. + // a LivePathEffectObject. (mad laughter) Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe(); if (lpe) { PathParam *pathparam = dynamic_cast(lpe->getParameter(_lpe_key.data())); @@ -1059,14 +1059,15 @@ void PathManipulator::_setGeometry() } } -/** LPE brain damage */ +/** Figure out in what attribute to store the nodetype string. */ Glib::ustring PathManipulator::_nodetypesKey() { if (_lpe_key.empty()) return "sodipodi:nodetypes"; return _lpe_key + "-nodetypes"; } -/** LPE brain damage */ +/** Return the XML node we are editing. + * This method is wrong but necessary at the moment. */ Inkscape::XML::Node *PathManipulator::_getXMLNode() { if (_lpe_key.empty()) return _path->repr; -- cgit v1.2.3 From 1075267dd1ba150a82b7e1aad543fbf0a69a1c00 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sat, 26 Dec 2009 04:13:01 +0100 Subject: Implement selection spatial grow (bzr r8846.2.7) --- src/ui/tool/path-manipulator.cpp | 58 +++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 10 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index f247e5537..42db45321 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -85,10 +85,11 @@ private: void build_segment(Geom::PathBuilder &, Node *, Node *); -PathManipulator::PathManipulator(PathSharedData const &data, SPPath *path, +PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, Geom::Matrix const &et, guint32 outline_color, Glib::ustring lpe_key) - : PointManipulator(data.node_data.desktop, *data.node_data.selection) - , _path_data(data) + : PointManipulator(mpm._path_data.node_data.desktop, *mpm._path_data.node_data.selection) + , _subpaths(*this) + , _multi_path_manipulator(mpm) , _path(path) , _spcurve(NULL) , _dragpoint(new CurveDragPoint(*this)) @@ -109,7 +110,7 @@ PathManipulator::PathManipulator(PathSharedData const &data, SPPath *path, _getGeometry(); - _outline = sp_canvas_bpath_new(_path_data.outline_group, NULL); + _outline = sp_canvas_bpath_new(_multi_path_manipulator._path_data.outline_group, NULL); sp_canvas_item_hide(_outline); sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(_outline), outline_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT); @@ -297,6 +298,11 @@ void PathManipulator::shiftSelection(int dir) } } +void PathManipulator::linearGrow(NodeList::iterator center, int dir) +{ + g_message("linearGrow unimplemented"); +} + /** Invert selection in the entire path. */ void PathManipulator::invertSelection() { @@ -343,7 +349,7 @@ void PathManipulator::insertNodes() } /** Replace contiguous selections of nodes in each subpath with one node. */ -void PathManipulator::weldNodes(NodeList::iterator const &preserve_pos) +void PathManipulator::weldNodes(NodeList::iterator preserve_pos) { if (!_num_selected) return; _dragpoint->setVisible(false); @@ -453,7 +459,7 @@ void PathManipulator::breakNodes() ins = new_sp; } - Node *n = new Node(_path_data.node_data, cur->position()); + Node *n = new Node(_multi_path_manipulator._path_data.node_data, cur->position()); ins->insert(ins->end(), n); cur->setType(NODE_CUSP, false); n->back()->setRelativePos(cur->back()->relativePos()); @@ -747,7 +753,7 @@ NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, d NodeList::iterator inserted; if (first->front()->isDegenerate() && second->back()->isDegenerate()) { // for a line segment, insert a cusp node - Node *n = new Node(_path_data.node_data, + Node *n = new Node(_multi_path_manipulator._path_data.node_data, Geom::lerp(t, first->position(), second->position())); n->setType(NODE_CUSP, false); inserted = list.insert(insert_at, n); @@ -759,7 +765,7 @@ NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, d std::vector seg1 = div.first.points(), seg2 = div.second.points(); // set new handle positions - Node *n = new Node(_path_data.node_data, seg2[0]); + Node *n = new Node(_multi_path_manipulator._path_data.node_data, seg2[0]); n->back()->setPosition(seg1[2]); n->front()->setPosition(seg2[1]); n->setType(NODE_SMOOTH, false); @@ -771,6 +777,38 @@ NodeList::iterator PathManipulator::subdivideSegment(NodeList::iterator first, d return inserted; } +/** Find the node that is closest/farthest from the origin + * @param origin Point of reference + * @param search_selected Consider selected nodes + * @param search_unselected Consider unselected nodes + * @param closest If true, return closest node, if false, return farthest + * @return The matching node, or an empty iterator if none found + */ +NodeList::iterator PathManipulator::extremeNode(NodeList::iterator origin, bool search_selected, + bool search_unselected, bool closest) +{ + NodeList::iterator match; + double extr_dist = closest ? HUGE_VAL : -HUGE_VAL; + if (_num_selected == 0 && !search_unselected) return match; + + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if(j->selected()) { + if (!search_selected) continue; + } else { + if (!search_unselected) continue; + } + double dist = Geom::distance(*j, *origin); + bool cond = closest ? (dist < extr_dist) : (dist > extr_dist); + if (cond) { + match = j; + extr_dist = dist; + } + } + } + return match; +} + /** Called by the XML observer when something else than us modifies the path. */ void PathManipulator::_externalChange(unsigned type) { @@ -839,7 +877,7 @@ void PathManipulator::_createControlPointsFromGeometry() SubpathPtr subpath(new NodeList(_subpaths)); _subpaths.push_back(subpath); - Node *previous_node = new Node(_path_data.node_data, pit->initialPoint()); + Node *previous_node = new Node(_multi_path_manipulator._path_data.node_data, pit->initialPoint()); subpath->push_back(previous_node); Geom::Curve const &cseg = pit->back_closed(); bool fuse_ends = pit->closed() @@ -856,7 +894,7 @@ void PathManipulator::_createControlPointsFromGeometry() /* regardless of segment type, create a new node at the end * of this segment (unless this is the last segment of a closed path * with a degenerate closing segment */ - current_node = new Node(_path_data.node_data, pos); + current_node = new Node(_multi_path_manipulator._path_data.node_data, pos); subpath->push_back(current_node); } // if this is a bezier segment, move handles appropriately -- cgit v1.2.3 From 6286e1b266d79742170df705ba6a1e6f94ca32d6 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 27 Dec 2009 00:59:01 +0100 Subject: Implement selection linear grow (bzr r8846.2.8) --- src/ui/tool/path-manipulator.cpp | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 42db45321..2755d6fb3 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -298,11 +298,6 @@ void PathManipulator::shiftSelection(int dir) } } -void PathManipulator::linearGrow(NodeList::iterator center, int dir) -{ - g_message("linearGrow unimplemented"); -} - /** Invert selection in the entire path. */ void PathManipulator::invertSelection() { -- cgit v1.2.3 From b52865a71a9f83da9719a3ec5f50a4a2cd7cdace Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 10 Jan 2010 01:46:28 +0100 Subject: * Implement node snapping. * Fix minor bug in linear grow. * Add --fixes. * Move some node selection-related functions to ControlPointSelection. Fixed bugs: - https://launchpad.net/bugs/170561 - https://launchpad.net/bugs/171893 - https://launchpad.net/bugs/182585 - https://launchpad.net/bugs/446773 (bzr r8846.2.9) --- src/ui/tool/path-manipulator.cpp | 48 +--------------------------------------- 1 file changed, 1 insertion(+), 47 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 2755d6fb3..cfa3846f8 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -214,42 +214,6 @@ void PathManipulator::selectSubpaths() } } -/** Select all nodes in the path. */ -void PathManipulator::selectAll() -{ - for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { - for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { - _selection.insert(j.ptr()); - } - } -} - -/** Select points inside the given rectangle. If all points inside it are already selected, - * they will be deselected. - * @param area Area to select - */ -void PathManipulator::selectArea(Geom::Rect const &area) -{ - bool nothing_selected = true; - std::vector in_area; - for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { - for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { - if (area.contains(j->position())) { - in_area.push_back(j.ptr()); - if (!j->selected()) { - _selection.insert(j.ptr()); - nothing_selected = false; - } - } - } - } - if (nothing_selected) { - for (std::vector::iterator i = in_area.begin(); i != in_area.end(); ++i) { - _selection.erase(*i); - } - } -} - /** Move the selection forward or backward by one node in each subpath, based on the sign * of the parameter. */ void PathManipulator::shiftSelection(int dir) @@ -298,17 +262,6 @@ void PathManipulator::shiftSelection(int dir) } } -/** Invert selection in the entire path. */ -void PathManipulator::invertSelection() -{ - for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { - for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { - if (j->selected()) _selection.erase(j.ptr()); - else _selection.insert(j.ptr()); - } - } -} - /** Invert selection in the selected subpaths. */ void PathManipulator::invertSelectionInSubpaths() { @@ -724,6 +677,7 @@ void PathManipulator::setControlsTransform(Geom::Matrix const &tnew) _createGeometryFromControlPoints(); } +/** Hide the curve drag point until the next motion event. */ void PathManipulator::hideDragPoint() { _dragpoint->setVisible(false); -- cgit v1.2.3 From dd3076a51d8a53223c771a39fa5f976db0c85af5 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 14 Jan 2010 09:42:20 +0100 Subject: Implement segment weld to make segment join similar to node join (bzr r8846.2.12) --- src/ui/tool/path-manipulator.cpp | 58 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index cfa3846f8..9889eb787 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -317,19 +317,21 @@ void PathManipulator::weldNodes(NodeList::iterator preserve_pos) } // Start from unselected node in closed paths, so that we don't start in the middle - // of a contiguous selection + // of a selection NodeList::iterator sel_beg = sp->begin(), sel_end; if (sp->closed()) { while (sel_beg->selected()) ++sel_beg; } - // Main loop + // Work loop while (num_selected > 0) { // Find selected node while (sel_beg && !sel_beg->selected()) sel_beg = sel_beg.next(); if (!sel_beg) throw std::logic_error("Join nodes: end of open path reached, " "but there are still nodes to process!"); + // note: this is initialized to zero, because the loop below counts sel_beg as well + // the loop conditions are simpler that way unsigned num_points = 0; bool use_pos = false; Geom::Point back_pos, front_pos; @@ -373,7 +375,57 @@ void PathManipulator::weldNodes(NodeList::iterator preserve_pos) /** Remove nodes in the middle of selected segments. */ void PathManipulator::weldSegments() { - // TODO + if (!_num_selected) return; + _dragpoint->setVisible(false); + + for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { + SubpathPtr sp = *i; + unsigned num_selected = 0, num_unselected = 0; + for (NodeList::iterator j = sp->begin(); j != sp->end(); ++j) { + if (j->selected()) ++num_selected; + else ++num_unselected; + } + if (num_selected < 3) continue; + if (num_unselected == 0 && sp->closed()) { + // if all nodes in a closed subpath are selected, the operation doesn't make much sense + continue; + } + + // Start from unselected node in closed paths, so that we don't start in the middle + // of a selection + NodeList::iterator sel_beg = sp->begin(), sel_end; + if (sp->closed()) { + while (sel_beg->selected()) ++sel_beg; + } + + // Work loop + while (num_selected > 0) { + // Find selected node + while (sel_beg && !sel_beg->selected()) sel_beg = sel_beg.next(); + if (!sel_beg) throw std::logic_error("Join nodes: end of open path reached, " + "but there are still nodes to process!"); + + // note: this is initialized to zero, because the loop below counts sel_beg as well + // the loop conditions are simpler that way + unsigned num_points = 0; + + // find the end of selected segment + for (sel_end = sel_beg; sel_end && sel_end->selected(); sel_end = sel_end.next()) { + ++num_points; + } + if (num_points > 2) { + // remove nodes in the middle + sel_beg = sel_beg.next(); + while (sel_beg != sel_end.prev()) { + NodeList::iterator next = sel_beg.next(); + sp->erase(sel_beg); + sel_beg = next; + } + sel_beg = sel_end; + } + num_selected -= num_points; + } + } } /** Break the subpath at selected nodes. It also works for single node closed paths. */ -- cgit v1.2.3 From 4756aa99f5756a6cac199c1aae6c37514cf1c562 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 14 Jan 2010 23:38:54 +0100 Subject: Replace std::tr1::unordered_(map|set) with __gnu_cxx::hash_(map|set), to work around broken headers in some GCC versions. (bzr r8980) --- src/ui/tool/path-manipulator.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 9889eb787..9eabd8992 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -1157,17 +1157,24 @@ bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event) { // cycle between node types on ctrl+click if (event->button != 1 || !held_control(*event)) return false; - if (n->isEndNode()) { - if (n->type() == NODE_CUSP) { - n->setType(NODE_SMOOTH); + /*if (held_alt(*event)) { + // TODO delete nodes with Ctrl+Alt+click + n->list()->erase(NodeList::get_iterator(n)); + update(); + _commit(_("Delete node")); + } else*/ { + if (n->isEndNode()) { + if (n->type() == NODE_CUSP) { + n->setType(NODE_SMOOTH); + } else { + n->setType(NODE_CUSP); + } } else { - n->setType(NODE_CUSP); + n->setType(static_cast((n->type() + 1) % NODE_LAST_REAL_TYPE)); } - } else { - n->setType(static_cast((n->type() + 1) % NODE_LAST_REAL_TYPE)); + update(); + _commit(_("Cycle node type")); } - update(); - _commit(_("Cycle node type")); return true; } -- cgit v1.2.3 From 54d1a17856d9f0e79063f84c5c4dc27f71393c0d Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Wed, 20 Jan 2010 16:06:54 +0100 Subject: Select the first node of the path when Tab is pressed and nothing is selected. (bzr r9002) --- src/ui/tool/path-manipulator.cpp | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 9eabd8992..3be332b80 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -95,6 +95,7 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, , _dragpoint(new CurveDragPoint(*this)) , _observer(new PathManipulatorObserver(this)) , _edit_transform(et) + , _num_selected(0) , _show_handles(true) , _show_outline(false) , _lpe_key(lpe_key) @@ -219,6 +220,15 @@ void PathManipulator::selectSubpaths() void PathManipulator::shiftSelection(int dir) { if (dir == 0) return; + if (_num_selected == 0) { + // select the first node of the path. + SubpathList::iterator s = _subpaths.begin(); + if (s == _subpaths.end()) return; + NodeList::iterator n = (*s)->begin(); + if (n != (*s)->end()) + _selection.insert(n.ptr()); + return; + } // We cannot do any tricks here, like iterating in different directions based on // the sign and only setting the selection of nodes behind us, because it would break // for closed paths. @@ -231,7 +241,7 @@ void PathManipulator::shiftSelection(int dir) _selection.erase(j.ptr()); ++num; } - if (num == 0) continue; // should never happen! + if (num == 0) continue; // should never happen! zero-node subpaths are not allowed num = 0; // In closed subpath, shift the selection cyclically. In an open one, @@ -283,7 +293,7 @@ void PathManipulator::invertSelectionInSubpaths() /** Insert a new node in the middle of each selected segment. */ void PathManipulator::insertNodes() { - if (!_num_selected) return; + if (_num_selected < 2) return; for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { @@ -299,8 +309,8 @@ void PathManipulator::insertNodes() /** Replace contiguous selections of nodes in each subpath with one node. */ void PathManipulator::weldNodes(NodeList::iterator preserve_pos) { - if (!_num_selected) return; - _dragpoint->setVisible(false); + if (_num_selected < 2) return; + hideDragPoint(); bool pos_valid = preserve_pos; for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { @@ -375,8 +385,8 @@ void PathManipulator::weldNodes(NodeList::iterator preserve_pos) /** Remove nodes in the middle of selected segments. */ void PathManipulator::weldSegments() { - if (!_num_selected) return; - _dragpoint->setVisible(false); + if (_num_selected < 2) return; + hideDragPoint(); for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { SubpathPtr sp = *i; @@ -478,7 +488,7 @@ void PathManipulator::breakNodes() * in a way that attempts to preserve the original shape of the curve. */ void PathManipulator::deleteNodes(bool keep_shape) { - if (!_num_selected) return; + if (_num_selected == 0) return; hideDragPoint(); unsigned const samples_per_segment = 10; @@ -656,7 +666,7 @@ void PathManipulator::reverseSubpaths() /** Make selected segments curves / lines. */ void PathManipulator::setSegmentType(SegmentType type) { - if (!_num_selected) return; + if (_num_selected == 0) return; for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { NodeList::iterator k = j.next(); @@ -729,7 +739,9 @@ void PathManipulator::setControlsTransform(Geom::Matrix const &tnew) _createGeometryFromControlPoints(); } -/** Hide the curve drag point until the next motion event. */ +/** Hide the curve drag point until the next motion event. + * This should be called at the beginning of every method that can delete nodes. + * Otherwise the invalidated iterator in the dragpoint can cause crashes. */ void PathManipulator::hideDragPoint() { _dragpoint->setVisible(false); -- cgit v1.2.3 From ef88d874ff89882a9222234591b328584a172799 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Wed, 20 Jan 2010 16:31:22 +0100 Subject: Fix path reverse action (Shift+R) in the node tool. (bzr r9003) --- src/ui/tool/path-manipulator.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 3be332b80..e15349e06 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -100,7 +100,6 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, , _show_outline(false) , _lpe_key(lpe_key) { - /* Because curve drag point is always created first, it does not cover nodes */ if (_lpe_key.empty()) { _i2d_transform = sp_item_i2d_affine(SP_ITEM(path)); } else { @@ -651,14 +650,18 @@ void PathManipulator::deleteSegments() } /** Reverse the subpaths that have anything selected. */ -void PathManipulator::reverseSubpaths() +void PathManipulator::reverseSubpaths(bool selected_only) { for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { - for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { - if (j->selected()) { - (*i)->reverse(); - break; // continue with the next subpath + if (selected_only) { + for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) { + if (j->selected()) { + (*i)->reverse(); + break; // continue with the next subpath + } } + } else { + (*i)->reverse(); } } } -- cgit v1.2.3 From 70584617ebd4d01312eb991e2a2946c367c2405c Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Wed, 20 Jan 2010 18:28:49 +0100 Subject: Make Ctrl+Alt+click delete nodes. (bzr r9005) --- src/ui/tool/path-manipulator.cpp | 171 ++++++++++++++++++++++----------------- 1 file changed, 99 insertions(+), 72 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index e15349e06..82f7f9da0 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -118,8 +118,11 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, _subpaths.signal_insert_node.connect( sigc::mem_fun(*this, &PathManipulator::_attachNodeHandlers)); - _subpaths.signal_remove_node.connect( - sigc::mem_fun(*this, &PathManipulator::_removeNodeHandlers)); + // NOTE: signal_remove_node is called just before destruction. Nodes are trackable, + // so removing the signals manually is not necessary. + /*_subpaths.signal_remove_node.connect( + sigc::mem_fun(*this, &PathManipulator::_removeNodeHandlers));*/ + _selection.signal_update.connect( sigc::mem_fun(*this, &PathManipulator::update)); _selection.signal_point_changed.connect( @@ -489,9 +492,6 @@ void PathManipulator::deleteNodes(bool keep_shape) { if (_num_selected == 0) return; hideDragPoint(); - - unsigned const samples_per_segment = 10; - double const t_step = 1.0 / samples_per_segment; for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end();) { SubpathPtr sp = *i; @@ -521,59 +521,81 @@ void PathManipulator::deleteNodes(bool keep_shape) sel_end = sel_beg; while (num_selected > 0) { - while (!sel_beg->selected()) sel_beg = sel_beg.next(); + while (!sel_beg->selected()) { + sel_beg = sel_beg.next(); + } sel_end = sel_beg; - unsigned del_len = 0; + while (sel_end && sel_end->selected()) { - ++del_len; sel_end = sel_end.next(); } - // set surrounding node types to cusp if: - // 1. keep_shape is on, or - // 2. we are deleting at the end or beginning of an open path - // if !sel_end then sel_beg.prev() must be valid, otherwise the entire subpath - // would be deleted before we get here - if ((keep_shape || !sel_end) && sel_beg.prev()) sel_beg.prev()->setType(NODE_CUSP, false); - if ((keep_shape || !sel_beg.prev()) && sel_end) sel_end->setType(NODE_CUSP, false); - - if (keep_shape && sel_beg.prev() && sel_end) { - // Fill fit data - unsigned num_samples = (del_len + 1) * samples_per_segment + 1; - Geom::Point *bezier_data = new Geom::Point[num_samples]; - Geom::Point result[4]; - unsigned seg = 0; - - for (NodeList::iterator cur = sel_beg.prev(); cur != sel_end; cur = cur.next()) { - Geom::CubicBezier bc(*cur, *cur->front(), *cur.next(), *cur.next()->back()); - for (unsigned s = 0; s < samples_per_segment; ++s) { - bezier_data[seg * samples_per_segment + s] = bc.pointAt(t_step * s); - } - ++seg; - } - // Fill last point - bezier_data[num_samples - 1] = sel_end->position(); - // Compute replacement bezier curve - // TODO the fitting algorithm sucks - rewrite it to be awesome - bezier_fit_cubic(result, bezier_data, num_samples, 0.5); - delete[] bezier_data; - - sel_beg.prev()->front()->setPosition(result[1]); - sel_end->back()->setPosition(result[2]); - } - // We cannot simply use sp->erase(sel_beg, sel_end), because it would break - // for cases when the selected stretch crosses the beginning of the path - while (sel_beg != sel_end) { - NodeList::iterator next = sel_beg.next(); - sp->erase(sel_beg); - sel_beg = next; - } - num_selected -= del_len; + num_selected -= _deleteStretch(sel_beg, sel_end, keep_shape); } ++i; } } +/** @brief Delete nodes between the two iterators. + * The given range can cross the beginning of the subpath in closed subpaths. + * @param start Beginning of the range to delete + * @param end End of the range + * @param keep_shape Whether to fit the handles at surrounding nodes to approximate + * the shape before deletion + * @return Number of deleted nodes */ +unsigned PathManipulator::_deleteStretch(NodeList::iterator start, NodeList::iterator end, bool keep_shape) +{ + unsigned const samples_per_segment = 10; + double const t_step = 1.0 / samples_per_segment; + + unsigned del_len = 0; + for (NodeList::iterator i = start; i != end; ++i) { + ++del_len; + } + if (del_len == 0) return 0; + + // set surrounding node types to cusp if: + // 1. keep_shape is on, or + // 2. we are deleting at the end or beginning of an open path + if ((keep_shape || !end) && start.prev()) start.prev()->setType(NODE_CUSP, false); + if ((keep_shape || !start.prev()) && end) end->setType(NODE_CUSP, false); + + if (keep_shape && start.prev() && end) { + unsigned num_samples = (del_len + 1) * samples_per_segment + 1; + Geom::Point *bezier_data = new Geom::Point[num_samples]; + Geom::Point result[4]; + unsigned seg = 0; + + for (NodeList::iterator cur = start.prev(); cur != end; cur = cur.next()) { + Geom::CubicBezier bc(*cur, *cur->front(), *cur.next(), *cur.next()->back()); + for (unsigned s = 0; s < samples_per_segment; ++s) { + bezier_data[seg * samples_per_segment + s] = bc.pointAt(t_step * s); + } + ++seg; + } + // Fill last point + bezier_data[num_samples - 1] = end->position(); + // Compute replacement bezier curve + // TODO the fitting algorithm sucks - rewrite it to be awesome + bezier_fit_cubic(result, bezier_data, num_samples, 0.5); + delete[] bezier_data; + + start.prev()->front()->setPosition(result[1]); + end->back()->setPosition(result[2]); + } + + // We can't use nl->erase(start, end), because it would break when the stretch + // crosses the beginning of a closed subpath + NodeList *nl = start->list(); + while (start != end) { + NodeList::iterator next = start.next(); + nl->erase(start); + start = next; + } + + return del_len; +} + /** Removes selected segments */ void PathManipulator::deleteSegments() { @@ -649,7 +671,9 @@ void PathManipulator::deleteSegments() } } -/** Reverse the subpaths that have anything selected. */ +/** Reverse subpaths of the path. + * @param selected_only If true, only paths that have at least one selected node + * will be reversed. Otherwise all subpaths will be reversed. */ void PathManipulator::reverseSubpaths(bool selected_only) { for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) { @@ -1152,32 +1176,34 @@ void PathManipulator::_attachNodeHandlers(Node *node) sigc::mem_fun(*this, &PathManipulator::_nodeClicked), node)); } -void PathManipulator::_removeNodeHandlers(Node *node) -{ - // It is safe to assume that nobody else connected to handles' signals after us, - // so we pop our slots from the back. This preserves existing connections - // created by Node and Handle constructors. - Handle *handles[2] = { node->front(), node->back() }; - for (int i = 0; i < 2; ++i) { - handles[i]->signal_update.slots().pop_back(); - handles[i]->signal_grabbed.slots().pop_back(); - handles[i]->signal_ungrabbed.slots().pop_back(); - handles[i]->signal_clicked.slots().pop_back(); - } - // Same for this one: CPS only connects to grab, drag, and ungrab - node->signal_clicked.slots().pop_back(); -} bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event) { // cycle between node types on ctrl+click - if (event->button != 1 || !held_control(*event)) return false; - /*if (held_alt(*event)) { - // TODO delete nodes with Ctrl+Alt+click - n->list()->erase(NodeList::get_iterator(n)); - update(); - _commit(_("Delete node")); - } else*/ { + if (event->button != 1) return false; + if (held_alt(*event) && held_control(*event)) { + // Ctrl+Alt+click: delete nodes + hideDragPoint(); + NodeList::iterator iter = NodeList::get_iterator(n); + NodeList *nl = iter->list(); + + if (nl->size() <= 1 || (nl->size() <= 2 && !nl->closed())) { + // Removing last node of closed path - delete it + nl->kill(); + } else { + // In other cases, delete the node under cursor + _deleteStretch(iter, iter.next(), true); + } + + if (!empty()) { + update(); + } + // We need to call MPM's method because it could have been our last node + _multi_path_manipulator._doneWithCleanup(_("Delete node")); + + return true; + } else if (held_control(*event)) { + // Ctrl+click: cycle between node types if (n->isEndNode()) { if (n->type() == NODE_CUSP) { n->setType(NODE_SMOOTH); @@ -1189,8 +1215,9 @@ bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event) } update(); _commit(_("Cycle node type")); + return true; } - return true; + return false; } void PathManipulator::_handleGrabbed() -- cgit v1.2.3 From 944aec996cd9ed6edc63a48d6000691ab8ad07e6 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 24 Jan 2010 19:47:19 +0100 Subject: Fix freezes when deleting nodes. (bzr r9020) --- src/ui/tool/path-manipulator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 82f7f9da0..4b42c16b0 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -549,7 +549,7 @@ unsigned PathManipulator::_deleteStretch(NodeList::iterator start, NodeList::ite double const t_step = 1.0 / samples_per_segment; unsigned del_len = 0; - for (NodeList::iterator i = start; i != end; ++i) { + for (NodeList::iterator i = start; i != end; i = i.next()) { ++del_len; } if (del_len == 0) return 0; -- cgit v1.2.3 From d19e24cd4ba022caef1ca7c05286ee6b59d8328a Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 28 Jan 2010 20:25:14 +0100 Subject: Fix some actions failing when show handles is off. (bzr r9027) --- src/ui/tool/path-manipulator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 4b42c16b0..2d4df86f3 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -1245,6 +1245,9 @@ bool PathManipulator::_handleClicked(Handle *h, GdkEventButton *event) void PathManipulator::_selectionChanged(SelectableControlPoint *p, bool selected) { + if (selected) ++_num_selected; + else --_num_selected; + // don't do anything if we do not show handles if (!_show_handles) return; @@ -1279,9 +1282,6 @@ void PathManipulator::_selectionChanged(SelectableControlPoint *p, bool selected } } } - - if (selected) ++_num_selected; - else --_num_selected; } /** Removes all nodes belonging to this manipulator from the control pont selection */ -- cgit v1.2.3 From bb4d4442a7ec92e15c976689756aa9566cd2430e Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 31 Jan 2010 20:31:21 +0100 Subject: Add pref settings that control updating the display of paths when dragging or transforming nodes them. Fixed bugs: - https://launchpad.net/bugs/380762 (bzr r9038) --- src/ui/tool/path-manipulator.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 2d4df86f3..0ce02aa95 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -98,6 +98,9 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, , _num_selected(0) , _show_handles(true) , _show_outline(false) + , _show_path_direction(false) + , _live_outline(true) + , _live_objects(true) , _lpe_key(lpe_key) { if (_lpe_key.empty()) { @@ -190,6 +193,13 @@ void PathManipulator::writeXML() _path = 0; } _observer->unblock(); + + if (!empty()) { + if (!_live_outline) + _updateOutline(); + if (!_live_objects) + _setGeometry(); + } } /** Remove all nodes from the path. */ @@ -754,6 +764,16 @@ void PathManipulator::showPathDirection(bool show) _updateOutline(); } +void PathManipulator::setLiveOutline(bool set) +{ + _live_outline = set; +} + +void PathManipulator::setLiveObjects(bool set) +{ + _live_objects = set; +} + void PathManipulator::setControlsTransform(Geom::Matrix const &tnew) { Geom::Matrix delta = _i2d_transform.inverse() * _edit_transform.inverse() * tnew * _i2d_transform; @@ -1016,8 +1036,10 @@ void PathManipulator::_createGeometryFromControlPoints() } builder.finish(); _spcurve->set_pathvector(builder.peek() * (_edit_transform * _i2d_transform).inverse()); - _updateOutline(); - _setGeometry(); + if (_live_outline) + _updateOutline(); + if (_live_objects) + _setGeometry(); } /** Build one segment of the geometric representation. -- cgit v1.2.3 From 7ce8847f2410a24a6bce4ca8a43ad7ebdb4839eb Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 4 Feb 2010 03:14:09 +0100 Subject: Reduce libsigc++ usage to partially fix performance regressions in the new node tool. (bzr r9044) --- src/ui/tool/path-manipulator.cpp | 35 ----------------------------------- 1 file changed, 35 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 0ce02aa95..43955edbf 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -119,19 +119,10 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT); sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(_outline), 0, SP_WIND_RULE_NONZERO); - _subpaths.signal_insert_node.connect( - sigc::mem_fun(*this, &PathManipulator::_attachNodeHandlers)); - // NOTE: signal_remove_node is called just before destruction. Nodes are trackable, - // so removing the signals manually is not necessary. - /*_subpaths.signal_remove_node.connect( - sigc::mem_fun(*this, &PathManipulator::_removeNodeHandlers));*/ - _selection.signal_update.connect( sigc::mem_fun(*this, &PathManipulator::update)); _selection.signal_point_changed.connect( sigc::mem_fun(*this, &PathManipulator::_selectionChanged)); - _dragpoint->signal_update.connect( - sigc::mem_fun(*this, &PathManipulator::update)); _desktop->signal_zoom_changed.connect( sigc::hide( sigc::mem_fun(*this, &PathManipulator::_updateOutlineOnZoomChange))); @@ -1174,34 +1165,8 @@ Inkscape::XML::Node *PathManipulator::_getXMLNode() return LIVEPATHEFFECT(_path)->repr; } -void PathManipulator::_attachNodeHandlers(Node *node) -{ - Handle *handles[2] = { node->front(), node->back() }; - for (int i = 0; i < 2; ++i) { - handles[i]->signal_update.connect( - sigc::mem_fun(*this, &PathManipulator::update)); - handles[i]->signal_ungrabbed.connect( - sigc::hide( - sigc::mem_fun(*this, &PathManipulator::_handleUngrabbed))); - handles[i]->signal_grabbed.connect( - sigc::bind_return( - sigc::hide( - sigc::mem_fun(*this, &PathManipulator::_handleGrabbed)), - false)); - handles[i]->signal_clicked.connect( - sigc::bind<0>( - sigc::mem_fun(*this, &PathManipulator::_handleClicked), - handles[i])); - } - node->signal_clicked.connect( - sigc::bind<0>( - sigc::mem_fun(*this, &PathManipulator::_nodeClicked), - node)); -} - bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event) { - // cycle between node types on ctrl+click if (event->button != 1) return false; if (held_alt(*event) && held_control(*event)) { // Ctrl+Alt+click: delete nodes -- cgit v1.2.3 From 9d9e9264afc4e6f83d59bd25ccae505eadb739d8 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sat, 6 Feb 2010 22:45:14 +0100 Subject: Fix performance regressions in the node tool and a stupid crash bug when deleting more than one stretch of selected nodes (bzr r9061) --- src/ui/tool/path-manipulator.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 43955edbf..b1a86dd77 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -522,7 +522,7 @@ void PathManipulator::deleteNodes(bool keep_shape) sel_end = sel_beg; while (num_selected > 0) { - while (!sel_beg->selected()) { + while (sel_beg && !sel_beg->selected()) { sel_beg = sel_beg.next(); } sel_end = sel_beg; @@ -532,6 +532,7 @@ void PathManipulator::deleteNodes(bool keep_shape) } num_selected -= _deleteStretch(sel_beg, sel_end, keep_shape); + sel_beg = sel_end; } ++i; } @@ -1294,11 +1295,12 @@ void PathManipulator::_commit(Glib::ustring const &annotation) void PathManipulator::_updateDragPoint(Geom::Point const &evp) { // TODO find a way to make this faster (no transform required) - Geom::PathVector pv = _spcurve->get_pathvector() * (_edit_transform * _i2d_transform); + Geom::Matrix to_desktop = _edit_transform * _i2d_transform; + Geom::PathVector pv = _spcurve->get_pathvector(); boost::optional pvp - = Geom::nearestPoint(pv, _desktop->w2d(evp)); + = Geom::nearestPoint(pv, _desktop->w2d(evp) * to_desktop.inverse()); if (!pvp) return; - Geom::Point nearest_point = _desktop->d2w(pv.at(pvp->path_nr).pointAt(pvp->t)); + Geom::Point nearest_point = _desktop->d2w(pv.at(pvp->path_nr).pointAt(pvp->t) * to_desktop); double fracpart; std::list::iterator spi = _subpaths.begin(); -- cgit v1.2.3 From d050d7648b9702db0fe55fe188343ff0acf764c9 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Mon, 8 Feb 2010 00:23:09 +0100 Subject: Fix new path update preference. (bzr r9066) --- src/ui/tool/path-manipulator.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index b1a86dd77..3a6b15f37 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -170,6 +170,11 @@ void PathManipulator::update() /** Store the changes to the path in XML. */ void PathManipulator::writeXML() { + if (!_live_outline) + _updateOutline(); + if (!_live_objects) + _setGeometry(); + if (!_path) return; _observer->block(); if (!empty()) { @@ -184,13 +189,6 @@ void PathManipulator::writeXML() _path = 0; } _observer->unblock(); - - if (!empty()) { - if (!_live_outline) - _updateOutline(); - if (!_live_objects) - _setGeometry(); - } } /** Remove all nodes from the path. */ -- cgit v1.2.3 From 81f88ca0856da56bdf426cd065ff0acd3414567f Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Tue, 9 Feb 2010 03:20:18 +0100 Subject: Fix multiple minor problems in the node tool (bzr r9070) --- src/ui/tool/path-manipulator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 3a6b15f37..fd21970ee 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -1331,7 +1331,7 @@ double PathManipulator::_getStrokeTolerance() * drag tolerance setting. */ Inkscape::Preferences *prefs = Inkscape::Preferences::get(); double ret = prefs->getIntLimited("/options/dragtolerance/value", 2, 0, 100); - if (_path && !SP_OBJECT_STYLE(_path)->stroke.isNone()) { + if (_path && SP_OBJECT_STYLE(_path) && !SP_OBJECT_STYLE(_path)->stroke.isNone()) { ret += SP_OBJECT_STYLE(_path)->stroke_width.computed * 0.5 * (_edit_transform * _i2d_transform).descrim() // scale to desktop coords * _desktop->current_zoom(); // == _d2w.descrim() - scale to window coords -- cgit v1.2.3 From b1e63b3bb59ae5accdacf4b1945e7ed208cfceed Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Wed, 10 Feb 2010 16:21:40 +0100 Subject: (Probably) fix a crash in the node tool and fix Ctrl+Alt dragging (bzr r9075) --- src/ui/tool/path-manipulator.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index fd21970ee..d2f90bbca 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -58,8 +58,19 @@ enum PathChange { */ class PathManipulatorObserver : public Inkscape::XML::NodeObserver { public: - PathManipulatorObserver(PathManipulator *p) : _pm(p), _blocked(false) {} - virtual void notifyAttributeChanged(Inkscape::XML::Node &, GQuark attr, + PathManipulatorObserver(PathManipulator *p, Inkscape::XML::Node *node) + : _pm(p) + , _node(node) + , _blocked(false) + { + Inkscape::GC::anchor(_node); + _node->addObserver(*this); + } + ~PathManipulatorObserver() { + _node->removeObserver(*this); + Inkscape::GC::release(_node); + } + virtual void notifyAttributeChanged(Inkscape::XML::Node &node, GQuark attr, Util::ptr_shared, Util::ptr_shared) { // do nothing if blocked @@ -80,6 +91,7 @@ public: void unblock() { _blocked = false; } private: PathManipulator *_pm; + Inkscape::XML::Node *_node; bool _blocked; }; @@ -93,7 +105,7 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, , _path(path) , _spcurve(NULL) , _dragpoint(new CurveDragPoint(*this)) - , _observer(new PathManipulatorObserver(this)) + , _observer(new PathManipulatorObserver(this, SP_OBJECT(path)->repr)) , _edit_transform(et) , _num_selected(0) , _show_handles(true) @@ -127,14 +139,11 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, sigc::hide( sigc::mem_fun(*this, &PathManipulator::_updateOutlineOnZoomChange))); _createControlPointsFromGeometry(); - - _path->repr->addObserver(*_observer); } PathManipulator::~PathManipulator() { delete _dragpoint; - if (_path) _path->repr->removeObserver(*_observer); delete _observer; gtk_object_destroy(_outline); if (_spcurve) _spcurve->unref(); -- cgit v1.2.3 From 0fd4ff04adaf544d34a58b62e8fc9d9a9f06534a Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 18 Feb 2010 01:22:55 +0100 Subject: Make ControlPointSelection trackable to prevent random crashes in the node tool (bzr r9095) --- src/ui/tool/path-manipulator.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index d2f90bbca..13f1448b9 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -103,7 +103,7 @@ PathManipulator::PathManipulator(MultiPathManipulator &mpm, SPPath *path, , _subpaths(*this) , _multi_path_manipulator(mpm) , _path(path) - , _spcurve(NULL) + , _spcurve(new SPCurve()) , _dragpoint(new CurveDragPoint(*this)) , _observer(new PathManipulatorObserver(this, SP_OBJECT(path)->repr)) , _edit_transform(et) @@ -146,7 +146,7 @@ PathManipulator::~PathManipulator() delete _dragpoint; delete _observer; gtk_object_destroy(_outline); - if (_spcurve) _spcurve->unref(); + _spcurve->unref(); clear(); } @@ -923,8 +923,11 @@ void PathManipulator::_createControlPointsFromGeometry() // so that _updateDragPoint doesn't crash on paths with naked movetos Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers(_spcurve->get_pathvector()); for (Geom::PathVector::iterator i = pathv.begin(); i != pathv.end(); ) { - if (i->empty()) pathv.erase(i++); - else ++i; + if (i->empty()) { + pathv.erase(i++); + } else { + ++i; + } } _spcurve->set_pathvector(pathv); @@ -1123,13 +1126,11 @@ void PathManipulator::_getGeometry() Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe(); if (lpe) { PathParam *pathparam = dynamic_cast(lpe->getParameter(_lpe_key.data())); - if (!_spcurve) - _spcurve = new SPCurve(pathparam->get_pathvector()); - else - _spcurve->set_pathvector(pathparam->get_pathvector()); + _spcurve->unref(); + _spcurve = new SPCurve(pathparam->get_pathvector()); } } else { - if (_spcurve) _spcurve->unref(); + _spcurve->unref(); _spcurve = sp_path_get_curve_for_edit(_path); } } @@ -1152,7 +1153,7 @@ void PathManipulator::_setGeometry() } } else { if (_path->repr->attribute("inkscape:original-d")) - sp_path_set_original_curve(_path, _spcurve, true, false); + sp_path_set_original_curve(_path, _spcurve, false, false); else sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false); } @@ -1301,7 +1302,6 @@ void PathManipulator::_commit(Glib::ustring const &annotation) * point of the path. */ void PathManipulator::_updateDragPoint(Geom::Point const &evp) { - // TODO find a way to make this faster (no transform required) Geom::Matrix to_desktop = _edit_transform * _i2d_transform; Geom::PathVector pv = _spcurve->get_pathvector(); boost::optional pvp -- cgit v1.2.3 From 64175d46a59752af36075fb07cfb0b526b159b7b Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 18 Feb 2010 01:34:15 +0100 Subject: Fix node tool crash on path where the last subpath is a lone moveto (bzr r9096) --- src/ui/tool/path-manipulator.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 13f1448b9..82fe53440 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -923,8 +923,11 @@ void PathManipulator::_createControlPointsFromGeometry() // so that _updateDragPoint doesn't crash on paths with naked movetos Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers(_spcurve->get_pathvector()); for (Geom::PathVector::iterator i = pathv.begin(); i != pathv.end(); ) { + // NOTE: this utilizes the fact that Geom::PathVector is an std::vector. + // When we erase an element, the next one slides into position, + // so we do not increment the iterator even though it is theoretically invalidated. if (i->empty()) { - pathv.erase(i++); + pathv.erase(i); } else { ++i; } -- cgit v1.2.3 From 1a455b9b06bfde3589574b241dfb140d598608b0 Mon Sep 17 00:00:00 2001 From: "Jon A. Cruz" Date: Wed, 24 Feb 2010 22:01:43 -0800 Subject: Warning cleanup. (bzr r9110) --- src/ui/tool/path-manipulator.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 82fe53440..0b0254108 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -66,11 +66,13 @@ public: Inkscape::GC::anchor(_node); _node->addObserver(*this); } + ~PathManipulatorObserver() { _node->removeObserver(*this); Inkscape::GC::release(_node); } - virtual void notifyAttributeChanged(Inkscape::XML::Node &node, GQuark attr, + + virtual void notifyAttributeChanged(Inkscape::XML::Node &/*node*/, GQuark attr, Util::ptr_shared, Util::ptr_shared) { // do nothing if blocked @@ -87,6 +89,7 @@ public: _pm->_externalChange(PATH_CHANGE_TRANSFORM); } } + void block() { _blocked = true; } void unblock() { _blocked = false; } private: -- cgit v1.2.3 From 90e813701a7865bc36755fb0f35ab74c4b6963a2 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 14 Mar 2010 18:38:50 +0100 Subject: Implement keyboard shortcuts for single handle adjustments. Minor disambiguating cleanup in node.h. (bzr r9190) --- src/ui/tool/path-manipulator.cpp | 79 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 5 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index 0b0254108..d395d0e0a 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -598,10 +598,10 @@ unsigned PathManipulator::_deleteStretch(NodeList::iterator start, NodeList::ite // We can't use nl->erase(start, end), because it would break when the stretch // crosses the beginning of a closed subpath - NodeList *nl = start->list(); + NodeList &nl = start->nodeList(); while (start != end) { NodeList::iterator next = start.next(); - nl->erase(start); + nl.erase(start); start = next; } @@ -728,6 +728,68 @@ void PathManipulator::setSegmentType(SegmentType type) } } +void PathManipulator::scaleHandle(Node *n, int which, int dir, bool pixel) +{ + if (n->type() == NODE_SYMMETRIC || n->type() == NODE_AUTO) { + n->setType(NODE_SMOOTH); + } + Handle *h = _chooseHandle(n, which); + double length_change; + + if (pixel) { + length_change = 1.0 / _desktop->current_zoom() * dir; + } else { + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + length_change = prefs->getDoubleLimited("/options/defaultscale/value", 2, 1, 1000); + length_change *= dir; + } + + Geom::Point relpos = h->relativePos(); + double rellen = relpos.length(); + h->setRelativePos(relpos * ((rellen + length_change) / rellen)); + update(); + + gchar const *key = which < 0 ? "handle:scale:left" : "handle:scale:right"; + _commit(_("Scale handle"), key); +} + +void PathManipulator::rotateHandle(Node *n, int which, int dir, bool pixel) +{ + if (n->type() != NODE_CUSP) { + n->setType(NODE_CUSP); + } + Handle *h = _chooseHandle(n, which); + double angle; + + if (pixel) { + // Rotate by "one pixel" + angle = atan2(1.0 / _desktop->current_zoom(), h->length()) * dir; + } else { + Inkscape::Preferences *prefs = Inkscape::Preferences::get(); + int snaps = prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000); + angle = M_PI * dir / snaps; + } + h->setRelativePos(h->relativePos() * Geom::Rotate(angle)); + update(); + gchar const *key = which < 0 ? "handle:rotate:left" : "handle:rotate:right"; + _commit(_("Rotate handle"), key); +} + +Handle *PathManipulator::_chooseHandle(Node *n, int which) +{ + Geom::Point f = n->front()->position(), b = n->back()->position(); + if (which < 0) { + // pick left handle. + // we just swap the handles and pick the right handle below. + std::swap(f, b); + } + if (f[Geom::X] >= b[Geom::X]) { + return n->front(); + } else { + return n->back(); + } +} + /** Set the visibility of handles. */ void PathManipulator::showHandles(bool show) { @@ -1187,11 +1249,11 @@ bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event) // Ctrl+Alt+click: delete nodes hideDragPoint(); NodeList::iterator iter = NodeList::get_iterator(n); - NodeList *nl = iter->list(); + NodeList &nl = iter->nodeList(); - if (nl->size() <= 1 || (nl->size() <= 2 && !nl->closed())) { + if (nl.size() <= 1 || (nl.size() <= 2 && !nl.closed())) { // Removing last node of closed path - delete it - nl->kill(); + nl.kill(); } else { // In other cases, delete the node under cursor _deleteStretch(iter, iter.next(), true); @@ -1304,6 +1366,13 @@ void PathManipulator::_commit(Glib::ustring const &annotation) sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, annotation.data()); } +void PathManipulator::_commit(Glib::ustring const &annotation, gchar const *key) +{ + writeXML(); + sp_document_maybe_done(sp_desktop_document(_desktop), key, SP_VERB_CONTEXT_NODE, + annotation.data()); +} + /** Update the position of the curve drag point such that it is over the nearest * point of the path. */ void PathManipulator::_updateDragPoint(Geom::Point const &evp) -- cgit v1.2.3 From a87f933596b37ac2194537f20d4bf91b8899adba Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Sun, 14 Mar 2010 22:04:08 +0100 Subject: New node tool: implement handle snapping Fixed bugs: - https://launchpad.net/bugs/538487 (bzr r9192) --- src/ui/tool/path-manipulator.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index d395d0e0a..ebf0f3828 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -1045,6 +1045,7 @@ void PathManipulator::_createControlPointsFromGeometry() // we need to set the nodetypes after all the handles are in place, // so that pickBestType works correctly // TODO maybe migrate to inkscape:node-types? + // TODO move this into SPPath - do not manipulate directly gchar const *nts_raw = _path ? _path->repr->attribute(_nodetypesKey().data()) : 0; std::string nodetype_string = nts_raw ? nts_raw : ""; /* Calculate the needed length of the nodetype string. -- cgit v1.2.3 From 6f0f105886528bff81e43b32c9ab8dd9efa3fc22 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 18 Mar 2010 03:18:56 +0100 Subject: Fix scaling of degenerate handles using keybard shortcuts. (bzr r9203) --- src/ui/tool/path-manipulator.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index ebf0f3828..f6d5bde37 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -720,6 +720,7 @@ void PathManipulator::setSegmentType(SegmentType type) case SEGMENT_CUBIC_BEZIER: if (!j->front()->isDegenerate() || !k->back()->isDegenerate()) break; + // move both handles to 1/3 of the line j->front()->move(j->position() + (k->position() - j->position()) / 3); k->back()->move(k->position() + (j->position() - k->position()) / 3); break; @@ -744,9 +745,17 @@ void PathManipulator::scaleHandle(Node *n, int which, int dir, bool pixel) length_change *= dir; } - Geom::Point relpos = h->relativePos(); - double rellen = relpos.length(); - h->setRelativePos(relpos * ((rellen + length_change) / rellen)); + Geom::Point relpos; + if (h->isDegenerate()) { + Node *nh = n->nodeToward(h); + if (!nh) return; + relpos = Geom::unit_vector(nh->position() - n->position()) * length_change; + } else { + relpos = h->relativePos(); + double rellen = relpos.length(); + relpos *= ((rellen + length_change) / rellen); + } + h->setRelativePos(relpos); update(); gchar const *key = which < 0 ? "handle:scale:left" : "handle:scale:right"; @@ -759,8 +768,9 @@ void PathManipulator::rotateHandle(Node *n, int which, int dir, bool pixel) n->setType(NODE_CUSP); } Handle *h = _chooseHandle(n, which); - double angle; + if (h->isDegenerate()) return; + double angle; if (pixel) { // Rotate by "one pixel" angle = atan2(1.0 / _desktop->current_zoom(), h->length()) * dir; @@ -769,6 +779,7 @@ void PathManipulator::rotateHandle(Node *n, int which, int dir, bool pixel) int snaps = prefs->getIntLimited("/options/rotationsnapsperpi/value", 12, 1, 1000); angle = M_PI * dir / snaps; } + h->setRelativePos(h->relativePos() * Geom::Rotate(angle)); update(); gchar const *key = which < 0 ? "handle:rotate:left" : "handle:rotate:right"; @@ -777,7 +788,14 @@ void PathManipulator::rotateHandle(Node *n, int which, int dir, bool pixel) Handle *PathManipulator::_chooseHandle(Node *n, int which) { - Geom::Point f = n->front()->position(), b = n->back()->position(); + // Rationale for this choice: + // Imagine you have two handles pointing right, where one of them is only slighty higher + // than the other. Extending one of the handles could make its X coord larger than + // the second one, and keeping the shortcut pressed would result in two handles being + // extended alternately. This appears like extending both handles at once and is confusing. + // Using the unit vector avoids this problem and remains fairly intuitive. + Geom::Point f = Geom::unit_vector(n->front()->position()); + Geom::Point b = Geom::unit_vector(n->back()->position()); if (which < 0) { // pick left handle. // we just swap the handles and pick the right handle below. -- cgit v1.2.3 From e46805d62ddb2975490a42c16a44d2232c7dbf37 Mon Sep 17 00:00:00 2001 From: Krzysztof Kosi??ski Date: Thu, 18 Mar 2010 03:59:43 +0100 Subject: Fix a few remaining oddities in handle scaling via keyboard (bzr r9205) --- src/ui/tool/path-manipulator.cpp | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/ui/tool/path-manipulator.cpp') diff --git a/src/ui/tool/path-manipulator.cpp b/src/ui/tool/path-manipulator.cpp index f6d5bde37..66f72f379 100644 --- a/src/ui/tool/path-manipulator.cpp +++ b/src/ui/tool/path-manipulator.cpp @@ -747,6 +747,7 @@ void PathManipulator::scaleHandle(Node *n, int which, int dir, bool pixel) Geom::Point relpos; if (h->isDegenerate()) { + if (dir < 0) return; Node *nh = n->nodeToward(h); if (!nh) return; relpos = Geom::unit_vector(nh->position() - n->position()) * length_change; @@ -788,20 +789,24 @@ void PathManipulator::rotateHandle(Node *n, int which, int dir, bool pixel) Handle *PathManipulator::_chooseHandle(Node *n, int which) { - // Rationale for this choice: - // Imagine you have two handles pointing right, where one of them is only slighty higher - // than the other. Extending one of the handles could make its X coord larger than - // the second one, and keeping the shortcut pressed would result in two handles being - // extended alternately. This appears like extending both handles at once and is confusing. - // Using the unit vector avoids this problem and remains fairly intuitive. - Geom::Point f = Geom::unit_vector(n->front()->position()); - Geom::Point b = Geom::unit_vector(n->back()->position()); + NodeList::iterator i = NodeList::get_iterator(n); + Node *prev = i.prev().ptr(); + Node *next = i.next().ptr(); + + // on an endnode, the remaining handle automatically wins + if (!next) return n->back(); + if (!prev) return n->front(); + + // compare X coord ofline segments + Geom::Point npos = next->position(); + Geom::Point ppos = prev->position(); if (which < 0) { // pick left handle. // we just swap the handles and pick the right handle below. - std::swap(f, b); + std::swap(npos, ppos); } - if (f[Geom::X] >= b[Geom::X]) { + + if (npos[Geom::X] >= ppos[Geom::X]) { return n->front(); } else { return n->back(); -- cgit v1.2.3