index b1a86dd7766f12059c46c38b4de8645d59ff7ee3..7c60efbabbef193183547e4583c8d0daa0a9e626 100644 (file)
*/
/* Authors:
* Krzysztof Kosiński <tweenk.pl@gmail.com>
*/
/* 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
*
* Copyright (C) 2009 Authors
* Released under GNU GPL, read the file 'COPYING' for more information
*/
class PathManipulatorObserver : public Inkscape::XML::NodeObserver {
public:
*/
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<char>, Util::ptr_shared<char>)
{
// do nothing if blocked
Util::ptr_shared<char>, Util::ptr_shared<char>)
{
// do nothing if blocked
_pm->_externalChange(PATH_CHANGE_TRANSFORM);
}
}
_pm->_externalChange(PATH_CHANGE_TRANSFORM);
}
}
+
void block() { _blocked = true; }
void unblock() { _blocked = false; }
private:
PathManipulator *_pm;
void block() { _blocked = true; }
void unblock() { _blocked = false; }
private:
PathManipulator *_pm;
+ Inkscape::XML::Node *_node;
bool _blocked;
};
bool _blocked;
};
, _subpaths(*this)
, _multi_path_manipulator(mpm)
, _path(path)
, _subpaths(*this)
, _multi_path_manipulator(mpm)
, _path(path)
- , _spcurve(NULL)
+ , _spcurve(new SPCurve())
, _dragpoint(new CurveDragPoint(*this))
, _dragpoint(new CurveDragPoint(*this))
- , _observer(new PathManipulatorObserver(this))
+ , /* XML Tree being used here directly while it shouldn't be*/_observer(new PathManipulatorObserver(this, SP_OBJECT(path)->getRepr()))
, _edit_transform(et)
, _num_selected(0)
, _show_handles(true)
, _edit_transform(et)
, _num_selected(0)
, _show_handles(true)
, _lpe_key(lpe_key)
{
if (_lpe_key.empty()) {
, _lpe_key(lpe_key)
{
if (_lpe_key.empty()) {
- _i2d_transform = sp_item_i2d_affine(SP_ITEM(path));
+ _i2d_transform = SP_ITEM(path)->i2d_affine();
} else {
_i2d_transform = Geom::identity();
}
} else {
_i2d_transform = Geom::identity();
}
sigc::hide( sigc::mem_fun(*this, &PathManipulator::_updateOutlineOnZoomChange)));
_createControlPointsFromGeometry();
sigc::hide( sigc::mem_fun(*this, &PathManipulator::_updateOutlineOnZoomChange)));
_createControlPointsFromGeometry();
-
- _path->repr->addObserver(*_observer);
}
PathManipulator::~PathManipulator()
{
delete _dragpoint;
}
PathManipulator::~PathManipulator()
{
delete _dragpoint;
- if (_path) _path->repr->removeObserver(*_observer);
delete _observer;
gtk_object_destroy(_outline);
delete _observer;
gtk_object_destroy(_outline);
- if (_spcurve) _spcurve->unref();
+ _spcurve->unref();
clear();
}
clear();
}
/** Store the changes to the path in XML. */
void PathManipulator::writeXML()
{
/** 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()) {
if (!_path) return;
_observer->block();
if (!empty()) {
_path = 0;
}
_observer->unblock();
_path = 0;
}
_observer->unblock();
-
- if (!empty()) {
- if (!_live_outline)
- _updateOutline();
- if (!_live_objects)
- _setGeometry();
- }
}
/** Remove all nodes from the path. */
}
/** Remove all nodes from the path. */
/** Select all nodes in subpaths that have something selected. */
void PathManipulator::selectSubpaths()
{
/** Select all nodes in subpaths that have something selected. */
void PathManipulator::selectSubpaths()
{
- for (std::list<SubpathPtr>::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
+ for (SubpathList::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()) {
NodeList::iterator sp_start = (*i)->begin(), sp_end = (*i)->end();
for (NodeList::iterator j = sp_start; j != sp_end; ++j) {
if (j->selected()) {
}
}
}
}
-/** 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;
- 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.
- for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
- std::deque<bool> 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! zero-node subpaths are not allowed
-
- 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 selected subpaths. */
void PathManipulator::invertSelectionInSubpaths()
{
/** Invert selection in the selected subpaths. */
void PathManipulator::invertSelectionInSubpaths()
{
}
}
}
}
+/** 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;
+
+ for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
+ for (NodeList::iterator j = (*i)->begin(); j != (*i)->end(); ++j) {
+ if (j->selected()) {
+ NodeList::iterator k = j.next();
+ Node *n = new Node(_multi_path_manipulator._path_data.node_data, *j);
+
+ // 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();
+
+ n->front()->setPosition(*j->front());
+ j->front()->retract();
+ j->setType(NODE_CUSP, false);
+ (*i)->insert(k, n);
+
+ // 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);
+ }
+ }
+ }
+}
+
/** Replace contiguous selections of nodes in each subpath with one node. */
void PathManipulator::weldNodes(NodeList::iterator preserve_pos)
{
/** Replace contiguous selections of nodes in each subpath with one node. */
void PathManipulator::weldNodes(NodeList::iterator preserve_pos)
{
@@ -588,10 +575,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
// 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();
while (start != end) {
NodeList::iterator next = start.next();
- nl->erase(start);
+ nl.erase(start);
start = next;
}
start = next;
}
case SEGMENT_CUBIC_BEZIER:
if (!j->front()->isDegenerate() || !k->back()->isDegenerate())
break;
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;
j->front()->move(j->position() + (k->position() - j->position()) / 3);
k->back()->move(k->position() + (j->position() - k->position()) / 3);
break;
}
}
}
}
+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;
+ 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;
+ } 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";
+ _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);
+ if (h->isDegenerate()) return;
+
+ 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)
+{
+ 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(npos, ppos);
+ }
+
+ if (npos[Geom::X] >= ppos[Geom::X]) {
+ return n->front();
+ } else {
+ return n->back();
+ }
+}
+
/** Set the visibility of handles. */
void PathManipulator::showHandles(bool show)
{
/** Set the visibility of handles. */
void PathManipulator::showHandles(bool show)
{
/** Called by the XML observer when something else than us modifies the path. */
void PathManipulator::_externalChange(unsigned type)
{
/** Called by the XML observer when something else than us modifies the path. */
void PathManipulator::_externalChange(unsigned type)
{
+ hideDragPoint();
+
switch (type) {
case PATH_CHANGE_D: {
_getGeometry();
switch (type) {
case PATH_CHANGE_D: {
_getGeometry();
} break;
case PATH_CHANGE_TRANSFORM: {
Geom::Matrix i2d_change = _d2i_transform;
} break;
case PATH_CHANGE_TRANSFORM: {
Geom::Matrix i2d_change = _d2i_transform;
- _i2d_transform = sp_item_i2d_affine(SP_ITEM(_path));
+ _i2d_transform = SP_ITEM(_path)->i2d_affine();
_d2i_transform = _i2d_transform.inverse();
i2d_change *= _i2d_transform;
for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
_d2i_transform = _i2d_transform.inverse();
i2d_change *= _i2d_transform;
for (SubpathList::iterator i = _subpaths.begin(); i != _subpaths.end(); ++i) {
// 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(); ) {
// 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;
+ // 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);
+ } else {
+ ++i;
+ }
}
_spcurve->set_pathvector(pathv);
}
_spcurve->set_pathvector(pathv);
// 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?
// 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(_nodetypesKey().data()) : 0;
+ // TODO move this into SPPath - do not manipulate directly
+
+ //XML Tree being used here directly while it shouldn't be.
+ gchar const *nts_raw = _path ? _path->getRepr()->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,
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,
Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe();
if (lpe) {
PathParam *pathparam = dynamic_cast<PathParam *>(lpe->getParameter(_lpe_key.data()));
Effect *lpe = LIVEPATHEFFECT(_path)->get_lpe();
if (lpe) {
PathParam *pathparam = dynamic_cast<PathParam *>(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 {
}
} else {
- if (_spcurve) _spcurve->unref();
+ _spcurve->unref();
_spcurve = sp_path_get_curve_for_edit(_path);
}
}
_spcurve = sp_path_get_curve_for_edit(_path);
}
}
LIVEPATHEFFECT(_path)->requestModified(SP_OBJECT_MODIFIED_FLAG);
}
} else {
LIVEPATHEFFECT(_path)->requestModified(SP_OBJECT_MODIFIED_FLAG);
}
} else {
- if (_path->repr->attribute("inkscape:original-d"))
- sp_path_set_original_curve(_path, _spcurve, true, false);
+ //XML Tree being used here directly while it shouldn't be.
+ if (_path->getRepr()->attribute("inkscape:original-d"))
+ sp_path_set_original_curve(_path, _spcurve, false, false);
else
else
- sp_shape_set_curve(SP_SHAPE(_path), _spcurve, false);
+ SP_SHAPE(_path)->setCurve(_spcurve, false);
}
}
}
}
* This method is wrong but necessary at the moment. */
Inkscape::XML::Node *PathManipulator::_getXMLNode()
{
* This method is wrong but necessary at the moment. */
Inkscape::XML::Node *PathManipulator::_getXMLNode()
{
- if (_lpe_key.empty()) return _path->repr;
- return LIVEPATHEFFECT(_path)->repr;
+ //XML Tree being used here directly while it shouldn't be.
+ if (_lpe_key.empty()) return _path->getRepr();
+ //XML Tree being used here directly while it shouldn't be.
+ return LIVEPATHEFFECT(_path)->getRepr();
}
bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event)
}
bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event)
// Ctrl+Alt+click: delete nodes
hideDragPoint();
NodeList::iterator iter = NodeList::get_iterator(n);
// 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
// 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);
} else {
// In other cases, delete the node under cursor
_deleteStretch(iter, iter.next(), true);
return true;
} else if (held_control(*event)) {
// Ctrl+click: cycle between node types
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);
- } else {
- n->setType(NODE_CUSP);
- }
- } else {
+ if (!n->isEndNode()) {
n->setType(static_cast<NodeType>((n->type() + 1) % NODE_LAST_REAL_TYPE));
n->setType(static_cast<NodeType>((n->type() + 1) % NODE_LAST_REAL_TYPE));
+ update();
+ _commit(_("Cycle node type"));
}
}
- update();
- _commit(_("Cycle node type"));
return true;
}
return false;
return true;
}
return false;
void PathManipulator::_commit(Glib::ustring const &annotation)
{
writeXML();
void PathManipulator::_commit(Glib::ustring const &annotation)
{
writeXML();
- sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, annotation.data());
+ DocumentUndo::done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_NODE, annotation.data());
+}
+
+void PathManipulator::_commit(Glib::ustring const &annotation, gchar const *key)
+{
+ writeXML();
+ DocumentUndo::maybeDone(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)
{
}
/** 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::Matrix to_desktop = _edit_transform * _i2d_transform;
Geom::PathVector pv = _spcurve->get_pathvector();
boost::optional<Geom::PathVectorPosition> pvp
Geom::Matrix to_desktop = _edit_transform * _i2d_transform;
Geom::PathVector pv = _spcurve->get_pathvector();
boost::optional<Geom::PathVectorPosition> pvp
NodeList::iterator first = (*spi)->before(pvp->t, &fracpart);
double stroke_tolerance = _getStrokeTolerance();
NodeList::iterator first = (*spi)->before(pvp->t, &fracpart);
double stroke_tolerance = _getStrokeTolerance();
- if (Geom::distance(evp, nearest_point) < stroke_tolerance) {
+ if (first && first.next() &&
+ fracpart != 0.0 &&
+ Geom::distance(evp, nearest_point) < stroke_tolerance)
+ {
_dragpoint->setVisible(true);
_dragpoint->setPosition(_desktop->w2d(nearest_point));
_dragpoint->setSize(2 * stroke_tolerance);
_dragpoint->setVisible(true);
_dragpoint->setPosition(_desktop->w2d(nearest_point));
_dragpoint->setSize(2 * stroke_tolerance);
* drag tolerance setting. */
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
double ret = prefs->getIntLimited("/options/dragtolerance/value", 2, 0, 100);
* 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
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
fill-column:99
End:
*/
fill-column:99
End:
*/
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :