Code

NR::Maybe => boost::optional
[inkscape.git] / src / sp-item-group.cpp
index 4fc1608cfdef380889a5d36566e29e80defbfe23..186b766635f1115ee63e0817401f4fd83af249c5 100644 (file)
 #endif
 
 #include <glibmm/i18n.h>
+#include <cstring>
+#include <string>
 
 #include "display/nr-arena-group.h"
+#include "display/curve.h"
 #include "libnr/nr-matrix-ops.h"
 #include "libnr/nr-matrix-fns.h"
 #include "xml/repr.h"
 #include "sp-mask.h"
 #include "sp-path.h"
 #include "box3d.h"
-#include "box3d-side.h"
+#include "persp3d.h"
+#include "inkscape.h"
+#include "desktop-handles.h"
+#include "selection.h"
+#include "live_effects/lpeobject.h"
+#include "live_effects/lpeobject-reference.h"
 
 static void sp_group_class_init (SPGroupClass *klass);
 static void sp_group_init (SPGroup *group);
@@ -49,17 +57,21 @@ static void sp_group_remove_child (SPObject * object, Inkscape::XML::Node * chil
 static void sp_group_order_changed (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * old_ref, Inkscape::XML::Node * new_ref);
 static void sp_group_update (SPObject *object, SPCtx *ctx, guint flags);
 static void sp_group_modified (SPObject *object, guint flags);
-static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_group_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_group_set(SPObject *object, unsigned key, char const *value);
 
 static void sp_group_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
 static void sp_group_print (SPItem * item, SPPrintContext *ctx);
 static gchar * sp_group_description (SPItem * item);
+static NR::Matrix sp_group_set_transform(SPItem *item, NR::Matrix const &xform);
 static NRArenaItem *sp_group_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
 static void sp_group_hide (SPItem * item, unsigned int key);
 static void sp_group_snappoints (SPItem const *item, SnapPointsIter p);
 
-static SPItemClass * parent_class;
+static void sp_group_update_patheffect(SPLPEItem *lpeitem, bool write);
+static void sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup);
+
+static SPLPEItemClass * parent_class;
 
 GType
 sp_group_get_type (void)
@@ -78,7 +90,7 @@ sp_group_get_type (void)
                        (GInstanceInitFunc) sp_group_init,
                        NULL,   /* value_table */
                };
-               group_type = g_type_register_static (SP_TYPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
+               group_type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPGroup", &group_info, (GTypeFlags)0);
        }
        return group_type;
 }
@@ -89,12 +101,14 @@ sp_group_class_init (SPGroupClass *klass)
        GObjectClass * object_class;
        SPObjectClass * sp_object_class;
        SPItemClass * item_class;
+       SPLPEItemClass * lpe_item_class;
 
        object_class = (GObjectClass *) klass;
        sp_object_class = (SPObjectClass *) klass;
        item_class = (SPItemClass *) klass;
+       lpe_item_class = (SPLPEItemClass *) klass;
 
-       parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
+       parent_class = (SPLPEItemClass *)g_type_class_ref (SP_TYPE_LPE_ITEM);
 
        object_class->dispose = sp_group_dispose;
 
@@ -111,9 +125,12 @@ sp_group_class_init (SPGroupClass *klass)
        item_class->bbox = sp_group_bbox;
        item_class->print = sp_group_print;
        item_class->description = sp_group_description;
+        item_class->set_transform = sp_group_set_transform;
        item_class->show = sp_group_show;
        item_class->hide = sp_group_hide;
        item_class->snappoints = sp_group_snappoints;
+
+       lpe_item_class->update_patheffect = sp_group_update_patheffect;
 }
 
 static void
@@ -194,11 +211,14 @@ sp_group_update (SPObject *object, SPCtx *ctx, unsigned int flags)
 static void
 sp_group_modified (SPObject *object, guint flags)
 {
+    if (((SPObjectClass *) (parent_class))->modified)
+        ((SPObjectClass *) (parent_class))->modified (object, flags);
+
     SP_GROUP(object)->group->onModified(flags);
 }
 
 static Inkscape::XML::Node *
-sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_group_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
        SPGroup *group;
        SPObject *child;
@@ -209,12 +229,11 @@ sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
        if (flags & SP_OBJECT_WRITE_BUILD) {
                GSList *l;
                if (!repr) {
-                    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
                     repr = xml_doc->createElement("svg:g");
                 }
                l = NULL;
                for (child = sp_object_first_child(object); child != NULL; child = SP_OBJECT_NEXT(child) ) {
-                       crepr = child->updateRepr(NULL, flags);
+                       crepr = child->updateRepr(xml_doc, NULL, flags);
                        if (crepr) l = g_slist_prepend (l, crepr);
                }
                while (l) {
@@ -241,7 +260,7 @@ sp_group_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
        }
 
        if (((SPObjectClass *) (parent_class))->write)
-               ((SPObjectClass *) (parent_class))->write (object, repr, flags);
+               ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
 
        return repr;
 }
@@ -263,17 +282,35 @@ static gchar * sp_group_description (SPItem * item)
     return SP_GROUP(item)->group->getDescription();
 }
 
+static NR::Matrix
+sp_group_set_transform(SPItem *item, NR::Matrix const &xform)
+{
+    Inkscape::Selection *selection = sp_desktop_selection(inkscape_active_desktop());
+    persp3d_split_perspectives_according_to_selection(selection);
+
+    NR::Matrix last_trans;
+    sp_svg_transform_read(SP_OBJECT_REPR(item)->attribute("transform"), &last_trans);
+    NR::Matrix inc_trans = last_trans.inverse()*xform;
+
+    std::list<Persp3D *> plist = selection->perspList();
+    for (std::list<Persp3D *>::iterator i = plist.begin(); i != plist.end(); ++i) {
+        persp3d_apply_affine_transformation(*i, inc_trans);
+    }
+
+    return xform;
+}
+
 static void sp_group_set(SPObject *object, unsigned key, char const *value) {
-       SPGroup *group=SP_GROUP(object);
+       SPGroup *group = SP_GROUP(object);
 
        switch (key) {
-               case SP_ATTR_INKSCAPE_GROUPMODE: {
+               case SP_ATTR_INKSCAPE_GROUPMODE:
                        if ( value && !strcmp(value, "layer") ) {
                                group->setLayerMode(SPGroup::LAYER);
                        } else {
                                group->setLayerMode(SPGroup::GROUP);
                        }
-               } break;
+                   break;
                default: {
                        if (((SPObjectClass *) (parent_class))->set) {
                                (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
@@ -328,6 +365,13 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
        SPItem *pitem = SP_ITEM (SP_OBJECT_PARENT (gitem));
        Inkscape::XML::Node *prepr = SP_OBJECT_REPR (pitem);
 
+        if (SP_IS_BOX3D(gitem)) {
+            group = box3d_convert_to_group(SP_BOX3D(gitem));
+            gitem = SP_ITEM(group);
+        }
+
+        sp_lpe_item_remove_all_path_effects(SP_LPE_ITEM(group), false);
+
        /* Step 1 - generate lists of children objects */
        GSList *items = NULL;
        GSList *objects = NULL;
@@ -337,13 +381,6 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
 
                        SPItem *citem = SP_ITEM (child);
 
-                        if (SP_IS_BOX3D_SIDE(child)) {
-                            Inkscape::XML::Node *repr = SP_OBJECT_REPR(child);
-                            // FIXME: This doesn't remove the attribute "inkscape:box3dsidetype". Why?
-                            repr->setAttribute("inkscape:box3dsidetype", NULL);
-                            repr->setAttribute("sodipodi:type", NULL);
-                        }
-
                        /* Merging of style */
                        // this converts the gradient/pattern fill/stroke, if any, to userSpaceOnUse; we need to do
                        // it here _before_ the new transform is set, so as to use the pre-transform bbox
@@ -420,8 +457,10 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
        if (objects) {
            Inkscape::XML::Node *last_def = SP_OBJECT_REPR(defs)->lastChild();
            while (objects) {
-               SP_OBJECT_REPR(defs)->addChild((Inkscape::XML::Node *) objects->data, last_def);
-               Inkscape::GC::release((Inkscape::XML::Node *) objects->data);
+                Inkscape::XML::Node *repr = (Inkscape::XML::Node *) objects->data;
+                if (!sp_repr_is_meta_element(repr))
+                    SP_OBJECT_REPR(defs)->addChild(repr, last_def);
+                Inkscape::GC::release(repr);
                objects = g_slist_remove (objects, objects->data);
            }
        }
@@ -439,7 +478,7 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
                SPItem *item = (SPItem *) doc->getObjectByRepr(repr);
 
                sp_item_write_transform(item, repr, item->transform, NULL, false);
-               
+
                Inkscape::GC::release(repr);
                if (children && SP_IS_ITEM (item))
                        *children = g_slist_prepend (*children, item);
@@ -447,7 +486,7 @@ sp_item_group_ungroup (SPGroup *group, GSList **children, bool do_done)
                items = g_slist_remove (items, items->data);
        }
 
-       if (do_done) 
+       if (do_done)
                sp_document_done (doc, SP_VERB_NONE, _("Ungroup"));
 }
 
@@ -594,7 +633,7 @@ void CGroup::onChildRemoved(Inkscape::XML::Node */*child*/) {
     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
 }
 
-void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {    
+void CGroup::onUpdate(SPCtx *ctx, unsigned int flags) {
     SPItemCtx *ictx, cctx;
 
     ictx = (SPItemCtx *) ctx;
@@ -656,16 +695,21 @@ void CGroup::onModified(guint flags) {
 }
 
 void CGroup::calculateBBox(NRRect *bbox, NR::Matrix const &transform, unsigned const flags) {
+
+    boost::optional<NR::Rect> dummy_bbox;
+
     GSList *l = _group->childList(false, SPObject::ActionBBox);
     while (l) {
         SPObject *o = SP_OBJECT (l->data);
-        if (SP_IS_ITEM(o)) {
+        if (SP_IS_ITEM(o) && !SP_ITEM(o)->isHidden()) {
             SPItem *child = SP_ITEM(o);
             NR::Matrix const ct(child->transform * transform);
-            sp_item_invoke_bbox_full(child, bbox, ct, flags, FALSE);
-        }        
+            sp_item_invoke_bbox_full(child, &dummy_bbox, ct, flags, FALSE);
+        }
         l = g_slist_remove (l, o);
     }
+
+    *bbox = NRRect(dummy_bbox);
 }
 
 void CGroup::onPrint(SPPrintContext *ctx) {
@@ -674,7 +718,7 @@ void CGroup::onPrint(SPPrintContext *ctx) {
         SPObject *o = SP_OBJECT (l->data);
         if (SP_IS_ITEM(o)) {
             sp_item_invoke_print (SP_ITEM (o), ctx);
-        }        
+        }
         l = g_slist_remove (l, o);
     }
 }
@@ -686,7 +730,7 @@ gint CGroup::getItemCount() {
             len++;
         }
     }
-    
+
     return len;
 }
 
@@ -765,6 +809,60 @@ void CGroup::onOrderChanged (Inkscape::XML::Node *child, Inkscape::XML::Node *,
     _group->requestModified(SP_OBJECT_MODIFIED_FLAG);
 }
 
+static void
+sp_group_update_patheffect (SPLPEItem *lpeitem, bool write)
+{
+#ifdef GROUP_VERBOSE
+    g_message("sp_group_update_patheffect: %p\n", lpeitem);
+#endif
+    g_return_if_fail (lpeitem != NULL);
+    g_return_if_fail (SP_IS_GROUP (lpeitem));
+
+    GSList const *item_list = sp_item_group_item_list(SP_GROUP(lpeitem));
+    for ( GSList const *iter = item_list; iter; iter = iter->next ) {
+        SPObject *subitem = static_cast<SPObject *>(iter->data);
+        if (SP_IS_LPE_ITEM(subitem)) {
+            if (SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect) {
+                SP_LPE_ITEM_CLASS (G_OBJECT_GET_CLASS (subitem))->update_patheffect (SP_LPE_ITEM(subitem), write);
+            }
+        }
+    }
+    
+    if (sp_lpe_item_has_path_effect(lpeitem) && sp_lpe_item_path_effects_enabled(lpeitem)) {
+        for (PathEffectList::iterator it = lpeitem->path_effect_list->begin(); it != lpeitem->path_effect_list->end(); it++)
+        {
+            LivePathEffectObject *lpeobj = (*it)->lpeobject;
+            lpeobj->lpe->doBeforeEffect(lpeitem);
+        }
+        
+        sp_group_perform_patheffect(SP_GROUP(lpeitem), SP_GROUP(lpeitem));
+    }
+}
+
+static void
+sp_group_perform_patheffect(SPGroup *group, SPGroup *topgroup)
+{
+    GSList const *item_list = sp_item_group_item_list(SP_GROUP(group));
+    for ( GSList const *iter = item_list; iter; iter = iter->next ) {
+        SPObject *subitem = static_cast<SPObject *>(iter->data);
+        if (SP_IS_GROUP(subitem)) {
+            sp_group_perform_patheffect(SP_GROUP(subitem), topgroup);
+        } else if (SP_IS_SHAPE(subitem)) {
+            SPCurve * c = sp_shape_get_curve(SP_SHAPE(subitem));
+            sp_lpe_item_perform_path_effect(SP_LPE_ITEM(topgroup), c);
+            sp_shape_set_curve(SP_SHAPE(subitem), c, TRUE);
+            
+            Inkscape::XML::Node *repr = SP_OBJECT_REPR(subitem);
+            gchar *str = sp_svg_write_path(c->get_pathvector());
+            repr->setAttribute("d", str);
+            g_free(str);
+
+            c->unref();
+        }
+    }
+}
+
 /*
   Local Variables:
   mode:c++