2887
2889
DocumentUndo::done(doc, SP_VERB_EDIT_SELECTION_2_GUIDES, _("Objects to guides"));
2893
* Convert <g> to <symbol>, leaving all <use> elements referencing group unchanged.
2895
void sp_selection_symbol(SPDesktop *desktop, bool apply )
2898
if (desktop == NULL) {
2902
SPDocument *doc = sp_desktop_document(desktop);
2903
Inkscape::XML::Document *xml_doc = doc->getReprDoc();
2905
Inkscape::Selection *selection = sp_desktop_selection(desktop);
2907
// Check if something is selected.
2908
if (selection->isEmpty()) {
2909
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select one <b>group</b> to convert to symbol."));
2913
SPObject* group = selection->single();
2915
// Make sure we have only one object in selection.
2916
if( group == NULL ) {
2917
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select only one <b>group</b> to convert to symbol."));
2921
// Make sure we convert the original.
2922
if( SP_IS_USE( group ) ) {
2923
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select original (<b>Shift+D</b>) to convert to symbol."));
2927
// Require that we really have a group.
2928
if( !SP_IS_GROUP( group ) ) {
2929
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Group selection first to convert to symbol."));
2933
doc->ensureUpToDate();
2935
Inkscape::XML::Node *symbol = xml_doc->createElement("svg:symbol");
2936
symbol->setAttribute("style", group->getAttribute("style"));
2937
symbol->setAttribute("title", group->getAttribute("title"));
2938
symbol->setAttribute("transform", group->getAttribute("transform"));
2940
Glib::ustring id = group->getAttribute("id");
2942
// Now we need to copy all children of group
2943
GSList* children = group->childList(false);
2944
children = g_slist_reverse(children);
2945
for (GSList* i = children; i != NULL; i = i->next ) {
2946
SPObject* child = SP_OBJECT(i->data);
2947
Inkscape::XML::Node *dup = child->getRepr()->duplicate(xml_doc);
2948
symbol->appendChild(dup);
2949
child->deleteObject(true);
2952
// Need to delete <g>; all <use> elements that referenced <g> should
2953
// auto-magically reference <symbol>.
2954
doc->getDefs()->getRepr()->appendChild(symbol);
2955
symbol->setAttribute("id",id.c_str()); // After we delete group with same id.
2956
// Mysterious, must set symbol ID before deleting group or all <use>
2957
// refering to symbol get turned into groups. (Linked to unlinking clones?)
2958
group->deleteObject(true);
2960
Inkscape::GC::release(symbol);
2962
// Group just disappears, nothing to select.
2964
// Need to signal Symbol dialog to update
2966
g_slist_free(children);
2968
DocumentUndo::done(doc, SP_VERB_EDIT_SYMBOL, _("Group to symbol"));
2972
* Convert <symbol> to <g>. All <use> elements referencing symbol remain unchanged.
2974
void sp_selection_unsymbol(SPDesktop *desktop)
2977
if (desktop == NULL) {
2981
SPDocument *doc = sp_desktop_document(desktop);
2982
Inkscape::XML::Document *xml_doc = doc->getReprDoc();
2984
Inkscape::Selection *selection = sp_desktop_selection(desktop);
2986
// Check if something is selected.
2987
if (selection->isEmpty()) {
2988
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select a <b>symbol</b> to extract objects from."));
2992
SPObject* use = selection->single();
2994
// Make sure we have only one object in selection.
2996
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select only one <b>symbol</b> to convert to group."));
3000
// Require that we really have a <use> that references a <symbol>.
3001
if( !SP_IS_USE( use ) && !SP_IS_SYMBOL( use->firstChild() ) ) {
3002
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("Select only one <b>symbol</b> to convert to group."));
3006
doc->ensureUpToDate();
3008
SPObject* symbol = use->firstChild();
3010
Inkscape::XML::Node *group = xml_doc->createElement("svg:g");
3011
group->setAttribute("style", symbol->getAttribute("style"));
3012
group->setAttribute("title", symbol->getAttribute("title"));
3013
group->setAttribute("transform", symbol->getAttribute("transform"));
3015
Glib::ustring id = symbol->getAttribute("id");
3017
// Now we need to copy all children of symbol
3018
GSList* children = symbol->childList(false);
3019
children = g_slist_reverse(children);
3020
for (GSList* i = children; i != NULL; i = i->next ) {
3021
SPObject* child = SP_OBJECT(i->data);
3022
Inkscape::XML::Node *dup = child->getRepr()->duplicate(xml_doc);
3023
group->appendChild(dup);
3024
child->deleteObject(true);
3027
SPObject* parent = use->parent; // So we insert <g> next to <use> (easier to find)
3029
// Need to delete <symbol>; all other <use> elements that referenced <symbol> should
3030
// auto-magically reference <g>.
3031
symbol->deleteObject(true);
3032
group->setAttribute("id",id.c_str()); // After we delete symbol with same id.
3033
parent->getRepr()->appendChild(group);
3034
//use->deleteObject(true);
3036
SPItem *group_item = static_cast<SPItem *>(sp_desktop_document(desktop)->getObjectByRepr(group));
3037
Inkscape::GC::release(group);
3039
selection->set(group_item);
3041
// Need to signal Symbol dialog to update
3043
g_slist_free(children);
3045
DocumentUndo::done(doc, SP_VERB_EDIT_UNSYMBOL, _("Group from symbol"));
2891
3049
sp_selection_tile(SPDesktop *desktop, bool apply)