Code

Remove 'default scale origin' from selector tool's preferences
[inkscape.git] / src / splivarot.cpp
index 8c9f6e819da481a2a27407fe2e8e996acc4b4ce1..f1ce029f4f36b95c17bcb3c718e2bf9234dbd678 100644 (file)
@@ -23,7 +23,7 @@
 #include "svg/svg.h"
 #include "sp-path.h"
 #include "sp-shape.h"
-#include "sp-marker.h"
+#include "marker.h"
 #include "enums.h"
 #include "sp-text.h"
 #include "sp-item-group.h"
@@ -66,6 +66,12 @@ sp_selected_path_union()
     sp_selected_path_boolop(bool_op_union, SP_VERB_SELECTION_UNION, _("Union"));
 }
 
+void
+sp_selected_path_union_skip_undo()
+{
+    sp_selected_path_boolop(bool_op_union, SP_VERB_NONE, _("Union"));
+}
+
 void
 sp_selected_path_intersect()
 {
@@ -103,13 +109,18 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
 
     Inkscape::Selection *selection = sp_desktop_selection(desktop);
-
+    
     GSList *il = (GSList *) selection->itemList();
-
-    if (g_slist_length(il) < 2 && bop != bool_op_union) {
+    
+    // allow union on a single object for the purpose of removing self overlapse (svn log, revision 13334)
+    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;
     }
+    else if ( g_slist_length(il) < 1 ) {
+        desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, _("Select <b>at least 1 path</b> to perform a boolean union."));
+        return;
+    }
 
     if (g_slist_length(il) > 2) {
         if (bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
@@ -414,22 +425,37 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         return;
     }
 
-    // remember important aspects of the source path, to be restored
-    Inkscape::XML::Node *repr_source;
+    // get the source path object
+    SPObject *source;
     if ( bop == bool_op_diff || bop == bool_op_symdiff || bop == bool_op_cut || bop == bool_op_slice ) {
         if (reverseOrderForOp) {
-             repr_source = SP_OBJECT_REPR(il->data);
+             source = SP_OBJECT(il->data);
         } else {
-             repr_source = SP_OBJECT_REPR(il->next->data);
+             source = SP_OBJECT(il->next->data);
         }
     } else {
         // find out the bottom object
         GSList *sorted = g_slist_copy((GSList *) selection->reprList());
 
         sorted = g_slist_sort(sorted, (GCompareFunc) sp_repr_compare_position);
-        repr_source = ((Inkscape::XML::Node *) sorted->data);
+
+        source = sp_desktop_document(desktop)->
+            getObjectByRepr((Inkscape::XML::Node *)sorted->data);
+
         g_slist_free(sorted);
     }
+
+    // adjust style properties that depend on a possible transform in the source object in order
+    // to get a correct style attribute for the new path
+    SPItem* item_source = SP_ITEM(source);
+    NR::Matrix i2root = sp_item_i2root_affine(item_source);
+    sp_item_adjust_stroke(item_source, i2root.expansion());
+    sp_item_adjust_pattern(item_source, i2root);
+    sp_item_adjust_gradient(item_source, i2root);
+
+    Inkscape::XML::Node *repr_source = SP_OBJECT_REPR(source);
+
+    // remember important aspects of the source path, to be restored
     gint pos = repr_source->position();
     Inkscape::XML::Node *parent = sp_repr_parent(repr_source);
     gchar const *id = repr_source->attribute("id");
@@ -437,7 +463,6 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     gchar const *mask = repr_source->attribute("mask");
     gchar const *clip_path = repr_source->attribute("clip-path");
 
-
     // remove source paths
     selection->clear();
     for (GSList *l = il; l != NULL; l = l->next) {
@@ -455,11 +480,7 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     // premultiply by the inverse of parent's repr
     SPItem *parent_item = SP_ITEM(sp_desktop_document(desktop)->getObjectByRepr(parent));
     NR::Matrix local = sp_item_i2doc_affine(parent_item);
-    gchar affinestr[80];
-    gchar *transform = NULL;
-    if (!local.test_identity() && sp_svg_transform_write(affinestr, 79, local.inverse())) {
-        transform = affinestr;
-    }
+    gchar *transform = sp_svg_transform_write(local);
 
     // now that we have the result, add it on the canvas
     if ( bop == bool_op_cut || bop == bool_op_slice ) {
@@ -487,7 +508,8 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         for (int i=0;i<nbRP;i++) {
             gchar *d = resPath[i]->svg_dump_path();
 
-            Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
             repr->setAttribute("style", style);
             if (mask)
                 repr->setAttribute("mask", mask);
@@ -532,7 +554,8 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
     } else {
         gchar *d = res->svg_dump_path();
 
-        Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+        Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
         repr->setAttribute("style", style);
 
         if ( mask )
@@ -554,7 +577,11 @@ sp_selected_path_boolop(bool_op bop, const unsigned int verb, const Glib::ustrin
         Inkscape::GC::release(repr);
     }
 
-    sp_document_done(sp_desktop_document(desktop), verb, description);
+    g_free(transform);
+
+    if (verb != SP_VERB_NONE) {
+        sp_document_done(sp_desktop_document(desktop), verb, description);
+    }
 
     delete res;
 }
@@ -750,7 +777,8 @@ sp_selected_path_outline()
 
         if (res->descr_cmd.size() > 1) { // if there's 0 or 1 node left, drop this path altogether
 
-            Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
             // restore old style
             repr->setAttribute("style", style);
@@ -771,7 +799,8 @@ sp_selected_path_outline()
 
             if (SP_IS_SHAPE(item) && sp_shape_has_markers (SP_SHAPE(item))) {
 
-                Inkscape::XML::Node *g_repr = sp_repr_new("svg:g");
+                Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+                Inkscape::XML::Node *g_repr = xml_doc->createElement("svg:g");
 
                 // add the group to the parent
                 parent->appendChild(g_repr);
@@ -802,7 +831,7 @@ sp_selected_path_outline()
                             tr = marker_item->transform * marker->c2p * tr * transform;
 
                             if (SP_OBJECT_REPR(marker_item)) {
-                                Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate();
+                                Inkscape::XML::Node *m_repr = SP_OBJECT_REPR(marker_item)->duplicate(xml_doc);
                                 g_repr->appendChild(m_repr);
                                 SPItem *marker_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(m_repr);
                                 sp_item_write_transform(marker_item, m_repr, tr);
@@ -1072,7 +1101,8 @@ sp_selected_path_create_offset_object(int expand, bool updating)
 
         tstr[79] = '\0';
 
-        repr = sp_repr_new("svg:path");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+        repr = xml_doc->createElement("svg:path");
         repr->setAttribute("sodipodi:type", "inkscape:offset");
         sp_repr_set_svg_double(repr, "inkscape:radius", ( expand > 0
                                                           ? o_width
@@ -1337,7 +1367,8 @@ sp_selected_path_do_offset(bool expand, double prefOffset)
 
             tstr[79] = '\0';
 
-            Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+            Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+            Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
             repr->setAttribute("style", style);
 
@@ -1464,7 +1495,8 @@ sp_selected_path_simplify_item(SPDesktop *desktop,
         orig->Simplify(threshold * size);
     }
 
-    Inkscape::XML::Node *repr = sp_repr_new("svg:path");
+    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
+    Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
 
     // restore style, mask and clip-path
     repr->setAttribute("style", style);
@@ -1524,22 +1556,26 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
   
   gchar *simplificationType;
   if (simplifyIndividualPaths) {
-    simplificationType = "individual paths";
+      simplificationType = _("Simplifying paths (separately):");
   } else {
-    simplificationType = "as a group";
+      simplificationType = _("Simplifying paths:");
   }
 
   bool didSomething = false;
 
-  NR::Rect selectionBbox = selection->bounds();
-  gdouble selectionSize  = L2(selectionBbox.dimensions());
+  NR::Maybe<NR::Rect> selectionBbox = selection->bounds();
+  if (!selectionBbox) {
+    return false;
+  }
+  gdouble selectionSize  = L2(selectionBbox->dimensions());
 
   gdouble simplifySize  = selectionSize;
   
   int pathsSimplified = 0;
   int totalPathCount  = g_slist_length(items);
   
-  desktop->disableInteraction();
+  // set "busy" cursor
+  desktop->setWaitingCursor();
   
   for (; items != NULL; items = items->next) {
       SPItem *item = (SPItem *) items->data;
@@ -1548,27 +1584,29 @@ sp_selected_path_simplify_items(SPDesktop *desktop,
           continue;
 
       if (simplifyIndividualPaths) {
-          NR::Rect itemBbox = item->invokeBbox(sp_item_i2d_affine(item));        
-          simplifySize      = L2(itemBbox.dimensions());
+          NR::Maybe<NR::Rect> itemBbox = item->getBounds(sp_item_i2d_affine(item));        
+          if (itemBbox) {
+              simplifySize      = L2(itemBbox->dimensions());
+          } else {
+              simplifySize      = 0;
+          }
       }
 
-
       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();
+        gchar *message = g_strdup_printf(_("%s <b>%d</b> of <b>%d</b> paths simplified..."), simplificationType, pathsSimplified, totalPathCount);
+        desktop->messageStack()->flash(Inkscape::IMMEDIATE_MESSAGE, message);
       }
 
       didSomething |= sp_selected_path_simplify_item(desktop, selection, item,
                           threshold, justCoalesce, angleLimit, breakableAngles, simplifySize, modifySelection);
   }
 
-  desktop->enableInteraction();
-  
+  desktop->clearWaitingCursor();
+
   if (pathsSimplified > 20) {
-    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("Done - <b>%d</b> paths simplified."), pathsSimplified));
+    desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, g_strdup_printf(_("<b>%d</b> paths simplified."), pathsSimplified));
   }
   
   return didSomething;
@@ -1697,6 +1735,17 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
         bpath=SP_CURVE_BPATH(curve);
     }
 
+    Path *dest = bpath_to_Path(bpath);
+
+    if ( doTransformation ) {
+        if ( bpath ) g_free(bpath);
+    } else {
+        sp_curve_unref(curve);
+    }
+    return dest;
+}
+
+Path *bpath_to_Path(NArtBpath const *bpath) {
     Path *dest = new Path;
     dest->SetBackData(false);
     {
@@ -1750,12 +1799,6 @@ Path_for_item(SPItem *item, bool doTransformation, bool transformFull)
         if (closed)
             dest->Close();
     }
-
-    if ( doTransformation ) {
-        if ( bpath ) g_free(bpath);
-    } else {
-        sp_curve_unref(curve);
-    }
     return dest;
 }