Code

i18n. Color palette items are now translatable (see Bug #667402, Color palette not...
[inkscape.git] / src / ui / tool / path-manipulator.cpp
index 0b0254108d37e2851a7fa45a1189cd098902b109..41be81d4f38da06bfce547c51354f909876de687 100644 (file)
@@ -319,6 +319,39 @@ void PathManipulator::insertNodes()
     }
 }
 
+/** 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)
 {
@@ -598,10 +631,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;
     }
 
@@ -720,6 +753,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;
@@ -728,6 +762,90 @@ 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;
+    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)
 {
@@ -983,6 +1101,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.
@@ -1187,11 +1306,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);
@@ -1206,17 +1325,11 @@ bool PathManipulator::_nodeClicked(Node *n, GdkEventButton *event)
         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));
+            update();
+            _commit(_("Cycle node type"));
         }
-        update();
-        _commit(_("Cycle node type"));
         return true;
     }
     return false;
@@ -1304,6 +1417,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)
@@ -1321,7 +1441,10 @@ void PathManipulator::_updateDragPoint(Geom::Point const &evp)
     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);