Code

rearrange code to make rubberband a bit more interruptibility-proof
[inkscape.git] / src / splivarot.cpp
index 057ebac5a03f8de1deb618cd3aa180c720702770..8c9f6e819da481a2a27407fe2e8e996acc4b4ce1 100644 (file)
@@ -18,6 +18,7 @@
 #endif
 
 #include <vector>
+#include <glib/gmem.h>
 #include "xml/repr.h"
 #include "svg/svg.h"
 #include "sp-path.h"
 
 bool   Ancetre(Inkscape::XML::Node *a, Inkscape::XML::Node *who);
 
-void sp_selected_path_boolop(bool_op bop);
+void sp_selected_path_boolop(bool_op bop, const unsigned int verb=SP_VERB_NONE, const Glib::ustring description="");
 void sp_selected_path_do_offset(bool expand, double prefOffset);
 void sp_selected_path_create_offset_object(int expand, bool updating);
 
 void
 sp_selected_path_union()
 {
-    sp_selected_path_boolop(bool_op_union);
+    sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
 }
 
 void
 sp_selected_path_intersect()
 {
-    sp_selected_path_boolop(bool_op_inters);
+    sp_selected_path_boolop(bool_op_inters, SP_VERB_SELECTION_INTERSECT, _("Intersection"));
 }
 
 void
 sp_selected_path_diff()
 {
-    sp_selected_path_boolop(bool_op_diff);
+    sp_selected_path_boolop(bool_op_diff, SP_VERB_SELECTION_DIFF, _("Difference"));
 }
 
 void
 sp_selected_path_symdiff()
 {
-    sp_selected_path_boolop(bool_op_symdiff);
+    sp_selected_path_boolop(bool_op_symdiff, SP_VERB_SELECTION_SYMDIFF, _("Exclusion"));
 }
 void
 sp_selected_path_cut()
 {
-    sp_selected_path_boolop(bool_op_cut);
+    sp_selected_path_boolop(bool_op_cut, SP_VERB_SELECTION_CUT, _("Division"));
 }
 void
 sp_selected_path_slice()
 {
-    sp_selected_path_boolop(bool_op_slice);
+    sp_selected_path_boolop(bool_op_slice, SP_VERB_SELECTION_SLICE,  _("Cut path"));
 }
 
 
 // boolean operations
 // take the source paths from the file, do the operation, delete the originals and add the results
 void
-sp_selected_path_boolop(bool_op bop)
+sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustring description)
 {
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
 
@@ -105,7 +106,7 @@ sp_selected_path_boolop(bool_op bop)
 
     GSList *il = (GSList *) selection->itemList();
 
-    if (g_slist_length(il) < 2) {
+    if (g_slist_length(il) < 2 && bop != bool_op_union) {
         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 2 paths</b> to perform a boolean operation."));
         return;
     }
@@ -404,7 +405,8 @@ sp_selected_path_boolop(bool_op bop)
         {
             SP_OBJECT(l->data)->deleteObject();
         }
-        sp_document_done(sp_desktop_document(desktop));
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
+                         description);
         selection->clear();
 
         delete res;
@@ -430,8 +432,10 @@ sp_selected_path_boolop(bool_op bop)
     }
     gint pos = repr_source->position();
     Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
-    char const *id = repr_source->attribute("id");
-    char const *style = repr_source->attribute("style");
+    gchar const *id = repr_source->attribute("id");
+    gchar const *style = repr_source->attribute("style");
+    gchar const *mask = repr_source->attribute("mask");
+    gchar const *clip_path = repr_source->attribute("clip-path");
 
 
     // remove source paths
@@ -485,6 +489,11 @@ sp_selected_path_boolop(bool_op bop)
 
             Inkscape::XML::Node *repr = sp_repr_new("svg:path");
             repr->setAttribute("style", style);
+            if (mask)
+                repr->setAttribute("mask", mask);
+            if (clip_path)
+                repr->setAttribute("clip-path", clip_path);
+
             repr->setAttribute("d", d);
             g_free(d);
 
@@ -526,6 +535,12 @@ sp_selected_path_boolop(bool_op bop)
         Inkscape::XML::Node *repr = sp_repr_new("svg:path");
         repr->setAttribute("style", style);
 
+        if ( mask )
+            repr->setAttribute("mask", mask);
+
+        if ( clip_path )
+            repr->setAttribute("clip-path", clip_path);
+
         repr->setAttribute("d", d);
         g_free(d);
 
@@ -539,7 +554,7 @@ sp_selected_path_boolop(bool_op bop)
         Inkscape::GC::release(repr);
     }
 
-    sp_document_done(sp_desktop_document(desktop));
+    sp_document_done(sp_desktop_document(desktop), verb, description);
 
     delete res;
 }
@@ -553,8 +568,7 @@ sp_selected_path_outline()
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
 
     if (selection->isEmpty()) {
-        // TRANSLATORS: "to outline" means "to convert stroke to path"
-        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>path(s)</b> to outline."));
+        desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select <b>stroked path(s)</b> to convert stroke to path."));
         return;
     }
 
@@ -617,6 +631,8 @@ sp_selected_path_outline()
         float const scale = transform.expansion();
         gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
         SPStyle *i_style = SP_OBJECT(item)->style;
+        gchar const *mask = SP_OBJECT_REPR(item)->attribute("mask");
+        gchar const *clip_path = SP_OBJECT_REPR(item)->attribute("clip-path");
 
         float o_width, o_miter;
         JoinType o_join;
@@ -748,6 +764,10 @@ sp_selected_path_outline()
             repr->setAttribute("d", str);
             g_free(str);
 
+            if (mask)
+                repr->setAttribute("mask", mask);
+            if (clip_path)
+                repr->setAttribute("clip-path", clip_path);
 
             if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
 
@@ -829,10 +849,11 @@ sp_selected_path_outline()
     }
 
     if (did) {
-        sp_document_done(sp_desktop_document(desktop));
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_OUTLINE, 
+                         _("Convert stroke to path"));
     } else {
         // TRANSLATORS: "to outline" means "to convert stroke to path"
-        desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> to outline in the selection."));
+        desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No stroked paths</b> in the selection."));
         return;
     }
 }
@@ -1033,7 +1054,11 @@ sp_selected_path_create_offset_object(int expand, bool updating)
     {
         // pas vraiment de points sur le resultat
         // donc il ne reste rien
-        sp_document_done(sp_desktop_document(desktop));
+        sp_document_done(sp_desktop_document(desktop), 
+                         (updating ? SP_VERB_SELECTION_LINKED_OFFSET 
+                          : SP_VERB_SELECTION_DYNAMIC_OFFSET),
+                         (updating ? _("Create linked offset")
+                          : _("Create dynamic offset")));
         selection->clear();
 
         delete res;
@@ -1097,7 +1122,11 @@ sp_selected_path_create_offset_object(int expand, bool updating)
         selection->set(nitem);
     }
 
-    sp_document_done(sp_desktop_document(desktop));
+    sp_document_done(sp_desktop_document(desktop), 
+                     (updating ? SP_VERB_SELECTION_LINKED_OFFSET 
+                      : SP_VERB_SELECTION_DYNAMIC_OFFSET),
+                     (updating ? _("Create linked offset")
+                      : _("Create dynamic offset")));
 
     delete res;
     delete orig;
@@ -1339,7 +1368,9 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
     }
 
     if (did) {
-        sp_document_done(sp_desktop_document(desktop));
+        sp_document_done(sp_desktop_document(desktop), 
+                         (expand ? SP_VERB_SELECTION_OFFSET : SP_VERB_SELECTION_INSET),
+                         (expand ? _("Outset path") : _("Inset path")));
     } else {
         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to inset/outset in the selection."));
         return;
@@ -1347,10 +1378,18 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
 }
 
 
+static bool
+sp_selected_path_simplify_items(SPDesktop *desktop,
+                                Inkscape::Selection *selection, GSList *items,
+                                float threshold,  bool justCoalesce,
+                                float angleLimit, bool breakableAngles,
+                                bool modifySelection);
+
 
 //return true if we changed something, else false
 bool
-sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selection, SPItem *item,
+sp_selected_path_simplify_item(SPDesktop *desktop,
+                 Inkscape::Selection *selection, SPItem *item,
                  float threshold,  bool justCoalesce,
                  float angleLimit, bool breakableAngles,
                  gdouble size,     bool modifySelection)
@@ -1360,18 +1399,12 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio
 
     //If this is a group, do the children instead
     if (SP_IS_GROUP(item)) {
-
-        bool didSomething = false;
-
-        for ( GSList *children = sp_item_group_item_list(SP_GROUP(item));
-                 children  ; children = children->next) {
-
-            SPItem *child = (SPItem *) children->data;
-            didSomething |= sp_selected_path_simplify_item(desktop, selection, child, threshold, justCoalesce,
-                   angleLimit, breakableAngles, size, false);
-        }
-
-        return didSomething;
+        GSList *items = sp_item_group_item_list(SP_GROUP(item));
+        
+        return sp_selected_path_simplify_items(desktop, selection, items,
+                                               threshold, justCoalesce,
+                                               angleLimit, breakableAngles,
+                                               false);
     }
 
 
@@ -1400,6 +1433,8 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio
     sp_item_write_transform(item, SP_OBJECT_REPR(item), NR::identity());
 
     gchar *style = g_strdup(SP_OBJECT_REPR(item)->attribute("style"));
+    gchar *mask = g_strdup(SP_OBJECT_REPR(item)->attribute("mask"));
+    gchar *clip_path = g_strdup(SP_OBJECT_REPR(item)->attribute("clip-path"));
 
     Path *orig = Path_for_item(item, false);
     if (orig == NULL) {
@@ -1431,8 +1466,21 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio
 
     Inkscape::XML::Node *repr = sp_repr_new("svg:path");
 
+    // restore style, mask and clip-path
     repr->setAttribute("style", style);
+    g_free(style);
+
+    if ( mask ) {
+        repr->setAttribute("mask", mask);
+        g_free(mask);
+    }
+
+    if ( clip_path ) {
+        repr->setAttribute("clip-path", clip_path);
+        g_free(clip_path);
+    }
 
+    // path
     gchar *str = orig->svg_dump_path();
     repr->setAttribute("d", str);
     g_free(str);
@@ -1464,6 +1512,68 @@ sp_selected_path_simplify_item(SPDesktop *desktop, Inkscape::Selection *selectio
 }
 
 
+bool
+sp_selected_path_simplify_items(SPDesktop *desktop,
+                                Inkscape::Selection *selection, GSList *items,
+                                float threshold,  bool justCoalesce,
+                                float angleLimit, bool breakableAngles,
+                                bool modifySelection)
+{
+  bool simplifyIndividualPaths =
+    (bool) prefs_get_int_attribute("options.simplifyindividualpaths", "value", 0);
+  
+  gchar *simplificationType;
+  if (simplifyIndividualPaths) {
+    simplificationType = "individual paths";
+  } else {
+    simplificationType = "as a group";
+  }
+
+  bool didSomething = false;
+
+  NR::Rect selectionBbox = selection->bounds();
+  gdouble selectionSize  = L2(selectionBbox.dimensions());
+
+  gdouble simplifySize  = selectionSize;
+  
+  int pathsSimplified = 0;
+  int totalPathCount  = g_slist_length(items);
+  
+  desktop->disableInteraction();
+  
+  for (; items != NULL; items = items->next) {
+      SPItem *item = (SPItem *) items->data;
+      
+      if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
+          continue;
+
+      if (simplifyIndividualPaths) {
+          NR::Rect itemBbox = item->invokeBbox(sp_item_i2d_affine(item));        
+          simplifySize      = L2(itemBbox.dimensions());
+      }
+
+
+      pathsSimplified++;
+
+      if (pathsSimplified % 20 == 0) {
+        gchar *message = g_strdup_printf(_("Simplifying %s - <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
+        desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, message);
+        desktop->updateCanvasNow();
+      }
+
+      didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
+                          threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
+  }
+
+  desktop->enableInteraction();
+  
+  if (pathsSimplified > 20) {
+    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("Done - <b>%d</b> paths simplified."), pathsSimplified));
+  }
+  
+  return didSomething;
+}
+
 void
 sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
                        float angleLimit, bool breakableAngles)
@@ -1478,28 +1588,17 @@ sp_selected_path_simplify_selection(float threshold, bool justCoalesce,
         return;
     }
 
-    // remember selection size
-    NR::Rect bbox = selection->bounds();
-    gdouble size  = L2(bbox.dimensions());
-
-    bool didSomething = false;
-
-    //Loop through all of the items in the selection
-    for (GSList *items = g_slist_copy((GSList *) selection->itemList());
-                        items != NULL; items = items->next) {
-
-        SPItem *item = (SPItem *) items->data;
-
-        if (!(SP_IS_GROUP(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item)))
-            continue;
-
-        didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
-                           threshold, justCoalesce, angleLimit, breakableAngles, size, true);
-    }
+    GSList *items = g_slist_copy((GSList *) selection->itemList());
 
+    bool didSomething = sp_selected_path_simplify_items(desktop, selection,
+                                                        items, threshold,
+                                                        justCoalesce,
+                                                        angleLimit,
+                                                        breakableAngles, true);
 
     if (didSomething)
-        sp_document_done(sp_desktop_document(desktop));
+        sp_document_done(sp_desktop_document(desktop), SP_VERB_SELECTION_SIMPLIFY, 
+                         _("Simplify"));
     else
         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("<b>No paths</b> to simplify in the selection."));
 
@@ -1653,7 +1752,7 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
     }
 
     if ( doTransformation ) {
-        if ( bpath ) nr_free(bpath);
+        if ( bpath ) g_free(bpath);
     } else {
         sp_curve_unref(curve);
     }