Code

Fix for Bug 238113 (preserve title/desc for various Inkscape operations)
[inkscape.git] / src / path-chemistry.cpp
index d43d950fb999e3a593b37ac2c539fdf72ae8ad8e..32a7968b28b76b1ed68aa91e4a654cb63596c24e 100644 (file)
 #include "libnr/nr-path.h"
 #include "text-editing.h"
 #include "style.h"
-#include "inkscape.h"
 #include "desktop.h"
 #include "document.h"
 #include "message-stack.h"
 #include "selection.h"
 #include "desktop-handles.h"
 #include "box3d.h"
-
+#include <2geom/pathvector.h>
 #include "path-chemistry.h"
 
 /* Helper functions for sp_selected_path_to_curves */
-static void sp_selected_path_to_curves0(gboolean do_document_done, guint32 text_grouping_policy);
+static void sp_selected_path_to_curves0(SPDesktop *desktop, bool do_document_done, guint32 text_grouping_policy);
 static bool sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_select);
 
 enum {
@@ -53,10 +52,10 @@ enum {
 };
 
 void
-sp_selected_path_combine(void)
+sp_selected_path_combine(SPDesktop *desktop)
 {
-    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."));
@@ -68,18 +67,17 @@ sp_selected_path_combine(void)
     desktop->setWaitingCursor();
 
     GSList *items = g_slist_copy((GSList *) selection->itemList());
-    GSList *already_paths = NULL;
     GSList *to_paths = NULL;
     for (GSList *i = items; i != NULL; i = i->next) {
         SPItem *item = (SPItem *) i->data;
-        if (SP_IS_PATH(item))
-            already_paths = g_slist_prepend(already_paths, item);
-        else
+        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);
-    items = g_slist_concat (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);
@@ -149,7 +147,7 @@ sp_selected_path_combine(void)
         g_free(style);
 
         // set path data corresponding to new curve
-        gchar *dstring = sp_svg_write_path(SP_CURVE_BPATH(curve));
+        gchar *dstring = sp_svg_write_path(curve->get_pathvector());
         curve->unref();
         repr->setAttribute("d", dstring);
         if (path_effect)
@@ -180,10 +178,8 @@ sp_selected_path_combine(void)
 }
 
 void
-sp_selected_path_break_apart(void)
+sp_selected_path_break_apart(SPDesktop *desktop)
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
-
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     if (selection->isEmpty()) {
@@ -221,14 +217,14 @@ sp_selected_path_break_apart(void)
         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);
+        Geom::PathVector apv = curve->get_pathvector() * SP_ITEM(path)->transform;
 
         curve->unref();
 
         // it's going to resurrect as one of the pieces, so we delete without advertisement
         SP_OBJECT(item)->deleteObject(false);
 
-        curve = SPCurve::new_from_bpath(abp);
+        curve = new SPCurve(apv);
         g_assert(curve != NULL);
 
         GSList *list = curve->split();
@@ -242,7 +238,7 @@ 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);
@@ -285,20 +281,14 @@ sp_selected_path_break_apart(void)
 
 /* This function is an entry point from GUI */
 void
-sp_selected_path_to_curves(bool interactive)
+sp_selected_path_to_curves(SPDesktop *desktop, bool interactive)
 {
-    if (interactive) {
-        sp_selected_path_to_curves0(TRUE, SP_TOCURVE_INTERACTIVE);
-    } else {
-        sp_selected_path_to_curves0(false, 0);
-    }
+    sp_selected_path_to_curves0(desktop, interactive, interactive ? SP_TOCURVE_INTERACTIVE : 0);
 }
 
 static void
-sp_selected_path_to_curves0(gboolean interactive, guint32 /*text_grouping_policy*/)
+sp_selected_path_to_curves0(SPDesktop *desktop, bool interactive, guint32 /*text_grouping_policy*/)
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
-
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     if (selection->isEmpty()) {
@@ -349,6 +339,7 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
          items = items->next) {
 
         SPItem *item = SP_ITEM(items->data);
+       SPDocument *document = item->document;
 
         if (SP_IS_PATH(item) && !SP_PATH(item)->original_curve) {
             continue; // already a path, and no path effect
@@ -368,7 +359,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;
@@ -397,6 +388,10 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
         Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
         // remember id
         char const *id = SP_OBJECT_REPR(item)->attribute("id");
+        // remember title
+        gchar *title = item->title();
+        // remember description
+        gchar *desc = item->desc();
 
         // It's going to resurrect, so we delete without notifying listeners.
         SP_OBJECT(item)->deleteObject(false);
@@ -405,6 +400,16 @@ sp_item_list_to_curves(const GSList *items, GSList **selected, GSList **to_selec
         repr->setAttribute("id", id);
         // add the new repr to the parent
         parent->appendChild(repr);
+        SPObject* newObj = document->getObjectByRepr(repr);
+        if (title && newObj) {
+               newObj->setTitle(title);
+               g_free(title);
+        }
+        if (desc && newObj) {
+               newObj->setDesc(desc);
+               g_free(desc);
+        }
+
         // move to the saved position
         repr->setPosition(pos > 0 ? pos : 0);
 
@@ -435,7 +440,7 @@ 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->is_empty())
     {
         curve->unref();
         return NULL;
@@ -466,7 +471,7 @@ sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
     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);
     curve->unref();
@@ -476,10 +481,8 @@ sp_selected_item_to_curved_repr(SPItem *item, guint32 /*text_grouping_policy*/)
 
 // FIXME: THIS DOES NOT REVERSE THE NODETYPES ORDER!
 void
-sp_selected_path_reverse()
+sp_selected_path_reverse(SPDesktop *desktop)
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
-
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
     GSList *items = (GSList *) selection->itemList();
 
@@ -503,9 +506,9 @@ sp_selected_path_reverse()
         did = true;
         SPPath *path = SP_PATH(i->data);
 
-        SPCurve *rcurve = sp_path_get_curve_reference(path)->reverse();
+        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 {