path-manipulator.cpp revision 70d31ae8a7a27e57cfcdc921ea0d2f47c92442a4
/** @file
* Path manipulator - implementation
*/
/* Authors:
* Krzysztof KosiĆski <tweenk.pl@gmail.com>
*
* 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:
{
// 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))
, _observer(new PathManipulatorObserver(this))
, _show_handles(true)
, _show_outline(false)
{
/* Because curve drag point is always created first, it does not cover nodes */
} 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 (!_path) return;
if (!empty()) {
} 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;
}
}
}
}
/** Move the selection forward or backward by one node in each subpath, based on the sign
* of the parameter. */
{
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.
unsigned num = 0;
++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()) {
} else {
sels.push_front(false);
}
} else {
if ((*i)->closed()) {
} else {
num = 1;
}
}
++num;
}
}
}
/** 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) return;
j = subdivideSegment(j, 0.5);
}
}
}
}
/** Replace contiguous selections of nodes in each subpath with one node. */
{
if (!_num_selected) return;
_dragpoint->setVisible(false);
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 contiguous selection
}
// Main loop
while (num_selected > 0) {
// Find selected node
"but there are still nodes to process!");
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()
{
// TODO
}
/** 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) return;
unsigned const samples_per_segment = 10;
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) {
unsigned del_len = 0;
++del_len;
}
// 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
// Fill fit data
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 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
}
num_selected -= del_len;
}
++i;
}
}
/** 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 the subpaths that have anything selected. */
void PathManipulator::reverseSubpaths()
{
if (j->selected()) {
(*i)->reverse();
break; // continue with the next subpath
}
}
}
}
/** Make selected segments curves / lines. */
{
if (!_num_selected) return;
switch (type) {
case SEGMENT_STRAIGHT:
break;
break;
case SEGMENT_CUBIC_BEZIER:
break;
break;
}
}
}
}
/** 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;
}
{
}
}
}
/** Hide the curve drag point until the next motion event. */
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
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
{
}
}
// 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?
/* 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;
}
_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) {
if (!_spcurve)
else
}
} 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 {
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. */
{
}
{
for (int i = 0; i < 2; ++i) {
false));
handles[i]));
}
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.
for (int i = 0; i < 2; ++i) {
}
// Same for this one: CPS only connects to grab, drag, and ungrab
}
{
// cycle between node types on ctrl+click
if (n->isEndNode()) {
n->setType(NODE_SMOOTH);
} else {
}
} else {
}
update();
_commit(_("Cycle node type"));
return true;
}
void PathManipulator::_handleGrabbed()
{
}
void PathManipulator::_handleUngrabbed()
{
_commit(_("Drag handle"));
}
{
// retracting by Ctrl+click
update();
_commit(_("Retract handle"));
return true;
}
return false;
}
{
// 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);
}
}
}
if (selected) ++_num_selected;
else --_num_selected;
}
/** 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();
}
/** Update the position of the curve drag point such that it is over the nearest
* point of the path. */
{
// TODO find a way to make this faster (no transform required)
if (!pvp) return;
double fracpart;
double stroke_tolerance = _getStrokeTolerance();
_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:encoding=utf-8:textwidth=99 :