Code

cleanup: Remove some commented-out code.
[inkscape.git] / src / sp-item.cpp
index 33bbe9e05bda723775054098279c5cd5ce21e471..6aae4609e6908add2bf5aa81780a1e93be2104ae 100644 (file)
@@ -25,7 +25,7 @@
 #endif
 
 
-
+#include "sp-item.h"
 #include "svg/svg.h"
 #include "print.h"
 #include "display/nr-arena.h"
@@ -134,8 +134,8 @@ sp_item_init(SPItem *item)
 
     item->sensitive = TRUE;
 
-    item->r_cx = 0;
-    item->r_cx = 0;
+    item->transform_center_x = 0;
+    item->transform_center_y = 0;
 
     item->transform = NR::identity();
 
@@ -234,6 +234,42 @@ SPItem::setExplicitlyHidden(bool const val) {
     this->updateRepr();
 }
 
+/**
+ * Sets the transform_center_x and transform_center_y properties to retain the rotation centre
+ */
+void
+SPItem::setCenter(NR::Point object_centre) {
+    NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
+    if (!bbox.isEmpty()) {
+        transform_center_x = object_centre[NR::X] - bbox.midpoint()[NR::X];
+        if (fabs(transform_center_x) < 1e-5) // rounding error
+            transform_center_x = 0;
+        transform_center_y = object_centre[NR::Y] - bbox.midpoint()[NR::Y];
+        if (fabs(transform_center_y) < 1e-5) // rounding error
+            transform_center_y = 0;
+    }
+}
+
+void
+SPItem::unsetCenter() {
+    transform_center_x = 0;
+    transform_center_y = 0;
+}
+
+bool SPItem::isCenterSet() {
+    return (transform_center_x != 0 || transform_center_y != 0);
+}
+
+NR::Point SPItem::getCenter() {
+    NR::Rect bbox = invokeBbox(sp_item_i2d_affine(this));
+    if (!bbox.isEmpty()) {
+        return bbox.midpoint() + NR::Point (this->transform_center_x, this->transform_center_y);
+    } else {
+        return NR::Point (0, 0); // something's wrong!
+    }
+}
+
+
 namespace {
 
 bool is_item(SPObject const &object) {
@@ -313,8 +349,8 @@ sp_item_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
     sp_object_read_attr(object, "mask");
     sp_object_read_attr(object, "sodipodi:insensitive");
     sp_object_read_attr(object, "sodipodi:nonprintable");
-       sp_object_read_attr(object, "inkscape:r_cx");
-    sp_object_read_attr(object, "inkscape:r_cy");
+    sp_object_read_attr(object, "inkscape:transform-center-x");
+    sp_object_read_attr(object, "inkscape:transform-center-y");
     sp_object_read_attr(object, "inkscape:connector-avoid");
 
     if (((SPObjectClass *) (parent_class))->build) {
@@ -416,6 +452,20 @@ sp_item_set(SPObject *object, unsigned key, gchar const *value)
         case SP_ATTR_CONNECTOR_AVOID:
             item->avoidRef->setAvoid(value);
             break;
+        case SP_ATTR_TRANSFORM_CENTER_X:
+            if (value) {
+                item->transform_center_x = g_strtod(value, NULL);
+            } else {
+                item->transform_center_x = 0;
+            }
+            break;
+        case SP_ATTR_TRANSFORM_CENTER_Y:
+            if (value) {
+                item->transform_center_y = g_strtod(value, NULL);
+            } else {
+                item->transform_center_y = 0;
+            }
+            break;
         default:
             if (SP_ATTRIBUTE_IS_CSS(key)) {
                 sp_style_read_from_object(object->style, object);
@@ -453,6 +503,7 @@ clip_ref_changed(SPObject *old_clip, SPObject *clip, SPItem *item)
             nr_arena_item_set_clip(v->arenaitem, ai);
             nr_arena_item_unref(ai);
             sp_clippath_set_bbox(SP_CLIPPATH(clip), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
+            SP_OBJECT(clip)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
     }
 }
@@ -480,6 +531,7 @@ mask_ref_changed(SPObject *old_mask, SPObject *mask, SPItem *item)
             nr_arena_item_set_mask(v->arenaitem, ai);
             nr_arena_item_unref(ai);
             sp_mask_set_bbox(SP_MASK(mask), NR_ARENA_ITEM_GET_KEY(v->arenaitem), &bbox);
+            SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
     }
 }
@@ -583,8 +635,14 @@ sp_item_write(SPObject *const object, Inkscape::XML::Node *repr, guint flags)
 
     if (flags & SP_OBJECT_WRITE_EXT) {
         repr->setAttribute("sodipodi:insensitive", ( item->sensitive ? NULL : "true" ));
-        repr->setAttribute("inkscape:r_cx", ( item->r_cx ? NULL : "true" ));
-        repr->setAttribute("inkscape:r_cy", ( item->r_cy ? NULL : "true" ));
+        if (item->transform_center_x != 0)
+            sp_repr_set_svg_double (repr, "inkscape:transform-center-x", item->transform_center_x);
+        else
+            repr->setAttribute ("inkscape:transform-center-x", NULL);
+        if (item->transform_center_y != 0)
+            sp_repr_set_svg_double (repr, "inkscape:transform-center-y", item->transform_center_y);
+        else
+            repr->setAttribute ("inkscape:transform-center-y", NULL);
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
@@ -622,6 +680,13 @@ sp_item_invoke_bbox_full(SPItem const *item, NRRect *bbox, NR::Matrix const &tra
     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox) {
         ((SPItemClass *) G_OBJECT_GET_CLASS(item))->bbox(item, bbox, transform, flags);
     }
+
+    // crop the bbox by clip path, if any
+    if (item->clip_ref->getObject()) {
+        NRRect b;
+        sp_clippath_get_bbox(SP_CLIPPATH(item->clip_ref->getObject()), &b, transform, flags);
+        nr_rect_d_intersect (bbox, bbox, &b);
+    }
 }
 
 unsigned sp_item_pos_in_parent(SPItem *item)
@@ -723,7 +788,18 @@ sp_item_description(SPItem *item)
     g_assert(SP_IS_ITEM(item));
 
     if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->description) {
-        return ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
+        gchar *s = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->description(item);
+        if (s && item->clip_ref->getObject()) {
+            gchar *snew = g_strdup_printf ("%s; <i>clipped</i>", s);
+            g_free (s);
+            s = snew;
+        }
+        if (s && item->mask_ref->getObject()) {
+            gchar *snew = g_strdup_printf ("%s; <i>masked</i>", s);
+            g_free (s);
+            s = snew;
+        }
+        return s;
     }
 
     g_assert_not_reached();
@@ -766,18 +842,42 @@ sp_item_invoke_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags)
         nr_arena_item_set_visible(ai, !item->isHidden());
         nr_arena_item_set_sensitive(ai, item->sensitive);
         if (item->clip_ref->getObject()) {
-            NRArenaItem *ac;
-            if (!item->display->arenaitem->key) NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
-            ac = sp_clippath_show(item->clip_ref->getObject(), arena, NR_ARENA_ITEM_GET_KEY(item->display->arenaitem));
+            SPClipPath *cp = item->clip_ref->getObject();
+
+            if (!item->display->arenaitem->key) {
+                NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
+            }
+            int clip_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
+
+            // Show and set clip
+            NRArenaItem *ac = sp_clippath_show(cp, arena, clip_key);
             nr_arena_item_set_clip(ai, ac);
             nr_arena_item_unref(ac);
+
+            // Update bbox, in case the clip uses bbox units
+            NRRect bbox;
+            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_clippath_set_bbox(SP_CLIPPATH(cp), clip_key, &bbox);
+            SP_OBJECT(cp)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
         if (item->mask_ref->getObject()) {
-            NRArenaItem *ac;
-            if (!item->display->arenaitem->key) NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
-            ac = sp_mask_show(item->mask_ref->getObject(), arena, NR_ARENA_ITEM_GET_KEY(item->display->arenaitem));
+            SPMask *mask = item->mask_ref->getObject();
+
+            if (!item->display->arenaitem->key) {
+                NR_ARENA_ITEM_SET_KEY(item->display->arenaitem, sp_item_display_key_new(3));
+            }
+            int mask_key = NR_ARENA_ITEM_GET_KEY(item->display->arenaitem);
+
+            // Show and set mask
+            NRArenaItem *ac = sp_mask_show(mask, arena, mask_key);
             nr_arena_item_set_mask(ai, ac);
             nr_arena_item_unref(ac);
+
+            // Update bbox, in case the mask uses bbox units
+            NRRect bbox;
+            sp_item_invoke_bbox(item, &bbox, NR::identity(), TRUE);
+            sp_mask_set_bbox(SP_MASK(mask), mask_key, &bbox);
+            SP_OBJECT(mask)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
         }
         NR_ARENA_ITEM_SET_DATA(ai, item);
     }
@@ -889,6 +989,7 @@ sp_item_adjust_stroke (SPItem *item, gdouble ex)
     if (style && style->stroke.type != SP_PAINT_TYPE_NONE && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
 
         style->stroke_width.computed *= ex;
+        style->stroke_width.set = TRUE;
 
         if (style->stroke_dash.n_dash != 0) {
             int i;
@@ -1046,10 +1147,11 @@ sp_item_write_transform(SPItem *item, Inkscape::XML::Node *repr, NR::Matrix cons
         sp_item_adjust_paint_recursive (item, NR::identity(), NR::identity(), false);
     }
 
-    // run the object's set_transform if transforms are stored optimized
+    // run the object's set_transform if transforms are stored optimized and there's no clippath or mask
     gint preserve = prefs_get_int_attribute("options.preservetransform", "value", 0);
     NR::Matrix transform_attr (transform);
-    if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform && !preserve) {
+    if (((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform 
+                && !preserve && !item->clip_ref->getObject() && !item->mask_ref->getObject()) {
         transform_attr = ((SPItemClass *) G_OBJECT_GET_CLASS(item))->set_transform(item, transform);
     }
     sp_item_set_item_transform(item, transform_attr);