Code

Improve file handling
[inkscape.git] / src / sp-rect.cpp
index d9caebd5a00eaeae4464aec2536b30deaf79c57a..55d2a3fde3b1fb14fff46d1e8446552f75ccb86e 100644 (file)
@@ -28,6 +28,8 @@
 #include "sp-rect.h"
 #include <glibmm/i18n.h>
 #include "xml/repr.h"
+#include "sp-guide.h"
+#include "prefs-utils.h"
 
 #define noRECT_VERBOSE
 
@@ -41,6 +43,7 @@ static Inkscape::XML::Node *sp_rect_write(SPObject *object, Inkscape::XML::Node
 
 static gchar *sp_rect_description(SPItem *item);
 static NR::Matrix sp_rect_set_transform(SPItem *item, NR::Matrix const &xform);
+static void sp_rect_convert_to_guides(SPItem *item);
 
 static void sp_rect_set_shape(SPShape *shape);
 static void sp_rect_snappoints(SPItem const *item, SnapPointsIter p);
@@ -86,6 +89,7 @@ sp_rect_class_init(SPRectClass *klass)
 
     item_class->description = sp_rect_description;
     item_class->set_transform = sp_rect_set_transform;
+    item_class->convert_to_guides = sp_rect_convert_to_guides;
     item_class->snappoints = sp_rect_snappoints; //override the default sp_shape_snappoints; see sp_rect_snappoints for details
 
     shape_class->set_shape = sp_rect_set_shape;
@@ -245,7 +249,10 @@ sp_rect_set_shape(SPShape *shape)
 {
     SPRect *rect = (SPRect *) shape;
 
-    if ((rect->height.computed < 1e-18) || (rect->width.computed < 1e-18)) return;
+    if ((rect->height.computed < 1e-18) || (rect->width.computed < 1e-18)) {
+        sp_shape_set_curve_insync(SP_SHAPE(rect), NULL, TRUE);
+        return;
+    }
 
     SPCurve *c = sp_curve_new();
 
@@ -578,6 +585,33 @@ static void sp_rect_snappoints(SPItem const *item, SnapPointsIter p)
     *p = NR::Point(rect->x.computed + rect->width.computed, rect->y.computed) * i2d;
 }
 
+void
+sp_rect_convert_to_guides(SPItem *item) {
+    SPRect *rect = SP_RECT(item);
+
+    if (prefs_get_int_attribute("tools.shapes.rect", "convertguides", 1) == 0) {
+        sp_item_convert_to_guides(SP_ITEM(rect));
+        return;
+    }
+
+    SPDocument *doc = SP_OBJECT_DOCUMENT(rect);
+    std::list<std::pair<Geom::Point, Geom::Point> > pts;
+
+    NR::Matrix const i2d (sp_item_i2d_affine(SP_ITEM(rect)));
+
+    NR::Point A1(NR::Point(rect->x.computed, rect->y.computed) * i2d);
+    NR::Point A2(NR::Point(rect->x.computed, rect->y.computed + rect->height.computed) * i2d);
+    NR::Point A3(NR::Point(rect->x.computed + rect->width.computed, rect->y.computed + rect->height.computed) * i2d);
+    NR::Point A4(NR::Point(rect->x.computed + rect->width.computed, rect->y.computed) * i2d);
+
+    pts.push_back(std::make_pair(A1.to_2geom(), A2.to_2geom()));
+    pts.push_back(std::make_pair(A2.to_2geom(), A3.to_2geom()));
+    pts.push_back(std::make_pair(A3.to_2geom(), A4.to_2geom()));
+    pts.push_back(std::make_pair(A4.to_2geom(), A1.to_2geom()));
+
+    sp_guide_pt_pairs_to_guides(doc, pts);
+}
+
 /*
   Local Variables:
   mode:c++