path-manipulator.cpp revision e898e8dd3fbc3e21f3a35f089a0d1c1dc6d48850
/** @file
* Path manipulator - implementation
*/
/* Authors:
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
* Abhishek Sharma
*
* Copyright (C) 2009 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <string>
#include <sstream>
#include <deque>
#include <stdexcept>
#include <boost/shared_ptr.hpp>
#include <glibmm.h>
#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/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 "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-observer.h"
namespace Inkscape {
namespace UI {
namespace {
/// Types of path changes that we must react to.
enum PathChange {
};
} // anonymous namespace
/**
* Notifies the path manipulator when something changes the path being edited
* (e.g. undo / redo)
*/
public:
: _pm(p)
, _blocked(false)
{
_node->addObserver(*this);
}
_node->removeObserver(*this);
}
{
// do nothing if blocked
if (_blocked) return;
// only react to "d" (path data) and "transform" attribute changes
} else if (attr == path_transform) {
}
}
private:
bool _blocked;
};
, _subpaths(*this)
, _dragpoint(new CurveDragPoint(*this))
, /* XML Tree being used here directly while it shouldn't be*/_observer(new PathManipulatorObserver(this, path->getRepr()))
, _num_selected(0)
, _show_handles(true)
, _show_outline(false)
, _show_path_direction(false)
, _live_outline(true)
, _live_objects(true)
{
} else {
}
_dragpoint->setVisible(false);
_getGeometry();
}
{
delete _dragpoint;
delete _observer;
clear();
}
/** Handle motion events to update the position of the curve drag point. */
{
if (empty()) return false;
{
case GDK_MOTION_NOTIFY:
break;
default: break;
}
return false;
}
/** Check whether the manipulator has any nodes. */
bool PathManipulator::empty() {
}
/** Update the display and the outline of the path. */
void PathManipulator::update()
{
}
/** Store the changes to the path in XML. */
void PathManipulator::writeXML()
{
if (!_live_outline)
if (!_live_objects)
_setGeometry();
if (!_path) return;
if (!empty()) {
_path->updateRepr();
} else {
// this manipulator will have to be destroyed right after this call
_path->deleteObject(true, true);
_path = 0;
}
}
/** Remove all nodes from the path. */
void PathManipulator::clear()
{
// no longer necessary since nodes remove themselves from selection on destruction
//_removeNodesFromSelection();
}
/** Select all nodes in subpaths that have something selected. */
void PathManipulator::selectSubpaths()
{
if (j->selected()) {
// if at least one of the nodes from this subpath is selected,
// select all nodes from this subpath
continue;
}
}
}
}
/** Invert selection in the selected subpaths. */
{
if (j->selected()) {
// found selected node - invert selection in this subpath
}
// next subpath
break;
}
}
}
}
/** Insert a new node in the middle of each selected segment. */
void PathManipulator::insertNodes()
{
if (_num_selected < 2) return;
j = subdivideSegment(j, 0.5);
}
}
}
}
/** Insert new nodes exactly at the positions of selected nodes while preserving shape.
* This is equivalent to breaking, except that it doesn't split into subpaths. */
void PathManipulator::duplicateNodes()
{
if (_num_selected == 0) return;
if (j->selected()) {
if (k) {
// Move the new node to the bottom of the Z-order. This way you can drag all
// nodes that were selected before this operation without deselecting
// everything because there is a new node above.
n->sink();
}
(*i)->insert(k, n);
if (k) {
// We need to manually call the selection change callback to refresh
// the handle display correctly.
// This call changes num_selected, but we call this once for a selected node
// and once for an unselected node, so in the end the number stays correct.
_selectionChanged(j.ptr(), true);
_selectionChanged(n, false);
} else {
// select the new end node instead of the node just before it
_selection.insert(n);
break; // this was the end node, nothing more to do
}
}
}
}
}
/** Replace contiguous selections of nodes in each subpath with one node. */
{
if (_num_selected < 2) return;
bool pos_valid = preserve_pos;
SubpathPtr sp = *i;
unsigned num_selected = 0, num_unselected = 0;
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 selection
}
// Work loop
while (num_selected > 0) {
// Find selected node
"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;
++num_points;
}
if (num_points > 1) {
if (use_pos) {
pos_valid = false;
} else {
}
// do not move handles if they aren't degenerate
}
}
--num_selected;
}
}
--num_selected; // for the joined node or single selected node
}
}
}
/** Remove nodes in the middle of selected segments. */
void PathManipulator::weldSegments()
{
if (_num_selected < 2) return;
SubpathPtr sp = *i;
unsigned num_selected = 0, num_unselected = 0;
if (j->selected()) ++num_selected;
else ++num_unselected;
}
if (num_selected < 3) continue;
// 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
}
// Work loop
while (num_selected > 0) {
// Find selected node
"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
++num_points;
}
if (num_points > 2) {
// remove nodes in the middle
}
}
}
}
}
/** Break the subpath at selected nodes. It also works for single node closed paths. */
void PathManipulator::breakNodes()
{
SubpathPtr sp = *i;
// Each open path must have at least two nodes so no checks are required.
// For 2-node open paths, cur == end
++cur;
--end;
}
bool becomes_open = false;
// Move the node to break at to the beginning of path
becomes_open = true;
} else {
}
n->sink();
if (becomes_open) {
}
}
}
}
/** 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. */
{
if (_num_selected == 0) return;
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;
if (j->selected()) ++num_selected;
else ++num_unselected;
}
if (num_selected == 0) {
++i;
continue;
}
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
}
while (num_selected > 0) {
}
}
}
++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;
unsigned del_len = 0;
++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
unsigned seg = 0;
for (unsigned s = 0; s < samples_per_segment; ++s) {
}
++seg;
}
// Fill last point
// Compute replacement bezier curve
// TODO the fitting algorithm sucks - rewrite it to be awesome
delete[] bezier_data;
}
// We can't use nl->erase(start, end), because it would break when the stretch
// crosses the beginning of a closed subpath
}
return del_len;
}
/** Removes selected segments */
void PathManipulator::deleteSegments()
{
if (_num_selected == 0) return;
SubpathPtr sp = *i;
bool has_unselected = false;
unsigned num_selected = 0;
if (j->selected()) {
++num_selected;
} else {
has_unselected = true;
}
}
if (!has_unselected) {
continue;
}
}
while (num_selected > 0) {
continue;
}
unsigned num_points = 0;
++num_points;
}
if (num_points >= 2) {
// Retract end handles
// 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.
} 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
} else {
}
}
}
}
++i;
}
}
/** 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. */
{
if (selected_only) {
if (j->selected()) {
(*i)->reverse();
break; // continue with the next subpath
}
}
} else {
(*i)->reverse();
}
}
}
/** Make selected segments curves / lines. */
{
if (_num_selected == 0) return;
switch (type) {
case SEGMENT_STRAIGHT:
break;
break;
case SEGMENT_CUBIC_BEZIER:
break;
// move both handles to 1/3 of the line
break;
}
}
}
}
{
n->setType(NODE_SMOOTH);
}
double length_change;
if (pixel) {
} else {
length_change *= dir;
}
if (h->isDegenerate()) {
if (dir < 0) return;
if (!nh) return;
} else {
relpos = h->relativePos();
}
h->setRelativePos(relpos);
update();
}
{
}
if (h->isDegenerate()) return;
double angle;
if (pixel) {
// Rotate by "one pixel"
} else {
}
update();
}
{
// on an endnode, the remaining handle automatically wins
// compare X coord ofline segments
if (which < 0) {
// pick left handle.
// we just swap the handles and pick the right handle below.
}
return n->front();
} else {
return n->back();
}
}
/** Set the visibility of handles. */
{
if (show == _show_handles) return;
if (show) {
if (!j->selected()) continue;
j->showHandles(true);
}
}
} else {
j->showHandles(false);
}
}
}
}
/** Set the visibility of outline. */
{
if (show == _show_outline) return;
}
{
if (show == _show_path_direction) return;
}
{
_live_outline = set;
}
{
_live_objects = set;
}
{
}
}
}
/** 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);
}
/** Insert a node in the segment beginning with the supplied iterator,
* at the given time value */
{
// 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.
++insert_at;
// for a line segment, insert a cusp node
} else {
// build bezier curve and subdivide
// set new handle positions
n->setType(NODE_SMOOTH, false);
}
return inserted;
}
* @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
*/
bool search_unselected, bool closest)
{
if(j->selected()) {
if (!search_selected) continue;
} else {
if (!search_unselected) continue;
}
if (cond) {
match = j;
}
}
}
return match;
}
/** Called by the XML observer when something else than us modifies the path. */
{
switch (type) {
case PATH_CHANGE_D: {
_getGeometry();
// ugly: stored offsets of selected nodes in a vector
// vector<bool> should be specialized so that it takes only 1 bit per value
}
}
++curpos;
}
}
} break;
case PATH_CHANGE_TRANSFORM: {
j->transform(i2d_change);
}
}
} break;
default: break;
}
}
/** Create nodes and handles based on the XML of the edited path. */
{
clear();
// sanitize pathvector and store it in SPCurve,
// so that _updateDragPoint doesn't crash on paths with naked movetos
// 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()) {
} else {
++i;
}
}
// in this loop, we know that there are no zero-segment subpaths
// prepare new subpath
// 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
} 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 */
}
// if this is a bezier segment, move handles appropriately
// TODO: I don't know why the dynamic cast below doesn't want to work
// when I replace BezierCurve with CubicBezier. Might be a bug
// somewhere in pathv_to_linear_and_cubic_beziers
{
}
}
// If the path is closed, make the list cyclic
}
// 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
//XML Tree being used here directly while it shouldn't be.
/* 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. */
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 ((*i)->closed()) {
// STUPIDITY ALERT: it seems we need to use the duplicate type symbol instead of
// the first one to remain backward compatible.
}
}
}
/** Construct the geometric representation of nodes and handles, update the outline
* and display */
{
continue;
}
prev = i;
}
// Here we link the last and first node if the path is closed.
// If the last segment is Bezier, we add it.
}
// if that segment is linear, we just call closePath().
}
++spi;
}
if (_live_outline)
if (_live_objects)
_setGeometry();
}
/** Build one segment of the geometric representation.
* @relates PathManipulator */
{
{
// NOTE: It seems like the renderer cannot correctly handle vline / hline segments,
// and trying to display a path using them results in funny artifacts.
} else {
// this is a bezier segment
}
}
/** Construct a node type string to store in the sodipodi:nodetypes attribute. */
{
// precondition: no single-node subpaths
}
// nodestring format peculiarity: first node is counted twice for closed paths
}
}
/** Update the path outline. */
void PathManipulator::_updateOutline()
{
if (!_show_outline) {
return;
}
// This SPCurve thing has to be killed with extreme prejudice
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.
// rotate the point
}
}
}
}
/** Retrieve the geometry of the edited object from the object tree */
void PathManipulator::_getGeometry()
{
using namespace Inkscape::LivePathEffect;
if (lpe) {
}
} else {
}
}
/** 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;
// copied from nodepath.cpp
// NOTE: if we are editing an LPE param, _path is not actually an SPPath, it is
// a LivePathEffectObject. (mad laughter)
if (lpe) {
}
} else {
//XML Tree being used here directly while it shouldn't be.
else
}
}
/** Figure out in what attribute to store the nodetype string. */
{
return _lpe_key + "-nodetypes";
}
/** Return the XML node we are editing.
* This method is wrong but necessary at the moment. */
{
//XML Tree being used here directly while it shouldn't be.
//XML Tree being used here directly while it shouldn't be.
}
{
// Ctrl+Alt+click: delete nodes
// Removing last node of closed path - delete it
} else {
// In other cases, delete the node under cursor
}
if (!empty()) {
update();
}
// We need to call MPM's method because it could have been our last node
return true;
} else if (held_control(*event)) {
// Ctrl+click: cycle between node types
if (!n->isEndNode()) {
update();
_commit(_("Cycle node type"));
}
return true;
}
return false;
}
void PathManipulator::_handleGrabbed()
{
}
void PathManipulator::_handleUngrabbed()
{
_commit(_("Drag handle"));
}
{
// retracting by Ctrl+click
update();
_commit(_("Retract handle"));
return true;
}
return false;
}
{
if (selected) ++_num_selected;
else --_num_selected;
// don't do anything if we do not show handles
if (!_show_handles) return;
// only do something if a node changed selection state
if (!node) return;
// update handle display
if (selected) {
// selection - show handles on this node and adjacent ones
node->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. */
bool nodesel[5];
for (int i = 0; i < 5; ++i) {
}
for (int i = 1; i < 4; ++i) {
iters[i]->showHandles(false);
}
}
}
}
/** Removes all nodes belonging to this manipulator from the control pont selection */
{
// remove this manipulator's nodes from selection
}
}
}
/** Update the XML representation and put the specified annotation on the undo stack */
{
writeXML();
}
{
writeXML();
annotation.data());
}
/** Update the position of the curve drag point such that it is over the nearest
* point of the path. */
{
if (!pvp) return;
double fracpart;
double stroke_tolerance = _getStrokeTolerance();
fracpart != 0.0 &&
{
_dragpoint->setVisible(true);
} else {
_dragpoint->setVisible(false);
}
}
/// This is called on zoom change to update the direction arrows
{
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. */
}
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:fileencoding=utf-8:textwidth=99 :