Code

plumb XML::Documents in everywhere
[inkscape.git] / src / path-chemistry.cpp
index cfdbc0b54d276472e500be1762e1ffb3e6047da8..aac75756e305ffc7098b146b00f97a15cd9be940 100644 (file)
@@ -57,6 +57,7 @@ sp_selected_path_combine(void)
 {
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
+    SPDocument *doc = sp_desktop_document(desktop);
     
     if (g_slist_length((GSList *) selection->itemList()) < 2) {
         sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>at least two objects</b> to combine."));
@@ -67,19 +68,30 @@ sp_selected_path_combine(void)
     // set "busy" cursor
     desktop->setWaitingCursor();
 
-    sp_selected_path_to_curves0(FALSE, 0);
-
     GSList *items = g_slist_copy((GSList *) selection->itemList());
+    GSList *to_paths = NULL;
+    for (GSList *i = items; i != NULL; i = i->next) {
+        SPItem *item = (SPItem *) i->data;
+        if (!SP_IS_PATH(item))
+            to_paths = g_slist_prepend(to_paths, item);
+    }
+    GSList *converted = NULL;
+    bool did = sp_item_list_to_curves(to_paths, &items, &converted);
+    g_slist_free(to_paths);
+    for (GSList *i = converted; i != NULL; i = i->next)
+        items = g_slist_prepend(items, doc->getObjectByRepr((Inkscape::XML::Node*)(i->data)));
+
     items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
     items = g_slist_reverse(items);
 
-    // remember the position, id and style of the topmost path, they will be assigned to the combined one
+    // remember the position, id, transform and style of the topmost path, they will be assigned to the combined one
     gint position = 0;
     char const *id = NULL;
+    char const *transform = NULL;
     gchar *style = NULL;
+    gchar *path_effect = NULL;
 
     SPCurve* curve = 0;
-    bool did = false;
     SPItem *first = NULL;
     Inkscape::XML::Node *parent = NULL; 
 
@@ -90,20 +102,22 @@ sp_selected_path_combine(void)
             continue;
         did = true;
 
-        SPCurve *c = sp_shape_get_curve(SP_SHAPE(item));
+        SPCurve *c = sp_path_get_curve_for_edit(SP_PATH(item));
         if (first == NULL) {  // this is the topmost path
             first = item;
             parent = SP_OBJECT_REPR(first)->parent();
             position = SP_OBJECT_REPR(first)->position();
             id = SP_OBJECT_REPR(first)->attribute("id");
+            transform = SP_OBJECT_REPR(first)->attribute("transform");
             // FIXME: merge styles of combined objects instead of using the first one's style
             style = g_strdup(SP_OBJECT_REPR(first)->attribute("style"));
-            sp_curve_transform(c, item->transform);
+            path_effect = g_strdup(SP_OBJECT_REPR(first)->attribute("inkscape:path-effect"));
+            //c->transform(item->transform);
             curve = c;
         } else {
-            sp_curve_transform(c, item->getRelativeTransform(SP_OBJECT(first)));
-            sp_curve_append(curve, c, false);
-            sp_curve_unref(c);
+            c->transform(item->getRelativeTransform(SP_OBJECT(first)));
+            curve->append(c, false);
+            c->unref();
         }
 
         // unless this is the topmost object,
@@ -128,18 +142,23 @@ sp_selected_path_combine(void)
         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
         Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
-        // restore id and style
+        // restore id, transform, path effect, and style
         repr->setAttribute("id", id);
-
+        if (transform) repr->setAttribute("transform", transform);
         repr->setAttribute("style", style);
         g_free(style);
 
         // set path data corresponding to new curve
-        gchar *dstring = sp_svg_write_path(SP_CURVE_BPATH(curve));
-        sp_curve_unref(curve);
+        gchar *dstring = sp_svg_write_path(curve->get_pathvector());
+        curve->unref();
         repr->setAttribute("d", dstring);
+        if (path_effect)
+            repr->setAttribute("inkscape:original-d", dstring);
         g_free(dstring);
 
+        repr->setAttribute("inkscape:path-effect", path_effect);
+        g_free(path_effect);
+
         // add the new group to the parent of the topmost
         parent->appendChild(repr);
 
@@ -189,7 +208,7 @@ sp_selected_path_break_apart(void)
 
         SPPath *path = SP_PATH(item);
 
-        SPCurve *curve = sp_shape_get_curve(SP_SHAPE(path));
+        SPCurve *curve = sp_path_get_curve_for_edit(SP_PATH(path));
         if (curve == NULL)
             continue;
 
@@ -200,20 +219,21 @@ sp_selected_path_break_apart(void)
         char const *id = SP_OBJECT_REPR(item)->attribute("id");
 
         gchar *style = g_strdup(SP_OBJECT(item)->repr->attribute("style"));
+        gchar *path_effect = g_strdup(SP_OBJECT(item)->repr->attribute("inkscape:path-effect"));
 
         NArtBpath *abp = nr_artpath_affine(SP_CURVE_BPATH(curve), (SP_ITEM(path))->transform);
 
-        sp_curve_unref(curve);
+        curve->unref();
 
         // it's going to resurrect as one of the pieces, so we delete without advertisement
         SP_OBJECT(item)->deleteObject(false);
 
-        curve = sp_curve_new_from_bpath(abp);
+        curve = SPCurve::new_from_bpath(abp);
         g_assert(curve != NULL);
 
-        GSList *list = sp_curve_split(curve);
+        GSList *list = curve->split();
 
-        sp_curve_unref(curve);
+        curve->unref();
 
         GSList *reprs = NULL;
         for (GSList *l = list; l != NULL; l = l->next) {
@@ -222,10 +242,14 @@ sp_selected_path_break_apart(void)
             Inkscape::XML::Node *repr = parent->document()->createElement("svg:path");
             repr->setAttribute("style", style);
 
-            gchar *str = sp_svg_write_path(SP_CURVE_BPATH(curve));
+            gchar *str = sp_svg_write_path(curve->get_pathvector());
             repr->setAttribute("d", str);
+            if (path_effect)
+                repr->setAttribute("inkscape:original-d", str);
             g_free(str);
 
+            repr->setAttribute("inkscape:path-effect", path_effect);
+
             // add the new repr to the parent
             parent->appendChild(repr);
 
@@ -332,7 +356,7 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
 
         if (SP_IS_BOX3D(item)) {
             // convert 3D box to ordinary group of paths; replace the old element in 'selected' with the new group
-            Inkscape::XML::Node *repr = box3d_convert_to_group(SP_BOX3D(item));
+            Inkscape::XML::Node *repr = SP_OBJECT_REPR(box3d_convert_to_group(SP_BOX3D(item)));
             
             if (repr) {
                 *to_select = g_slist_prepend (*to_select, repr);
@@ -344,7 +368,7 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
         }
         
         if (SP_IS_GROUP(item)) {
-            sp_lpe_item_remove_path_effect(SP_LPE_ITEM(item), true);
+            sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(item), true);
             GSList *item_list = sp_item_group_item_list(SP_GROUP(item));
             
             GSList *item_to_select = NULL;
@@ -411,9 +435,9 @@ sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
 
     // Prevent empty paths from being added to the document
     // otherwise we end up with zomby markup in the SVG file
-    if(curve->end <= 0)
+    if(curve->get_length() <= 0)
     {
-        sp_curve_unref(curve);
+        curve->unref();
         return NULL;
     }
 
@@ -438,14 +462,14 @@ sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
         repr->setAttribute("clip-path", clip_path_str);
 
     /* Rotation center */
-    sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
-    sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
+    repr->setAttribute("inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"), false);
+    repr->setAttribute("inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"), false);
 
     /* Definition */
-    gchar *def_str = sp_svg_write_path(SP_CURVE_BPATH(curve));
+    gchar *def_str = sp_svg_write_path(curve->get_pathvector());
     repr->setAttribute("d", def_str);
     g_free(def_str);
-    sp_curve_unref(curve);
+    curve->unref();
     return repr;
 }
 
@@ -479,9 +503,9 @@ sp_selected_path_reverse()
         did = true;
         SPPath *path = SP_PATH(i->data);
 
-        SPCurve *rcurve = sp_curve_reverse(sp_path_get_curve_reference(path));
+        SPCurve *rcurve = sp_path_get_curve_reference(path)->create_reverse();
 
-        gchar *str = sp_svg_write_path(SP_CURVE_BPATH(rcurve));
+        gchar *str = sp_svg_write_path(rcurve->get_pathvector());
         if ( sp_lpe_item_has_path_effect_recursive(SP_LPE_ITEM(path)) ) {
             SP_OBJECT_REPR(path)->setAttribute("inkscape:original-d", str);
         } else {
@@ -489,7 +513,7 @@ sp_selected_path_reverse()
         }
         g_free(str);
 
-        sp_curve_unref(rcurve);
+        rcurve->unref();
     }
 
     desktop->clearWaitingCursor();